diff --git a/Dalamud.Injector/EntryPoint.cs b/Dalamud.Injector/EntryPoint.cs index c4c553a47..ea0d1fadc 100644 --- a/Dalamud.Injector/EntryPoint.cs +++ b/Dalamud.Injector/EntryPoint.cs @@ -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)) - .MinimumLevel.ControlledBy(levelSwitch) - .CreateLogger(); + .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 { "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=] [--logpath=]"); 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; } diff --git a/Dalamud/DalamudStartInfo.cs b/Dalamud/DalamudStartInfo.cs index 4c8e7566d..63a61c97e 100644 --- a/Dalamud/DalamudStartInfo.cs +++ b/Dalamud/DalamudStartInfo.cs @@ -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 /// public string? ConfigurationPath { get; set; } + /// + /// Gets or sets the path of the log files. + /// + public string? LogPath { get; set; } + /// /// Gets or sets the name of the log file. /// diff --git a/Dalamud/EntryPoint.cs b/Dalamud/EntryPoint.cs index 26ff396d1..19b4f841c 100644 --- a/Dalamud/EntryPoint.cs +++ b/Dalamud/EntryPoint.cs @@ -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. diff --git a/Dalamud/Interface/Internal/DalamudInterface.cs b/Dalamud/Interface/Internal/DalamudInterface.cs index caf38d4d7..202b394c6 100644 --- a/Dalamud/Interface/Internal/DalamudInterface.cs +++ b/Dalamud/Interface/Internal/DalamudInterface.cs @@ -642,7 +642,7 @@ internal class DalamudInterface : IDisposable, IServiceType configuration.QueueSave(); EntryPoint.InitLogging( - startInfo.WorkingDirectory!, + startInfo.LogPath!, startInfo.BootShowConsole, configuration.LogSynchronously, startInfo.LogName);