From 3e21a26f6a95ae17aa7907c3ecfd79bdcd959e55 Mon Sep 17 00:00:00 2001 From: Hyper <34012267+hyperbx@users.noreply.github.com> Date: Thu, 12 Dec 2024 22:46:54 +0000 Subject: [PATCH] logger: make thread safe, use std::string_view with std::println --- UnleashedRecomp/main.cpp | 3 +++ UnleashedRecomp/os/logger.cpp | 7 ++++++- UnleashedRecomp/os/logger.h | 3 ++- UnleashedRecomp/os/logger_detail.h | 3 ++- UnleashedRecomp/os/win32/logger_win32.cpp | 15 +++++++++------ 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/UnleashedRecomp/main.cpp b/UnleashedRecomp/main.cpp index b7cad94..2dc8ab3 100644 --- a/UnleashedRecomp/main.cpp +++ b/UnleashedRecomp/main.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #define GAME_XEX_PATH "game:\\default.xex" @@ -144,6 +145,8 @@ uint32_t LdrLoadModule(const char* path) int main(int argc, char *argv[]) { + os::logger::Init(); + bool forceInstaller = false; bool forceDLCInstaller = false; for (uint32_t i = 1; i < argc; i++) diff --git a/UnleashedRecomp/os/logger.cpp b/UnleashedRecomp/os/logger.cpp index 07a1778..42c25ac 100644 --- a/UnleashedRecomp/os/logger.cpp +++ b/UnleashedRecomp/os/logger.cpp @@ -1,7 +1,12 @@ #include #include -void os::logger::Log(const std::string& str, detail::ELogType type, const char* func) +void os::logger::Init() +{ + detail::Init(); +} + +void os::logger::Log(const std::string_view str, detail::ELogType type, const char* func) { detail::Log(str, type, func); } diff --git a/UnleashedRecomp/os/logger.h b/UnleashedRecomp/os/logger.h index 7a05dec..5514ef4 100644 --- a/UnleashedRecomp/os/logger.h +++ b/UnleashedRecomp/os/logger.h @@ -51,5 +51,6 @@ namespace os::logger { - void Log(const std::string& str, detail::ELogType type = detail::ELogType::None, const char* func = nullptr); + void Init(); + void Log(const std::string_view str, detail::ELogType type = detail::ELogType::None, const char* func = nullptr); } diff --git a/UnleashedRecomp/os/logger_detail.h b/UnleashedRecomp/os/logger_detail.h index dde36f5..e0da89b 100644 --- a/UnleashedRecomp/os/logger_detail.h +++ b/UnleashedRecomp/os/logger_detail.h @@ -12,5 +12,6 @@ namespace os::logger::detail Error }; - void Log(const std::string& str, ELogType type = ELogType::None, const char* func = nullptr); + void Init(); + void Log(const std::string_view str, ELogType type = ELogType::None, const char* func = nullptr); } diff --git a/UnleashedRecomp/os/win32/logger_win32.cpp b/UnleashedRecomp/os/win32/logger_win32.cpp index 5bd13f9..927da16 100644 --- a/UnleashedRecomp/os/win32/logger_win32.cpp +++ b/UnleashedRecomp/os/win32/logger_win32.cpp @@ -1,15 +1,18 @@ #include +#include #define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE) #define FOREGROUND_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN) -HANDLE g_hStandardOutput = nullptr; +HANDLE g_hStandardOutput; -void os::logger::detail::Log(const std::string& str, detail::ELogType type, const char* func) +void os::logger::detail::Init() { - if (!g_hStandardOutput) - g_hStandardOutput = GetStdHandle(STD_OUTPUT_HANDLE); + g_hStandardOutput = GetStdHandle(STD_OUTPUT_HANDLE); +} +void os::logger::detail::Log(const std::string_view str, detail::ELogType type, const char* func) +{ switch (type) { case ELogType::Utility: @@ -31,11 +34,11 @@ void os::logger::detail::Log(const std::string& str, detail::ELogType type, cons if (func) { - printf("[%s] %s\n", func, str.c_str()); + std::println("[{}] {}", func, str); } else { - printf("%s\n", str.c_str()); + std::println("{}", str); } SetConsoleTextAttribute(g_hStandardOutput, FOREGROUND_WHITE);