Skip to content

Commit

Permalink
fix: consolidates log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ndorin committed Nov 22, 2023
1 parent 15024b5 commit 3debb30
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions src/Pepperdash Core/Logging/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static DebugWebsocketSink WebsocketSink
/// <summary>
/// When this is true, the configuration file will NOT be loaded until triggered by either a console command or a signal
/// </summary>
public static bool DoNotLoadOnNextBoot { get; private set; }
public static bool DoNotLoadConfigOnNextBoot { get; private set; }

private static DebugContextCollection _contexts;

Expand Down Expand Up @@ -107,6 +107,10 @@ public static DebugWebsocketSink WebsocketSink
static Debug()
{
_consoleLoggingLevelSwitch = new LoggingLevelSwitch(initialMinimumLevel: LogEventLevel.Information);
_consoleLoggingLevelSwitch.MinimumLevelChanged += (sender, args) =>
{
Debug.Console(0, "Console debug level set to {0}", _consoleLoggingLevelSwitch.MinimumLevel);
};
_websocketLoggingLevelSwitch = new LoggingLevelSwitch();
_websocketSink = new DebugWebsocketSink(new JsonFormatter(renderMessage: true));

Expand Down Expand Up @@ -167,9 +171,9 @@ static Debug()

var context = _contexts.GetOrCreateItem("DEFAULT");
Level = context.Level;
DoNotLoadOnNextBoot = context.DoNotLoadOnNextBoot;
DoNotLoadConfigOnNextBoot = context.DoNotLoadOnNextBoot;

if(DoNotLoadOnNextBoot)
if(DoNotLoadConfigOnNextBoot)
CrestronConsole.PrintLine(string.Format("Program {0} will not load config after next boot. Use console command go:{0} to load the config manually", InitialParametersClass.ApplicationNumber));

try
Expand Down Expand Up @@ -244,15 +248,15 @@ public static void SetDebugFromConsole(string levelString)
{
if (string.IsNullOrEmpty(levelString.Trim()))
{
CrestronConsole.ConsoleCommandResponse("AppDebug level = {0}", Level);
CrestronConsole.ConsoleCommandResponse("AppDebug level = {0}", _consoleLoggingLevelSwitch);
return;
}

SetDebugLevel(Convert.ToInt32(levelString));
}
catch
{
CrestronConsole.ConsoleCommandResponse("Usage: appdebug:P [0-2]");
CrestronConsole.ConsoleCommandResponse("Usage: appdebug:P [0-5]");
}
}

Expand All @@ -276,11 +280,11 @@ public static void SetDoNotLoadOnNextBootFromConsole(string stateString)
{
if (string.IsNullOrEmpty(stateString.Trim()))
{
CrestronConsole.ConsoleCommandResponse("DoNotLoadOnNextBoot = {0}", DoNotLoadOnNextBoot);
CrestronConsole.ConsoleCommandResponse("DoNotLoadOnNextBoot = {0}", DoNotLoadConfigOnNextBoot);
return;
}

SetDoNotLoadOnNextBoot(Boolean.Parse(stateString));
SetDoNotLoadConfigOnNextBoot(Boolean.Parse(stateString));
}
catch
{
Expand Down Expand Up @@ -376,20 +380,16 @@ public static void SetDebugLevel(int level)
{
_consoleLoggingLevelSwitch.MinimumLevel = (LogEventLevel)level;

if (level <= 5)
{
Level = level;

_contexts.GetOrCreateItem("DEFAULT").Level = level;
SaveMemoryOnTimeout();
//if (level <= 5)
//{

CrestronConsole.ConsoleCommandResponse("[Application {0}], Debug level set to {1}",
InitialParametersClass.ApplicationNumber, Level);
// 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);
}
// //var err = CrestronDataStoreStatic.SetLocalUintValue("DebugLevel", level);
// //if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS)
// // CrestronConsole.PrintLine("Error saving console debug level setting: {0}", err);
//}
}

/// <summary>
Expand Down Expand Up @@ -418,14 +418,14 @@ public static object GetDeviceDebugSettingsForKey(string deviceKey)
/// Sets the flag to prevent application starting on next boot
/// </summary>
/// <param name="state"></param>
public static void SetDoNotLoadOnNextBoot(bool state)
public static void SetDoNotLoadConfigOnNextBoot(bool state)
{
DoNotLoadOnNextBoot = state;
DoNotLoadConfigOnNextBoot = state;
_contexts.GetOrCreateItem("DEFAULT").DoNotLoadOnNextBoot = state;
SaveMemoryOnTimeout();

CrestronConsole.ConsoleCommandResponse("[Application {0}], Do Not Start on Next Boot set to {1}",
InitialParametersClass.ApplicationNumber, DoNotLoadOnNextBoot);
CrestronConsole.ConsoleCommandResponse("[Application {0}], Do Not Load Config on Next Boot set to {1}",
InitialParametersClass.ApplicationNumber, DoNotLoadConfigOnNextBoot);
}

/// <summary>
Expand All @@ -439,15 +439,15 @@ public static void ShowDebugLog(string s)
}


