Skip to content

Instantly share code, notes, and snippets.

@DanPuzey
Last active May 21, 2024 12:34
Show Gist options
  • Save DanPuzey/669c49f8c321ba447e88 to your computer and use it in GitHub Desktop.
Save DanPuzey/669c49f8c321ba447e88 to your computer and use it in GitHub Desktop.
Unity logging wrapper, for better performance and usage.
using UnityEngine;
namespace Assets.Phunk.Core
{
public static class Log
{
#region Error
public static void ErrorFormat(UnityEngine.Object context, string template, params object[] args)
{
var message = string.Format(template, args);
Error(context, message);
}
public static void ErrorFormat(string template, params object[] args)
{
var message = string.Format(template, args);
Error(message);
}
public static void Error(object message)
{
Debug.LogError(message);
}
public static void Error(UnityEngine.Object context, object message)
{
Debug.LogError(message, context);
}
#endregion
#region Warning
public static void WarningFormat(UnityEngine.Object context, string template, params object[] args)
{
var message = string.Format(template, args);
Warning(context, message);
}
public static void WarningFormat(string template, params object[] args)
{
var message = string.Format(template, args);
Warning(message);
}
public static void Warning(object message)
{
Debug.LogWarning(message);
}
public static void Warning(UnityEngine.Object context, object message)
{
Debug.LogWarning(message, context);
}
#endregion
#region Message
public static void MessageFormat(UnityEngine.Object context, string template, params object[] args)
{
var message = string.Format(template, args);
Message(context, message);
}
public static void MessageFormat(string template, params object[] args)
{
var message = string.Format(template, args);
Message(message);
}
public static void Message(object message)
{
Debug.Log(message);
}
public static void Message(UnityEngine.Object context, object message)
{
Debug.Log(message, context);
}
#endregion
#region Verbose
[System.Diagnostics.Conditional("DEBUG"), System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void VerboseFormat(UnityEngine.Object context, string template, params object[] args)
{
var message = string.Format(template, args);
Verbose(context, message);
}
[System.Diagnostics.Conditional("DEBUG"), System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void VerboseFormat(string template, params object[] args)
{
var message = string.Format(template, args);
Verbose(message);
}
[System.Diagnostics.Conditional("DEBUG"), System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Verbose(object message)
{
Debug.Log(string.Concat("<color=grey>[VERBOSE]</color> ", message));
}
[System.Diagnostics.Conditional("DEBUG"), System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Verbose(UnityEngine.Object context, object message)
{
Debug.Log(string.Concat("<color=grey>[VERBOSE]</color> ", message), context);
}
#endregion
}
}
using UnityEngine;
namespace Assets.Phunk.Core
{
public static class LogExtensions
{
#region Exception
public static void Exception(this Object context, System.Exception exception)
{
Debug.LogException(exception, context);
}
#endregion
#region Error
public static void ErrorFormat(this Object context, string template, params object[] args)
{
var message = string.Format(template, args);
Error(context, message);
}
public static void Error(this Object context, object message)
{
Debug.LogError(message, context);
}
#endregion
#region Warning
public static void WarningFormat(this Object context, string template, params object[] args)
{
var message = string.Format(template, args);
Warning(context, message);
}
public static void Warning(this Object context, object message)
{
Debug.LogWarning(message, context);
}
#endregion
#region Message
public static void MessageFormat(this Object context, string template, params object[] args)
{
var message = string.Format(template, args);
Message(context, message);
}
public static void Message(this Object context, object message)
{
Debug.Log(message, context);
}
#endregion
#region Verbose
[System.Diagnostics.Conditional("DEBUG"), System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void VerboseFormat(this Object context, string template, params object[] args)
{
var message = string.Format(template, args);
Verbose(context, message);
}
[System.Diagnostics.Conditional("DEBUG"), System.Diagnostics.Conditional("UNITY_EDITOR")]
public static void Verbose(this Object context, object message)
{
Debug.Log(string.Concat("<color=grey>[VERBOSE]</color> ", message), context);
}
#endregion
}
}
@DanPuzey
Copy link
Author

So, the idea with this (as a starting point) is twofold: first, make less logging calls to improve performance. The Verbose calls are removed entirely at compile time (for non-DEBUG builds, which is a nice advantage straight away. The plan then is to add command-line parameter detection so that the standard messages are only logged on request.

The second goal is to integrate string.Format into the methods' signature. This has an added advantage that, if the logging method isn't called, the string.Format isn't evaluated at all. (It's surprising how much CPU time you can spend on string manipulation, even when the result of that manipulation isn't logged.)

@xJayLee
Copy link

xJayLee commented Oct 3, 2015

it like the precompilation define in C++

@DanPuzey
Copy link
Author

Updated: now includes a file that provides extension methods to automatically set the context for each log. This is useful because - in theory - supplying the context allows Unity to highlight the source object in the scene/hierarchy view when you click on a log message. Handy for working out exactly which AI has gone rogue...

@TheMasquerader
Copy link

Would it be worth re-implementing this using theILogger and/or ILogHandler from https://docs.unity3d.com/ScriptReference/Logger.html?
Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment