diff --git a/src/Pepperdash.Core/Debug.cs b/src/Pepperdash.Core/Debug.cs index 1bcc586..3059ede 100644 --- a/src/Pepperdash.Core/Debug.cs +++ b/src/Pepperdash.Core/Debug.cs @@ -60,7 +60,12 @@ public static class Debug /// public static string PepperDashCoreVersion { get; } - public static int WebsocketPort { get; } + public static string WebsocketUrl { get; } = ""; + + public static LogEventLevel WebsocketMinimumLevel => WebsocketLoggingLevelSwitch.MinimumLevel; + public static LogEventLevel ConsoleMinimumLevel => ConsoleLoggingLevelSwitch.MinimumLevel; + public static LogEventLevel ErrorLogMinimumLevel => ErrorLogLevelSwitch.MinimumLevel; + public static LogEventLevel FileLogMinimumLevel => FileLevelSwitch.MinimumLevel; public static readonly LoggerConfiguration DefaultLoggerConfiguration; @@ -69,10 +74,11 @@ public static class Debug static Debug() { CrestronDataStoreStatic.InitCrestronDataStore(); + var directorySeparator = Path.DirectorySeparatorChar.ToString(); - var logFilePath = CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance ? - $@"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}user{Path.DirectorySeparatorChar}debug{Path.DirectorySeparatorChar}app{InitialParametersClass.ApplicationNumber}{Path.DirectorySeparatorChar}global-log.log" : - $@"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}user{Path.DirectorySeparatorChar}debug{Path.DirectorySeparatorChar}room{InitialParametersClass.RoomId}{Path.DirectorySeparatorChar}global-log.log"; + var logFilePath = CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance + ? Path.Combine(directorySeparator, "user", "program" + InitialParametersClass.ApplicationNumber, "logs", "global-log.log") + : Path.Combine(directorySeparator, "User", "logs", "global-log.log"); CrestronConsole.PrintLine($"Saving log files to {logFilePath}"); @@ -97,51 +103,41 @@ static Debug() ); #if NET472 - - const string certFilename = "cert.pfx"; - var certPath = IsRunningOnAppliance ? Path.Combine(Path.DirectorySeparatorChar.ToString(), "user", certFilename) : Path.Combine("User", certFilename); + var certPath = IsRunningOnAppliance ? Path.Combine("/", "user") : Path.Combine("/", "User"); var websocket = new DebugNet472WebSocket(certPath); - WebsocketPort = websocket.Port; + WebsocketUrl = websocket.Url; DefaultLoggerConfiguration.WriteTo.Sink(new DebugWebsocketSink(websocket, new JsonFormatter(renderMessage: true)), levelSwitch: WebsocketLoggingLevelSwitch); #endif + var supportsRemovableDrive = false; try { - if (InitialParametersClass.NumberOfRemovableDrives > 0) - { - CrestronConsole.PrintLine("{0} RM Drive(s) Present. Initializing Crestron Logger", InitialParametersClass.NumberOfRemovableDrives); - DefaultLoggerConfiguration.WriteTo.Sink(new DebugCrestronLoggerSink()); - } - else - CrestronConsole.PrintLine("No RM Drive(s) Present. Not using Crestron Logger"); + CrestronLogger.Initialize(1, LoggerModeEnum.RM); + supportsRemovableDrive = true; } - catch (Exception e) + catch (Exception) { - CrestronConsole.PrintLine("Initializing of CrestronLogger failed: {0}", e); + CrestronConsole.PrintLine("No RM Drive(s) Present. Not using Crestron Logger"); } + if (supportsRemovableDrive) + DefaultLoggerConfiguration.WriteTo.Sink(new DebugCrestronLoggerSink()); + var storedConsoleLevel = DebugContext.GetDataForKey(ConsoleLevelStoreKey, LogEventLevel.Information); ConsoleLoggingLevelSwitch.MinimumLevel = storedConsoleLevel.Level; + CrestronConsole.PrintLine("Beginning console logging with level:{0}", storedConsoleLevel.Level); Log.Logger = DefaultLoggerConfiguration.CreateLogger(); PepperDashCoreVersion = GetVersion(); + LogMessage(LogEventLevel.Information, "Using PepperDash_Core v{PepperDashCoreVersion}", PepperDashCoreVersion); - var msg = $"[App {InitialParametersClass.ApplicationNumber}] Using PepperDash_Core v{PepperDashCoreVersion}"; - - if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Server) - { - msg = $"[Room {InitialParametersClass.RoomId}] Using PepperDash_Core v{PepperDashCoreVersion}"; - } - - LogMessage(LogEventLevel.Information,msg); - if (CrestronEnvironment.RuntimeEnvironment == eRuntimeEnvironment.SimplSharpPro) { CrestronConsole.AddNewConsoleCommand(SetDoNotLoadOnNextBootFromConsole, "donotloadonnextboot", "donotloadonnextboot:P [true/false]: Should the application load on next boot", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(SetDebugFromConsole, "appdebug", - "appdebug:P [0-5] --devices [devices]: Sets the application's console debug message level.", + "appdebug:[p] [0-5] --devices [devices]: Sets console debug message level", ConsoleAccessLevelEnum.AccessOperator); CrestronConsole.AddNewConsoleCommand(ShowDebugLog, "appdebuglog", @@ -213,8 +209,10 @@ private static void SetDebugFromConsole(string levelString) CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level"); } } - - CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level"); + else + { + CrestronConsole.ConsoleCommandResponse($"Error: Unable to parse {levelString} to valid log level"); + } } catch { diff --git a/src/Pepperdash.Core/Logging/DebugClient.cs b/src/Pepperdash.Core/Logging/DebugClient.cs index 5dcddef..e5a32c5 100644 --- a/src/Pepperdash.Core/Logging/DebugClient.cs +++ b/src/Pepperdash.Core/Logging/DebugClient.cs @@ -24,39 +24,35 @@ public TimeSpan ConnectedDuration public DebugClient() { - Debug.Console(0, "DebugClient Created"); + Debug.LogInformation("DebugClient Created"); } protected override void OnOpen() { base.OnOpen(); - var url = Context.WebSocket.Url; - Debug.Console(0, Debug.ErrorLogLevel.Notice, "New WebSocket Connection from: {0}", url); - + Debug.LogInformation("New WebSocket Connection from: {Url}", url); connectionTime = DateTime.Now; } protected override void OnMessage(MessageEventArgs e) { base.OnMessage(e); - - Debug.Console(0, "WebSocket UiClient Message: {0}", e.Data); + Debug.LogVerbose("WebSocket UiClient Message: {Data}", e.Data); } protected override void OnClose(CloseEventArgs e) { base.OnClose(e); - - Debug.Console(0, Debug.ErrorLogLevel.Notice, "WebSocket UiClient Closing: {0} reason: {1}", e.Code, e.Reason); + Debug.LogInformation("WebSocket UiClient Closing: {Code} Reason: {Reason} Total Time: {Time}", e.Code, e.Reason, ConnectedDuration); } - protected override void OnError(WebSocketSharp.ErrorEventArgs e) + protected override void OnError(ErrorEventArgs e) { base.OnError(e); - - Debug.Console(2, Debug.ErrorLogLevel.Notice, "WebSocket UiClient Error: {0} message: {1}", e.Exception, e.Message); + Debug.LogError(e.Exception, "WebSocket UiClient Error"); } } } -#endif \ No newline at end of file + +#endif diff --git a/src/Pepperdash.Core/Logging/DebugContext.cs b/src/Pepperdash.Core/Logging/DebugContext.cs index 8f9d98f..35cdb90 100644 --- a/src/Pepperdash.Core/Logging/DebugContext.cs +++ b/src/Pepperdash.Core/Logging/DebugContext.cs @@ -6,7 +6,6 @@ using Newtonsoft.Json; using Serilog; using Serilog.Events; -using WebSocketSharp; namespace PepperDash.Core.Logging { @@ -18,8 +17,8 @@ public static class DebugContext private static readonly CTimer SaveTimer; private static readonly Dictionary CurrentData; - public static readonly string ApplianceFilePath = Path.Combine("user", "debug", $"app-{InitialParametersClass.ApplicationNumber}-Debug.json"); - public static readonly string ServerFilePath = Path.Combine("User", "debug", $"app-{InitialParametersClass.RoomId}-Debug.json"); + public static readonly string ApplianceFilePath = Path.Combine("/", "user", "debug", $"app{InitialParametersClass.ApplicationNumber.ToString().PadLeft(2, '0')}-debug.json"); + public static readonly string ServerFilePath = Path.Combine("/", "User", "debug", $"{InitialParametersClass.RoomId}-debug.json"); /// /// The name of the file containing the current debug settings. @@ -79,6 +78,10 @@ private static void SaveData() try { + var debugDirectory = Path.GetDirectoryName(FileName) ?? + throw new Exception("File directory is root"); + + Directory.CreateDirectory(debugDirectory); var json = JsonConvert.SerializeObject(CurrentData); File.WriteAllText(FileName, json); } @@ -104,4 +107,4 @@ private static Dictionary LoadData() /// /// public record DebugContextData(LogEventLevel Level, string[] Devices = null); -} \ No newline at end of file +} diff --git a/src/Pepperdash.Core/Logging/DebugCrestronLoggerSink.cs b/src/Pepperdash.Core/Logging/DebugCrestronLoggerSink.cs index 0814453..c4439f6 100644 --- a/src/Pepperdash.Core/Logging/DebugCrestronLoggerSink.cs +++ b/src/Pepperdash.Core/Logging/DebugCrestronLoggerSink.cs @@ -20,10 +20,5 @@ public void Emit(LogEvent logEvent) CrestronLogger.WriteToLog(message, (uint)logEvent.Level); } - - public DebugCrestronLoggerSink() - { - CrestronLogger.Initialize(1, LoggerModeEnum.RM); - } } } diff --git a/src/Pepperdash.Core/Logging/DebugNet472WebSocket.cs b/src/Pepperdash.Core/Logging/DebugNet472WebSocket.cs index e211ffa..6e7ec14 100644 --- a/src/Pepperdash.Core/Logging/DebugNet472WebSocket.cs +++ b/src/Pepperdash.Core/Logging/DebugNet472WebSocket.cs @@ -1,7 +1,9 @@ -#if NET472 - +using System; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; +using Crestron.SimplSharp; + +#if NET472 using WebSocketSharp.Net; using WebSocketSharp.Server; @@ -11,6 +13,9 @@ internal class DebugNet472WebSocket : DebugWebSocket { private const string Path = "/debug/join/"; + public string Url => + $"wss://{CrestronEthernetHelper.GetEthernetParameter(CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, 0)}:{server.Port}{server.WebSocketServices[Path].Path}"; + private readonly WebSocketServer server; public DebugNet472WebSocket(string certPath = "") : base(certPath) @@ -41,14 +46,8 @@ public DebugNet472WebSocket(string certPath = "") : base(certPath) public override void Broadcast(string message) => server.WebSocketServices.Broadcast(message); } -} -#else - -using System; -using System.Net; -namespace PepperDash.Core.Logging -{ + //TODO: NETCORE version internal class DebugNetWebSocket : DebugWebSocket { private const string Path = "/debug/join/"; @@ -57,7 +56,6 @@ internal class DebugNetWebSocket : DebugWebSocket public DebugNetWebSocket(int port, string certPath = "") : base(certPath) { - server.AuthenticationSchemeSelectorDelegate = (request) => AuthenticationSchemes.Anonymous; server.Prefixes.Add("wss://*:" + port + Path); } @@ -66,7 +64,4 @@ public DebugNetWebSocket(int port, string certPath = "") : base(certPath) public override void Broadcast(string message) => throw new NotImplementedException(); } } - #endif - - diff --git a/src/Pepperdash.Core/Logging/DebugWebSocket.cs b/src/Pepperdash.Core/Logging/DebugWebSocket.cs index 311e0c9..2dd49e4 100644 --- a/src/Pepperdash.Core/Logging/DebugWebSocket.cs +++ b/src/Pepperdash.Core/Logging/DebugWebSocket.cs @@ -1,7 +1,10 @@ using System; using System.IO; using Crestron.SimplSharp; + +#if NET472 using Org.BouncyCastle.Asn1.X509; +#endif namespace PepperDash.Core.Logging { @@ -29,6 +32,7 @@ protected DebugWebSocket(string certPath = "") private static void CreateCert(string filePath) { +#if NET472 try { var utility = new BouncyCertificate(); @@ -44,6 +48,7 @@ private static void CreateCert(string filePath) { Debug.LogError(ex, "WSS failed to create cert"); } +#endif } public abstract bool IsListening { get; } diff --git a/src/Pepperdash.Core/Logging/DebugWebsocketSink.cs b/src/Pepperdash.Core/Logging/DebugWebsocketSink.cs index f138790..7cb20dc 100644 --- a/src/Pepperdash.Core/Logging/DebugWebsocketSink.cs +++ b/src/Pepperdash.Core/Logging/DebugWebsocketSink.cs @@ -1,6 +1,4 @@ using System.IO; -using Serilog; -using Serilog.Configuration; using Serilog.Core; using Serilog.Events; using Serilog.Formatting; @@ -29,12 +27,4 @@ public void Emit(LogEvent logEvent) webSocket.Broadcast(sw.ToString()); } } - - public class NoopSink : ILogEventSink - { - public static readonly NoopSink Instance = new NoopSink(); - public void Emit(LogEvent logEvent) - { - } - } } diff --git a/src/Pepperdash.Core/PepperDash.Core.csproj b/src/Pepperdash.Core/PepperDash.Core.csproj index fc5d431..88772d8 100644 --- a/src/Pepperdash.Core/PepperDash.Core.csproj +++ b/src/Pepperdash.Core/PepperDash.Core.csproj @@ -29,17 +29,17 @@ - - - - - - - - - + + + + + + + - + + + all diff --git a/src/Pepperdash.Core/Web/BouncyCertificate.cs b/src/Pepperdash.Core/Web/BouncyCertificate.cs index 67c129f..8468c23 100644 --- a/src/Pepperdash.Core/Web/BouncyCertificate.cs +++ b/src/Pepperdash.Core/Web/BouncyCertificate.cs @@ -1,5 +1,6 @@ using Crestron.SimplSharp; +#if NET472 using System; using System.Collections.Generic; using System.IO; @@ -357,4 +358,5 @@ public bool AddCertToStore(X509Certificate2 cert, System.Security.Cryptography.X return bRet; } } -} \ No newline at end of file +} +#endif