Last active
December 9, 2023 13:54
-
-
Save hk1ll3r/06b32050bba64a4a4afcc23a46f0524e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
namespace NoSuchStudio.Common { | |
/// <summary> | |
/// Base class for MonoBehaviours that should have a separate logger. Useful for filtering | |
/// logs by class types. | |
/// </summary> | |
/// <remarks> | |
/// This class keeps track of all types that inherit it and creates a <see cref="UnityEngine.Logger"/> | |
/// for each. | |
/// Any messages logged through the helper methods will have the class name prepended to the message. | |
/// <code> | |
/// // will print "[MyClass] Hello World!" | |
/// MonoBehaviourWithLogger.LogLog<MyClass>("Hello World!"); | |
/// </code> | |
/// Using sample code like below, you can filter your logs by class. | |
/// <code> | |
/// MonoBehaviourWithLogger.GetLoggerByType<SubclassOfMonoBehaviourWithLogger>().Item1.filterLogType = LogType.Error; | |
/// </code> | |
/// </remarks> | |
public abstract class MonoBehaviourWithLogger : MonoBehaviour { | |
public static readonly Dictionary<Type, (Logger, string)> loggers = new Dictionary<Type, (Logger, string)>(); | |
public Logger logger { | |
get { | |
Type thisType = GetType(); | |
return GetLoggerByType(thisType).logger; | |
} | |
} | |
public string logTag { | |
get { | |
Type thisType = GetType(); | |
return GetLoggerByType(thisType).logTag; | |
} | |
} | |
protected void LogLog(string format, params object[] args) { | |
logger.LogFormat(LogType.Log, string.Format("{0} {1}", logTag, format), args); | |
} | |
protected void LogWarn(string format, params object[] args) { | |
logger.LogFormat(LogType.Warning, string.Format("{0} {1}", logTag, format), args); | |
} | |
protected void LogError(string format, params object[] args) { | |
logger.LogFormat(LogType.Error, string.Format("{0} {1}", logTag, format), args); | |
} | |
private static void AddType(Type type) { | |
if (!loggers.ContainsKey(type)) { | |
loggers.Add(type, (new Logger(Debug.unityLogger.logHandler), string.Format("[{0}]", type.Name))); | |
} | |
} | |
public static (Logger logger, string logTag) GetLoggerByType(Type type) { | |
AddType(type); | |
return loggers[type]; | |
} | |
public static (Logger logger, string logTag) GetLoggerByType<T>() { | |
return GetLoggerByType(typeof(T)); | |
} | |
protected static void LogLog<T>(string format, params object[] args) { | |
(Logger logger, string LogTag) = GetLoggerByType<T>(); | |
logger.LogFormat(LogType.Log, string.Format("{0} {1}", LogTag, format), args); | |
} | |
protected static void LogWarn<T>(string format, params object[] args) { | |
(Logger logger, string LogTag) = GetLoggerByType<T>(); | |
logger.LogFormat(LogType.Warning, string.Format("{0} {1}", LogTag, format), args); | |
} | |
protected static void LogError<T>(string format, params object[] args) { | |
(Logger logger, string LogTag) = GetLoggerByType<T>(); | |
logger.LogFormat(LogType.Error, string.Format("{0} {1}", LogTag, format), args); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment