Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility class for logging to channel #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions Examples/TestUberLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public class TestUberLogger : MonoBehaviour
{
public static readonly UberLoggerChannel TestChannelWrapper = new UberLoggerChannel("WrapperChannel");

Thread TestThread;
// Use this for initialization
void Start ()
Expand Down Expand Up @@ -71,6 +73,47 @@ 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");

// Will output all messages in test function.
RunChannelWrapperTests();

// Will hide .Log(...) calls in test function.
TestChannelWrapper.Filter = UberLoggerChannel.Filters.Logs;
RunChannelWrapperTests();

// Will hide .LogWarning(...) calls in test function.
TestChannelWrapper.Filter = UberLoggerChannel.Filters.Warnings;
RunChannelWrapperTests();

// Will hide .LogError(...) calls in test function.
TestChannelWrapper.Filter = UberLoggerChannel.Filters.Errors;
RunChannelWrapperTests();

// Will hide .Log(...) and LogWarning(...) calls in test function.
TestChannelWrapper.Filter = UberLoggerChannel.Filters.Logs | UberLoggerChannel.Filters.Warnings;
RunChannelWrapperTests();
}

private void RunChannelWrapperTests()
{
Debug.Log("Running Channel Wrapper Tests...");

TestChannelWrapper.Log("Wrapped Channel");
TestChannelWrapper.Log("Wrapped Channel with param {0}", "Test");
TestChannelWrapper.Log(gameObject, "Wrapped Channel with GameObject");
TestChannelWrapper.Log(gameObject, "Wrapped Channel with GameObject and param {0}", "Test");

TestChannelWrapper.LogWarning("Wrapped Channel Warning");
TestChannelWrapper.LogWarning("Wrapped Channel Warning with param {0}", "Test");
TestChannelWrapper.LogWarning(gameObject, "Wrapped Channel Warning with GameObject");
TestChannelWrapper.LogWarning(gameObject, "Wrapped Channel Warning with GameObject and param {0}", "Test");

TestChannelWrapper.LogError("Wrapped Channel Error");
TestChannelWrapper.LogError("Wrapped Channel Error with param {0}", "Test");
TestChannelWrapper.LogError(gameObject, "Wrapped Channel Error with GameObject");
TestChannelWrapper.LogError(gameObject, "Wrapped Channel Error with GameObject and param {0}", "Test");

Debug.Log("... Done Running Channel Wrapper Tests.");
}

// Update is called once per frame
Expand Down
89 changes: 89 additions & 0 deletions UberLoggerChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Collections;
using System.Collections.Generic;
using UberLogger;
using UnityEngine;

/// <summary>
/// Wraps access to a named channel in a class so that it can be used without having to
/// type the channel name each time to enforcing channel name checking at compile-time.
/// </summary>
public class UberLoggerChannel
{
private string ChannelName;
public UberLoggerChannel(string channelName)
{
ChannelName = channelName;
Filter = Filters.None;
}

/// <summary>
/// Filters for preventing display of certain message types.
/// </summary>
[System.Flags]
public enum Filters
{
None = 0,
Logs = 1 << 0,
Warnings = 1 << 1,
Errors = 1 << 2
}

/// <summary>
/// Gets or sets the current filters being applied to this channel. Messages that match the specified set of flags will be ignored.
/// </summary>
public Filters Filter { get; set; }

[StackTraceIgnore]
public void Log(string message, params object[] par)
{
if ((Filter & Filters.Logs) == 0)
{
UberDebug.LogChannel(ChannelName, message, par);
}
}

[StackTraceIgnore]
public void Log(Object context, string message, params object[] par)
{
if ((Filter & Filters.Logs) == 0)
{
UberDebug.LogChannel(context, ChannelName, message, par);
}
}

[StackTraceIgnore]
public void LogWarning(string message, params object[] par)
{
if ((Filter & Filters.Warnings) == 0)
{
UberDebug.LogWarningChannel(ChannelName, message, par);
}
}

[StackTraceIgnore]
public void LogWarning(Object context, string message, params object[] par)
{
if ((Filter & Filters.Warnings) == 0)
{
UberDebug.LogWarningChannel(context, ChannelName, message, par);
}
}

[StackTraceIgnore]
public void LogError(string message, params object[] par)
{
if ((Filter & Filters.Errors) == 0)
{
UberDebug.LogErrorChannel(ChannelName, message, par);
}
}

[StackTraceIgnore]
public void LogError(Object context, string message, params object[] par)
{
if ((Filter & Filters.Errors) == 0)
{
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.