From 73320d4180b7210226ecba048a80232f5453a00e Mon Sep 17 00:00:00 2001 From: Eladash Date: Fri, 2 Apr 2021 07:52:55 +0300 Subject: [PATCH] Output all fatal log messages to Console/debugger as well --- Utilities/Thread.cpp | 4 ---- rpcs3/main.cpp | 48 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index 9de654f7ab..79cc8baf48 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -1810,7 +1810,6 @@ static void signal_handler(int /*sig*/, siginfo_t* info, void* uct) noexcept if (IsDebuggerPresent()) { sys_log.fatal("\n%s", msg); - std::fprintf(stderr, "%s\n", msg.c_str()); sys_log.notice("\n%s", dump_useful_thread_info()); @@ -2511,12 +2510,9 @@ void thread_base::exec() sig_log.fatal("Thread terminated due to fatal error: %s", reason); - std::fprintf(stderr, "Thread '%s' terminated due to fatal error: %s\n", g_tls_log_prefix().c_str(), std::string(reason).c_str()); - #ifdef _WIN32 if (IsDebuggerPresent()) { - OutputDebugStringA(fmt::format("Thread '%s' terminated due to fatal error: %s\n", g_tls_log_prefix(), reason).c_str()); __debugbreak(); } #else diff --git a/rpcs3/main.cpp b/rpcs3/main.cpp index 5c8617087f..0b20374820 100644 --- a/rpcs3/main.cpp +++ b/rpcs3/main.cpp @@ -95,7 +95,7 @@ LOG_CHANNEL(q_debug, "QDEBUG"); if (AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole()) [[maybe_unused]] const auto con_out = freopen("conout$", "w", stderr); #endif - std::fprintf(stderr, "RPCS3: %.*s\n", static_cast(text.size()), text.data()); + std::cerr << fmt::format("RPCS3: %s\n", text); std::abort(); } @@ -112,7 +112,7 @@ LOG_CHANNEL(q_debug, "QDEBUG"); } else { - std::fprintf(stderr, "RPCS3: %.*s\n", static_cast(text.size()), text.data()); + std::cerr << fmt::format("RPCS3: %s\n", text); } auto show_report = [](std::string_view text) @@ -183,14 +183,46 @@ LOG_CHANNEL(q_debug, "QDEBUG"); std::abort(); } -struct pause_on_fatal final : logs::listener +struct fatal_errors_listener final : logs::listener { - ~pause_on_fatal() override = default; + ~fatal_errors_listener() override = default; - void log(u64 /*stamp*/, const logs::message& msg, const std::string& /*prefix*/, const std::string& /*text*/) override + void log(u64 /*stamp*/, const logs::message& msg, const std::string& prefix, const std::string& text) override { if (msg.sev == logs::level::fatal) { + std::string _msg = "RPCS3: "; + + if (!prefix.empty()) + { + _msg += prefix; + _msg += ": "; + } + + if (msg.ch && '\0' != *msg.ch->name) + { + _msg += msg.ch->name; + _msg += ": "; + } + + _msg += text; + _msg += '\n'; + +#ifdef _WIN32 + // If launched from CMD + if (AttachConsole(ATTACH_PARENT_PROCESS)) + [[maybe_unused]] const auto con_out = freopen("CONOUT$", "w", stderr); +#endif + // Output to error stream as is + std::cerr << _msg; + +#ifdef _WIN32 + if (IsDebuggerPresent()) + { + // Output string to attached debugger + OutputDebugStringA(_msg.c_str()); + } +#endif // Pause emulation if fatal error encountered Emu.Pause(); } @@ -419,7 +451,7 @@ int main(int argc, char** argv) log_file = logs::make_file_listener(fs::get_cache_dir() + "RPCS3.log", stats.avail_free / 4); } - static std::unique_ptr log_pauser = std::make_unique(); + static std::unique_ptr log_pauser = std::make_unique(); logs::listener::add(log_pauser.get()); { @@ -477,14 +509,14 @@ int main(int argc, char** argv) rlim.rlim_max = 4096; #ifdef RLIMIT_NOFILE if (::setrlimit(RLIMIT_NOFILE, &rlim) != 0) - std::fprintf(stderr, "Failed to set max open file limit (4096).\n"); + std::cerr << "Failed to set max open file limit (4096).\n"; #endif rlim.rlim_cur = 0x80000000; rlim.rlim_max = 0x80000000; #ifdef RLIMIT_MEMLOCK if (::setrlimit(RLIMIT_MEMLOCK, &rlim) != 0) - std::fprintf(stderr, "Failed to set RLIMIT_MEMLOCK size to 2 GiB. Try to update your system configuration.\n"); + std::cerr << "Failed to set RLIMIT_MEMLOCK size to 2 GiB. Try to update your system configuration.\n"; #endif // Work around crash on startup on KDE: https://bugs.kde.org/show_bug.cgi?id=401637 setenv( "KDE_DEBUG", "1", 0 );