private static void LogMessage(uint level, string message)
private static void LogMessage(uint level, string format, params object[] items)
{
_logger.Write((LogEventLevel)level, message);
_logger.Write((LogEventLevel)level, format, items);
}

private static void LogMessage(uint level, string message, IKeyed keyed)
private static void LogMessage(uint level, IKeyed keyed, string format, params object[] items)
{
var log = _logger.ForContext("Key", keyed.Key);
log.Write((LogEventLevel)level, message);
log.Write((LogEventLevel)level, format, items);
}


Expand All @@ -460,6 +460,9 @@ private static void LogMessage(uint level, string message, IKeyed keyed)
/// <param name="items">Object parameters</param>
public static void Console(uint level, string format, params object[] items)
{

LogMessage(level, format, items);

if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Server)
{
var logString = string.Format("[level {0}] {1}", level, string.Format(format, items));
Expand All @@ -468,10 +471,6 @@ public static void Console(uint level, string format, params object[] items)
return;
}

var message = string.Format(format, items);

_logger.Write((LogEventLevel)level, message);

//if (IsRunningOnAppliance)
//{
// CrestronConsole.PrintLine("[{0}]App {1} Lvl {2}:{3}", DateTime.Now.ToString("HH:mm:ss.fff"),
Expand All @@ -486,7 +485,7 @@ public static void Console(uint level, string format, params object[] items)
/// </summary>
public static void Console(uint level, IKeyed dev, string format, params object[] items)
{
LogMessage(level, string.Format(format, items), dev);
LogMessage(level, dev, format, items);

//if (Level >= level)
// Console(level, "[{0}] {1}", dev.Key, message);
Expand All @@ -506,7 +505,7 @@ public static void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel,
LogError(errorLogLevel, str);
}

LogMessage(level, str, dev);
LogMessage(level, dev, format, items);

//var log = _logger.ForContext("Key", dev.Key);
//var message = string.Format(format, items);
Expand All @@ -519,9 +518,9 @@ public static void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel,
//}
}

/// <summary>
/// Logs to Console when at-level, and all messages to error log
/// </summary>
/// <summary>
/// Logs to Console when at-level, and all messages to error log
/// </summary>
public static void Console(uint level, ErrorLogLevel errorLogLevel,
string format, params object[] items)
{
Expand All @@ -531,7 +530,7 @@ public static void Console(uint level, ErrorLogLevel errorLogLevel,
LogError(errorLogLevel, str);
}

LogMessage(level, str);
LogMessage(level, format, items);
//if (Level >= level)
//{
// Console(level, str);
Expand All @@ -545,10 +544,11 @@ public static void Console(uint level, ErrorLogLevel errorLogLevel,
/// </summary>
public static void ConsoleWithLog(uint level, string format, params object[] items)
{
LogMessage(level, format, items);

var str = string.Format(format, items);
//if (Level >= level)
// CrestronConsole.PrintLine("App {0}:{1}", InitialParametersClass.ApplicationNumber, str);
LogMessage(level, str);
CrestronLogger.WriteToLog(str, level);
}

Expand All @@ -559,9 +559,9 @@ public static void ConsoleWithLog(uint level, string format, params object[] ite
/// </summary>
public static void ConsoleWithLog(uint level, IKeyed dev, string format, params object[] items)
{
var str = string.Format(format, items);
LogMessage(level, dev, format, items);

LogMessage(level, str, dev);
var str = string.Format(format, items);
CrestronLogger.WriteToLog(string.Format("[{0}] {1}", dev.Key, str), level);
}

Expand Down

0 comments on commit 3debb30

Please sign in to comment.