summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Services/Encode
diff options
context:
space:
mode:
Diffstat (limited to 'win/CS/HandBrakeWPF/Services/Encode')
-rw-r--r--win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs198
-rw-r--r--win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs10
-rw-r--r--win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs69
3 files changed, 41 insertions, 236 deletions
diff --git a/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs b/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs
index 9ae0398ee..801a8c9cc 100644
--- a/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs
+++ b/win/CS/HandBrakeWPF/Services/Encode/EncodeBase.cs
@@ -13,11 +13,10 @@ namespace HandBrakeWPF.Services.Encode
using System.Diagnostics;
using System.Globalization;
using System.IO;
- using System.Text;
using HandBrake.ApplicationServices.Model;
-
- using HandBrakeWPF.Utilities;
+ using HandBrake.ApplicationServices.Services.Logging;
+ using HandBrake.ApplicationServices.Services.Logging.Interfaces;
using EncodeCompletedEventArgs = HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs;
using EncodeCompletedStatus = HandBrakeWPF.Services.Encode.Interfaces.EncodeCompletedStatus;
@@ -31,38 +30,11 @@ namespace HandBrakeWPF.Services.Encode
/// </summary>
public class EncodeBase
{
- #region Private Variables
-
- /// <summary>
- /// A Lock for the filewriter
- /// </summary>
- private static readonly object FileWriterLock = new object();
-
- /// <summary>
- /// The Log File Header
- /// </summary>
- private readonly StringBuilder header;
-
- /// <summary>
- /// The Log Buffer
- /// </summary>
- private StringBuilder logBuffer;
-
- /// <summary>
- /// The Log file writer
- /// </summary>
- private StreamWriter fileWriter;
-
- #endregion
-
/// <summary>
/// Initializes a new instance of the <see cref="EncodeBase"/> class.
/// </summary>
public EncodeBase()
- {
- this.logBuffer = new StringBuilder();
- this.header = GeneralUtilities.CreateLogHeader();
- this.LogIndex = 0;
+ {
}
#region Events
@@ -91,39 +63,6 @@ namespace HandBrakeWPF.Services.Encode
/// </summary>
public bool IsEncoding { get; protected set; }
- /// <summary>
- /// Gets ActivityLog.
- /// </summary>
- public string ActivityLog
- {
- get
- {
- string noLog = "There is no log information to display." + Environment.NewLine + Environment.NewLine
- + "This window will only display logging information after you have started an encode." + Environment.NewLine
- + Environment.NewLine + "You can find previous log files in the log directory or by clicking the 'Open Log Directory' button above.";
-
- return string.IsNullOrEmpty(this.logBuffer.ToString())
- ? noLog
- : this.header + this.logBuffer.ToString();
- }
- }
-
- /// <summary>
- /// Gets the log index.
- /// </summary>
- public int LogIndex { get; private set; }
-
- /// <summary>
- /// Gets LogBuffer.
- /// </summary>
- public StringBuilder LogBuffer
- {
- get
- {
- return this.logBuffer;
- }
- }
-
#endregion
#region Invoke Events
@@ -156,8 +95,6 @@ namespace HandBrakeWPF.Services.Encode
{
handler(this, e);
}
-
- this.LogIndex = 0; // Reset
}
/// <summary>
@@ -186,6 +123,9 @@ namespace HandBrakeWPF.Services.Encode
/// <param name="destination">
/// The Destination File Path
/// </param>
+ /// <param name="isPreview">
+ /// The is Preview.
+ /// </param>
/// <param name="configuration">
/// The configuration.
/// </param>
@@ -193,15 +133,12 @@ namespace HandBrakeWPF.Services.Encode
{
try
{
- string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
- "\\HandBrake\\logs";
-
- string tempLogFile = Path.Combine(logDir, isPreview ? $"preview_encode_log{GeneralUtilities.ProcessId}.txt" : string.Format("last_encode_log{0}.txt", GeneralUtilities.ProcessId));
-
+ string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
string encodeDestinationPath = Path.GetDirectoryName(destination);
string destinationFile = Path.GetFileName(destination);
- string encodeLogFile = destinationFile + " " +
- DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", "-").Replace(":", "-") + ".txt";
+ string encodeLogFile = destinationFile + " " + DateTime.Now.ToString(CultureInfo.InvariantCulture).Replace("/", "-").Replace(":", "-") + ".txt";
+ ILog log = LogService.GetLogger();
+ string logContent = log.ActivityLog;
// Make sure the log directory exists.
if (!Directory.Exists(logDir))
@@ -210,19 +147,18 @@ namespace HandBrakeWPF.Services.Encode
}
// Copy the Log to HandBrakes log folder in the users applciation data folder.
- File.Copy(tempLogFile, Path.Combine(logDir, encodeLogFile));
+ this.WriteFile(logContent, Path.Combine(logDir, encodeLogFile));
// Save a copy of the log file in the same location as the enocde.
if (configuration.SaveLogWithVideo)
{
- File.Copy(tempLogFile, Path.Combine(encodeDestinationPath, encodeLogFile));
+ this.WriteFile(logContent, Path.Combine(encodeDestinationPath, encodeLogFile));
}
// Save a copy of the log file to a user specified location
if (Directory.Exists(configuration.SaveLogCopyDirectory) && configuration.SaveLogToCopyDirectory)
{
- File.Copy(
- tempLogFile, Path.Combine(configuration.SaveLogCopyDirectory, encodeLogFile));
+ this.WriteFile(logContent, Path.Combine(configuration.SaveLogCopyDirectory, encodeLogFile));
}
}
catch (Exception exc)
@@ -232,116 +168,26 @@ namespace HandBrakeWPF.Services.Encode
}
/// <summary>
- /// Setup the logging.
+ /// The write file.
/// </summary>
- protected void SetupLogging(bool isPreviewEncode)
- {
- this.ShutdownFileWriter();
- string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
- string logFile = Path.Combine(logDir, isPreviewEncode ? $"preview_last_encode_log{GeneralUtilities.ProcessId}.txt" : $"last_encode_log{GeneralUtilities.ProcessId}.txt");
-
- try
- {
- this.logBuffer = new StringBuilder();
-
- this.logBuffer.AppendLine();
-
- // Clear the current Encode Logs)
- if (File.Exists(logFile))
- {
- File.Delete(logFile);
- }
-
- lock (FileWriterLock)
- {
- this.fileWriter = new StreamWriter(logFile) { AutoFlush = true };
- this.fileWriter.WriteLine(this.header);
- this.fileWriter.WriteLine();
- }
- }
- catch (Exception)
- {
- if (this.fileWriter != null)
- {
- lock (FileWriterLock)
- {
- this.fileWriter.Flush();
- this.fileWriter.Close();
- this.fileWriter.Dispose();
- }
- }
-
- throw;
- }
- }
-
- /// <summary>
- /// The service log message.
- /// </summary>
- /// <param name="message">
- /// The message.
+ /// <param name="fileName">
+ /// The file name.
/// </param>
- protected void ServiceLogMessage(string message)
- {
- this.ProcessLogMessage(string.Format("# {0}", message));
- }
-
- /// <summary>
- /// Process an Incomming Log Message.
- /// </summary>
- /// <param name="message">
- /// The message.
+ /// <param name="content">
+ /// The content.
/// </param>
- protected void ProcessLogMessage(string message)
- {
- if (!string.IsNullOrEmpty(message))
- {
- try
- {
- this.LogIndex = this.LogIndex + 1;
-
- lock (this.LogBuffer)
- {
- this.LogBuffer.AppendLine(message);
- }
-
- lock (FileWriterLock)
- {
- if (this.fileWriter != null && this.fileWriter.BaseStream.CanWrite)
- {
- this.fileWriter.WriteLine(message);
- }
- }
- }
- catch (Exception exc)
- {
- Debug.WriteLine(exc); // This exception doesn't warrent user interaction, but it should be logged
- }
- }
- }
-
- /// <summary>
- /// Shutdown and Dispose of the File Writer.
- /// </summary>
- protected void ShutdownFileWriter()
+ private void WriteFile(string fileName, string content)
{
try
{
- lock (FileWriterLock)
+ using (StreamWriter fileWriter = new StreamWriter(fileName) { AutoFlush = true })
{
- if (this.fileWriter != null)
- {
- this.fileWriter.Flush();
- this.fileWriter.Close();
- this.fileWriter.Dispose();
- }
-
- this.fileWriter = null;
+ fileWriter.WriteLineAsync(content);
}
}
catch (Exception exc)
{
- Debug.WriteLine(exc); // This exception doesn't warrent user interaction, but it should be logged
+ Debug.WriteLine(exc);
}
}
diff --git a/win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs b/win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs
index 691df1500..b9a5f0aff 100644
--- a/win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs
+++ b/win/CS/HandBrakeWPF/Services/Encode/Interfaces/IEncode.cs
@@ -65,16 +65,6 @@ namespace HandBrakeWPF.Services.Encode.Interfaces
bool IsEncoding { get; }
/// <summary>
- /// Gets ActivityLog.
- /// </summary>
- string ActivityLog { get; }
-
- /// <summary>
- /// Gets the log index. The current log row counter.
- /// </summary>
- int LogIndex { get; }
-
- /// <summary>
/// Gets a value indicating whether is pasued.
/// </summary>
bool IsPasued { get; }
diff --git a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs
index 4bf64e5e1..e8b920c57 100644
--- a/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs
+++ b/win/CS/HandBrakeWPF/Services/Encode/LibEncode.cs
@@ -16,6 +16,9 @@ namespace HandBrakeWPF.Services.Encode
using HandBrake.ApplicationServices.Interop.EventArgs;
using HandBrake.ApplicationServices.Interop.Interfaces;
using HandBrake.ApplicationServices.Model;
+ using HandBrake.ApplicationServices.Services.Logging;
+ using HandBrake.ApplicationServices.Services.Logging.Interfaces;
+ using HandBrake.ApplicationServices.Services.Logging.Model;
using HandBrakeWPF.Exceptions;
using HandBrakeWPF.Services.Encode.Factories;
@@ -30,7 +33,7 @@ namespace HandBrakeWPF.Services.Encode
{
#region Private Variables
- private static readonly object LogLock = new object();
+ private ILog log = LogService.GetLogger();
private IHandBrakeInstance instance;
private DateTime startTime;
private EncodeTask currentTask;
@@ -70,8 +73,6 @@ namespace HandBrakeWPF.Services.Encode
// Create a new HandBrake instance
// Setup the HandBrake Instance
- HandBrakeUtils.MessageLogged += this.HandBrakeInstanceMessageLogged;
- HandBrakeUtils.ErrorLogged += this.HandBrakeInstanceErrorLogged;
this.instance = task.IsPreviewEncode ? HandBrakeInstanceManager.GetPreviewInstance(configuration.Verbosity) : HandBrakeInstanceManager.GetEncodeInstance(configuration.Verbosity);
this.instance.EncodeCompleted += this.InstanceEncodeCompleted;
@@ -79,11 +80,11 @@ namespace HandBrakeWPF.Services.Encode
this.IsEncoding = true;
this.isPreviewInstance = task.IsPreviewEncode;
- this.SetupLogging(task.IsPreviewEncode);
// Verify the Destination Path Exists, and if not, create it.
this.VerifyEncodeDestinationPath(task);
+ this.log.Reset(); // Reset so we have a clean log for the start of the encode.
this.ServiceLogMessage("Starting Encode ...");
// Get an EncodeJob object for the Interop Library
@@ -97,7 +98,7 @@ namespace HandBrakeWPF.Services.Encode
this.IsEncoding = false;
this.ServiceLogMessage("Failed to start encoding ..." + Environment.NewLine + exc);
- this.InvokeEncodeCompleted(new HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", task.Source));
+ this.InvokeEncodeCompleted(new EventArgs.EncodeCompletedEventArgs(false, exc, "Unable to start encoding", task.Source));
}
}
@@ -150,40 +151,6 @@ namespace HandBrakeWPF.Services.Encode
#region HandBrakeInstance Event Handlers.
/// <summary>
- /// Log a message
- /// </summary>
- /// <param name="sender">
- /// The sender.
- /// </param>
- /// <param name="e">
- /// The MessageLoggedEventArgs.
- /// </param>
- private void HandBrakeInstanceErrorLogged(object sender, MessageLoggedEventArgs e)
- {
- lock (LogLock)
- {
- this.ProcessLogMessage(e.Message);
- }
- }
-
- /// <summary>
- /// Log a message
- /// </summary>
- /// <param name="sender">
- /// The sender.
- /// </param>
- /// <param name="e">
- /// The MessageLoggedEventArgs.
- /// </param>
- private void HandBrakeInstanceMessageLogged(object sender, MessageLoggedEventArgs e)
- {
- lock (LogLock)
- {
- this.ProcessLogMessage(e.Message);
- }
- }
-
- /// <summary>
/// Encode Progress Event Handler
/// </summary>
/// <param name="sender">
@@ -194,7 +161,7 @@ namespace HandBrakeWPF.Services.Encode
/// </param>
private void InstanceEncodeProgress(object sender, EncodeProgressEventArgs e)
{
- HandBrakeWPF.Services.Encode.EventArgs.EncodeProgressEventArgs args = new HandBrakeWPF.Services.Encode.EventArgs.EncodeProgressEventArgs
+ EventArgs.EncodeProgressEventArgs args = new EventArgs.EncodeProgressEventArgs
{
AverageFrameRate = e.AverageFrameRate,
CurrentFrameRate = e.CurrentFrameRate,
@@ -221,22 +188,24 @@ namespace HandBrakeWPF.Services.Encode
{
this.IsEncoding = false;
this.ServiceLogMessage("Encode Completed ...");
-
- // Stop Logging.
- HandBrakeUtils.MessageLogged -= this.HandBrakeInstanceMessageLogged;
- HandBrakeUtils.ErrorLogged -= this.HandBrakeInstanceErrorLogged;
-
+
// Handling Log Data
this.ProcessLogs(this.currentTask.Destination, this.isPreviewInstance, this.currentConfiguration);
- // Cleanup
- this.ShutdownFileWriter();
-
// Raise the Encode Completed EVent.
this.InvokeEncodeCompleted(
e.Error
- ? new HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs(false, null, string.Empty, this.currentTask.Destination)
- : new HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs(true, null, string.Empty, this.currentTask.Destination));
+ ? new EventArgs.EncodeCompletedEventArgs(false, null, string.Empty, this.currentTask.Destination)
+ : new EventArgs.EncodeCompletedEventArgs(true, null, string.Empty, this.currentTask.Destination));
+ }
+
+ /// <summary>
+ /// Service Log Message.
+ /// </summary>
+ /// <param name="message">Log message content</param>
+ protected void ServiceLogMessage(string message)
+ {
+ this.log.LogMessage(string.Format("# {0}", message), LogMessageType.ScanOrEncode, LogLevel.Info);
}
#endregion
}