Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Examples/TestUberLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UberLogger.IFilter>() { new UberLogger.FilterWarnings() });
DoTest();
TestThread = new Thread(new ThreadStart(TestThreadEntry));
TestThread.Start();
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down
107 changes: 93 additions & 14 deletions UberLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -250,8 +274,9 @@ public static class Logger
static long StartTick;
static bool AlreadyLogging = false;
static Regex UnityMessageRegex;
static List<IFilter> Filters = new List<IFilter>();

static List<IFilter> GlobalFilters = new List<IFilter>();
static Dictionary<string, List<IFilter>> ChannelFilters = new Dictionary<string, List<IFilter>>();

static Logger()
{
// Register with Unity's logging system
Expand All @@ -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;
Expand Down Expand Up @@ -304,16 +329,60 @@ static public void AddLogger(ILogger logger, bool populateWithExistingMessages=t
}

/// <summary>
/// Registers a new filter mechanism, which will be able to silence any future log messages
/// Adds a filter to the global list of filters.
/// </summary>
static public void AddGlobalFilter(IFilter filter)
{
lock (Loggers)
{
GlobalFilters.Add(filter);
}
}

/// <summary>
/// Removes a filter from the global list of filters.
/// </summary>
static public void AddFilter(IFilter filter)
static public void RemoveGlobalFilter(IFilter filter)
{
lock (Loggers)
{
Filters.Add(filter);
GlobalFilters.Remove(filter);
}
}

/// <summary>
/// Adds a filter to the named channel
/// </summary>
static public void AddChannelFilter(string channelName, IFilter filter)
{
lock (Loggers)
{
List<IFilter> channelFilterList;
if(!ChannelFilters.TryGetValue(channelName, out channelFilterList))
{
channelFilterList = new List<IFilter>();
ChannelFilters[channelName] = channelFilterList;
}
channelFilterList.Add(filter);
}
}

/// <summary>
/// Removes a filter from the named channel's list of filters.
/// </summary>
static public void RemoveChannelFilter(string channelName, IFilter filter)
{
lock (Loggers)
{
List<IFilter> channelFilterList;
if(ChannelFilters.TryGetValue(channelName, out channelFilterList))
{
channelFilterList.Remove(filter);
}
}
}


/// <summary>
/// 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.
Expand All @@ -340,7 +409,7 @@ static public bool ExtractInfoFromUnityMessage(string log, ref string filename,
}
return false;
}


/// <summary>
/// Tries to extract useful information about the log from a Unity stack trace
Expand Down Expand Up @@ -470,7 +539,7 @@ static bool GetCallstack(ref List<LogStackFrame> callstack, out LogStackFrame or
if (showHideMode == IgnoredUnityMethod.Mode.Show)
{
var logStackFrame = new LogStackFrame(stackFrame);

callstack.Add(logStackFrame);

if (setOriginatingSourceLocation)
Expand All @@ -481,7 +550,7 @@ static bool GetCallstack(ref List<LogStackFrame> callstack, out LogStackFrame or

// Callstack has been processed backwards -- correct order for presentation
callstack.Reverse();

return false;
}

Expand Down Expand Up @@ -527,7 +596,7 @@ static void UnityLogInternal(string unityMessage, string unityCallStack, UnityEn
try
{
AlreadyLogging = true;

var callstack = new List<LogStackFrame>();
LogStackFrame originatingSourceLocation;
var unityOnly = GetCallstack(ref callstack, out originatingSourceLocation);
Expand All @@ -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);
Expand All @@ -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))
{
Expand Down Expand Up @@ -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<IFilter> 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>();
LogStackFrame originatingSourceLocation;
var unityOnly = GetCallstack(ref callstack, out originatingSourceLocation);
Expand Down
67 changes: 67 additions & 0 deletions UberLoggerChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Collections.Generic;
using UberLogger;
using UnityEngine;


public class UberLoggerChannel
{
private string ChannelName;
public UberLoggerChannel(string channelName, List<IFilter> 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);
}
}
13 changes: 13 additions & 0 deletions UberLoggerChannel.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.