summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrake.Worker/Logging
diff options
context:
space:
mode:
authorsr55 <[email protected]>2019-12-29 17:29:36 +0000
committersr55 <[email protected]>2019-12-29 17:29:53 +0000
commit282b04f2fe39205664fbccc81aaab1048294811c (patch)
tree68e6b1a9d16e2d4e8479338462b641aa05bf4d4c /win/CS/HandBrake.Worker/Logging
parentf63e7b79c83d62055f96f0a1a687cffae208a9c8 (diff)
WinGui: Experimental Worker Process (Basic logging is now operational) + UI Infrastructure (currently hidden) needed for the feature. + More Refactoring.
Diffstat (limited to 'win/CS/HandBrake.Worker/Logging')
-rw-r--r--win/CS/HandBrake.Worker/Logging/Interfaces/ILogHandler.cs17
-rw-r--r--win/CS/HandBrake.Worker/Logging/LogHandler.cs175
-rw-r--r--win/CS/HandBrake.Worker/Logging/Models/LogHandlerConfig.cs34
-rw-r--r--win/CS/HandBrake.Worker/Logging/Models/LogMessage.cs4
4 files changed, 23 insertions, 207 deletions
diff --git a/win/CS/HandBrake.Worker/Logging/Interfaces/ILogHandler.cs b/win/CS/HandBrake.Worker/Logging/Interfaces/ILogHandler.cs
index 3daa69ba0..eaa527ed6 100644
--- a/win/CS/HandBrake.Worker/Logging/Interfaces/ILogHandler.cs
+++ b/win/CS/HandBrake.Worker/Logging/Interfaces/ILogHandler.cs
@@ -9,31 +9,22 @@
namespace HandBrake.Worker.Logging.Interfaces
{
+ using System.Collections.Generic;
+
using HandBrake.Worker.Logging.Models;
public interface ILogHandler
{
- /// <summary>
- /// Enable logging for this worker process.
- /// </summary>
- /// <param name="config">
- /// Configuration for the logger.
- /// </param>
- /// <remarks>
- /// If this is not called, all log messages from libhb will be ignored.
- /// </remarks>
- void ConfigureLogging(LogHandlerConfig config);
-
string GetFullLog();
- long GetLatestLogIndex();
+ List<LogMessage> GetLogMessages();
/// <summary>
/// Get the log data from a given index
/// </summary>
/// <param name="index">index is zero based</param>
/// <returns>Full log as a string</returns>
- string GetLogFromIndex(int index);
+ List<LogMessage> GetLogMessagesFromIndex(int index);
/// <summary>
/// Empty the log cache and reset the log handler to defaults.
diff --git a/win/CS/HandBrake.Worker/Logging/LogHandler.cs b/win/CS/HandBrake.Worker/Logging/LogHandler.cs
index a3390a881..b0f11c1e7 100644
--- a/win/CS/HandBrake.Worker/Logging/LogHandler.cs
+++ b/win/CS/HandBrake.Worker/Logging/LogHandler.cs
@@ -1,5 +1,5 @@
// --------------------------------------------------------------------------------------------------------------------
-// <copyright file="LogHandler.cs" company="HandBrake Project (https://handbrake.fr)">
+// <copyright file="LogHandler.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>
@@ -10,13 +10,11 @@
namespace HandBrake.Worker.Logging
{
- using System;
using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
using System.Linq;
using System.Text;
+ using HandBrake.Interop.Interop;
using HandBrake.Interop.Interop.EventArgs;
using HandBrake.Worker.Logging.Interfaces;
using HandBrake.Worker.Logging.Models;
@@ -24,17 +22,17 @@ namespace HandBrake.Worker.Logging
public class LogHandler : ILogHandler
{
private readonly object lockObject = new object();
- private readonly object fileWriterLock = new object();
private readonly StringBuilder logBuilder = new StringBuilder();
private readonly List<LogMessage> logMessages = new List<LogMessage>();
- private bool isLoggingEnabled;
- private long messageIndex;
- private string diskLogPath;
- private bool deleteLogFirst;
- private bool isDiskLoggingEnabled;
- private StreamWriter fileWriter;
- private string logHeader;
+ private bool isLoggingEnabled = true;
+ private int messageIndex;
+
+ public LogHandler()
+ {
+ HandBrakeUtils.MessageLogged += this.HandBrakeUtils_MessageLogged;
+ HandBrakeUtils.ErrorLogged += this.HandBrakeUtils_ErrorLogged;
+ }
public IEnumerable<LogMessage> LogMessages
{
@@ -55,27 +53,27 @@ namespace HandBrake.Worker.Logging
}
}
- public long GetLatestLogIndex()
+ public List<LogMessage> GetLogMessages()
{
lock (this.lockObject)
{
- return this.messageIndex;
+ return new List<LogMessage>(this.logMessages);
}
}
- public string GetLogFromIndex(int index)
+ public List<LogMessage> GetLogMessagesFromIndex(int index)
{
- StringBuilder log = new StringBuilder();
+ List<LogMessage> log = new List<LogMessage>();
lock (this.lockObject)
{
// Note messageIndex is not 0 based.
- for (int i = index; i < this.messageIndex; i++)
+ for (int i = index; i < this.messageIndex; i++)
{
- log.AppendLine(this.logMessages[i].Content);
+ log.Add(this.logMessages[i]);
}
}
- return log.ToString();
+ return log;
}
public void LogMessage(string content)
@@ -91,7 +89,6 @@ namespace HandBrake.Worker.Logging
this.messageIndex = this.messageIndex + 1;
this.logMessages.Add(msg);
this.logBuilder.AppendLine(msg.Content);
- this.LogMessageToDisk(msg);
if (this.logMessages.Count > 50000)
{
@@ -99,25 +96,12 @@ namespace HandBrake.Worker.Logging
msg = new LogMessage("Log Service Pausing. Too Many Log messages. This may indicate a problem with your encode.", this.messageIndex);
this.logMessages.Add(msg);
this.logBuilder.AppendLine(msg.Content);
- this.LogMessageToDisk(msg);
this.isLoggingEnabled = false;
}
}
}
- public void ConfigureLogging(LogHandlerConfig config)
- {
- this.isLoggingEnabled = true;
- this.logHeader = config.Header;
- this.LogMessage(config.Header);
-
- if (config.EnableDiskLogging)
- {
- this.EnableLoggingToDisk(config.LogFile, config.DeleteCurrentLogFirst);
- }
- }
-
public void Reset()
{
lock (this.lockObject)
@@ -125,131 +109,6 @@ namespace HandBrake.Worker.Logging
this.logMessages.Clear();
this.logBuilder.Clear();
this.messageIndex = 0;
-
- try
- {
- lock (this.fileWriterLock)
- {
- if (this.fileWriter != null)
- {
- this.fileWriter.Flush();
- this.fileWriter.Close();
- this.fileWriter.Dispose();
- }
-
- this.fileWriter = null;
- }
- }
- catch (Exception exc)
- {
- Debug.WriteLine(exc);
- }
-
- if (this.fileWriter == null)
- {
- this.isDiskLoggingEnabled = false;
- this.EnableLoggingToDisk(this.diskLogPath, this.deleteLogFirst);
- }
-
- if (!string.IsNullOrEmpty(this.logHeader))
- {
- this.SetupLogHeader(this.logHeader);
- }
- }
- }
-
- protected void ShutdownFileWriter()
- {
- try
- {
- lock (this.fileWriterLock)
- {
- if (this.fileWriter != null)
- {
- this.fileWriter.Flush();
- this.fileWriter.Close();
- this.fileWriter.Dispose();
- }
-
- this.fileWriter = null;
- }
- }
- catch (Exception exc)
- {
- Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged
- }
- }
-
- private void SetupLogHeader(string header)
- {
- this.logHeader = header;
- this.LogMessage(header);
- }
-
- private void EnableLoggingToDisk(string logFile, bool deleteCurrentLogFirst)
- {
- if (this.isDiskLoggingEnabled)
- {
- throw new Exception("Disk Logging already enabled!");
- }
-
- try
- {
- if (!Directory.Exists(Path.GetDirectoryName(logFile)))
- {
- throw new Exception("Log Directory does not exist. This service will not create it for you!");
- }
-
- if (deleteCurrentLogFirst && File.Exists(logFile))
- {
- File.Delete(logFile);
- }
-
- this.diskLogPath = logFile;
- this.isDiskLoggingEnabled = true;
- this.deleteLogFirst = deleteCurrentLogFirst;
-
- lock (this.fileWriterLock)
- {
- this.fileWriter = new StreamWriter(logFile) { AutoFlush = true };
- }
- }
- catch (Exception exc)
- {
- this.LogMessage("Failed to Initialise Disk Logging. " + Environment.NewLine + exc);
-
- if (this.fileWriter != null)
- {
- lock (this.fileWriterLock)
- {
- this.fileWriter.Flush();
- this.fileWriter.Close();
- this.fileWriter.Dispose();
- }
- }
- }
- }
-
- 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
}
}
diff --git a/win/CS/HandBrake.Worker/Logging/Models/LogHandlerConfig.cs b/win/CS/HandBrake.Worker/Logging/Models/LogHandlerConfig.cs
deleted file mode 100644
index 2cbb97cb3..000000000
--- a/win/CS/HandBrake.Worker/Logging/Models/LogHandlerConfig.cs
+++ /dev/null
@@ -1,34 +0,0 @@
-// --------------------------------------------------------------------------------------------------------------------
-// <copyright file="LogHandlerConfig.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 LogHandlerConfig type.
-// </summary>
-// --------------------------------------------------------------------------------------------------------------------
-
-namespace HandBrake.Worker.Logging.Models
-{
- public class LogHandlerConfig
- {
- public LogHandlerConfig(bool enableDiskLogging, string logFile, bool deleteCurrentLogFirst, string header)
- {
- this.EnableDiskLogging = enableDiskLogging;
- this.LogFile = logFile;
- this.DeleteCurrentLogFirst = deleteCurrentLogFirst;
- this.Header = header;
- }
-
- public LogHandlerConfig()
- {
- }
-
- public bool EnableDiskLogging { get; set; }
-
- public string LogFile { get; set; }
-
- public bool DeleteCurrentLogFirst { get; set; }
-
- public string Header { get; set; }
- }
-}
diff --git a/win/CS/HandBrake.Worker/Logging/Models/LogMessage.cs b/win/CS/HandBrake.Worker/Logging/Models/LogMessage.cs
index c47e331b0..63472cd49 100644
--- a/win/CS/HandBrake.Worker/Logging/Models/LogMessage.cs
+++ b/win/CS/HandBrake.Worker/Logging/Models/LogMessage.cs
@@ -11,7 +11,7 @@ namespace HandBrake.Worker.Logging.Models
{
public class LogMessage
{
- public LogMessage(string content, long messageIndex)
+ public LogMessage(string content, int messageIndex)
{
this.Content = content;
this.MessageIndex = messageIndex;
@@ -19,6 +19,6 @@ namespace HandBrake.Worker.Logging.Models
public string Content { get; private set; }
- public long MessageIndex { get; private set; }
+ public int MessageIndex { get; private set; }
}
}