diff options
author | sr55 <[email protected]> | 2020-12-06 21:36:08 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2020-12-06 21:36:08 +0000 |
commit | 436cc757f258a1abf34f9f1b362adb5b975496ca (patch) | |
tree | 9da7a0cd6fbec266f6be44dc5947bec40c7a46b1 /win/CS | |
parent | f52c796246f41f120bc82671fe3fb8e8f9cca5af (diff) |
WinGui: Refactor the logging code to better handle multiple instance behaviours.
Diffstat (limited to 'win/CS')
-rw-r--r-- | win/CS/HandBrakeWPF/Helpers/LogManager.cs | 2 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs | 141 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs | 145 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Logging/EventArgs/LogFileEventArgs.cs | 28 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILog.cs | 12 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILogInstanceManager.cs | 13 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Logging/LogInstanceManager.cs | 36 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Logging/LogService.cs | 11 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/Scan/LibScan.cs | 16 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs | 35 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Views/LogView.xaml | 2 |
11 files changed, 224 insertions, 217 deletions
diff --git a/win/CS/HandBrakeWPF/Helpers/LogManager.cs b/win/CS/HandBrakeWPF/Helpers/LogManager.cs index d6100781e..4d2418084 100644 --- a/win/CS/HandBrakeWPF/Helpers/LogManager.cs +++ b/win/CS/HandBrakeWPF/Helpers/LogManager.cs @@ -31,7 +31,7 @@ namespace HandBrakeWPF.Helpers string logDir = DirectoryUtilities.GetLogDirectory(); string filename = string.Format("activity_log_main.{0}.txt", GeneralUtilities.ProcessId); string logFile = Path.Combine(logDir, filename); - generalAppLogger.ConfigureLogging(logFile); + generalAppLogger.ConfigureLogging(filename, logFile); IoC.Get<ILogInstanceManager>().Register(filename, generalAppLogger, true); diff --git a/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs b/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs deleted file mode 100644 index 75dcee5e9..000000000 --- a/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs +++ /dev/null @@ -1,141 +0,0 @@ -// -------------------------------------------------------------------------------------------------------------------- -// <copyright file="EncodeBase.cs" company="HandBrake Project (http://handbrake.fr)"> -// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. -// </copyright> -// <summary> -// A Base Class for the Encode Services. -// </summary> -// -------------------------------------------------------------------------------------------------------------------- - -namespace HandBrakeWPF.Services.Encode -{ - using System; - using System.Diagnostics; - using System.Globalization; - using System.IO; - - using HandBrake.Interop.Model; - - using HandBrakeWPF.Services.Interfaces; - using HandBrakeWPF.Utilities; - - using EncodeCompletedEventArgs = EventArgs.EncodeCompletedEventArgs; - using EncodeCompletedStatus = Interfaces.EncodeCompletedStatus; - using EncodeProgessStatus = Interfaces.EncodeProgessStatus; - using EncodeProgressEventArgs = EventArgs.EncodeProgressEventArgs; - using EncodeTask = Model.EncodeTask; - using GeneralApplicationException = Exceptions.GeneralApplicationException; - using ILog = Logging.Interfaces.ILog; - - public class EncodeBase - { - protected ILog encodeLogService; - - private readonly IUserSettingService userSettingService; - - public EncodeBase(IUserSettingService userSettingService) - { - this.userSettingService = userSettingService; - } - - public event EventHandler EncodeStarted; - - public event EncodeCompletedStatus EncodeCompleted; - - public event EncodeProgessStatus EncodeStatusChanged; - - public bool IsEncoding { get; protected set; } - - public void InvokeEncodeStatusChanged(EncodeProgressEventArgs e) - { - EncodeProgessStatus handler = this.EncodeStatusChanged; - handler?.Invoke(this, e); - } - - public void InvokeEncodeCompleted(EncodeCompletedEventArgs e) - { - EncodeCompletedStatus handler = this.EncodeCompleted; - handler?.Invoke(this, e); - } - - public void InvokeEncodeStarted(System.EventArgs e) - { - EventHandler handler = this.EncodeStarted; - handler?.Invoke(this, e); - } - - public string ProcessLogs(string destination) - { - try - { - string logDir = DirectoryUtilities.GetLogDirectory(); - string encodeDestinationPath = Path.GetDirectoryName(destination); - string destinationFile = Path.GetFileName(destination); - string encodeLogFile = destinationFile + " " + DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", "-").Replace(":", "-") + ".txt"; - string logContent = this.encodeLogService.GetFullLog(); - - // Make sure the log directory exists. - if (!Directory.Exists(logDir)) - { - Directory.CreateDirectory(logDir); - } - - // Copy the Log to HandBrakes log folder in the users applciation data folder. - this.WriteFile(logContent, Path.Combine(logDir, encodeLogFile)); - - // Save a copy of the log file in the same location as the enocde. - if (this.userSettingService.GetUserSetting<bool>(UserSettingConstants.SaveLogWithVideo)) - { - this.WriteFile(logContent, Path.Combine(encodeDestinationPath, encodeLogFile)); - } - - // Save a copy of the log file to a user specified location - if (Directory.Exists(this.userSettingService.GetUserSetting<string>(UserSettingConstants.SaveLogCopyDirectory)) && this.userSettingService.GetUserSetting<bool>(UserSettingConstants.SaveLogToCopyDirectory)) - { - this.WriteFile(logContent, Path.Combine(this.userSettingService.GetUserSetting<string>(UserSettingConstants.SaveLogCopyDirectory), encodeLogFile)); - } - - return Path.Combine(logDir, encodeLogFile); - } - catch (Exception exc) - { - Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged - } - - return null; - } - - protected void VerifyEncodeDestinationPath(EncodeTask task) - { - // Make sure the path exists, attempt to create it if it doesn't - try - { - string path = Directory.GetParent(task.Destination).ToString(); - if (!Directory.Exists(path)) - { - Directory.CreateDirectory(path); - } - } - catch (Exception exc) - { - throw new GeneralApplicationException( - "Unable to create directory for the encoded output.", "Please verify that you have a valid path.", exc); - } - } - - private void WriteFile(string content, string fileName) - { - try - { - using (StreamWriter fileWriter = new StreamWriter(fileName) { AutoFlush = true }) - { - fileWriter.Write(content); - } - } - catch (Exception exc) - { - Debug.WriteLine(exc); - } - } - } -}
\ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs index 6754f534e..8e9e9f9bc 100644 --- a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs +++ b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs @@ -11,6 +11,7 @@ namespace HandBrakeWPF.Services.Encode { using System; using System.Diagnostics; + using System.Globalization; using System.IO; using HandBrake.Interop.Interop.EventArgs; @@ -22,6 +23,7 @@ namespace HandBrakeWPF.Services.Encode using HandBrakeWPF.Exceptions; using HandBrakeWPF.Properties; using HandBrakeWPF.Services.Encode.Factories; + using HandBrakeWPF.Services.Encode.Interfaces; using HandBrakeWPF.Services.Interfaces; using HandBrakeWPF.Services.Logging.Interfaces; using HandBrakeWPF.Utilities; @@ -31,16 +33,15 @@ namespace HandBrakeWPF.Services.Encode using IEncode = Interfaces.IEncode; using LogService = Logging.LogService; - /// <summary> - /// LibHB Implementation of IEncode - /// </summary> - public class LibEncode : EncodeBase, IEncode + public class LibEncode : IEncode { private readonly IUserSettingService userSettingService; private readonly ILogInstanceManager logInstanceManager; private readonly IPortService portService; private readonly object portLock = new object(); private readonly EncodeTaskFactory encodeTaskFactory; + private ILog encodeLogService; + private IEncodeInstance instance; private DateTime startTime; private EncodeTask currentTask; @@ -49,7 +50,7 @@ namespace HandBrakeWPF.Services.Encode private bool isEncodeComplete; private int encodeCounter; - public LibEncode(IUserSettingService userSettingService, ILogInstanceManager logInstanceManager, int encodeCounter, IPortService portService) : base(userSettingService) + public LibEncode(IUserSettingService userSettingService, ILogInstanceManager logInstanceManager, int encodeCounter, IPortService portService) { this.userSettingService = userSettingService; this.logInstanceManager = logInstanceManager; @@ -58,8 +59,16 @@ namespace HandBrakeWPF.Services.Encode this.encodeTaskFactory = new EncodeTaskFactory(this.userSettingService); } + public event EventHandler EncodeStarted; + + public event EncodeCompletedStatus EncodeCompleted; + + public event EncodeProgessStatus EncodeStatusChanged; + public bool IsPasued { get; private set; } + public bool IsEncoding { get; protected set; } + public void Start(EncodeTask task, HBConfiguration configuration, string basePresetName) { try @@ -77,11 +86,11 @@ namespace HandBrakeWPF.Services.Encode if (this.userSettingService.GetUserSetting<bool>(UserSettingConstants.ProcessIsolationEnabled)) { - this.InitLogging(); + this.InitLogging(task.Destination); } else { - this.encodeLogService = this.logInstanceManager.MasterLogInstance; + this.encodeLogService = this.logInstanceManager.ApplicationLogInstance; this.encodeLogService.Reset(); } @@ -110,9 +119,9 @@ namespace HandBrakeWPF.Services.Encode int verbosity = this.userSettingService.GetUserSetting<int>(UserSettingConstants.Verbosity); // Prevent port stealing if multiple jobs start at the same time. - lock (portLock) + lock (this.portLock) { - this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(verbosity, this.userSettingService) : HandBrakeInstanceManager.GetEncodeInstance(verbosity, configuration, this.encodeLogService, userSettingService, this.portService); + this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(verbosity, this.userSettingService) : HandBrakeInstanceManager.GetEncodeInstance(verbosity, configuration, this.encodeLogService, this.userSettingService, this.portService); this.instance.EncodeCompleted += this.InstanceEncodeCompleted; this.instance.EncodeProgress += this.InstanceEncodeProgress; @@ -123,7 +132,7 @@ namespace HandBrakeWPF.Services.Encode this.VerifyEncodeDestinationPath(task); // Get an EncodeJob object for the Interop Library - JsonEncodeObject work = encodeTaskFactory.Create(task, configuration); + JsonEncodeObject work = this.encodeTaskFactory.Create(task, configuration); this.instance.StartEncode(work); } @@ -198,6 +207,24 @@ namespace HandBrakeWPF.Services.Encode this.encodeLogService.LogMessage(string.Format("[{0}] {1}", DateTime.Now.ToString("HH:mm:ss"), message)); } + private void InvokeEncodeStatusChanged(EventArgs.EncodeProgressEventArgs e) + { + EncodeProgessStatus handler = this.EncodeStatusChanged; + handler?.Invoke(this, e); + } + + private void InvokeEncodeCompleted(EventArgs.EncodeCompletedEventArgs e) + { + EncodeCompletedStatus handler = this.EncodeCompleted; + handler?.Invoke(this, e); + } + + private void InvokeEncodeStarted(System.EventArgs e) + { + EventHandler handler = this.EncodeStarted; + handler?.Invoke(this, e); + } + private void InstanceEncodeProgress(object sender, EncodeProgressEventArgs e) { EventArgs.EncodeProgressEventArgs args = new EventArgs.EncodeProgressEventArgs @@ -221,7 +248,7 @@ namespace HandBrakeWPF.Services.Encode { this.IsEncoding = false; - if (isEncodeComplete) + if (this.isEncodeComplete) { return; // Prevent phantom events bubbling up the stack. } @@ -259,7 +286,7 @@ namespace HandBrakeWPF.Services.Encode ? new EventArgs.EncodeCompletedEventArgs(false, null, e.Error.ToString(), this.currentTask.Source, this.currentTask.Destination, hbLog, filesize) : new EventArgs.EncodeCompletedEventArgs(true, null, string.Empty, this.currentTask.Source, this.currentTask.Destination, hbLog, filesize)); - this.logInstanceManager.Deregister(this.GetLogFilename()); + this.logInstanceManager.Deregister(Path.GetFileName(hbLog)); } private long GetFilesize(string destination) @@ -282,27 +309,101 @@ namespace HandBrakeWPF.Services.Encode return 0; } - private void InitLogging() + private void InitLogging(string destination) { - if (!isLoggingInitialised) + if (!this.isLoggingInitialised) { + string logType = this.isPreviewInstance ? "preview" : "encode"; + string destinationFile = Path.GetFileNameWithoutExtension(destination); + string logFileName = string.Format("{0}_{1}_{2}.txt", DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", ".").Replace(":", "-"), logType, destinationFile); + string fullLogPath = Path.Combine(DirectoryUtilities.GetLogDirectory(), logFileName); + this.encodeLogService = new LogService(); - this.encodeLogService.ConfigureLogging(GetFullLogPath()); + this.encodeLogService.ConfigureLogging(logFileName, fullLogPath); this.encodeLogService.SetId(this.encodeCounter); - this.logInstanceManager.Register(this.GetLogFilename(), this.encodeLogService, false); - isLoggingInitialised = true; + this.logInstanceManager.Register(logFileName, this.encodeLogService, false); + this.isLoggingInitialised = true; + } + } + + private string ProcessLogs(string destination) + { + try + { + string logDir = DirectoryUtilities.GetLogDirectory(); + string filename = this.encodeLogService.FileName; + string logContent = this.encodeLogService.GetFullLog(); + + // Make sure the log directory exists. + if (!Directory.Exists(logDir)) + { + Directory.CreateDirectory(logDir); + } + + // Copy the Log to HandBrakes log folder in the users application data folder. + // Only needed for process isolation mode. Worker will handle it's own logging. + if (!this.userSettingService.GetUserSetting<bool>(UserSettingConstants.ProcessIsolationEnabled)) + { + string logType = this.isPreviewInstance ? "preview" : "encode"; + string destinationFile = Path.GetFileNameWithoutExtension(destination); + string logFileName = string.Format("{0}_{1}_{2}.txt", DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", ".").Replace(":", "-"), logType, destinationFile); + this.WriteFile(logContent, Path.Combine(logDir, logFileName)); + filename = logFileName; + } + + // Save a copy of the log file in the same location as the encode. + if (this.userSettingService.GetUserSetting<bool>(UserSettingConstants.SaveLogWithVideo)) + { + this.WriteFile(logContent, Path.Combine(Path.GetDirectoryName(destination), filename)); + } + + // Save a copy of the log file to a user specified location + if (Directory.Exists(this.userSettingService.GetUserSetting<string>(UserSettingConstants.SaveLogCopyDirectory)) && this.userSettingService.GetUserSetting<bool>(UserSettingConstants.SaveLogToCopyDirectory)) + { + this.WriteFile(logContent, Path.Combine(this.userSettingService.GetUserSetting<string>(UserSettingConstants.SaveLogCopyDirectory), filename)); + } + + return Path.Combine(logDir, filename); + } + catch (Exception exc) + { + Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged } + + return null; } - private string GetLogFilename() + private void VerifyEncodeDestinationPath(EncodeTask task) { - string logType = this.isPreviewInstance ? "preview" : "encode"; - return string.Format("activity_log.{0}.{1}.{2}.txt", encodeCounter, logType, GeneralUtilities.ProcessId); + // Make sure the path exists, attempt to create it if it doesn't + try + { + string path = Directory.GetParent(task.Destination).ToString(); + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + } + catch (Exception exc) + { + throw new GeneralApplicationException( + "Unable to create directory for the encoded output.", "Please verify that you have a valid path.", exc); + } } - private string GetFullLogPath() + private void WriteFile(string content, string fileName) { - return Path.Combine(DirectoryUtilities.GetLogDirectory(), GetLogFilename()); + try + { + using (StreamWriter fileWriter = new StreamWriter(fileName) { AutoFlush = true }) + { + fileWriter.Write(content); + } + } + catch (Exception exc) + { + Debug.WriteLine(exc); + } } } } diff --git a/win/CS/HandBrakeWPF/Services/Logging/EventArgs/LogFileEventArgs.cs b/win/CS/HandBrakeWPF/Services/Logging/EventArgs/LogFileEventArgs.cs new file mode 100644 index 000000000..5da70403d --- /dev/null +++ b/win/CS/HandBrakeWPF/Services/Logging/EventArgs/LogFileEventArgs.cs @@ -0,0 +1,28 @@ +// -------------------------------------------------------------------------------------------------------------------- +// <copyright file="LogFileEventArgs.cs" company="HandBrake Project (http://handbrake.fr)"> +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// </copyright> +// <summary> +// Defines the LogEventArgs type. +// </summary> +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Services.Logging.EventArgs +{ + using System; + + using HandBrakeWPF.Services.Logging.Interfaces; + + public class LogFileEventArgs : EventArgs + { + public LogFileEventArgs(string fileName, ILog logInstance) + { + this.FileName = fileName; + this.LogInstance = logInstance; + } + + public string FileName { get; } + + public ILog LogInstance { get; } + } +} diff --git a/win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILog.cs b/win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILog.cs index b20a04b72..551d49f5a 100644 --- a/win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILog.cs +++ b/win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILog.cs @@ -14,12 +14,9 @@ namespace HandBrakeWPF.Services.Logging.Interfaces using HandBrake.Worker.Logging.Models; - using LogEventArgs = HandBrakeWPF.Services.Logging.EventArgs.LogEventArgs; + using LogEventArgs = EventArgs.LogEventArgs; - /// <summary> - /// The Log interface. - /// </summary> - public interface ILog + public interface ILog : IDisposable { /// <summary> /// The message logged. @@ -47,10 +44,13 @@ namespace HandBrakeWPF.Services.Logging.Interfaces /// <param name="filename"> /// The filename. /// </param> + /// <param name="fullLogPath"> + /// The full Log Path. + /// </param> /// <remarks> /// If this is not called, all log messages from libhb will be ignored. /// </remarks> - void ConfigureLogging(string filename); + void ConfigureLogging(string filename, string fullLogPath); /// <summary> /// Log a message. diff --git a/win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILogInstanceManager.cs b/win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILogInstanceManager.cs index a5c151308..3283bbcee 100644 --- a/win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILogInstanceManager.cs +++ b/win/CS/HandBrakeWPF/Services/Logging/Interfaces/ILogInstanceManager.cs @@ -12,13 +12,13 @@ namespace HandBrakeWPF.Services.Logging.Interfaces using System; using System.Collections.Generic; + using HandBrakeWPF.Services.Logging.EventArgs; + public interface ILogInstanceManager { - event EventHandler NewLogInstanceRegistered; + event EventHandler<LogFileEventArgs> NewLogInstanceRegistered; - string ApplicationAndScanLog { get; } - - ILog MasterLogInstance { get; } + ILog ApplicationLogInstance { get; } /// <summary> /// Register an ILog instance. @@ -41,6 +41,11 @@ namespace HandBrakeWPF.Services.Logging.Interfaces void Deregister(string filename); /// <summary> + /// Reset the application log file. + /// </summary> + void ResetApplicationLog(); + + /// <summary> /// Gets a list of files without their associated ILog instances. /// </summary> /// <returns>List of filenames being logged</returns> diff --git a/win/CS/HandBrakeWPF/Services/Logging/LogInstanceManager.cs b/win/CS/HandBrakeWPF/Services/Logging/LogInstanceManager.cs index 02983da21..02b40dcc5 100644 --- a/win/CS/HandBrakeWPF/Services/Logging/LogInstanceManager.cs +++ b/win/CS/HandBrakeWPF/Services/Logging/LogInstanceManager.cs @@ -13,6 +13,7 @@ namespace HandBrakeWPF.Services.Logging using System.IO; using System.Linq; + using HandBrakeWPF.Services.Logging.EventArgs; using HandBrakeWPF.Services.Logging.Interfaces; public class LogInstanceManager : ILogInstanceManager @@ -20,31 +21,23 @@ namespace HandBrakeWPF.Services.Logging private readonly object instanceLock = new object(); private Dictionary<string, ILog> logInstances = new Dictionary<string, ILog>(); - public event EventHandler NewLogInstanceRegistered; + public event EventHandler<LogFileEventArgs> NewLogInstanceRegistered; - public string ApplicationAndScanLog { get; private set; } - - public ILog MasterLogInstance { get; private set; } + public ILog ApplicationLogInstance { get; private set; } public void Register(string filename, ILog log, bool isMaster) { lock (this.instanceLock) { - if (string.IsNullOrEmpty(this.ApplicationAndScanLog)) - { - // The application startup sets the initial log file. - this.ApplicationAndScanLog = filename; - } - this.logInstances.Add(filename, log); if (isMaster) { - this.MasterLogInstance = log; + this.ApplicationLogInstance = log; } } - this.OnNewLogInstanceRegistered(); + this.OnNewLogInstanceRegistered(new LogFileEventArgs(filename, log)); } public void Deregister(string filename) @@ -53,11 +46,20 @@ namespace HandBrakeWPF.Services.Logging { if (this.logInstances.ContainsKey(filename)) { + ILog logInstance = this.logInstances[filename]; + logInstance.Dispose(); + this.logInstances.Remove(filename); } } - this.OnNewLogInstanceRegistered(); + this.OnNewLogInstanceRegistered(new LogFileEventArgs(filename, null)); + } + + public void ResetApplicationLog() + { + this.ApplicationLogInstance.Reset(); + this.OnNewLogInstanceRegistered(new LogFileEventArgs(this.ApplicationLogInstance.FileName, this.ApplicationLogInstance)); } public List<string> GetLogFiles() @@ -67,9 +69,9 @@ namespace HandBrakeWPF.Services.Logging List<string> encodeLogs = this.logInstances.Keys.Where(s => !s.Contains("main")).OrderBy(s => s).ToList(); List<string> finalList = new List<string>(); - if (this.MasterLogInstance != null) + if (this.ApplicationLogInstance != null) { - finalList.Add(Path.GetFileName(this.MasterLogInstance.FileName)); + finalList.Add(Path.GetFileName(this.ApplicationLogInstance.FileName)); } finalList.AddRange(encodeLogs); @@ -97,9 +99,9 @@ namespace HandBrakeWPF.Services.Logging return null; } - protected virtual void OnNewLogInstanceRegistered() + protected virtual void OnNewLogInstanceRegistered(LogFileEventArgs e) { - this.NewLogInstanceRegistered?.Invoke(this, System.EventArgs.Empty); + this.NewLogInstanceRegistered?.Invoke(this, e); } } } diff --git a/win/CS/HandBrakeWPF/Services/Logging/LogService.cs b/win/CS/HandBrakeWPF/Services/Logging/LogService.cs index 119a387ea..bc416bb6e 100644 --- a/win/CS/HandBrakeWPF/Services/Logging/LogService.cs +++ b/win/CS/HandBrakeWPF/Services/Logging/LogService.cs @@ -64,6 +64,8 @@ namespace HandBrakeWPF.Services.Logging public string FileName { get; private set; } + public string FullLogPath { get; private set; } + public void LogMessage(string content) { if (!this.isLoggingEnabled) @@ -94,17 +96,18 @@ namespace HandBrakeWPF.Services.Logging this.OnMessageLogged(msg); // Must be outside lock to be thread safe. } - public void ConfigureLogging(string filename) + public void ConfigureLogging(string filename, string fullLogPath) { this.isLoggingEnabled = true; this.FileName = filename; + this.FullLogPath = fullLogPath; - if (!string.IsNullOrEmpty(filename) && !Directory.Exists(Path.GetDirectoryName(filename))) + if (!string.IsNullOrEmpty(fullLogPath) && !Directory.Exists(Path.GetDirectoryName(fullLogPath))) { - Directory.CreateDirectory(Path.GetDirectoryName(filename)); + Directory.CreateDirectory(Path.GetDirectoryName(fullLogPath)); } - this.EnableLoggingToDisk(filename); + this.EnableLoggingToDisk(fullLogPath); this.logHeader = GeneralUtilities.CreateLogHeader().ToString(); this.LogMessage(logHeader); diff --git a/win/CS/HandBrakeWPF/Services/Scan/LibScan.cs b/win/CS/HandBrakeWPF/Services/Scan/LibScan.cs index 5cb336d46..8ea3e097a 100644 --- a/win/CS/HandBrakeWPF/Services/Scan/LibScan.cs +++ b/win/CS/HandBrakeWPF/Services/Scan/LibScan.cs @@ -20,11 +20,11 @@ namespace HandBrakeWPF.Services.Scan using HandBrake.Interop.Interop.Model; using HandBrake.Interop.Interop.Model.Encoding; using HandBrake.Interop.Interop.Model.Preview; - using HandBrake.Interop.Model; using HandBrakeWPF.Instance; using HandBrakeWPF.Services.Encode.Model; using HandBrakeWPF.Services.Interfaces; + using HandBrakeWPF.Services.Logging.Interfaces; using HandBrakeWPF.Services.Scan.EventArgs; using HandBrakeWPF.Services.Scan.Factories; using HandBrakeWPF.Services.Scan.Interfaces; @@ -32,17 +32,14 @@ namespace HandBrakeWPF.Services.Scan using HandBrakeWPF.Utilities; using ILog = Logging.Interfaces.ILog; - using LogService = Logging.LogService; using ScanProgressEventArgs = HandBrake.Interop.Interop.EventArgs.ScanProgressEventArgs; using Title = Model.Title; - /// <summary> - /// Scan a Source - /// </summary> public class LibScan : IScan, IDisposable { private readonly ILog log = null; private readonly IUserSettingService userSettingService; + private readonly ILogInstanceManager logInstanceManager; private TitleFactory titleFactory = new TitleFactory(); private string currentSourceScanPath; @@ -50,14 +47,14 @@ namespace HandBrakeWPF.Services.Scan private Action<bool, Source> postScanOperation; private bool isCancelled = false; - public LibScan(ILog logService, IUserSettingService userSettingService) + public LibScan(ILog logService, IUserSettingService userSettingService, ILogInstanceManager logInstanceManager) { this.log = logService; this.userSettingService = userSettingService; + this.logInstanceManager = logInstanceManager; this.IsScanning = false; } - - + public event EventHandler ScanStarted; public event ScanCompletedStatus ScanCompleted; @@ -96,6 +93,9 @@ namespace HandBrakeWPF.Services.Scan this.isCancelled = false; + // Reset the log + this.logInstanceManager.ResetApplicationLog(); + // Handle the post scan operation. this.postScanOperation = postAction; diff --git a/win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs index d98c99d48..907b33a40 100644 --- a/win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs @@ -23,6 +23,7 @@ namespace HandBrakeWPF.ViewModels using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Interfaces;
+ using HandBrakeWPF.Services.Logging.EventArgs;
using HandBrakeWPF.Services.Logging.Interfaces;
using HandBrakeWPF.Services.Queue.Interfaces;
using HandBrakeWPF.Utilities;
@@ -37,7 +38,6 @@ namespace HandBrakeWPF.ViewModels private readonly IErrorService errorService;
private readonly ILogInstanceManager logInstanceManager;
private readonly IQueueService queueService;
- private readonly Dictionary<string, string> inactiveLogs = new Dictionary<string, string>();
private readonly object readLockObject = new object();
private ILog logService;
@@ -102,7 +102,7 @@ namespace HandBrakeWPF.ViewModels this.logInstanceManager.NewLogInstanceRegistered += this.LogInstanceManager_NewLogInstanceRegistered;
this.queueService.QueueChanged += this.QueueService_QueueChanged;
- this.CollectLogFiles();
+ this.CollectLogFiles(null);
base.OnActivate();
}
@@ -142,24 +142,27 @@ namespace HandBrakeWPF.ViewModels }
this.logService = this.logInstanceManager.GetLogInstance(this.SelectedLogFile);
+ string logDir = DirectoryUtilities.GetLogDirectory();
+ string logFile = Path.Combine(logDir, this.selectedLogFile);
// This is not an active log, so read from disk.
if (this.logService == null)
{
try
{
- if (this.inactiveLogs.ContainsKey(this.SelectedLogFile) && File.Exists(this.inactiveLogs[this.selectedLogFile]))
+ if (File.Exists(logFile))
{
this.log.Clear();
- using (StreamReader logReader = new StreamReader(this.inactiveLogs[this.selectedLogFile]))
+ using (StreamReader logReader = new StreamReader(logFile))
{
string logContent = logReader.ReadToEnd();
this.log.AppendLine(logContent);
}
- }
+ }
else
{
- this.log.AppendLine("Log file not found.");
+ this.log.Clear();
+ this.log.AppendLine("Sorry, The log file was not found.");
}
}
catch (Exception exc)
@@ -230,17 +233,17 @@ namespace HandBrakeWPF.ViewModels }
}
- private void LogInstanceManager_NewLogInstanceRegistered(object sender, EventArgs e)
+ private void LogInstanceManager_NewLogInstanceRegistered(object sender, LogFileEventArgs e)
{
- this.CollectLogFiles();
+ this.CollectLogFiles(e.FileName);
}
private void QueueService_QueueChanged(object sender, EventArgs e)
{
- this.CollectLogFiles();
+ this.CollectLogFiles(null);
}
- private void CollectLogFiles()
+ private void CollectLogFiles(string filename)
{
lock (readLockObject)
{
@@ -248,10 +251,8 @@ namespace HandBrakeWPF.ViewModels BindingList<string> logfiles = new BindingList<string>();
// Add Inactive Logs First.
- inactiveLogs.Clear();
foreach (string logFile in this.queueService.GetLogFilePaths())
{
- this.inactiveLogs.Add(Path.GetFileName(logFile), logFile);
logfiles.Add(Path.GetFileName(logFile));
}
@@ -264,7 +265,15 @@ namespace HandBrakeWPF.ViewModels this.LogFiles = logfiles;
this.NotifyOfPropertyChange(() => this.LogFiles);
- this.SelectedLogFile = this.LogFiles.LastOrDefault(c => !c.Contains("activity_log_main"));
+ if (!string.IsNullOrEmpty(filename))
+ {
+ this.SelectedLogFile = this.LogFiles.FirstOrDefault(c => c.Equals(filename, StringComparison.InvariantCultureIgnoreCase));
+ }
+ else
+ {
+ this.SelectedLogFile = this.LogFiles.LastOrDefault(c => !c.Contains("activity_log_main"));
+ }
+
if (this.SelectedLogFile == null)
{
this.SelectedLogFile = this.LogFiles.LastOrDefault();
diff --git a/win/CS/HandBrakeWPF/Views/LogView.xaml b/win/CS/HandBrakeWPF/Views/LogView.xaml index bc19642c5..34dc9c433 100644 --- a/win/CS/HandBrakeWPF/Views/LogView.xaml +++ b/win/CS/HandBrakeWPF/Views/LogView.xaml @@ -52,7 +52,7 @@ </ToolBar>
- <TextBox Grid.Row="2" ScrollViewer.VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" x:Name="logText">
+ <TextBox Grid.Row="2" ScrollViewer.VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" x:Name="logText" VerticalContentAlignment="Top">
<TextBox.ContextMenu>
<ContextMenu>
|