feat: update appdebug command

`appdebug` command now takes levels as either integers 0-5 or Serilg `LogEventLevel` strings, `Information` etc.
This commit is contained in:
Andrew Welker
2024-03-20 15:56:00 -05:00
parent b41f4190be
commit a7f9799370
2 changed files with 52 additions and 39 deletions

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using Crestron.SimplSharp.Reflection; using System.Reflection;
using Crestron.SimplSharp.CrestronLogger; using Crestron.SimplSharp.CrestronLogger;
using Crestron.SimplSharp.CrestronIO; using Crestron.SimplSharp.CrestronIO;
using Newtonsoft.Json; using Newtonsoft.Json;
@@ -21,8 +21,9 @@ namespace PepperDash.Core
/// </summary> /// </summary>
public static class Debug public static class Debug
{ {
private static string LevelStoreKey = "ConsoleDebugLevel"; private static readonly string LevelStoreKey = "ConsoleDebugLevel";
private static Dictionary<uint, LogEventLevel> _logLevels = new Dictionary<uint, LogEventLevel>()
private static readonly Dictionary<uint, LogEventLevel> _logLevels = new Dictionary<uint, LogEventLevel>()
{ {
{0, LogEventLevel.Information }, {0, LogEventLevel.Information },
{3, LogEventLevel.Warning }, {3, LogEventLevel.Warning },
@@ -32,18 +33,18 @@ namespace PepperDash.Core
{2, LogEventLevel.Verbose }, {2, LogEventLevel.Verbose },
}; };
private static Logger _logger; private static readonly Logger _logger;
private static LoggingLevelSwitch _consoleLoggingLevelSwitch; private static readonly LoggingLevelSwitch _consoleLoggingLevelSwitch;
private static LoggingLevelSwitch _websocketLoggingLevelSwitch; private static readonly LoggingLevelSwitch _websocketLoggingLevelSwitch;
public static LogEventLevel WebsocketMinimumLogLevel public static LogEventLevel WebsocketMinimumLogLevel
{ {
get { return _websocketLoggingLevelSwitch.MinimumLevel; } get { return _websocketLoggingLevelSwitch.MinimumLevel; }
} }
private static DebugWebsocketSink _websocketSink; private static readonly DebugWebsocketSink _websocketSink;
public static DebugWebsocketSink WebsocketSink public static DebugWebsocketSink WebsocketSink
{ {
@@ -102,13 +103,14 @@ namespace PepperDash.Core
static Debug() static Debug()
{ {
CrestronDataStoreStatic.InitCrestronDataStore();
var defaultConsoleLevel = GetStoredLogEventLevel(); var defaultConsoleLevel = GetStoredLogEventLevel();
_consoleLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: defaultConsoleLevel); _consoleLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Information);
_consoleLoggingLevelSwitch.MinimumLevelChanged += (sender, args) =>
{ _consoleLoggingLevelSwitch.MinimumLevel = defaultConsoleLevel;
Console(0, "Console debug level set to {0}", _consoleLoggingLevelSwitch.MinimumLevel);
};
_websocketLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Verbose); _websocketLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Verbose);
_websocketSink = new DebugWebsocketSink(new JsonFormatter(renderMessage: true)); _websocketSink = new DebugWebsocketSink(new JsonFormatter(renderMessage: true));
@@ -125,15 +127,11 @@ namespace PepperDash.Core
// Get the assembly version and print it to console and the log // Get the assembly version and print it to console and the log
GetVersion(); GetVersion();
string msg = ""; string msg = $"[App {InitialParametersClass.ApplicationNumber}] Using PepperDash_Core v{PepperDashCoreVersion}";
if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance) if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Server)
{ {
msg = string.Format("[App {0}] Using PepperDash_Core v{1}", InitialParametersClass.ApplicationNumber, PepperDashCoreVersion); msg = $"[Room {InitialParametersClass.RoomId}] Using PepperDash_Core v{PepperDashCoreVersion}";
}
else if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Server)
{
msg = string.Format("[Room {0}] Using PepperDash_Core v{1}", InitialParametersClass.RoomId, PepperDashCoreVersion);
} }
CrestronConsole.PrintLine(msg); CrestronConsole.PrintLine(msg);
@@ -142,7 +140,6 @@ namespace PepperDash.Core
IncludedExcludedKeys = new Dictionary<string, object>(); IncludedExcludedKeys = new Dictionary<string, object>();
//CrestronDataStoreStatic.InitCrestronDataStore();
if (CrestronEnvironment.RuntimeEnvironment == eRuntimeEnvironment.SimplSharpPro) if (CrestronEnvironment.RuntimeEnvironment == eRuntimeEnvironment.SimplSharpPro)
{ {
// Add command to console // Add command to console
@@ -188,22 +185,35 @@ namespace PepperDash.Core
CrestronConsole.PrintLine("Initializing of CrestronLogger failed: {0}", e); CrestronConsole.PrintLine("Initializing of CrestronLogger failed: {0}", e);
} }
_consoleLoggingLevelSwitch.MinimumLevelChanged += (sender, args) =>
{
Console(0, "Console debug level set to {0}", _consoleLoggingLevelSwitch.MinimumLevel);
};
} }
private static LogEventLevel GetStoredLogEventLevel() private static LogEventLevel GetStoredLogEventLevel()
{ {
try try
{ {
var result = CrestronDataStoreStatic.GetLocalUintValue(LevelStoreKey, out uint logLevel); var result = CrestronDataStoreStatic.GetLocalIntValue(LevelStoreKey, out int logLevel);
if(result != CrestronDataStore.CDS_ERROR.CDS_SUCCESS || logLevel > 5 || logLevel < 0) if (result != CrestronDataStore.CDS_ERROR.CDS_SUCCESS)
{ {
CrestronConsole.Print($"Unable to retrieve stored log level.\r\nError: {result}.\r\nSetting level to {LogEventLevel.Information}\r\n");
return LogEventLevel.Information; return LogEventLevel.Information;
} }
return _logLevels[logLevel]; if(logLevel < 0 || logLevel > 5)
} catch {
CrestronConsole.PrintLine($"Stored Log level not valid: {logLevel}. Setting level to {LogEventLevel.Information}");
return LogEventLevel.Information;
}
return (LogEventLevel)logLevel;
} catch (Exception ex)
{ {
CrestronConsole.PrintLine($"Exception retrieving log level: {ex.Message}");
return LogEventLevel.Information; return LogEventLevel.Information;
} }
} }
@@ -217,9 +227,7 @@ namespace PepperDash.Core
if (ver != null && ver.Length > 0) if (ver != null && ver.Length > 0)
{ {
var verAttribute = ver[0] as AssemblyInformationalVersionAttribute; if (ver[0] is AssemblyInformationalVersionAttribute verAttribute)
if (verAttribute != null)
{ {
PepperDashCoreVersion = verAttribute.InformationalVersion; PepperDashCoreVersion = verAttribute.InformationalVersion;
} }
@@ -227,8 +235,7 @@ namespace PepperDash.Core
else else
{ {
var version = assembly.GetName().Version; var version = assembly.GetName().Version;
PepperDashCoreVersion = string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, PepperDashCoreVersion = version.ToString();
version.Revision);
} }
} }
@@ -287,7 +294,7 @@ namespace PepperDash.Core
CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level. If using a number, value must be between 0-5"); CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level. If using a number, value must be between 0-5");
return; return;
} }
SetDebugLevel((LogEventLevel)levelInt); SetDebugLevel((uint) levelInt);
return; return;
} }
@@ -315,7 +322,7 @@ namespace PepperDash.Core
{ {
logLevel = LogEventLevel.Information; logLevel = LogEventLevel.Information;
CrestronConsole.PrintLine($"{level} not valid. Setting level to {logLevel}"); CrestronConsole.ConsoleCommandResponse($"{level} not valid. Setting level to {logLevel}");
SetDebugLevel(logLevel); SetDebugLevel(logLevel);
} }
@@ -327,10 +334,15 @@ namespace PepperDash.Core
{ {
_consoleLoggingLevelSwitch.MinimumLevel = level; _consoleLoggingLevelSwitch.MinimumLevel = level;
CrestronConsole.ConsoleCommandResponse("[Application {0}], Debug level set to {1}", CrestronConsole.ConsoleCommandResponse("[Application {0}], Debug level set to {1}\r\n",
InitialParametersClass.ApplicationNumber, _consoleLoggingLevelSwitch.MinimumLevel); InitialParametersClass.ApplicationNumber, _consoleLoggingLevelSwitch.MinimumLevel);
var err = CrestronDataStoreStatic.SetLocalUintValue("ConsoleDebugLevel", (uint) level); CrestronConsole.ConsoleCommandResponse($"Storing level {level}:{(int) level}");
var err = CrestronDataStoreStatic.SetLocalIntValue(LevelStoreKey, (int) level);
CrestronConsole.ConsoleCommandResponse($"Store result: {err}:{(int)level}");
if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS) if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS)
CrestronConsole.PrintLine($"Error saving console debug level setting: {err}"); CrestronConsole.PrintLine($"Error saving console debug level setting: {err}");
} }
@@ -361,7 +373,7 @@ namespace PepperDash.Core
return; return;
} }
SetDoNotLoadConfigOnNextBoot(Boolean.Parse(stateString)); SetDoNotLoadConfigOnNextBoot(bool.Parse(stateString));
} }
catch catch
{ {
@@ -734,7 +746,7 @@ namespace PepperDash.Core
{ {
if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance) if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance)
{ {
CheckForMigration(); // CheckForMigration();
return string.Format(@"\user\debugSettings\program{0}", InitialParametersClass.ApplicationNumber); return string.Format(@"\user\debugSettings\program{0}", InitialParametersClass.ApplicationNumber);
} }

View File

@@ -3,7 +3,7 @@
<RootNamespace>PepperDash.Core</RootNamespace> <RootNamespace>PepperDash.Core</RootNamespace>
<AssemblyName>PepperDashCore</AssemblyName> <AssemblyName>PepperDashCore</AssemblyName>
<TargetFrameworks>net472;net6</TargetFrameworks> <TargetFrameworks>net472;net6</TargetFrameworks>
<Deterministic>false</Deterministic> <Deterministic>true</Deterministic>
<NeutralLanguage>en</NeutralLanguage> <NeutralLanguage>en</NeutralLanguage>
<OutputPath>bin\$(Configuration)\</OutputPath> <OutputPath>bin\$(Configuration)\</OutputPath>
<SignAssembly>False</SignAssembly> <SignAssembly>False</SignAssembly>
@@ -13,9 +13,10 @@
<RepositoryType>git</RepositoryType> <RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/PepperDash/PepperDashCore</RepositoryUrl> <RepositoryUrl>https://github.com/PepperDash/PepperDashCore</RepositoryUrl>
<PackageTags>crestron;4series;</PackageTags> <PackageTags>crestron;4series;</PackageTags>
<Version>$(Version)</Version> <Version>2.0.0-local</Version>
<InformationalVersion>$(Version)</InformationalVersion> <InformationalVersion>$(Version)</InformationalVersion>
<PackageOutputPath>../../package</PackageOutputPath> <PackageOutputPath>../../package</PackageOutputPath>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType> <DebugType>full</DebugType>
@@ -28,7 +29,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="System.Data.DataSetExtensions" /> <Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Net.Http" /> <Reference Include="System.Net.Http" />D
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="BouncyCastle" Version="1.8.9" /> <PackageReference Include="BouncyCastle" Version="1.8.9" />