diff --git a/Dalamud.Boot/dllmain.cpp b/Dalamud.Boot/dllmain.cpp index 64e2b41be..243779926 100644 --- a/Dalamud.Boot/dllmain.cpp +++ b/Dalamud.Boot/dllmain.cpp @@ -9,8 +9,17 @@ HMODULE g_hModule; HINSTANCE g_hGameInstance = GetModuleHandleW(nullptr); DllExport DWORD WINAPI Initialize(LPVOID lpParam, HANDLE hMainThreadContinue) { + logging::log_file.open(utils::get_env("DALAMUD_BOOT_LOGFILE"), std::ios_base::out | std::ios_base::app); + if (bootconfig::is_show_console()) ConsoleSetup(L"Dalamud Boot"); + + if (!logging::log_file) { + logging::print("Couldn't open log file!"); + } + + logging::print("Dalamud.Boot Injectable, (c) 2021 XIVLauncher Contributors"); + logging::print("Built at: " __DATE__ "@" __TIME__); if (bootconfig::is_wait_messagebox()) MessageBoxW(nullptr, L"Press OK to continue", L"Dalamud Boot", MB_OK); @@ -22,9 +31,6 @@ DllExport DWORD WINAPI Initialize(LPVOID lpParam, HANDLE hMainThreadContinue) { logging::print("Error: {}", e.what()); } - logging::print("Dalamud.Boot Injectable, (c) 2021 XIVLauncher Contributors"); - logging::print("Built at : " __DATE__ "@" __TIME__); - if (bootconfig::is_wait_debugger()) { logging::print("Waiting for debugger to attach..."); while (!IsDebuggerPresent()) @@ -87,6 +93,7 @@ BOOL APIENTRY DllMain(const HMODULE hModule, const DWORD dwReason, LPVOID lpRese case DLL_PROCESS_DETACH: xivfixes::apply_all(false); veh::remove_handler(); + //logging::log_file.close(); break; } return TRUE; diff --git a/Dalamud.Boot/logging.cpp b/Dalamud.Boot/logging.cpp index 925fcdb18..7d977eaa3 100644 --- a/Dalamud.Boot/logging.cpp +++ b/Dalamud.Boot/logging.cpp @@ -32,4 +32,10 @@ void logging::print(Level level, const char* s) { DWORD wr; WriteFile(GetStdHandle(STD_ERROR_HANDLE), &estr[0], static_cast(estr.size()), &wr, nullptr); + + if (log_file.is_open()) + { + log_file << estr; + log_file.flush(); + } } diff --git a/Dalamud.Boot/logging.h b/Dalamud.Boot/logging.h index a9b88d605..3b2204ee8 100644 --- a/Dalamud.Boot/logging.h +++ b/Dalamud.Boot/logging.h @@ -3,10 +3,13 @@ #include #include #include +#include #include "unicode.h" namespace logging { + inline std::ofstream log_file; + enum Level : int { Verbose = 0, V = 0, diff --git a/Dalamud.Injector/EntryPoint.cs b/Dalamud.Injector/EntryPoint.cs index 411509110..62cc2580c 100644 --- a/Dalamud.Injector/EntryPoint.cs +++ b/Dalamud.Injector/EntryPoint.cs @@ -101,6 +101,19 @@ namespace Dalamud.Injector } } + private static string GetLogPath(string filename) + { + var baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + +#if DEBUG + var logPath = Path.Combine(baseDirectory, $"{filename}.log"); +#else + var logPath = Path.Combine(baseDirectory, "..", "..", "..", "dalamud.injector.log"); +#endif + + return logPath; + } + private static void Init(List args) { InitUnhandledException(args); @@ -161,14 +174,6 @@ namespace Dalamud.Injector private static void InitLogging() { - var baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - -#if DEBUG - var logPath = Path.Combine(baseDirectory, "dalamud.injector.log"); -#else - var logPath = Path.Combine(baseDirectory, "..", "..", "..", "dalamud.injector.log"); -#endif - var levelSwitch = new LoggingLevelSwitch(); #if DEBUG @@ -177,6 +182,10 @@ namespace Dalamud.Injector levelSwitch.MinimumLevel = LogEventLevel.Information; #endif + var logPath = GetLogPath("dalamud.injector"); + + Environment.SetEnvironmentVariable("DALAMUD_BOOT_LOGFILE", GetLogPath("dalamud.boot")); + CullLogFile(logPath, 1 * 1024 * 1024); Log.Logger = new LoggerConfiguration() diff --git a/lib/CoreCLR/boot.cpp b/lib/CoreCLR/boot.cpp index 74e488b2a..ebb81a0ac 100644 --- a/lib/CoreCLR/boot.cpp +++ b/lib/CoreCLR/boot.cpp @@ -73,9 +73,9 @@ int InitializeClrAndGetEntryPoint( // =========================================================================== // - logging::print(L"with dotnet_path: %s", dotnet_path); - logging::print(L"with config_path: %s", runtimeconfig_path.c_str()); - logging::print(L"with module_path: %s", module_path.c_str()); + logging::print(L"with dotnet_path: {}", dotnet_path); + logging::print(L"with config_path: {}", runtimeconfig_path.c_str()); + logging::print(L"with module_path: {}", module_path.c_str()); if (!std::filesystem::exists(dotnet_path)) {