From 9dfc56740a5300937f62feb1bf89e4d7ceca8310 Mon Sep 17 00:00:00 2001 From: Nick Genovese Date: Mon, 11 Nov 2024 09:41:25 -0500 Subject: [PATCH] feat: added --devices all flag for console debug context --- src/Pepperdash.Core/Debug.cs | 16 +++++++++---- .../Logging/DebugConsoleSink.cs | 23 ++++++++++--------- src/Pepperdash.Core/Logging/DebugContext.cs | 13 +++++------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/Pepperdash.Core/Debug.cs b/src/Pepperdash.Core/Debug.cs index aee50a3..dcbce40 100644 --- a/src/Pepperdash.Core/Debug.cs +++ b/src/Pepperdash.Core/Debug.cs @@ -124,7 +124,7 @@ namespace PepperDash.Core if (supportsRemovableDrive) DefaultLoggerConfiguration.WriteTo.Sink(new DebugCrestronLoggerSink()); - var storedConsoleLevel = DebugContext.GetDataForKey(ConsoleLevelStoreKey, LogEventLevel.Information); + var storedConsoleLevel = DebugContext.GetOrCreateDataForKey(ConsoleLevelStoreKey, LogEventLevel.Information); ConsoleLoggingLevelSwitch.MinimumLevel = storedConsoleLevel.Level; CrestronConsole.PrintLine("Beginning console logging with level:{0}", storedConsoleLevel.Level); @@ -202,9 +202,17 @@ namespace PepperDash.Core .ToArray() : []; + if (LogLevels.TryGetValue(level, out var logEventLevel)) { - SetConsoleDebugLevel(logEventLevel, devices); + if (devices.Length == 1 && devices[0].ToLower() == "all") + { + SetConsoleDebugLevel(logEventLevel, true, []); + } + else + { + SetConsoleDebugLevel(logEventLevel, false, devices); + } } else { @@ -222,10 +230,10 @@ namespace PepperDash.Core } } - public static void SetConsoleDebugLevel(LogEventLevel level, string[] includedDevices = null) + public static void SetConsoleDebugLevel(LogEventLevel level, bool logAllDevices = false, string[] includedDevices = null) { ConsoleLoggingLevelSwitch.MinimumLevel = level; - DebugContext.SetDataForKey(ConsoleLevelStoreKey, level, includedDevices); + DebugContext.SetDataForKey(ConsoleLevelStoreKey, level, logAllDevices, includedDevices); var includedDevicesMessage = includedDevices == null || includedDevices.Length == 0 ? "all" : string.Join(",", includedDevices); CrestronConsole.ConsoleCommandResponse($"Success: set console debug level to {level} with devices:{includedDevicesMessage}"); diff --git a/src/Pepperdash.Core/Logging/DebugConsoleSink.cs b/src/Pepperdash.Core/Logging/DebugConsoleSink.cs index dc9ef15..bf14643 100644 --- a/src/Pepperdash.Core/Logging/DebugConsoleSink.cs +++ b/src/Pepperdash.Core/Logging/DebugConsoleSink.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Linq; using System.Text; using Crestron.SimplSharp; @@ -57,21 +58,21 @@ namespace PepperDash.Core.Logging return false; } - if (@event.Properties.TryGetValue("Key", out var value) && value is ScalarValue { Value: string rawValue } - && DebugContext.TryGetDataForKey(Debug.ConsoleLevelStoreKey, out var data) - && data.Devices != null) + if (@event.Level >= LogEventLevel.Error) { - if (data.Devices.Length == 0) + return true; + } + + if (@event.Properties.TryGetValue("Key", out var value) && value is ScalarValue { Value: string rawValue } + && DebugContext.TryGetDataForKey(Debug.ConsoleLevelStoreKey, out var ctx)) + { + if (ctx.LogAllDevices) { return true; } - if (data.Devices.Any(d => d == rawValue)) - { - return true; - } - - return false; + return ctx.Devices is not null + && ctx.Devices.Any(deviceKey => string.Equals(deviceKey, rawValue, StringComparison.OrdinalIgnoreCase)); } return true; diff --git a/src/Pepperdash.Core/Logging/DebugContext.cs b/src/Pepperdash.Core/Logging/DebugContext.cs index 6e093e9..1409966 100644 --- a/src/Pepperdash.Core/Logging/DebugContext.cs +++ b/src/Pepperdash.Core/Logging/DebugContext.cs @@ -49,21 +49,20 @@ namespace PepperDash.Core.Logging } public static bool TryGetDataForKey(string key, out DebugContextData data) => - CurrentData.TryGetValue(key, out var data); + CurrentData.TryGetValue(key, out data); public static DebugContextData GetOrCreateDataForKey(string key, LogEventLevel defaultLevel) => - CurrentData.TryGetValue(key, out var data) ? data : new DebugContextData(defaultLevel); + CurrentData.TryGetValue(key, out var data) ? data : new DebugContextData(defaultLevel, false, []); - - public static void SetDataForKey(string key, LogEventLevel defaultLevel, string[] devices = null) + public static void SetDataForKey(string key, LogEventLevel defaultLevel, bool logAllDevices = false, string[] devices = null) { if (CurrentData.ContainsKey(key)) { - CurrentData[key] = new DebugContextData(defaultLevel, devices); + CurrentData[key] = new DebugContextData(defaultLevel, logAllDevices, devices); } else { - CurrentData.Add(key, new DebugContextData(defaultLevel, devices)); + CurrentData.Add(key, new DebugContextData(defaultLevel, logAllDevices, devices)); } SaveTimer.Reset(5000); @@ -103,5 +102,5 @@ namespace PepperDash.Core.Logging /// /// /// - public record DebugContextData(LogEventLevel Level, string[] Devices = null); + public record DebugContextData(LogEventLevel Level, bool LogAllDevices = false, string[] Devices = null); }