feat: allow specifying logging path via --logpath, make sure serilog can always write to injector log

This commit is contained in:
goat 2023-06-19 19:52:31 +02:00
parent b901ad5aff
commit 2e380b10d5
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B
4 changed files with 40 additions and 17 deletions

View file

@ -115,9 +115,9 @@ namespace Dalamud.Injector
}
}
private static string GetLogPath(string fileName, string logName)
private static string GetLogPath(string? baseDirectory, string fileName, string? logName)
{
var baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
baseDirectory ??= Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
fileName = !string.IsNullOrEmpty(logName) ? $"{fileName}-{logName}.log" : $"{fileName}.log";
#if DEBUG
@ -168,6 +168,7 @@ namespace Dalamud.Injector
Log.Error("A fatal error has occurred: {Exception}", eventArgs.ExceptionObject.ToString());
}
Log.CloseAndFlush();
Environment.Exit(-1);
};
}
@ -180,15 +181,18 @@ namespace Dalamud.Injector
};
var logName = args.FirstOrDefault(x => x.StartsWith("--logname="))?[10..];
var logPath = GetLogPath("dalamud.injector", logName);
var logBaseDir = args.FirstOrDefault(x => x.StartsWith("--logpath="))?[10..];
var logPath = GetLogPath(logBaseDir, "dalamud.injector", logName);
CullLogFile(logPath, 1 * 1024 * 1024);
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(standardErrorFromLevel: LogEventLevel.Verbose)
.WriteTo.Async(a => a.File(logPath))
.WriteTo.File(logPath, fileSizeLimitBytes: null)
.MinimumLevel.ControlledBy(levelSwitch)
.CreateLogger();
Log.Information(new string('-', 80));
Log.Information("Dalamud.Injector, (c) 2023 XIVLauncher Contributors");
}
private static void CullLogFile(string logPath, int cullingFileSize)
@ -199,9 +203,10 @@ namespace Dalamud.Injector
var logFile = new FileInfo(logPath);
// Leave it to serilog
if (!logFile.Exists)
{
logFile.Create();
return;
}
if (logFile.Length <= cullingFileSize)
@ -256,6 +261,7 @@ namespace Dalamud.Injector
var assetDirectory = startInfo.AssetDirectory;
var delayInitializeMs = startInfo.DelayInitializeMs;
var logName = startInfo.LogName;
var logPath = startInfo.LogPath;
var languageStr = startInfo.Language.ToString().ToLowerInvariant();
var troubleshootingData = "{\"empty\": true, \"description\": \"No troubleshooting data supplied.\"}";
@ -293,6 +299,10 @@ namespace Dalamud.Injector
{
logName = args[i][key.Length..];
}
else if (args[i].StartsWith(key = "--logpath="))
{
logPath = args[i][key.Length..];
}
else
{
continue;
@ -357,11 +367,19 @@ namespace Dalamud.Injector
startInfo.GameVersion = null;
startInfo.TroubleshootingPackData = troubleshootingData;
startInfo.LogName = logName;
startInfo.LogPath = logPath;
// TODO: XL should set --logpath to its roaming path. We are only doing this here until that's rolled out.
#if DEBUG
startInfo.LogPath ??= startInfo.WorkingDirectory;
#else
startInfo.LogPath = xivlauncherDir;
#endif
// Set boot defaults
startInfo.BootShowConsole = args.Contains("--console");
startInfo.BootEnableEtw = args.Contains("--etw");
startInfo.BootLogPath = GetLogPath("dalamud.boot", startInfo.LogName);
startInfo.BootLogPath = GetLogPath(startInfo.LogPath, "dalamud.boot", startInfo.LogName);
startInfo.BootEnabledGameFixes = new List<string> { "prevent_devicechange_crashes", "disable_game_openprocess_access_check", "redirect_openprocess", "backup_userdata_save", "clr_failfast_hijack", "prevent_icmphandle_crashes" };
startInfo.BootDotnetOpenProcessHookMode = 0;
startInfo.BootWaitMessageBox |= args.Contains("--msgbox1") ? 1 : 0;
@ -418,6 +436,7 @@ namespace Dalamud.Injector
Console.WriteLine("Enable VEH:\t[--veh], [--veh-full]");
Console.WriteLine("Show messagebox:\t[--msgbox1], [--msgbox2], [--msgbox3]");
Console.WriteLine("No plugins:\t[--no-plugin] [--no-3rd-plugin]");
Console.WriteLine("Logging:\t[--logname=<logfile suffix>] [--logpath=<log base directory>]");
return 0;
}
@ -520,6 +539,7 @@ namespace Dalamud.Injector
foreach (var process in processes)
Inject(process, AdjustStartInfo(dalamudStartInfo, process.MainModule.FileName), tryFixAcl);
Log.CloseAndFlush();
return 0;
}
@ -808,6 +828,7 @@ namespace Dalamud.Injector
Console.WriteLine($"{{\"pid\": {process.Id}, \"handle\": {processHandleForOwner}}}");
Log.CloseAndFlush();
return 0;
}

