From 1a16ebead7e49a214276859f53ce18a0c844f102 Mon Sep 17 00:00:00 2001 From: sr55 Date: Tue, 8 May 2018 21:26:35 +0100 Subject: [PATCH] WinGui: Remove the need for ILog on the key Interop API surface. --- .../Interop/HandBrakeInstance.cs | 16 +--- .../Interop/HandBrakePresetService.cs | 9 -- .../Interop/HandBrakeUtils.cs | 19 +--- .../Services/Logging/LogService.cs | 86 ++++++++++++------- 4 files changed, 64 insertions(+), 66 deletions(-) diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs index d8f432fc4..f94c6fe24 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeInstance.cs @@ -48,8 +48,6 @@ namespace HandBrake.ApplicationServices.Interop /// private const double EncodePollIntervalMs = 250; - private readonly ILog log = LogService.GetLogger(); - /// /// The native handle to the HandBrake instance. /// @@ -246,7 +244,7 @@ namespace HandBrake.ApplicationServices.Interop catch (Exception exc) { Debug.WriteLine(exc); - this.log.LogMessage(exc.ToString(), LogMessageType.API, LogLevel.Error); + HandBrakeUtils.SendErrorEvent(exc.ToString()); } }; this.scanPollTimer.Start(); @@ -480,7 +478,6 @@ namespace HandBrake.ApplicationServices.Interop { IntPtr json = HBFunctions.hb_get_state_json(this.hbHandle); string statusJson = Marshal.PtrToStringAnsi(json); - this.log.LogMessage(statusJson, LogMessageType.Progress, LogLevel.Trace); JsonState state = null; if (!string.IsNullOrEmpty(statusJson)) { @@ -502,14 +499,9 @@ namespace HandBrake.ApplicationServices.Interop var jsonMsg = HBFunctions.hb_get_title_set_json(this.hbHandle); this.titlesJson = InteropUtilities.ToStringFromUtf8Ptr(jsonMsg); - this.log.LogMessage(this.titlesJson, LogMessageType.Progress, LogLevel.Trace); - if (string.IsNullOrEmpty(this.titlesJson)) - { - this.log.LogMessage("Scan Error: No Scan Data Returned.", LogMessageType.API, LogLevel.Error); - } - else - { + if (!string.IsNullOrEmpty(this.titlesJson)) + { this.titles = JsonConvert.DeserializeObject(this.titlesJson); if (this.titles != null) { @@ -533,8 +525,6 @@ namespace HandBrake.ApplicationServices.Interop IntPtr json = HBFunctions.hb_get_state_json(this.hbHandle); string statusJson = Marshal.PtrToStringAnsi(json); - this.log.LogMessage(statusJson, LogMessageType.Progress, LogLevel.Trace); - JsonState state = JsonConvert.DeserializeObject(statusJson); TaskState taskState = state != null ? TaskState.FromRepositoryValue(state.State) : null; diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakePresetService.cs b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakePresetService.cs index ae3387006..e3667a966 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakePresetService.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakePresetService.cs @@ -17,9 +17,6 @@ namespace HandBrake.ApplicationServices.Interop using HandBrake.ApplicationServices.Interop.HbLib; using HandBrake.ApplicationServices.Interop.Helpers; using HandBrake.ApplicationServices.Interop.Json.Presets; - using HandBrake.ApplicationServices.Services.Logging; - using HandBrake.ApplicationServices.Services.Logging.Interfaces; - using HandBrake.ApplicationServices.Services.Logging.Model; using Newtonsoft.Json; @@ -28,8 +25,6 @@ namespace HandBrake.ApplicationServices.Interop /// public class HandBrakePresetService { - private static readonly ILog log = LogService.GetLogger(); - /// /// The get built in presets. /// Requires an hb_init to have been invoked. @@ -41,9 +36,6 @@ namespace HandBrake.ApplicationServices.Interop { IntPtr presets = HBFunctions.hb_presets_builtin_get_json(); string presetJson = Marshal.PtrToStringAnsi(presets); - - log.LogMessage(presetJson, LogMessageType.API, LogLevel.Debug); - IList presetList = JsonConvert.DeserializeObject>(presetJson); return presetList; @@ -62,7 +54,6 @@ namespace HandBrake.ApplicationServices.Interop { IntPtr presetStringPointer = HBFunctions.hb_presets_read_file_json(InteropUtilities.ToUtf8PtrFromString(filename)); string presetJson = Marshal.PtrToStringAnsi(presetStringPointer); - log.LogMessage(presetJson, LogMessageType.API, LogLevel.Debug); if (!string.IsNullOrEmpty(presetJson)) { diff --git a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeUtils.cs b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeUtils.cs index b8fb24778..b16300fb4 100644 --- a/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeUtils.cs +++ b/win/CS/HandBrake.ApplicationServices/Interop/HandBrakeUtils.cs @@ -28,8 +28,6 @@ namespace HandBrake.ApplicationServices.Interop /// public static class HandBrakeUtils { - private static readonly ILog log = LogService.GetLogger(); - /// /// The callback for log messages from HandBrake. /// @@ -298,7 +296,6 @@ namespace HandBrake.ApplicationServices.Interop public static Geometry GetAnamorphicSize(AnamorphicGeometry anamorphicGeometry) { string encode = JsonConvert.SerializeObject(anamorphicGeometry, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); - log.LogMessage(encode, LogMessageType.API, LogLevel.Debug); IntPtr json = HBFunctions.hb_set_anamorphic_size_json(Marshal.StringToHGlobalAnsi(encode)); string result = Marshal.PtrToStringAnsi(json); return JsonConvert.DeserializeObject(result); @@ -333,13 +330,9 @@ namespace HandBrake.ApplicationServices.Interop /// /// The message to send. /// - private static void SendMessageEvent(string message) + public static void SendMessageEvent(string message) { - if (MessageLogged != null) - { - log.LogMessage(message, LogMessageType.ScanOrEncode, LogLevel.Info); - MessageLogged(null, new MessageLoggedEventArgs(message)); - } + MessageLogged?.Invoke(null, new MessageLoggedEventArgs(message)); } /// @@ -348,13 +341,9 @@ namespace HandBrake.ApplicationServices.Interop /// /// The message to send /// - private static void SendErrorEvent(string message) + public static void SendErrorEvent(string message) { - if (ErrorLogged != null) - { - log.LogMessage(message, LogMessageType.ScanOrEncode, LogLevel.Error); - ErrorLogged(null, new MessageLoggedEventArgs(message)); - } + ErrorLogged?.Invoke(null, new MessageLoggedEventArgs(message)); } } } diff --git a/win/CS/HandBrake.ApplicationServices/Services/Logging/LogService.cs b/win/CS/HandBrake.ApplicationServices/Services/Logging/LogService.cs index b0195ed50..78296795c 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Logging/LogService.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Logging/LogService.cs @@ -19,6 +19,8 @@ namespace HandBrake.ApplicationServices.Services.Logging using System.Linq; using System.Text; + using HandBrake.ApplicationServices.Interop; + using HandBrake.ApplicationServices.Interop.EventArgs; using HandBrake.ApplicationServices.Services.Logging.EventArgs; using HandBrake.ApplicationServices.Services.Logging.Interfaces; using HandBrake.ApplicationServices.Services.Logging.Model; @@ -46,6 +48,12 @@ namespace HandBrake.ApplicationServices.Services.Logging private StreamWriter fileWriter; private string logHeader; + public LogService() + { + HandBrakeUtils.MessageLogged += this.HandBrakeUtils_MessageLogged; + HandBrakeUtils.ErrorLogged += this.HandBrakeUtils_ErrorLogged; + } + /// /// Fires when a new QueueTask starts /// @@ -284,35 +292,6 @@ namespace HandBrake.ApplicationServices.Services.Logging } } - /// - /// Helper method for logging content to disk - /// - /// - /// Log message to write. - /// - private void LogMessageToDisk(LogMessage msg) - { - if (!this.isDiskLoggingEnabled) - { - return; - } - - try - { - lock (this.fileWriterLock) - { - if (this.fileWriter != null && this.fileWriter.BaseStream.CanWrite) - { - this.fileWriter.WriteLine(msg.Content); - } - } - } - catch (Exception exc) - { - Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged - } - } - /// /// Called when a log message is created. /// @@ -360,5 +339,54 @@ namespace HandBrake.ApplicationServices.Services.Logging { this.LogReset?.Invoke(this, System.EventArgs.Empty); } + + /// + /// Helper method for logging content to disk + /// + /// + /// Log message to write. + /// + private void LogMessageToDisk(LogMessage msg) + { + if (!this.isDiskLoggingEnabled) + { + return; + } + + try + { + lock (this.fileWriterLock) + { + if (this.fileWriter != null && this.fileWriter.BaseStream.CanWrite) + { + this.fileWriter.WriteLine(msg.Content); + } + } + } + catch (Exception exc) + { + Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged + } + } + + private void HandBrakeUtils_ErrorLogged(object sender, MessageLoggedEventArgs e) + { + if (e == null || string.IsNullOrEmpty(e.Message)) + { + return; + } + + this.LogMessage(e.Message, LogMessageType.ScanOrEncode, LogLevel.Error); + } + + private void HandBrakeUtils_MessageLogged(object sender, MessageLoggedEventArgs e) + { + if (e == null || string.IsNullOrEmpty(e.Message)) + { + return; + } + + this.LogMessage(e.Message, LogMessageType.ScanOrEncode, LogLevel.Info); + } } } -- 2.40.0