{"id":100,"date":"2013-06-11T13:20:02","date_gmt":"2013-06-11T13:20:02","guid":{"rendered":"https:\/\/8bitroots.com\/?p=100"},"modified":"2015-06-30T15:23:26","modified_gmt":"2015-06-30T15:23:26","slug":"better-debug-logging-in-unity","status":"publish","type":"post","link":"https:\/\/8bitroots.com\/?p=100","title":{"rendered":"Better Debug Logging in Unity"},"content":{"rendered":"<p>Debug logging for me, and I&#8217;m sure I&#8217;m not alone, has always been a love-hate relationship. I love getting my valuable information but hate the slow-downs associated with it. With these things in mind I took it upon myself to improve the experience. <!--more--><\/p>\n<p>It was important to me to be able to set filters for the types of messages that I wanted to show, so I created a small static wrapper for the built-in debug logger. Using these filters I can determine what messages I want to see by setting a few flags when initializing the wrapper. Since the wrapper contains flag checks for each filter type it would also be easy to leave these filters turned on in release and gather assert information as telemetry. Keep in mind that this isn&#8217;t the most optimized version of this script, and it can in fact be extended quite a bit. This is, more than anything, a concept for debug logging that I wanted to share.<\/p>\n<p>Using the wrapper is easy, but I&#8217;ll demonstrate how to use it. First create a c# script named CDebugLogger and just copy over this text into the script.<\/p>\n<pre class=\"brush: csharp\">\r\n\/*********************************************************************\r\n *\tFile : \t\tCDebugLogger.cs\r\n * \r\n * \tAuthor :\tChase Cobb\r\n * \r\n * \tPurpose : \tTo wrap the functionality of the Unity Debugger to allow\r\n * \t\t\t\tcontrol over what messages should be shown\r\n *\/\r\n\r\nusing UnityEngine;\r\nusing System.Collections;\r\nusing System;\r\n\r\n\r\npublic static class CDebugLogger\r\n{\r\n    private static string WARN_MESSAGE =            \"Warning : \";\r\n    private static string ERROR_MESSAGE =           \"Error : \";\r\n    private static string CRITICAL_MESSAGE =        \"Critical : \";\r\n    private static string INFO_MESSAGE =            \"Information : \";\r\n\t\r\n\t\/\/Each enumerated value should be a power of two\r\n    [Flags]\r\n    public enum EDebugLevel\r\n    {\r\n        DEBUG_WARN = 0x1,\r\n        DEBUG_ERROR = 0x2,\r\n        DEBUG_CRITICAL = 0x4,\r\n        DEBUG_INFO = 0x8\r\n    }\r\n\r\n    \/\/\/ <summary>\r\n    \/\/\/ This property determines which debug messages are seen int the output\r\n    \/\/\/ to set this property use SetDebugLoggingLevel\r\n    \/\/\/ <\/summary>\r\n    public static int DebugLevel\r\n    {\r\n        get;set;\r\n    }\r\n\r\n    \/\/\/ <summary>\r\n    \/\/\/ Sets which debug messages should be shown\r\n    \/\/\/ <\/summary>\r\n    \/\/\/ <param name=\"nDebugLevel\">flags to determine which levels are shown<\/param>\r\n    public static void SetDebugLoggingLevel(int nDebugLevel)\r\n    {\r\n        DebugLevel = nDebugLevel;\r\n    }\r\n\r\n    \/\/\/ <summary>\r\n    \/\/\/ Function responsible for writing to the debug log\r\n    \/\/\/ <\/summary>\r\n    \/\/\/ <param name=\"eLevel\">Debug level of this message, from enum EDebugLevel<\/param>\r\n    \/\/\/ <param name=\"cMessage\">The message to write<\/param>\r\n    public static void WriteToLog(EDebugLevel eLevel, object cMessage)\r\n    {\r\n        if ((eLevel & EDebugLevel.DEBUG_WARN) != 0 && (DebugLevel & (int)EDebugLevel.DEBUG_WARN) != 0)\r\n        {\r\n            Debug.Log(WARN_MESSAGE + cMessage);\r\n        }\r\n        else if ((eLevel & EDebugLevel.DEBUG_ERROR) != 0 && (DebugLevel & (int)EDebugLevel.DEBUG_ERROR) != 0)\r\n        {\r\n            Debug.Log(ERROR_MESSAGE + cMessage);\r\n        }\r\n        else if ((eLevel & EDebugLevel.DEBUG_CRITICAL) != 0 && (DebugLevel & (int)EDebugLevel.DEBUG_CRITICAL) != 0)\r\n        {\r\n            Debug.Log(CRITICAL_MESSAGE + cMessage);\r\n        }\r\n        else if ((eLevel & EDebugLevel.DEBUG_INFO) != 0 && (DebugLevel & (int)EDebugLevel.DEBUG_INFO) != 0)\r\n        {\r\n            Debug.Log(INFO_MESSAGE + cMessage);\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>After that, be sure to initialize the script and set the desired flags using the OR bitwise operator. This needs to be done in a script that will be guaranteed to load at the beginning of the scene. I use a persistent game manager class to initialize mine.<\/p>\n<p>The only messages that will show are the ones that you set here!<\/p>\n<pre class=\"brush: csharp\">\r\n\/\/Set the debug levels to show in the debug log\r\nCDebugLogger.SetDebugLoggingLevel((int)CDebugLogger.EDebugLevel.DEBUG_WARN |\r\n                                  (int)CDebugLogger.EDebugLevel.DEBUG_ERROR |\r\n                                  (int)CDebugLogger.EDebugLevel.DEBUG_CRITICAL |\r\n                                  (int)CDebugLogger.EDebugLevel.DEBUG_INFO);\r\n\r\n<\/pre>\n<p>All that remains now is to use the wrapper for logging. Include your flag to determine the level of debugging that you desire for this message. Then include the message you wish to see as the second parameter( the second parameter is used exactly like the normal debug.log() function).<\/p>\n<pre class=\"brush: csharp\">\r\nCDebugLogger.WriteToLog(CDebugLogger.EDebugLevel.DEBUG_INFO, \"m_rSpawnRect.xMax = \" + m_rSpawnRect.xMax);\r\n\r\n<\/pre>\n<p>Once that is done just run your game and enjoy your new easily controlled debug logging!<\/p>\n<p><a href=\"https:\/\/8bitroots.com\/wp-content\/uploads\/2013\/06\/CDebugLogger.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/8bitroots.com\/wp-content\/uploads\/2013\/06\/CDebugLogger-300x141.png\" alt=\"CDebugLogger\" width=\"300\" height=\"141\" class=\"alignnone size-medium wp-image-103\" srcset=\"https:\/\/8bitroots.com\/wp-content\/uploads\/2013\/06\/CDebugLogger-300x141.png 300w, https:\/\/8bitroots.com\/wp-content\/uploads\/2013\/06\/CDebugLogger-500x235.png 500w, https:\/\/8bitroots.com\/wp-content\/uploads\/2013\/06\/CDebugLogger.png 689w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Debug logging for me, and I&#8217;m sure I&#8217;m not alone, has always been a love-hate relationship. I love getting my valuable information but hate the slow-downs associated with it. With these things in mind I took it upon myself to &hellip; <a href=\"https:\/\/8bitroots.com\/?p=100\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[5,3],"tags":[],"class_list":["post-100","post","type-post","status-publish","format-standard","hentry","category-tools","category-unity"],"_links":{"self":[{"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/posts\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/8bitroots.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100"}],"version-history":[{"count":7,"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions"}],"predecessor-version":[{"id":126,"href":"https:\/\/8bitroots.com\/index.php?rest_route=\/wp\/v2\/posts\/100\/revisions\/126"}],"wp:attachment":[{"href":"https:\/\/8bitroots.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/8bitroots.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/8bitroots.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}