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

View file

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

View file

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

View file

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