Skip to content

Commit

Permalink
Output all fatal log messages to Console/debugger as well
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 authored and Megamouse committed Apr 3, 2021
1 parent a1d31f6 commit 73320d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
4 changes: 0 additions & 4 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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
Expand Down
48 changes: 40 additions & 8 deletions rpcs3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(text.size()), text.data());
std::cerr << fmt::format("RPCS3: %s\n", text);
std::abort();
}

Expand All @@ -112,7 +112,7 @@ LOG_CHANNEL(q_debug, "QDEBUG");
}
else
{
std::fprintf(stderr, "RPCS3: %.*s\n", static_cast<int>(text.size()), text.data());
std::cerr << fmt::format("RPCS3: %s\n", text);
}

auto show_report = [](std::string_view text)
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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<logs::listener> log_pauser = std::make_unique<pause_on_fatal>();
static std::unique_ptr<logs::listener> log_pauser = std::make_unique<fatal_errors_listener>();
logs::listener::add(log_pauser.get());

{
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 73320d4

Please sign in to comment.