diff --git a/Examples/TestUberLogger.cs b/Examples/TestUberLogger.cs index 95d1c4f..934ba75 100755 --- a/Examples/TestUberLogger.cs +++ b/Examples/TestUberLogger.cs @@ -5,10 +5,12 @@ public class TestUberLogger : MonoBehaviour { Thread TestThread; - // Use this for initialization + UberLoggerChannel WigWamChannel; + void Start () { UberLogger.Logger.AddLogger(new UberLoggerFile("UberLogger.log"), false); + WigWamChannel = new UberLoggerChannel("WigWam", new List() { new UberLogger.FilterWarnings() }); DoTest(); TestThread = new Thread(new ThreadStart(TestThreadEntry)); TestThread.Start(); @@ -23,13 +25,15 @@ void OnDestroy() TestThread.Abort(); TestThread.Join(); } + void TestThreadEntry() { for(;;) { Debug.Log("Thread Log Message"); UberDebug.Log("Thread ULog Message"); - Thread.Sleep(100); + WigWamChannel.Log("Wigwam says 'threads'"); + Thread.Sleep(1000); } } @@ -71,6 +75,10 @@ public void DoTest() UberDebug.LogErrorChannel("Test", "ULogErrorChannel with param {0}", "Test"); UberDebug.LogErrorChannel(gameObject, "Test", "ULogErrorChannel with GameObject"); UberDebug.LogErrorChannel(gameObject, "Test", "ULogErrorChannel with GameObject and param {0}", "Test"); + + WigWamChannel.LogWarning("I should not be seen"); + WigWamChannel.Log("But I should be seen"); + } // Update is called once per frame diff --git a/UberLogger.cs b/UberLogger.cs index c3765d7..0ec05b7 100644 --- a/UberLogger.cs +++ b/UberLogger.cs @@ -51,6 +51,30 @@ public interface IFilter bool ApplyFilter(string channel, UnityEngine.Object source, LogSeverity severity, object message, params object[] par); } + public class FilterWarnings : IFilter + { + public bool ApplyFilter(string channel, UnityEngine.Object source, LogSeverity severity, object message, params object[] par) + { + return severity!=LogSeverity.Warning; + } + } + + public class FilterErrors : IFilter + { + public bool ApplyFilter(string channel, UnityEngine.Object source, LogSeverity severity, object message, params object[] par) + { + return severity!=LogSeverity.Error; + } + } + + public class FilterMessages : IFilter + { + public bool ApplyFilter(string channel, UnityEngine.Object source, LogSeverity severity, object message, params object[] par) + { + return severity!=LogSeverity.Message; + } + } + //Information about a particular frame of a callstack [System.Serializable] public class LogStackFrame @@ -250,8 +274,9 @@ public static class Logger static long StartTick; static bool AlreadyLogging = false; static Regex UnityMessageRegex; - static List Filters = new List(); - + static List GlobalFilters = new List(); + static Dictionary> ChannelFilters = new Dictionary>(); + static Logger() { // Register with Unity's logging system @@ -273,7 +298,7 @@ static void UnityLogHandler(string logString, string stackTrace, UnityEngine.Log { UnityLogInternal(logString, stackTrace, logType); } - + static public double GetRelativeTime() { long ticks = DateTime.Now.Ticks; @@ -304,16 +329,60 @@ static public void AddLogger(ILogger logger, bool populateWithExistingMessages=t } /// - /// Registers a new filter mechanism, which will be able to silence any future log messages + /// Adds a filter to the global list of filters. + /// + static public void AddGlobalFilter(IFilter filter) + { + lock (Loggers) + { + GlobalFilters.Add(filter); + } + } + + /// + /// Removes a filter from the global list of filters. /// - static public void AddFilter(IFilter filter) + static public void RemoveGlobalFilter(IFilter filter) { lock (Loggers) { - Filters.Add(filter); + GlobalFilters.Remove(filter); } } + /// + /// Adds a filter to the named channel + /// + static public void AddChannelFilter(string channelName, IFilter filter) + { + lock (Loggers) + { + List channelFilterList; + if(!ChannelFilters.TryGetValue(channelName, out channelFilterList)) + { + channelFilterList = new List(); + ChannelFilters[channelName] = channelFilterList; + } + channelFilterList.Add(filter); + } + } + + /// + /// Removes a filter from the named channel's list of filters. + /// + static public void RemoveChannelFilter(string channelName, IFilter filter) + { + lock (Loggers) + { + List channelFilterList; + if(ChannelFilters.TryGetValue(channelName, out channelFilterList)) + { + channelFilterList.Remove(filter); + } + } + } + + /// /// Paths provided by Unity will contain forward slashes as directory separators on all OSes. /// This method changes all forward slashes to OS-specific directory separators. @@ -340,7 +409,7 @@ static public bool ExtractInfoFromUnityMessage(string log, ref string filename, } return false; } - + /// /// Tries to extract useful information about the log from a Unity stack trace @@ -470,7 +539,7 @@ static bool GetCallstack(ref List callstack, out LogStackFrame or if (showHideMode == IgnoredUnityMethod.Mode.Show) { var logStackFrame = new LogStackFrame(stackFrame); - + callstack.Add(logStackFrame); if (setOriginatingSourceLocation) @@ -481,7 +550,7 @@ static bool GetCallstack(ref List callstack, out LogStackFrame or // Callstack has been processed backwards -- correct order for presentation callstack.Reverse(); - + return false; } @@ -527,7 +596,7 @@ static void UnityLogInternal(string unityMessage, string unityCallStack, UnityEn try { AlreadyLogging = true; - + var callstack = new List(); LogStackFrame originatingSourceLocation; var unityOnly = GetCallstack(ref callstack, out originatingSourceLocation); @@ -536,7 +605,7 @@ static void UnityLogInternal(string unityMessage, string unityCallStack, UnityEn return; } - //If we have no useful callstack, fall back to parsing Unity's callstack + //If we have no useful callstack, fall back to parsing Unity's callstack if(callstack.Count==0) { callstack = GetCallstackFromUnityLog(unityCallStack, out originatingSourceLocation); @@ -554,7 +623,7 @@ static void UnityLogInternal(string unityMessage, string unityCallStack, UnityEn string filename = ""; int lineNumber = 0; - + //Finally, parse the error message so we can get basic file and line information if(ExtractInfoFromUnityMessage(unityMessage, ref filename, ref lineNumber)) { @@ -597,12 +666,22 @@ static public void Log(string channel, UnityEngine.Object source, LogSeverity se { AlreadyLogging = true; - foreach (IFilter filter in Filters) + foreach (IFilter filter in GlobalFilters) { if (!filter.ApplyFilter(channel, source, severity, message, par)) return; } - + + List channelFilters; + if(ChannelFilters.TryGetValue(channel, out channelFilters)) + { + foreach (IFilter filter in channelFilters) + { + if (!filter.ApplyFilter(channel, source, severity, message, par)) + return; + } + } + var callstack = new List(); LogStackFrame originatingSourceLocation; var unityOnly = GetCallstack(ref callstack, out originatingSourceLocation); diff --git a/UberLoggerChannel.cs b/UberLoggerChannel.cs new file mode 100644 index 0000000..c248e92 --- /dev/null +++ b/UberLoggerChannel.cs @@ -0,0 +1,67 @@ +using System.Collections.Generic; +using UberLogger; +using UnityEngine; + + +public class UberLoggerChannel +{ + private string ChannelName; + public UberLoggerChannel(string channelName, List filters=null) + { + ChannelName = channelName; + if(filters!=null) + { + foreach(var filter in filters) + { + UberLogger.Logger.AddChannelFilter(ChannelName, filter); + } + } + } + + public void AddFilter(IFilter filter) + { + UberLogger.Logger.AddChannelFilter(ChannelName, filter); + } + + public void RemoveFilter(IFilter filter) + { + UberLogger.Logger.RemoveChannelFilter(ChannelName, filter); + } + + + [StackTraceIgnore] + public void Log(string message, params object[] par) + { + UberDebug.LogChannel(ChannelName, message, par); + } + + [StackTraceIgnore] + public void Log(Object context, string message, params object[] par) + { + UberDebug.LogChannel(context, ChannelName, message, par); + } + + [StackTraceIgnore] + public void LogWarning(string message, params object[] par) + { + UberDebug.LogWarningChannel(ChannelName, message, par); + } + + [StackTraceIgnore] + public void LogWarning(Object context, string message, params object[] par) + { + UberDebug.LogWarningChannel(context, ChannelName, message, par); + } + + [StackTraceIgnore] + public void LogError(string message, params object[] par) + { + UberDebug.LogErrorChannel(ChannelName, message, par); + } + + [StackTraceIgnore] + public void LogError(Object context, string message, params object[] par) + { + UberDebug.LogErrorChannel(context, ChannelName, message, par); + } +} diff --git a/UberLoggerChannel.cs.meta b/UberLoggerChannel.cs.meta new file mode 100644 index 0000000..ad0e910 --- /dev/null +++ b/UberLoggerChannel.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: f369d1720238d491f9b461b2026d6965 +timeCreated: 1521307306 +licenseType: Pro +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: