From 1abffd82cabec3f6a8dbcea732fa5415288fca6d Mon Sep 17 00:00:00 2001 From: Neil Dorin Date: Thu, 23 Nov 2023 12:11:36 -0700 Subject: [PATCH] fix: reworks how the console debug level is set --- .../JsonToSimpl/JsonToSimplFileMaster.cs | 2 +- .../JsonToSimplPortalFileMaster.cs | 2 +- src/Pepperdash Core/Logging/Debug.cs | 82 ++++++++++--------- 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/Pepperdash Core/JsonToSimpl/JsonToSimplFileMaster.cs b/src/Pepperdash Core/JsonToSimpl/JsonToSimplFileMaster.cs index 521cecc..011c63e 100644 --- a/src/Pepperdash Core/JsonToSimpl/JsonToSimplFileMaster.cs +++ b/src/Pepperdash Core/JsonToSimpl/JsonToSimplFileMaster.cs @@ -197,7 +197,7 @@ namespace PepperDash.Core.JsonToSimpl /// Sets the debug level /// /// - public void setDebugLevel(int level) + public void setDebugLevel(uint level) { Debug.SetDebugLevel(level); } diff --git a/src/Pepperdash Core/JsonToSimpl/JsonToSimplPortalFileMaster.cs b/src/Pepperdash Core/JsonToSimpl/JsonToSimplPortalFileMaster.cs index 8a85b33..fa0bc94 100644 --- a/src/Pepperdash Core/JsonToSimpl/JsonToSimplPortalFileMaster.cs +++ b/src/Pepperdash Core/JsonToSimpl/JsonToSimplPortalFileMaster.cs @@ -134,7 +134,7 @@ namespace PepperDash.Core.JsonToSimpl /// /// /// - public void setDebugLevel(int level) + public void setDebugLevel(uint level) { Debug.SetDebugLevel(level); } diff --git a/src/Pepperdash Core/Logging/Debug.cs b/src/Pepperdash Core/Logging/Debug.cs index eeccef5..8341f6e 100644 --- a/src/Pepperdash Core/Logging/Debug.cs +++ b/src/Pepperdash Core/Logging/Debug.cs @@ -12,6 +12,8 @@ using Serilog; using Serilog.Core; using Serilog.Events; using Serilog.Formatting.Json; +using Crestron.SimplSharp.CrestronDataStore; +using System.Linq; namespace PepperDash.Core { @@ -30,15 +32,15 @@ namespace PepperDash.Core /// public static class Debug { - //private static Dictionary> _logActions = new Dictionary>() - //{ - // {0, (s) => _logger.Information(s) }, - // {1, (s) => _logger.Warning(s) }, - // {2, (s) => _logger.Error(s) }, - // {3, (s) => _logger.Fatal(s) }, - // {4, (s) => _logger.Debug(s) }, - // {5, (s) => _logger.Verbose(s) }, - //}; + private static Dictionary _logLevels = new Dictionary() + { + {0, LogEventLevel.Information }, + {1, LogEventLevel.Warning }, + {2, LogEventLevel.Error }, + {3, LogEventLevel.Fatal }, + {4, LogEventLevel.Debug }, + {5, LogEventLevel.Verbose }, + }; private static Logger _logger; @@ -157,7 +159,7 @@ namespace PepperDash.Core "donotloadonnextboot:P [true/false]: Should the application load on next boot", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(SetDebugFromConsole, "appdebug", - "appdebug:P [0-2]: Sets the application's console debug message level", + "appdebug:P [0-5]: Sets the application's console debug message level", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(ShowDebugLog, "appdebuglog", "appdebuglog:P [all] Use \"all\" for full log.", @@ -252,14 +254,15 @@ namespace PepperDash.Core { if (levelString.Trim() == "?") { - CrestronConsole.ConsoleCommandResponse( +CrestronConsole.ConsoleCommandResponse( $@"Used to set the minimum level of debug messages to be printed to the console: -{eDebugLevel.Information.ToString()} = {eDebugLevel.Information} -{eDebugLevel.Warning.ToString()} = {eDebugLevel.Warning} -{eDebugLevel.Error.ToString()} = {eDebugLevel.Error} -{eDebugLevel.Fatal.ToString()} = {eDebugLevel.Fatal} -{eDebugLevel.Debug.ToString()} = {eDebugLevel.Debug} -{eDebugLevel.Verbose.ToString()} = {eDebugLevel.Verbose}"); +{eDebugLevel.Information} = 0 +{eDebugLevel.Warning} = 1 +{eDebugLevel.Error} = 2 +{eDebugLevel.Fatal} = 3 +{eDebugLevel.Debug} = 4 +{eDebugLevel.Verbose} = 5"); + return; } if (string.IsNullOrEmpty(levelString.Trim())) @@ -268,7 +271,10 @@ $@"Used to set the minimum level of debug messages to be printed to the console: return; } - SetDebugLevel(Convert.ToInt32(levelString)); + var level = Convert.ToUInt32(levelString); + + if (_logLevels.ContainsKey(level)) + SetDebugLevel(level); } catch { @@ -276,15 +282,33 @@ $@"Used to set the minimum level of debug messages to be printed to the console: } } - public static void SetConsoleDebugLevel(LogEventLevel level) + /// + /// Sets the debug level + /// + /// Valid values 0-5 + public static void SetDebugLevel(uint level) { - _consoleLoggingLevelSwitch.MinimumLevel = level; + if (_logLevels.ContainsKey(level)) + _consoleLoggingLevelSwitch.MinimumLevel = _logLevels[level]; + + CrestronConsole.ConsoleCommandResponse("[Application {0}], Debug level set to {1}", + InitialParametersClass.ApplicationNumber, _consoleLoggingLevelSwitch.MinimumLevel); + + var err = CrestronDataStoreStatic.SetLocalUintValue("ConsoleDebugLevel", level); + if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS) + CrestronConsole.PrintLine("Error saving console debug level setting: {0}", err); } public static void SetWebSocketMinimumDebugLevel(LogEventLevel level) { _websocketLoggingLevelSwitch.MinimumLevel = level; + var levelInt = _logLevels.FirstOrDefault((l) => l.Value.Equals(level)).Key; + + var err = CrestronDataStoreStatic.SetLocalUintValue("WebsocketDebugLevel", levelInt); + if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS) + Console(0, "Error saving websocket debug level setting: {0}", err); + Console(0, "Websocket debug level set to {0}", _websocketLoggingLevelSwitch.MinimumLevel); } @@ -390,25 +414,7 @@ $@"Used to set the minimum level of debug messages to be printed to the console: } - /// - /// Sets the debug level - /// - /// Valid values 0 (no debug), 1 (critical), 2 (all messages) - public static void SetDebugLevel(int level) - { - _consoleLoggingLevelSwitch.MinimumLevel = (LogEventLevel)level; - //if (level <= 5) - //{ - - // CrestronConsole.ConsoleCommandResponse("[Application {0}], Debug level set to {1}", - // InitialParametersClass.ApplicationNumber, _consoleLoggingLevelSwitch.MinimumLevel); - - // //var err = CrestronDataStoreStatic.SetLocalUintValue("DebugLevel", level); - // //if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS) - // // CrestronConsole.PrintLine("Error saving console debug level setting: {0}", err); - //} - } /// /// sets the settings for a device or creates a new entry