View file

@ -28,6 +28,7 @@ public record DalamudStartInfo : IServiceType
{
this.WorkingDirectory = other.WorkingDirectory;
this.ConfigurationPath = other.ConfigurationPath;
this.LogPath = other.LogPath;
this.LogName = other.LogName;
this.PluginDirectory = other.PluginDirectory;
this.AssetDirectory = other.AssetDirectory;
@ -61,6 +62,11 @@ public record DalamudStartInfo : IServiceType
/// </summary>
public string? ConfigurationPath { get; set; }
/// <summary>
/// Gets or sets the path of the log files.
/// </summary>
public string? LogPath { get; set; }
/// <summary>
/// Gets or sets the name of the log file.
/// </summary>

View file

@ -88,13 +88,9 @@ public sealed class EntryPoint
{
var logFileName = logName.IsNullOrEmpty() ? "dalamud" : $"dalamud-{logName}";
#if DEBUG
var logPath = new FileInfo(Path.Combine(baseDirectory, $"{logFileName}.log"));
var oldPath = new FileInfo(Path.Combine(baseDirectory, $"{logFileName}.old.log"));
#else
var logPath = Path.Combine(baseDirectory, "..", "..", "..", $"{logFileName}.log");
var oldPath = Path.Combine(baseDirectory, "..", "..", "..", $"{logFileName}.old.log");
#endif
Log.CloseAndFlush();
RetentionBehaviour behaviour;
@ -137,7 +133,7 @@ public sealed class EntryPoint
private static void RunThread(DalamudStartInfo info, IntPtr mainThreadContinueEvent)
{
// Setup logger
InitLogging(info.WorkingDirectory!, info.BootShowConsole, true, info.LogName);
InitLogging(info.LogPath!, info.BootShowConsole, true, info.LogName);
SerilogEventSink.Instance.LogLine += SerilogOnLogLine;
// Load configuration first to get some early persistent state, like log level
@ -145,7 +141,7 @@ public sealed class EntryPoint
// Set the appropriate logging level from the configuration
if (!configuration.LogSynchronously)
InitLogging(info.WorkingDirectory!, info.BootShowConsole, configuration.LogSynchronously, info.LogName);
InitLogging(info.LogPath!, info.BootShowConsole, configuration.LogSynchronously, info.LogName);
LogLevelSwitch.MinimumLevel = configuration.LogLevel;
// Log any unhandled exception.

View file

@ -642,7 +642,7 @@ internal class DalamudInterface : IDisposable, IServiceType
configuration.QueueSave();
EntryPoint.InitLogging(
startInfo.WorkingDirectory!,
startInfo.LogPath!,
startInfo.BootShowConsole,
configuration.LogSynchronously,
startInfo.LogName);