Tech: Simplify diagnostics api.

This commit is contained in:
Adrien Allard committed 2024-03-10 21:16:05 +01:00
1 parent 1d63309e28
commit 73a91a14bc
17 files changed
+162 -153

No files matched your search

+29 -34
View File
@@ -70,10 +70,15 @@ namespace FileConverter
}
}
public static void AskForShutdown()
{
Application.Current.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown(Debug.FirstErrorCode)));
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.RegisterServices();
this.Initialize();
@@ -117,7 +122,7 @@ namespace FileConverter
if (!this.isSessionEnding && upgradeService.UpgradeVersionDescription != null && upgradeService.UpgradeVersionDescription.NeedToUpgrade)
{
Debug.Log("A new version of file converter has been found: {0}.", upgradeService.UpgradeVersionDescription.LatestVersion);
Debug.Log($"A new version of file converter has been found: {upgradeService.UpgradeVersionDescription.LatestVersion}.");
if (string.IsNullOrEmpty(upgradeService.UpgradeVersionDescription.InstallerPath))
{
@@ -134,16 +139,16 @@ namespace FileConverter
string installerPath = upgradeService.UpgradeVersionDescription.InstallerPath;
if (!System.IO.File.Exists(installerPath))
{
Debug.LogError("Can't find upgrade installer ({0}). Try to restart the application.", installerPath);
Debug.LogError($"Can't find upgrade installer ({installerPath}). Try to restart the application.");
return;
}
// Start process.
Debug.Log("Start file converter upgrade from version {0} to {1}.", ApplicationVersion, upgradeService.UpgradeVersionDescription.LatestVersion);
Debug.Log($"Start file converter upgrade from version {ApplicationVersion} to {upgradeService.UpgradeVersionDescription.LatestVersion}.");
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(installerPath) { UseShellExecute = true, };
Debug.Log("Start upgrade process: {0}{1}.", System.IO.Path.GetFileName(startInfo.FileName), startInfo.Arguments);
Debug.Log($"Start upgrade process: {System.IO.Path.GetFileName(startInfo.FileName)}{startInfo.Arguments}.");
Process process = new System.Diagnostics.Process { StartInfo = startInfo };
process.Start();
@@ -172,7 +177,7 @@ namespace FileConverter
else
{
Debug.LogError("Can't retrieve view model locator.");
this.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Application.AskForShutdown();
}
if (this.TryFindResource("Upgrade") is UpgradeService upgradeService)
@@ -182,7 +187,7 @@ namespace FileConverter
else
{
Debug.LogError("Can't retrieve Upgrade service.");
this.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Application.AskForShutdown();
}
services
@@ -217,7 +222,7 @@ namespace FileConverter
for (int index = 0; index < args.Length; index++)
{
string argument = args[index];
Debug.Log("Arg{0}: {1}", index, argument);
Debug.Log($"Arg{index}: {argument}");
}
Debug.Log(string.Empty);
@@ -233,7 +238,6 @@ namespace FileConverter
// Parse arguments.
bool quitAfterStartup = false;
int quitExitCode = 0;
List<string> filePaths = new List<string>();
string conversionPresetName = null;
for (int index = 1; index < args.Length; index++)
@@ -254,8 +258,7 @@ namespace FileConverter
case "post-install-init":
if (!settingsService.PostInstallationInitialization())
{
quitExitCode = 0x0F;
Debug.LogError(quitExitCode, $"Failed to execute post install initialization.");
Debug.LogError(errorCode: 0x0F, $"Failed to execute post install initialization.");
}
quitAfterStartup = true;
@@ -267,8 +270,7 @@ namespace FileConverter
if (index >= args.Length - 1)
{
quitExitCode = 0x0B;
Debug.LogError(quitExitCode, $"Invalid format.");
Debug.LogError(errorCode: 0x0B, $"Invalid format.");
break;
}
@@ -277,8 +279,7 @@ namespace FileConverter
if (!Helpers.RegisterShellExtension(shellExtensionPath))
{
quitExitCode = 0x0C;
Debug.LogError(quitExitCode, $"Failed to register shell extension {shellExtensionPath}.");
Debug.LogError(errorCode: 0x0C, $"Failed to register shell extension {shellExtensionPath}.");
}
break;
@@ -290,8 +291,7 @@ namespace FileConverter
if (index >= args.Length - 1)
{
quitExitCode = 0x0D;
Debug.LogError(quitExitCode, $"Invalid format.");
Debug.LogError(errorCode: 0x0D, $"Invalid format.");
break;
}
@@ -300,8 +300,7 @@ namespace FileConverter
if (!Helpers.UnregisterExtension(shellExtensionPath))
{
quitExitCode = 0x0E;
Debug.LogError(quitExitCode, $"Failed to unregister shell extension {shellExtensionPath}.");
Debug.LogError(errorCode: 0x0E, $"Failed to unregister shell extension {shellExtensionPath}.");
}
break;
@@ -320,8 +319,7 @@ namespace FileConverter
if (index >= args.Length - 1)
{
quitAfterStartup = true;
quitExitCode = 0x01;
Debug.LogError(quitExitCode, $"Invalid format.");
Debug.LogError(errorCode: 0x01, $"Invalid format.");
break;
}
@@ -333,8 +331,7 @@ namespace FileConverter
if (index >= args.Length - 1)
{
quitAfterStartup = true;
quitExitCode = 0x02;
Debug.LogError(quitExitCode, $"Invalid format.");
Debug.LogError(errorCode: 0x02, $"Invalid format.");
break;
}
@@ -353,8 +350,7 @@ namespace FileConverter
catch (Exception exception)
{
quitAfterStartup = true;
quitExitCode = 0x03;
Debug.LogError(quitExitCode, $"Can't read input files list: {exception}");
Debug.LogError(errorCode: 0x03, $"Can't read input files list: {exception}");
}
index++;
@@ -368,7 +364,7 @@ namespace FileConverter
break;
default:
Debug.LogError("Unknown application argument: '--{0}'.", parameterTitle);
Debug.LogError($"Unknown application argument: '--{parameterTitle}'.");
return;
}
}
@@ -380,18 +376,17 @@ namespace FileConverter
if (settingsService.Settings == null)
{
Debug.LogError("Can't load File Converter settings. The application will now shutdown, if you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml.");
Debug.LogError(errorCode: 0x04, "Can't load File Converter settings. The application will now shutdown, if you want to fix the problem yourself please edit or delete the file: C:\\Users\\UserName\\AppData\\Local\\FileConverter\\Settings.user.xml.");
quitAfterStartup = true;
quitExitCode = 13;
}
if (quitAfterStartup)
{
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown(quitExitCode)));
Application.AskForShutdown();
return;
}
Debug.Assert(quitExitCode == 0, "An error happened during the initialization.");
Debug.Assert(Debug.FirstErrorCode == 0, "An error happened during the initialization.");
// Check for upgrade.
if (settingsService.Settings.CheckUpgradeAtStartup)
@@ -407,8 +402,8 @@ namespace FileConverter
conversionPreset = settingsService.Settings.GetPresetFromName(conversionPresetName);
if (conversionPreset == null)
{
Debug.LogError("Invalid conversion preset '{0}'. (code 0x02)", conversionPresetName);
Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Debug.LogError(errorCode: 0x02, $"Invalid conversion preset '{conversionPresetName}'.");
Application.AskForShutdown();
return;
}
}
@@ -418,7 +413,7 @@ namespace FileConverter
IConversionService conversionService = Ioc.Default.GetRequiredService<IConversionService>();
// Create conversion jobs.
Debug.Log("Create jobs for conversion preset: '{0}'", conversionPreset.FullName);
Debug.Log($"Create jobs for conversion preset: '{conversionPreset.FullName}'");
try
{
for (int index = 0; index < filePaths.Count; index++)
@@ -488,7 +483,7 @@ namespace FileConverter
this.OnApplicationTerminate.Invoke(this, new ApplicationTerminateArgs(remainingTime));
}
this.Dispatcher.BeginInvoke((Action)(() => Application.Current.Shutdown()));
Application.AskForShutdown();
}
}
}
@@ -312,7 +312,7 @@ namespace FileConverter.ConversionJobs
this.State = ConversionState.Ready;
}
Debug.Log("Job initialized: Preset: '{0}' Input: {1} Output: {2}", this.ConversionPreset.FullName, this.InputFilePath, this.OutputFilePath);
Debug.Log($"Job initialized: Preset: '{this.ConversionPreset.FullName}' Input: {this.InputFilePath} Output: {this.OutputFilePath}");
if (this.State != ConversionState.Failed)
{
@@ -332,7 +332,7 @@ namespace FileConverter.ConversionJobs
throw new Exception("Invalid conversion state.");
}
Debug.Log("Convert file {0} to {1}.", this.InputFilePath, this.OutputFilePath);
Debug.Log($"Convert file {this.InputFilePath} to {this.OutputFilePath}.");
this.StartTime = DateTime.Now;
this.State = ConversionState.InProgress;
@@ -407,8 +407,8 @@ namespace FileConverter.ConversionJobs
}
catch (Exception exception)
{
Debug.Log("Can't delete file '{0}' after conversion job failure.", outputFilePath);
Debug.Log("An exception as been thrown: {0}.", exception.ToString());
Debug.Log($"Can't delete file '{outputFilePath}' after conversion job failure.");
Debug.Log($"An exception as been thrown: {exception}.");
}
}
}
@@ -436,12 +436,12 @@ namespace FileConverter.ConversionJobs
string newPath = PathHelpers.GenerateUniquePath(archivePath + "\\" + inputFilename);
System.IO.File.Move(this.InputFilePath, newPath);
Debug.Log("Input file moved in archive folder: '{0}'", newPath);
Debug.Log($"Input file moved in archive folder: '{newPath}'");
break;
case InputPostConversionAction.Delete:
System.IO.File.Delete(this.InputFilePath);
Debug.Log("Input file deleted: '{0}'", this.initialInputPath);
Debug.Log($"Input file deleted: '{this.initialInputPath}'");
break;
}
@@ -455,7 +455,7 @@ namespace FileConverter.ConversionJobs
protected void ConversionFailed(string exitingMessage)
{
Debug.Log("Fail: {0}", exitingMessage);
Debug.Log($"Fail: {exitingMessage}");
if (this.State == ConversionState.Failed)
{
@@ -483,7 +483,7 @@ namespace FileConverter.ConversionJobs
var originalFileCreationTime = System.IO.File.GetCreationTimeUtc(this.InputFilePath);
var originalFileLastAccesTime = System.IO.File.GetLastAccessTimeUtc(this.InputFilePath);
var originalFileLastWriteTime = System.IO.File.GetLastWriteTimeUtc(this.InputFilePath);
Debug.Log(" original timestamp: {0}, {1}, {2}", originalFileCreationTime, originalFileLastAccesTime, originalFileLastWriteTime);
Debug.Log($" original timestamp: {originalFileCreationTime}, {originalFileLastAccesTime}, {originalFileLastWriteTime}");
for (int index = 0; index < this.OutputFilePaths.Length; index++)
{
@@ -493,12 +493,12 @@ namespace FileConverter.ConversionJobs
System.IO.File.SetCreationTimeUtc(outputFilePath, originalFileCreationTime);
System.IO.File.SetLastAccessTimeUtc(outputFilePath, originalFileLastAccesTime);
System.IO.File.SetLastWriteTimeUtc(outputFilePath, originalFileLastWriteTime);
Debug.Log(" output file '{0}' timestamp changed", outputFilePath);
Debug.Log($" output file '{outputFilePath}' timestamp changed");
}
catch (Exception exception)
{
Debug.Log("Can't change timestamp from file '{0}'", outputFilePath);
Debug.Log("An exception as been thrown: {0}.", exception.ToString());
Debug.Log($"Can't change timestamp from file '{outputFilePath}'");
Debug.Log($"An exception as been thrown: {exception}.");
}
}
@@ -107,10 +107,10 @@ namespace FileConverter.ConversionJobs
this.UserState = Properties.Resources.ConversionStateConversion;
Diagnostics.Debug.Log("Convert excel document to pdf.");
Debug.Log("Convert excel document to pdf.");
this.document.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, this.intermediateFilePath);
Diagnostics.Debug.Log("Close excel document '{0}'.", this.InputFilePath);
Debug.Log($"Close excel document '{this.InputFilePath}'.");
this.document.Close(false);
this.document = null;
@@ -126,7 +126,7 @@ namespace FileConverter.ConversionJobs
Task updateProgress = this.UpdateProgress();
Diagnostics.Debug.Log("Convert pdf to images.");
Debug.Log("Convert pdf to images.");
this.pdf2ImageConversionJob.StartConversion();
@@ -138,7 +138,7 @@ namespace FileConverter.ConversionJobs
if (!string.IsNullOrEmpty(this.intermediateFilePath))
{
Diagnostics.Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
Debug.Log($"Delete intermediate file {this.intermediateFilePath}.");
File.Delete(this.intermediateFilePath);
}
@@ -213,7 +213,7 @@ namespace FileConverter.ConversionJobs
if (this.document == null)
{
Diagnostics.Debug.Log("Load excel document '{0}'.", this.InputFilePath);
Debug.Log($"Load excel document '{this.InputFilePath}'.");
this.document = this.application.Workbooks.Open(this.InputFilePath, System.Reflection.Missing.Value, true);
}
@@ -167,7 +167,7 @@ namespace FileConverter.ConversionJobs
return;
}
Debug.Log("CDA extracted to {0}.", this.intermediateFilePath);
Debug.Log($"CDA extracted to {this.intermediateFilePath}.");
Debug.Log(string.Empty);
Debug.Log("Start compression.");
@@ -188,7 +188,7 @@ namespace FileConverter.ConversionJobs
}
Debug.Log(string.Empty);
Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
Debug.Log($"Delete intermediate file {this.intermediateFilePath}.");
File.Delete(this.intermediateFilePath);
}
@@ -75,7 +75,7 @@ namespace FileConverter.ConversionJobs
}
else
{
Diagnostics.Debug.LogError("Unsupported rotation: {0}°", rotationAngleInDegrees);
Diagnostics.Debug.LogError($"Unsupported rotation: {rotationAngleInDegrees}°");
}
}
@@ -68,7 +68,7 @@ namespace FileConverter.ConversionJobs
if (!System.IO.File.Exists(ffmpegPath))
{
this.ConversionFailed(Properties.Resources.ErrorCantFindFFMPEG);
Diagnostics.Debug.Log("Can't find ffmpeg executable ({0}). Try to reinstall the application.", ffmpegPath);
Diagnostics.Debug.Log($"Can't find ffmpeg executable ({ffmpegPath}). Try to reinstall the application.");
return;
}
@@ -419,7 +419,7 @@ namespace FileConverter.ConversionJobs
this.UserState = currentPass.Name;
this.ffmpegProcessStartInfo.Arguments = currentPass.Arguments;
Diagnostics.Debug.Log("Execute command: {0} {1}.", this.ffmpegProcessStartInfo.FileName, this.ffmpegProcessStartInfo.Arguments);
Diagnostics.Debug.Log($"Execute command: {this.ffmpegProcessStartInfo.FileName} {this.ffmpegProcessStartInfo.Arguments}.");
Diagnostics.Debug.Log(string.Empty);
try
@@ -439,7 +439,7 @@ namespace FileConverter.ConversionJobs
this.ParseFFMPEGOutput(result);
Diagnostics.Debug.Log("ffmpeg output: {0}", result);
Diagnostics.Debug.Log($"ffmpeg output: {result}");
}
}
@@ -465,7 +465,7 @@ namespace FileConverter.ConversionJobs
continue;
}
Diagnostics.Debug.Log("Delete intermediate file {0}.", currentPass.FileToDelete);
Diagnostics.Debug.Log($"Delete intermediate file {currentPass.FileToDelete}.");
File.Delete(currentPass.FileToDelete);
}
@@ -99,7 +99,7 @@ namespace FileConverter.ConversionJobs
if (!string.IsNullOrEmpty(this.intermediateFilePath))
{
Diagnostics.Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
Diagnostics.Debug.Log($"Delete intermediate file {this.intermediateFilePath}.");
File.Delete(this.intermediateFilePath);
}
@@ -76,7 +76,7 @@ namespace FileConverter.ConversionJobs
return;
}
Diagnostics.Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
Diagnostics.Debug.Log($"Delete intermediate file {this.intermediateFilePath}.");
File.Delete(this.intermediateFilePath);
}
@@ -92,7 +92,7 @@ namespace FileConverter.ConversionJobs
using (MagickImage image = new MagickImage(this.InputFilePath, readSettings))
{
Debug.Log("Load image {0} succeed.", this.InputFilePath);
Debug.Log($"Load image {this.InputFilePath} succeed.");
this.ConvertImage(image);
}
}
@@ -106,12 +106,12 @@ namespace FileConverter.ConversionJobs
float scaleFactor = this.ConversionPreset.GetSettingsValue<float>(ConversionPreset.ConversionSettingKeys.ImageScale);
if (Math.Abs(scaleFactor - 1f) >= 0.005f)
{
Debug.Log("Apply scale factor: {0}%.", scaleFactor * 100);
Debug.Log($"Apply scale factor: {scaleFactor * 100}%.");
dpi *= scaleFactor;
}
Debug.Log("Density: {0}dpi.", dpi);
Debug.Log($"Density: {dpi}dpi.");
settings.Density = new Density(dpi * PdfSuperSamplingRatio);
this.UserState = Properties.Resources.ConversionStateReadDocument;
@@ -120,7 +120,7 @@ namespace FileConverter.ConversionJobs
{
// Add all the pages of the pdf file to the collection
images.Read(this.InputFilePath, settings);
Debug.Log("Load pdf {0} succeed.", this.InputFilePath);
Debug.Log($"Load pdf {this.InputFilePath} succeed.");
this.pageCount = images.Count;
@@ -128,7 +128,7 @@ namespace FileConverter.ConversionJobs
foreach (MagickImage image in images)
{
Debug.Log("Write page {0}/{1}.", this.CurrentOutputFilePathIndex + 1, this.pageCount);
Debug.Log($"Write page {this.CurrentOutputFilePathIndex + 1}/{this.pageCount}.");
if (PdfSuperSamplingRatio > 1)
{
@@ -153,7 +153,7 @@ namespace FileConverter.ConversionJobs
float scaleFactor = this.ConversionPreset.GetSettingsValue<float>(ConversionPreset.ConversionSettingKeys.ImageScale);
if (Math.Abs(scaleFactor - 1f) >= 0.005f)
{
Debug.Log("Apply scale factor: {0}%.", scaleFactor * 100);
Debug.Log($"Apply scale factor: {scaleFactor * 100}%.");
image.Scale(new Percentage(scaleFactor * 100f));
}
@@ -164,7 +164,7 @@ namespace FileConverter.ConversionJobs
float rotateAngleInDegrees = this.ConversionPreset.GetSettingsValue<float>(ConversionPreset.ConversionSettingKeys.ImageRotation);
if (Math.Abs(rotateAngleInDegrees - 0f) >= 0.05f)
{
Debug.Log("Apply rotation: {0}°.", rotateAngleInDegrees);
Debug.Log($"Apply rotation: {rotateAngleInDegrees}°.");
image.Rotate(rotateAngleInDegrees);
}
@@ -182,7 +182,7 @@ namespace FileConverter.ConversionJobs
size *= 2;
}
Debug.Log("Clamp size to the nearest power of 2 size (from {0}x{1} to {2}x{2}).", image.Width, image.Height, size);
Debug.Log($"Clamp size to the nearest power of 2 size (from {image.Width}x{image.Height} to {size}x{size}).");
image.Scale(size, size);
}
@@ -196,13 +196,13 @@ namespace FileConverter.ConversionJobs
int width = System.Math.Min(image.Width, maximumSize);
int height = System.Math.Min(image.Height, maximumSize);
Debug.Log("Clamp size to maximum size of {2}x{2} (from {0}x{1} to {2}x{3}).", image.Width, image.Height, width, height);
Debug.Log($"Clamp size to maximum size of {width}x{width} (from {image.Width}x{image.Height} to {width}x{height}).");
image.Scale(width, height);
}
}
Debug.Log("Convert image (output: {0}).", this.OutputFilePath);
Debug.Log($"Convert image (output: {this.OutputFilePath}).");
switch (this.ConversionPreset.OutputType)
{
case OutputType.Png:
@@ -215,7 +215,7 @@ namespace FileConverter.ConversionJobs
break;
case OutputType.Pdf:
Debug.Log("Density: {0}dpi.", BaseDpiForPdfConversion);
Debug.Log($"Density: {BaseDpiForPdfConversion}dpi.");
image.Density = new Density(BaseDpiForPdfConversion);
break;
@@ -96,10 +96,10 @@ namespace FileConverter.ConversionJobs
this.UserState = Properties.Resources.ConversionStateConversion;
Diagnostics.Debug.Log("Convert PowerPoint document to pdf.");
Debug.Log("Convert PowerPoint document to pdf.");
this.document.ExportAsFixedFormat(this.intermediateFilePath, PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);
Diagnostics.Debug.Log("Close PowerPoint document '{0}'.", this.InputFilePath);
Debug.Log($"Close PowerPoint document '{this.InputFilePath}'.");
this.document.Close();
this.document = null;
@@ -115,7 +115,7 @@ namespace FileConverter.ConversionJobs
Task updateProgress = this.UpdateProgress();
Diagnostics.Debug.Log("Convert pdf to images.");
Debug.Log("Convert pdf to images.");
this.pdf2ImageConversionJob.StartConversion();
@@ -127,7 +127,7 @@ namespace FileConverter.ConversionJobs
if (!string.IsNullOrEmpty(this.intermediateFilePath))
{
Diagnostics.Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
Debug.Log($"Delete intermediate file {this.intermediateFilePath}.");
File.Delete(this.intermediateFilePath);
}
@@ -144,7 +144,7 @@ namespace FileConverter.ConversionJobs
}
// Initialize PowerPoint application.
Diagnostics.Debug.Log("Instantiate PowerPoint application via interop.");
Debug.Log("Instantiate PowerPoint application via interop.");
this.application = new PowerPoint.Application();
}
@@ -199,7 +199,7 @@ namespace FileConverter.ConversionJobs
if (this.document == null)
{
Diagnostics.Debug.Log("Load PowerPoint document '{0}'.", this.InputFilePath);
Debug.Log($"Load PowerPoint document '{this.InputFilePath}'.");
this.document = this.application.Presentations.Open(this.InputFilePath, ReadOnly: MsoTriState.msoTrue, WithWindow: MsoTriState.msoFalse);
}
@@ -99,7 +99,7 @@ namespace FileConverter.ConversionJobs
this.UserState = Properties.Resources.ConversionStateConversion;
Diagnostics.Debug.Log("Convert word document to pdf.");
Debug.Log("Convert word document to pdf.");
// this.document.ExportAsFixedFormat(this.intermediateFilePath, Word.WdExportFormat.wdExportFormatPDF);
this.document.ExportAsFixedFormat(this.intermediateFilePath,
Word.WdExportFormat.wdExportFormatPDF,
@@ -113,7 +113,7 @@ namespace FileConverter.ConversionJobs
Word.WdExportCreateBookmarks.wdExportCreateHeadingBookmarks,
true);
Diagnostics.Debug.Log("Close word document '{0}'.", this.InputFilePath);
Debug.Log($"Close word document '{this.InputFilePath}'.");
this.document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
this.document = null;
@@ -129,7 +129,7 @@ namespace FileConverter.ConversionJobs
Task updateProgress = this.UpdateProgress();
Diagnostics.Debug.Log("Convert pdf to images.");
Debug.Log("Convert pdf to images.");
this.pdf2ImageConversionJob.StartConversion();
@@ -141,7 +141,7 @@ namespace FileConverter.ConversionJobs
if (!string.IsNullOrEmpty(this.intermediateFilePath))
{
Diagnostics.Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath);
Debug.Log($"Delete intermediate file {this.intermediateFilePath}.");
File.Delete(this.intermediateFilePath);
}
@@ -158,7 +158,7 @@ namespace FileConverter.ConversionJobs
}
// Initialize word application.
Diagnostics.Debug.Log("Instantiate word application via interop.");
Debug.Log("Instantiate word application via interop.");
this.application = new Microsoft.Office.Interop.Word.Application
{
Visible = false
@@ -216,7 +216,7 @@ namespace FileConverter.ConversionJobs
if (this.document == null)
{
Diagnostics.Debug.Log("Load word document '{0}'.", this.InputFilePath);
Debug.Log($"Load word document '{this.InputFilePath}'.");
this.document = this.application.Documents.Open(this.InputFilePath, System.Reflection.Missing.Value, true);
}
+66 -52
View File
@@ -5,6 +5,7 @@ namespace FileConverter.Diagnostics
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
@@ -41,27 +42,74 @@ namespace FileConverter.Diagnostics
Debug.diagnosticsFolderPath = Path.Combine(path, diagnosticsFolderName);
Debug.diagnosticsFolderPath = PathHelpers.GenerateUniquePath(Debug.diagnosticsFolderPath);
Directory.CreateDirectory(Debug.diagnosticsFolderPath);
Debug.Log($"Diagnostics stored at path '{Debug.diagnosticsFolderPath}'");
}
public static int FirstErrorCode
{
get;
private set;
}
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
public static DiagnosticsData[] Data => Debug.diagnosticsDataById.Values.ToArray();
public static void Log(string message, params object[] arguments)
{
string log = arguments.Length > 0 ? string.Format(message, arguments) : message;
Debug.Log(log, ConsoleColor.White);
}
public static void Log(string message)
{
Debug.Log(message, ConsoleColor.White);
Debug.LogInternal(error: false, message, ConsoleColor.White);
}
public static void Log(string log, ConsoleColor color)
public static void Assert(bool condition)
{
if (!condition)
{
LogError("Assertion failed");
}
}
public static void Assert(bool condition, string message)
{
if (!condition)
{
LogError(message);
}
}
public static void LogError(string message)
{
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Debug.LogInternal(error: true, $"Error: {message}", ConsoleColor.Red);
}
public static void LogError(int errorCode, string message)
{
if (Debug.FirstErrorCode == 0)
{
Debug.FirstErrorCode = errorCode;
}
Debug.LogError($"{message} (code 0x{errorCode:X})");
}
public static void Release()
{
Debug.Log("Diagnostics manager released correctly.");
foreach (KeyValuePair<int, DiagnosticsData> kvp in Debug.diagnosticsDataById)
{
kvp.Value.Release();
}
Debug.diagnosticsDataById.Clear();
}
private static void LogInternal(bool error, string log, ConsoleColor color)
{
DiagnosticsData diagnosticsData;
Thread currentThread = Thread.CurrentThread;
int threadId = currentThread.ManagedThreadId;
@@ -69,7 +117,15 @@ namespace FileConverter.Diagnostics
if (threadId == Debug.mainThreadId)
{
Console.ForegroundColor = color;
Console.WriteLine(log);
if (error)
{
Console.Error.WriteLine(log);
}
else
{
Console.WriteLine(log);
}
Console.ResetColor();
}
@@ -89,47 +145,5 @@ namespace FileConverter.Diagnostics
diagnosticsData.Log(log);
}
public static void Assert(bool condition)
{
if (!condition)
{
LogError("Assertion failed");
}
}
public static void Assert(bool condition, string message)
{
if (!condition)
{
LogError(message);
}
}
public static void LogError(string message, params object[] arguments)
{
string log = string.Format(message, arguments);
MessageBox.Show(log, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
Debug.Log($"Error: {log}", ConsoleColor.Red);
}
public static void LogError(int errorCode, string message)
{
Debug.LogError($"{message} (code 0x{errorCode:X})");
}
public static void Release()
{
Debug.Log("Diagnostics manager released correctly.");
foreach (KeyValuePair<int, DiagnosticsData> kvp in Debug.diagnosticsDataById)
{
kvp.Value.Release();
}
Debug.diagnosticsDataById.Clear();
}
}
}
+6 -6
View File
@@ -58,7 +58,7 @@ namespace FileConverter
if (this.registryEntries.ContainsKey(entry.Key))
{
Diagnostics.Debug.Log("Ignore registry entry {0}.", entry.Key);
Diagnostics.Debug.Log($"Ignore registry entry {entry.Key}.");
continue;
}
@@ -106,7 +106,7 @@ namespace FileConverter
}
catch (Exception exception)
{
Diagnostics.Debug.LogError("Can't convert registry value: {0}.", exception.Message);
Diagnostics.Debug.LogError($"Can't convert registry value: {exception.Message}.");
}
return defaultValue;
@@ -128,7 +128,7 @@ namespace FileConverter
}
catch (Exception exception)
{
Diagnostics.Debug.LogError("Can't convert registry value: {0}.", exception.Message);
Diagnostics.Debug.LogError($"Can't convert registry value: {exception.Message}.");
}
}
@@ -143,7 +143,7 @@ namespace FileConverter
}
catch (Exception exception)
{
Diagnostics.Debug.LogError("Fail to save registry. {0}", exception.Message);
Diagnostics.Debug.LogError($"Fail to save registry. {exception.Message}");
}
}
@@ -164,11 +164,11 @@ namespace FileConverter
catch (Exception exception)
{
Registry.instance = new Registry();
Diagnostics.Debug.LogError("Fail to load registry. {0}", exception.Message);
Diagnostics.Debug.LogError($"Fail to load registry. {exception.Message}");
while (exception.InnerException != null)
{
exception = exception.InnerException;
Diagnostics.Debug.LogError("Inner exception: {0}", exception.Message);
Diagnostics.Debug.LogError($"Inner exception: {exception.Message}");
}
}
}
@@ -33,12 +33,12 @@ namespace FileConverter.Services
this.ConversionJobs = this.conversionJobs.AsReadOnly();
this.numberOfConversionThread = this.settingsService.Settings.MaximumNumberOfSimultaneousConversions;
Diagnostics.Debug.Log("Maximum number of conversion threads: {0}", this.numberOfConversionThread);
Debug.Log($"Maximum number of conversion threads: {this.numberOfConversionThread}");
if (this.numberOfConversionThread <= 0)
{
this.numberOfConversionThread = System.Math.Max(1, Environment.ProcessorCount / 2);
Diagnostics.Debug.Log("The number of processors on this computer is {0}. Set the default number of conversion threads to {0}", settingsService.Settings.MaximumNumberOfSimultaneousConversions);
Debug.Log($"The number of processors on this computer is {settingsService.Settings.MaximumNumberOfSimultaneousConversions}. Set the default number of conversion threads to {settingsService.Settings.MaximumNumberOfSimultaneousConversions}");
}
}
@@ -176,7 +176,7 @@ namespace FileConverter.Services
}
catch (Exception exception)
{
Debug.LogError("Failure during conversion: {0}", exception.ToString());
Debug.LogError($"Failure during conversion: {exception}");
}
}
@@ -189,13 +189,13 @@ namespace FileConverter.Services
Debug.Log("Output files copied to the clipboard:");
for (int index = 0; index < FilePaths.Count; index++)
{
Debug.Log(" {0}", FilePaths[index]);
Debug.Log($" {FilePaths[index]}");
}
}
catch (Exception exception)
{
Debug.Log("Can't copy files to the clipboard.");
Debug.Log("An exception has been thrown: {0}.", exception.ToString());
Debug.Log($"An exception has been thrown: {exception}.");
}
}
}
@@ -112,7 +112,7 @@ namespace FileConverter.Services
if (pageKey == Pages.Upgrade)
{
upgradeService.CancelUpgrade();
Application.Current.Shutdown();
Application.AskForShutdown();
}
else
{
@@ -123,7 +123,7 @@ namespace FileConverter.Services
}
else
{
Application.Current.Shutdown();
Application.AskForShutdown();
}
}
}
@@ -59,7 +59,7 @@ namespace FileConverter.Services
}
else
{
Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", FileConverterExtension.PathHelpers.DefaultSettingsFilePath);
Debug.LogError($"Default settings not found at path {FileConverterExtension.PathHelpers.DefaultSettingsFilePath}. You should try to reinstall the application.");
return false;
}
@@ -82,7 +82,7 @@ namespace FileConverter.Services
{
this.MigrateSettingsToCurrentVersion(userSettings);
Debug.Log("File converter settings have been imported from version {0} to version {1}.", userSettings.SerializationVersion, Settings.Version);
Debug.Log($"File converter settings have been imported from version {userSettings.SerializationVersion} to version {Settings.Version}.");
userSettings.SerializationVersion = Settings.Version;
}
@@ -154,7 +154,7 @@ namespace FileConverter.Services
{
this.MigrateSettingsToCurrentVersion(userSettings);
Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", userSettings.SerializationVersion, Settings.Version);
Debug.Log($"File converter settings has been imported from version {userSettings.SerializationVersion} to version {Settings.Version}.");
userSettings.SerializationVersion = Settings.Version;
this.Save(userSettings);
}
@@ -171,12 +171,12 @@ namespace FileConverter.Services
}
catch (Exception exception)
{
Debug.LogError("Fail to load file converter default settings. {0}", exception.Message);
Debug.LogError($"Fail to load file converter default settings. {exception.Message}");
}
}
else
{
Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", FileConverterExtension.PathHelpers.DefaultSettingsFilePath);
Debug.LogError($"Default settings not found at path {FileConverterExtension.PathHelpers.DefaultSettingsFilePath}. You should try to reinstall the application.");
}
}
+1 -1
View File
@@ -254,7 +254,7 @@ namespace FileConverter
}
else
{
Diagnostics.Debug.Log("Can't find supported culture info for culture {0}. Fallback to default culture.", currentUICulture);
Diagnostics.Debug.Log($"Can't find supported culture info for culture {currentUICulture}. Fallback to default culture.");
this.ApplicationLanguage = CultureInfo.GetCultureInfo("en");
}
}