diff --git a/Pepperdash Core/Pepperdash Core/Debug.cs b/Pepperdash Core/Pepperdash Core/Debug.cs deleted file mode 100644 index d6567c6..0000000 --- a/Pepperdash Core/Pepperdash Core/Debug.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Crestron.SimplSharp; -using Crestron.SimplSharp.CrestronDataStore; - - -namespace PepperDash.Core -{ - public static class Debug - { - - - public static uint Level { get; private set; } - - /// - /// This should called from the ControlSystem Initiailize method. It is only valid - /// in #pro environments because of direct console access and CrestronDataStoreStatic - /// - public static void Initialize() - { - if (CrestronEnvironment.RuntimeEnvironment == eRuntimeEnvironment.SimplSharpPro) - { - // Add command to console - CrestronConsole.AddNewConsoleCommand(SetDebugFromConsole, "appdebug", - "appdebug:P [0-2]: Sets the application's console debug message level", - ConsoleAccessLevelEnum.AccessOperator); - - uint level = 0; - var err = CrestronDataStoreStatic.GetGlobalUintValue("DebugLevel", out level); - if (err == CrestronDataStore.CDS_ERROR.CDS_SUCCESS) - SetDebugLevel(level); - else if (err == CrestronDataStore.CDS_ERROR.CDS_RECORD_NOT_FOUND) - CrestronDataStoreStatic.SetGlobalUintValue("DebugLevel", 0); - else - CrestronConsole.PrintLine("Error restoring console debug level setting: {0}", err); - } - } - - /// - /// Callback for console command - /// - /// - public static void SetDebugFromConsole(string levelString) - { - try - { - if (string.IsNullOrEmpty(levelString.Trim())) - { - CrestronConsole.PrintLine("AppDebug level = {0}", Level); - return; - } - - SetDebugLevel(Convert.ToUInt32(levelString)); - } - catch - { - CrestronConsole.PrintLine("Usage: appdebug:P [0-2]"); - } - } - - /// - /// Sets the debug level - /// - /// Valid values 0 (no debug), 1 (critical), 2 (all messages) - public static void SetDebugLevel(uint level) - { - if (level <= 2) - { - Level = level; - CrestronConsole.PrintLine("[Application {0}], Debug level set to {1}", - InitialParametersClass.ApplicationNumber, Level); - if (CrestronEnvironment.RuntimeEnvironment == eRuntimeEnvironment.SimplSharpPro) - { - var err = CrestronDataStoreStatic.SetGlobalUintValue("DebugLevel", level); - if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS) - CrestronConsole.PrintLine("Error saving console debug level setting: {0}", err); - } - } - } - - /// - /// Prints message to console if current debug level is equal to or higher than the level of this message. - /// Uses CrestronConsole.PrintLine. - /// - /// - /// Console format string - /// Object parameters - public static void Console(uint level, string format, params object[] items) - { - if (Level >= level) - CrestronConsole.PrintLine("App {0}:{1}", InitialParametersClass.ApplicationNumber, - string.Format(format, items)); - } - - /// - /// Appends a device Key to the beginning of a message - /// - public static void Console(uint level, IKeyed dev, string format, params object[] items) - { - if (Level >= level) - Console(level, "[{0}] {1}", dev.Key, string.Format(format, items)); - } - - public static void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel, - string format, params object[] items) - { - if (Level >= level) - { - var str = string.Format("[{0}] {1}", dev.Key, string.Format(format, items)); - Console(level, str); - LogError(errorLogLevel, str); - } - } - - public static void Console(uint level, ErrorLogLevel errorLogLevel, - string format, params object[] items) - { - if (Level >= level) - { - var str = string.Format(format, items); - Console(level, str); - LogError(errorLogLevel, str); - } - } - - public static void LogError(ErrorLogLevel errorLogLevel, string str) - { - string msg = string.Format("App {0}:{1}", InitialParametersClass.ApplicationNumber, str); - switch (errorLogLevel) - { - case ErrorLogLevel.Error: - ErrorLog.Error(msg); - break; - case ErrorLogLevel.Warning: - ErrorLog.Warn(msg); - break; - case ErrorLogLevel.Notice: - ErrorLog.Notice(msg); - break; - } - } - - public enum ErrorLogLevel - { - Error, Warning, Notice, None - } - } -} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/Debug/Debug.cs b/Pepperdash Core/Pepperdash Core/Debug/Debug.cs new file mode 100644 index 0000000..fe59717 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/Debug/Debug.cs @@ -0,0 +1,239 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharp.CrestronDataStore; +using Crestron.SimplSharp.CrestronIO; +using Newtonsoft.Json; +using PepperDash.Core.DebugThings; + + +namespace PepperDash.Core +{ + public static class Debug + { + /// + /// Describes the folder location where a given program stores it's debug level memory. By default, the + /// file written will be named appNdebug where N is 1-10. + /// + public static string FilePathPrefix = @"\nvram\debug\"; + + /// + /// The name of the file containing the current debug settings. + /// + public static string FileName = string.Format(@"app{0}Debug.json", InitialParametersClass.ApplicationNumber); + + public static int Level { get; private set; } + + static DebugContextCollection Contexts; + + static int SaveTimeoutMs = 30000; + + static CTimer SaveTimer; + + /// + /// This should called from the ControlSystem Initiailize method. Will attempt to call + /// CrestronDataStoreStatic.InitCrestronDataStore which may have been called elsewhere. + /// + public static void Initialize() + { + CrestronDataStoreStatic.InitCrestronDataStore(); + if (CrestronEnvironment.RuntimeEnvironment == eRuntimeEnvironment.SimplSharpPro) + { + // Add command to console + CrestronConsole.AddNewConsoleCommand(SetDebugFromConsole, "appdebug", + "appdebug:P [0-2]: Sets the application's console debug message level", + ConsoleAccessLevelEnum.AccessOperator); + } + + CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler; + + LoadMemory(); + Level = Contexts.GetOrCreateItem("DEFAULT").Level; + } + + /// + /// Used to save memory when shutting down + /// + /// + static void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType) + { + if (programEventType == eProgramStatusEventType.Stopping) + { + if (SaveTimer != null) + { + SaveTimer.Stop(); + SaveTimer = null; + } + Console(0, "Saving debug settings"); + SaveMemory(); + } + } + + /// + /// Callback for console command + /// + /// + public static void SetDebugFromConsole(string levelString) + { + try + { + if (string.IsNullOrEmpty(levelString.Trim())) + { + CrestronConsole.PrintLine("AppDebug level = {0}", Level); + return; + } + + SetDebugLevel(Convert.ToInt32(levelString)); + } + catch + { + CrestronConsole.PrintLine("Usage: appdebug:P [0-2]"); + } + } + + /// + /// Sets the debug level + /// + /// Valid values 0 (no debug), 1 (critical), 2 (all messages) + public static void SetDebugLevel(int level) + { + if (level <= 2) + { + Level = level; + Contexts.GetOrCreateItem("DEFAULT").Level = level; + SaveMemoryOnTimeout(); + + CrestronConsole.PrintLine("[Application {0}], Debug level set to {1}", + InitialParametersClass.ApplicationNumber, Level); + + //var err = CrestronDataStoreStatic.SetLocalUintValue("DebugLevel", level); + //if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS) + // CrestronConsole.PrintLine("Error saving console debug level setting: {0}", err); + } + } + + /// + /// Prints message to console if current debug level is equal to or higher than the level of this message. + /// Uses CrestronConsole.PrintLine. + /// + /// + /// Console format string + /// Object parameters + public static void Console(uint level, string format, params object[] items) + { + if (Level >= level) + CrestronConsole.PrintLine("App {0}:{1}", InitialParametersClass.ApplicationNumber, + string.Format(format, items)); + } + + /// + /// Appends a device Key to the beginning of a message + /// + public static void Console(uint level, IKeyed dev, string format, params object[] items) + { + if (Level >= level) + Console(level, "[{0}] {1}", dev.Key, string.Format(format, items)); + } + + public static void Console(uint level, IKeyed dev, ErrorLogLevel errorLogLevel, + string format, params object[] items) + { + if (Level >= level) + { + var str = string.Format("[{0}] {1}", dev.Key, string.Format(format, items)); + Console(level, str); + LogError(errorLogLevel, str); + } + } + + public static void Console(uint level, ErrorLogLevel errorLogLevel, + string format, params object[] items) + { + if (Level >= level) + { + var str = string.Format(format, items); + Console(level, str); + LogError(errorLogLevel, str); + } + } + + public static void LogError(ErrorLogLevel errorLogLevel, string str) + { + string msg = string.Format("App {0}:{1}", InitialParametersClass.ApplicationNumber, str); + switch (errorLogLevel) + { + case ErrorLogLevel.Error: + ErrorLog.Error(msg); + break; + case ErrorLogLevel.Warning: + ErrorLog.Warn(msg); + break; + case ErrorLogLevel.Notice: + ErrorLog.Notice(msg); + break; + } + } + + public enum ErrorLogLevel + { + Error, Warning, Notice, None + } + + + /// + /// Writes the memory object after timeout + /// + static void SaveMemoryOnTimeout() + { + if (SaveTimer == null) + SaveTimer = new CTimer(o => + { + SaveTimer = null; + SaveMemory(); + }, SaveTimeoutMs); + else + SaveTimer.Reset(SaveTimeoutMs); + } + + /// + /// Writes the memory - use SaveMemoryOnTimeout + /// + static void SaveMemory() + { + //var dir = @"\NVRAM\debug"; + //if (!Directory.Exists(dir)) + // Directory.Create(dir); + + using (StreamWriter sw = new StreamWriter(GetMemoryFileName())) + { + var json = JsonConvert.SerializeObject(Contexts); + sw.Write(json); + sw.Flush(); + } + } + + static void LoadMemory() + { + if (File.Exists(GetMemoryFileName())) + { + using (StreamReader sr = new StreamReader(GetMemoryFileName())) + { + var json = sr.ReadToEnd(); + Contexts = JsonConvert.DeserializeObject(json); + } + } + else + Contexts = new DebugContextCollection(); + } + + /// + /// Helper to get the file path for this app's debug memory + /// + static string GetMemoryFileName() + { + return string.Format(@"\NVRAM\debugSettings\program{0}", InitialParametersClass.ApplicationNumber); + } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/Debug/DebugContext.cs b/Pepperdash Core/Pepperdash Core/Debug/DebugContext.cs new file mode 100644 index 0000000..ce3c773 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/Debug/DebugContext.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +namespace PepperDash_Core.Debug +{ + public class DebugContext + { + public string Name { get; private set; } + + public int Level { get; private set; } + + public List Keys { get; private set; } + + + + public DebugContext(string name) + { + + } + + + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/Debug/DebugMemory.cs b/Pepperdash Core/Pepperdash Core/Debug/DebugMemory.cs new file mode 100644 index 0000000..9d1b741 --- /dev/null +++ b/Pepperdash Core/Pepperdash Core/Debug/DebugMemory.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; + +using Newtonsoft.Json; + +namespace PepperDash.Core.DebugThings +{ + public class DebugContextCollection + { + [JsonProperty("items")] + Dictionary Items; + + public DebugContextCollection() + { + Items = new Dictionary(); + } + + /// + /// Sets the level of a given context item, and adds that item if it does not + /// exist + /// + /// + /// + /// True if the 0 <= level <= 2 and the conte + public void SetLevel(string contextKey, int level) + { + if (level < 0 || level > 2) + return; + GetOrCreateItem(contextKey).Level = level; + } + + /// + /// Gets a level or creates it if not existing + /// + /// + /// + public DebugContextItem GetOrCreateItem(string contextKey) + { + if (!Items.ContainsKey(contextKey)) + Items[contextKey] = new DebugContextItem() { Level = 0 }; + return Items[contextKey]; + } + } + + public class DebugContextItem + { + [JsonProperty("level")] + public int Level { get; set; } + } +} \ No newline at end of file diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj index d4a5872..cbbd678 100644 --- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj +++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj @@ -67,7 +67,9 @@ - + + + diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.projectinfo b/Pepperdash Core/Pepperdash Core/PepperDash_Core.projectinfo index 32a3ec3..aa26281 100644 Binary files a/Pepperdash Core/Pepperdash Core/PepperDash_Core.projectinfo and b/Pepperdash Core/Pepperdash Core/PepperDash_Core.projectinfo differ diff --git a/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.clz b/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.clz index 80a2c3e..89cf368 100644 Binary files a/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.clz and b/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.clz differ diff --git a/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.config b/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.config index d4d0cb4..33a2e5b 100644 --- a/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.config +++ b/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.config @@ -10,8 +10,8 @@ - 11/15/2016 12:55:16 PM - 1.0.0.23257 + 11/28/2016 1:59:52 PM + 1.0.0.25195 Crestron.SIMPLSharp, Version=2.0.48.0, Culture=neutral, PublicKeyToken=812d080f93e2de10 diff --git a/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.dll b/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.dll index c42c718..f038257 100644 Binary files a/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.dll and b/Pepperdash Core/Pepperdash Core/bin/PepperDash_Core.dll differ diff --git a/Pepperdash Core/Pepperdash Core/bin/manifest.info b/Pepperdash Core/Pepperdash Core/bin/manifest.info index 09c4f6c..5d87102 100644 --- a/Pepperdash Core/Pepperdash Core/bin/manifest.info +++ b/Pepperdash Core/Pepperdash Core/bin/manifest.info @@ -1,4 +1,4 @@ -MainAssembly=PepperDash_Core.dll:2eb6c529d49d8d4a7e636f33e87b1b6c +MainAssembly=PepperDash_Core.dll:91882e81937f6bb6c3592368dedeea09 MainAssemblyMinFirmwareVersion=1.007.0017 MainAssemblyResource=SimplSharpData.dat:315526abf906cded47fb0c7510266a7e ü diff --git a/Pepperdash Core/Pepperdash Core/bin/manifest.ser b/Pepperdash Core/Pepperdash Core/bin/manifest.ser index 9c0682b..9b266a9 100644 Binary files a/Pepperdash Core/Pepperdash Core/bin/manifest.ser and b/Pepperdash Core/Pepperdash Core/bin/manifest.ser differ