fix(Logger): not logging anymore after setup is done (#2858)

Regression introduced in #2492
Closes #2773
This commit is contained in:
Andreas Maerten 2024-03-21 22:22:33 +01:00 committed by GitHub
parent 95259faf59
commit 5f553b3430
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 15 deletions

View File

@ -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());

View File

@ -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{};
}
}

View File

@ -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);