From 4576bc766f9b268b5ab37fe8292761ef5d0244aa Mon Sep 17 00:00:00 2001 From: Anton Struyk Date: Mon, 26 Feb 2018 16:01:29 -0500 Subject: [PATCH 1/3] Add helper class to make logging to a channel safer by supporting code completion and avoiding having to correct type channel name each time. --- UberLoggerChannel.cs | 76 +++++++++++++++++++++++++++++++++++++++ UberLoggerChannel.cs.meta | 13 +++++++ 2 files changed, 89 insertions(+) create mode 100644 UberLoggerChannel.cs create mode 100644 UberLoggerChannel.cs.meta diff --git a/UberLoggerChannel.cs b/UberLoggerChannel.cs new file mode 100644 index 0000000..a9ca85d --- /dev/null +++ b/UberLoggerChannel.cs @@ -0,0 +1,76 @@ +using System.Collections; +using System.Collections.Generic; +using UberLogger; +using UnityEngine; + +/// +/// 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. +/// +public class UberLoggerChannel +{ + private string _channelName; + public UberLoggerChannel(string channelName) + { + _channelName = channelName; + } + + /// + /// Gets or sets whether messages sent to this channel should actually be relayed to the logging system or not. + /// + public bool Mute { get; set; } + + [StackTraceIgnore] + public void Log(string message, params object[] par) + { + if (!Mute) + { + UberDebug.LogChannel(_channelName, message, par); + } + } + + [StackTraceIgnore] + public void Log(Object context, string message, params object[] par) + { + if (!Mute) + { + UberDebug.LogChannel(context, _channelName, message, par); + } + } + + [StackTraceIgnore] + public void LogWarning(string message, params object[] par) + { + if (!Mute) + { + UberDebug.LogWarningChannel(_channelName, message, par); + } + } + + [StackTraceIgnore] + public void LogWarning(Object context, string message, params object[] par) + { + if (!Mute) + { + UberDebug.LogWarningChannel(context, _channelName, message, par); + } + } + + [StackTraceIgnore] + public void LogError(string message, params object[] par) + { + if (!Mute) + { + UberDebug.LogErrorChannel(_channelName, message, par); + } + } + + [StackTraceIgnore] + public void LogError(Object context, string message, params object[] par) + { + if (!Mute) + { + UberDebug.LogErrorChannel(context, _channelName, message, par); + } + } +} diff --git a/UberLoggerChannel.cs.meta b/UberLoggerChannel.cs.meta new file mode 100644 index 0000000..291728a --- /dev/null +++ b/UberLoggerChannel.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 6ffc2b9216ac83f4db26a958afcdac7b +timeCreated: 1519677969 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 32ef3266227e4c673d5dd1c002e7f1b81d7e371e Mon Sep 17 00:00:00 2001 From: Anton Struyk Date: Tue, 27 Feb 2018 13:16:11 -0500 Subject: [PATCH 2/3] Added ability to filter based on log level (instead of just muting channel entirely) and added tests to ensure filters are working. --- Examples/TestUberLogger.cs | 43 ++++++++++++++++++++++++++++++++++++++ UberLoggerChannel.cs | 29 ++++++++++++++++++------- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/Examples/TestUberLogger.cs b/Examples/TestUberLogger.cs index 95d1c4f..26e8a8d 100755 --- a/Examples/TestUberLogger.cs +++ b/Examples/TestUberLogger.cs @@ -4,6 +4,8 @@ public class TestUberLogger : MonoBehaviour { + public static readonly UberLoggerChannel TestChannelWrapper = new UberLoggerChannel("WrapperChannel"); + Thread TestThread; // Use this for initialization void Start () @@ -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.HideLogs; + RunChannelWrapperTests(); + + // Will hide .LogWarning(...) calls in test function. + TestChannelWrapper.Filter = UberLoggerChannel.Filters.HideWarnings; + RunChannelWrapperTests(); + + // Will hide .LogError(...) calls in test function. + TestChannelWrapper.Filter = UberLoggerChannel.Filters.HideErrors; + RunChannelWrapperTests(); + + // Will hide .Log(...) and LogWarning(...) calls in test function. + TestChannelWrapper.Filter = UberLoggerChannel.Filters.HideLogs | UberLoggerChannel.Filters.HideWarnings; + 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 diff --git a/UberLoggerChannel.cs b/UberLoggerChannel.cs index a9ca85d..285a91a 100644 --- a/UberLoggerChannel.cs +++ b/UberLoggerChannel.cs @@ -13,17 +13,30 @@ public class UberLoggerChannel public UberLoggerChannel(string channelName) { _channelName = channelName; + Filter = Filters.None; } /// - /// Gets or sets whether messages sent to this channel should actually be relayed to the logging system or not. + /// Filters for preventing display of certain message types. /// - public bool Mute { get; set; } + [System.Flags] + public enum Filters + { + None = 0, + HideLogs = 1, + HideWarnings = 2, + HideErrors = 4 + } + + /// + /// Gets or sets the current filters being applied to this channel. Messages that match the specified set of flags will be ignored. + /// + public Filters Filter { get; set; } [StackTraceIgnore] public void Log(string message, params object[] par) { - if (!Mute) + if ((Filter & Filters.HideLogs) != Filters.HideLogs) { UberDebug.LogChannel(_channelName, message, par); } @@ -32,7 +45,7 @@ public void Log(string message, params object[] par) [StackTraceIgnore] public void Log(Object context, string message, params object[] par) { - if (!Mute) + if ((Filter & Filters.HideLogs) != Filters.HideLogs) { UberDebug.LogChannel(context, _channelName, message, par); } @@ -41,7 +54,7 @@ public void Log(Object context, string message, params object[] par) [StackTraceIgnore] public void LogWarning(string message, params object[] par) { - if (!Mute) + if ((Filter & Filters.HideWarnings) != Filters.HideWarnings) { UberDebug.LogWarningChannel(_channelName, message, par); } @@ -50,7 +63,7 @@ public void LogWarning(string message, params object[] par) [StackTraceIgnore] public void LogWarning(Object context, string message, params object[] par) { - if (!Mute) + if ((Filter & Filters.HideWarnings) != Filters.HideWarnings) { UberDebug.LogWarningChannel(context, _channelName, message, par); } @@ -59,7 +72,7 @@ public void LogWarning(Object context, string message, params object[] par) [StackTraceIgnore] public void LogError(string message, params object[] par) { - if (!Mute) + if ((Filter & Filters.HideErrors) != Filters.HideErrors) { UberDebug.LogErrorChannel(_channelName, message, par); } @@ -68,7 +81,7 @@ public void LogError(string message, params object[] par) [StackTraceIgnore] public void LogError(Object context, string message, params object[] par) { - if (!Mute) + if ((Filter & Filters.HideErrors) != Filters.HideErrors) { UberDebug.LogErrorChannel(context, _channelName, message, par); } From 57142fa555289c35bb5e52ecddc9336ac1fd5cfb Mon Sep 17 00:00:00 2001 From: Anton Struyk Date: Wed, 28 Feb 2018 12:19:13 -0500 Subject: [PATCH 3/3] Code review changes. * Use << to shift bits instead of literal values * Remove 'Hide' prefix from filters that cause calls to be ignored * Compare with '== 0' instead of ' != Filters.XXX' --- Examples/TestUberLogger.cs | 8 ++++---- UberLoggerChannel.cs | 34 +++++++++++++++++----------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Examples/TestUberLogger.cs b/Examples/TestUberLogger.cs index 26e8a8d..72f6e8e 100755 --- a/Examples/TestUberLogger.cs +++ b/Examples/TestUberLogger.cs @@ -78,19 +78,19 @@ public void DoTest() RunChannelWrapperTests(); // Will hide .Log(...) calls in test function. - TestChannelWrapper.Filter = UberLoggerChannel.Filters.HideLogs; + TestChannelWrapper.Filter = UberLoggerChannel.Filters.Logs; RunChannelWrapperTests(); // Will hide .LogWarning(...) calls in test function. - TestChannelWrapper.Filter = UberLoggerChannel.Filters.HideWarnings; + TestChannelWrapper.Filter = UberLoggerChannel.Filters.Warnings; RunChannelWrapperTests(); // Will hide .LogError(...) calls in test function. - TestChannelWrapper.Filter = UberLoggerChannel.Filters.HideErrors; + TestChannelWrapper.Filter = UberLoggerChannel.Filters.Errors; RunChannelWrapperTests(); // Will hide .Log(...) and LogWarning(...) calls in test function. - TestChannelWrapper.Filter = UberLoggerChannel.Filters.HideLogs | UberLoggerChannel.Filters.HideWarnings; + TestChannelWrapper.Filter = UberLoggerChannel.Filters.Logs | UberLoggerChannel.Filters.Warnings; RunChannelWrapperTests(); } diff --git a/UberLoggerChannel.cs b/UberLoggerChannel.cs index 285a91a..3f67c43 100644 --- a/UberLoggerChannel.cs +++ b/UberLoggerChannel.cs @@ -9,10 +9,10 @@ /// public class UberLoggerChannel { - private string _channelName; + private string ChannelName; public UberLoggerChannel(string channelName) { - _channelName = channelName; + ChannelName = channelName; Filter = Filters.None; } @@ -23,9 +23,9 @@ public UberLoggerChannel(string channelName) public enum Filters { None = 0, - HideLogs = 1, - HideWarnings = 2, - HideErrors = 4 + Logs = 1 << 0, + Warnings = 1 << 1, + Errors = 1 << 2 } /// @@ -36,54 +36,54 @@ public enum Filters [StackTraceIgnore] public void Log(string message, params object[] par) { - if ((Filter & Filters.HideLogs) != Filters.HideLogs) + if ((Filter & Filters.Logs) == 0) { - UberDebug.LogChannel(_channelName, message, par); + UberDebug.LogChannel(ChannelName, message, par); } } [StackTraceIgnore] public void Log(Object context, string message, params object[] par) { - if ((Filter & Filters.HideLogs) != Filters.HideLogs) + if ((Filter & Filters.Logs) == 0) { - UberDebug.LogChannel(context, _channelName, message, par); + UberDebug.LogChannel(context, ChannelName, message, par); } } [StackTraceIgnore] public void LogWarning(string message, params object[] par) { - if ((Filter & Filters.HideWarnings) != Filters.HideWarnings) + if ((Filter & Filters.Warnings) == 0) { - UberDebug.LogWarningChannel(_channelName, message, par); + UberDebug.LogWarningChannel(ChannelName, message, par); } } [StackTraceIgnore] public void LogWarning(Object context, string message, params object[] par) { - if ((Filter & Filters.HideWarnings) != Filters.HideWarnings) + if ((Filter & Filters.Warnings) == 0) { - UberDebug.LogWarningChannel(context, _channelName, message, par); + UberDebug.LogWarningChannel(context, ChannelName, message, par); } } [StackTraceIgnore] public void LogError(string message, params object[] par) { - if ((Filter & Filters.HideErrors) != Filters.HideErrors) + if ((Filter & Filters.Errors) == 0) { - UberDebug.LogErrorChannel(_channelName, message, par); + UberDebug.LogErrorChannel(ChannelName, message, par); } } [StackTraceIgnore] public void LogError(Object context, string message, params object[] par) { - if ((Filter & Filters.HideErrors) != Filters.HideErrors) + if ((Filter & Filters.Errors) == 0) { - UberDebug.LogErrorChannel(context, _channelName, message, par); + UberDebug.LogErrorChannel(context, ChannelName, message, par); } } }