From e46bb8884b15ca7c22881d30aab902d49363fc02 Mon Sep 17 00:00:00 2001 From: Andreas Maerten <24669514+Yimura@users.noreply.github.com> Date: Thu, 21 Mar 2024 22:22:33 +0100 Subject: [PATCH] fix(Logger): not logging anymore after setup is done (#2858) Regression introduced in #2492 Closes #2773 --- src/logger/logger.cpp | 37 +++++++++++++++++++++++++------------ src/logger/logger.hpp | 3 ++- src/main.cpp | 3 +-- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/logger/logger.cpp b/src/logger/logger.cpp index b30652e9..e478d5be 100644 --- a/src/logger/logger.cpp +++ b/src/logger/logger.cpp @@ -22,7 +22,6 @@ namespace big m_console_logger = &logger::format_console_simple; } - toggle_external_console(attach_console); create_backup(); m_file_out.open(m_file.get_path(), std::ios_base::out | std::ios_base::trunc); @@ -33,6 +32,8 @@ namespace big Logger::AddSink([this](LogMessagePtr msg) { format_file(std::move(msg)); }); + + toggle_external_console(attach_console); } void logger::destroy() @@ -44,6 +45,19 @@ namespace big void logger::toggle_external_console(bool toggle) { + if (m_is_console_open == toggle) + { + return; + } + m_is_console_open = toggle; + + m_console_out.close(); + if (m_did_console_exist) + SetConsoleMode(m_console_handle, m_original_console_mode); + + if (!m_did_console_exist) + FreeConsole(); + if (toggle) { if (m_did_console_exist = ::AttachConsole(GetCurrentProcessId()); !m_did_console_exist) @@ -60,23 +74,12 @@ namespace big // terminal like behaviour enable full color support console_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN; - // prevent clicking in terminal from suspending our main thread - console_mode &= ~(ENABLE_QUICK_EDIT_MODE); SetConsoleMode(m_console_handle, console_mode); } m_console_out.open("CONOUT$", std::ios_base::out | std::ios_base::app); - - return; } - - m_console_out.close(); - if (m_did_console_exist) - SetConsoleMode(m_console_handle, m_original_console_mode); - - if (!m_did_console_exist) - FreeConsole(); } void logger::create_backup() @@ -119,6 +122,11 @@ namespace big void logger::format_console(const LogMessagePtr msg) { + if (!m_is_console_open) + { + return; + } + const auto color = get_color(msg->Level()); const auto timestamp = std::format("{0:%H:%M:%S}", msg->Timestamp()); @@ -133,6 +141,11 @@ namespace big void logger::format_console_simple(const LogMessagePtr msg) { + if (!m_is_console_open) + { + return; + } + const auto color = get_color(msg->Level()); const auto timestamp = std::format("{0:%H:%M:%S}", msg->Timestamp()); diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp index 647ae01b..b1e46a76 100644 --- a/src/logger/logger.hpp +++ b/src/logger/logger.hpp @@ -28,6 +28,7 @@ namespace big private: bool m_attach_console = true; bool m_did_console_exist = false; + bool m_is_console_open = false; void (logger::*m_console_logger)(const LogMessagePtr msg) = &logger::format_console; @@ -59,4 +60,4 @@ namespace big }; inline logger g_log{}; -} \ No newline at end of file +} diff --git a/src/main.cpp b/src/main.cpp index 6873f2ef..3e0203fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -152,9 +152,8 @@ BOOL APIENTRY DllMain(HMODULE hmod, DWORD reason, PVOID) g_file_manager.init(base_dir); g.init(g_file_manager.get_project_file("./settings.json")); - LOG(INFO) << "Settings Loaded."; - g_log.initialize("YimMenu", g_file_manager.get_project_file("./cout.log"), g.debug.external_console); + LOG(INFO) << "Settings Loaded and logger initialized."; LOG(INFO) << "Yim's Menu Initializing"; LOGF(INFO, "Git Info\n\tBranch:\t{}\n\tHash:\t{}\n\tDate:\t{}", version::GIT_BRANCH, version::GIT_SHA1, version::GIT_DATE);