Handle write exception for log handler (#2135)

This commit is contained in:
pizzaboxer 2024-07-03 10:36:44 +04:00
parent 6871d5cd7d
commit b7e3b04f62
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
4 changed files with 31 additions and 1 deletions

View File

@ -182,7 +182,7 @@ namespace Bloxstrap
{ {
Logger.Initialize(LaunchSettings.IsUninstall); Logger.Initialize(LaunchSettings.IsUninstall);
if (!Logger.Initialized) if (!Logger.Initialized && !Logger.NoWriteMode)
{ {
Logger.WriteLine(LOG_IDENT, "Possible duplicate launch detected, terminating."); Logger.WriteLine(LOG_IDENT, "Possible duplicate launch detected, terminating.");
Terminate(); Terminate();

View File

@ -9,6 +9,7 @@
public readonly List<string> Backlog = new(); public readonly List<string> Backlog = new();
public bool Initialized = false; public bool Initialized = false;
public bool NoWriteMode = false;
public string? FileLocation; public string? FileLocation;
public void Initialize(bool useTempDir = false) public void Initialize(bool useTempDir = false)
@ -45,6 +46,23 @@
WriteLine(LOG_IDENT, "Failed to initialize because log file already exists"); WriteLine(LOG_IDENT, "Failed to initialize because log file already exists");
return; return;
} }
catch (UnauthorizedAccessException)
{
if (NoWriteMode)
return;
WriteLine(LOG_IDENT, $"Failed to initialize because Bloxstrap cannot write to {directory}");
Frontend.ShowMessageBox(
String.Format(Resources.Strings.Logger_NoWriteMode, directory),
System.Windows.MessageBoxImage.Warning,
System.Windows.MessageBoxButton.OK
);
NoWriteMode = true;
return;
}
Initialized = true; Initialized = true;

View File

@ -1317,6 +1317,15 @@ namespace Bloxstrap.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to No log file will be written for this launch because Bloxstrap is unable to write to the folder at &apos;{0}&apos;.
/// </summary>
public static string Logger_NoWriteMode {
get {
return ResourceManager.GetString("Logger.NoWriteMode", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Contributors. /// Looks up a localized string similar to Contributors.
/// </summary> /// </summary>

View File

@ -1079,4 +1079,7 @@ Selecting 'No' will ignore this warning and continue installation.</value>
<data name="Menu.FastFlagEditor.InvalidNumberValue" xml:space="preserve"> <data name="Menu.FastFlagEditor.InvalidNumberValue" xml:space="preserve">
<value>The entry for '{0}' is not valid as the value must be a number</value> <value>The entry for '{0}' is not valid as the value must be a number</value>
</data> </data>
<data name="Logger.NoWriteMode" xml:space="preserve">
<value>No log file will be written for this launch because Bloxstrap is unable to write to the folder at '{0}'</value>
</data>
</root> </root>