diff --git a/thprac/src/thprac/thprac_log.cpp b/thprac/src/thprac/thprac_log.cpp new file mode 100644 index 0000000..0a2dda6 --- /dev/null +++ b/thprac/src/thprac/thprac_log.cpp @@ -0,0 +1,74 @@ +#include +#include +#include "thprac_log.h" +#include "thprac_launcher_cfg.h" + +namespace THPrac { + +HANDLE hLog = INVALID_HANDLE_VALUE; +bool console_open = false; + +void log_print(const char* msg, size_t len) +{ + DWORD byteRet; + if (console_open) { + WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msg, len, &byteRet, nullptr); + } + if (hLog != INVALID_HANDLE_VALUE) { + WriteFile(hLog, msg, len, &byteRet, nullptr); + } +} + +void log_init(bool launcher, bool console) +{ + // The lengths I'll go to not allocate on the heap + + constexpr unsigned int rot_max = 9; + constexpr unsigned int scratch_size = 32; + wchar_t fn_rot_temp_1[scratch_size] = {}; + wchar_t fn_rot_temp_2[scratch_size] = {}; + + const wchar_t* const fn_launcher = L"thprac_launcher_log.txt"; + const wchar_t* const fn_ingame = L"thprac_log.txt"; + + const wchar_t* const fn_rot_launcher = L"thprac_launcher_log.9.txt"; + const wchar_t* const fn_rot_ingame = L"thprac_log.9.txt"; + + if (launcher) { + memcpy(fn_rot_temp_1, fn_rot_launcher, wcslen(fn_rot_launcher) * sizeof(wchar_t)); + } else { + memcpy(fn_rot_temp_1, fn_rot_ingame, wcslen(fn_rot_ingame) * sizeof(wchar_t)); + } + + const unsigned int rot_num_off = launcher ? 20 : 11; + const wchar_t* const fn = launcher ? fn_launcher : fn_ingame; + + memcpy(fn_rot_temp_2, fn_rot_temp_1, scratch_size * sizeof(wchar_t)); + + DeleteFileW(fn_rot_temp_1); + + for (size_t i = 0; i < rot_max - 1; i++) { + fn_rot_temp_2[rot_num_off]--; + MoveFileW(fn_rot_temp_2, fn_rot_temp_1); + fn_rot_temp_1[rot_num_off]--; + } + + MoveFileW(fn, fn_rot_temp_1); + hLog = CreateFileW(fn, GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + + // Attach to existing console if it exists, open new console if no console exists but console is set to true + // I wonder if this is gonna make command prompt appear for a millisecond for some people + BOOL console_success = AllocConsole(); + + if (console_success && !console) { + FreeConsole(); + } else if (!console_success) { + AttachConsole(GetCurrentProcessId()); + console_open = true; + } else { + console_open = true; + } + + log_print("THPrac: Logging initialized\r\n"); +} +} \ No newline at end of file diff --git a/thprac/src/thprac/thprac_log.h b/thprac/src/thprac/thprac_log.h new file mode 100644 index 0000000..2f9c1fd --- /dev/null +++ b/thprac/src/thprac/thprac_log.h @@ -0,0 +1,22 @@ +#pragma once +#include +#include + + +namespace THPrac { + +void log_print(const char* const msg, size_t len); + +inline void log_print(const char* const null_terminated) { + return log_print(null_terminated, strlen(null_terminated)); +} + +template +inline void log_printf(const std::format_string fmt, Args&&... args) { + auto str = std::vformat(fmt.get(), std::make_format_args(args...)); + return log_print(str.data(), str.length()); +} + +void log_init(bool launcher, bool console); + +} \ No newline at end of file diff --git a/thprac/thprac.vcxproj b/thprac/thprac.vcxproj index 32ba3cd..614e0df 100644 --- a/thprac/thprac.vcxproj +++ b/thprac/thprac.vcxproj @@ -74,6 +74,7 @@ + @@ -142,6 +143,7 @@ + diff --git a/thprac/thprac.vcxproj.filters b/thprac/thprac.vcxproj.filters index 2dd8008..ab47615 100644 --- a/thprac/thprac.vcxproj.filters +++ b/thprac/thprac.vcxproj.filters @@ -38,6 +38,9 @@ {653ef50e-68b6-4a06-8e84-3ddf50867dfe} + + {89b3b162-0590-4673-a372-b4699cc95cb7} + @@ -205,6 +208,9 @@ THPrac Games + + THPrac Logging + @@ -405,6 +411,9 @@ THPrac Games + + THPrac Logging +