Win32: Fix deadlock on std::cerr usage

This commit is contained in:
Eladash 2024-04-16 23:29:38 +03:00 committed by Elad Ashkenazi
parent 51e1598e42
commit b55f38290b
3 changed files with 30 additions and 4 deletions

View File

@ -154,7 +154,7 @@ LOG_CHANNEL(q_debug, "QDEBUG");
{
utils::attach_console(utils::console_stream::std_err, true);
std::cerr << fmt::format("RPCS3: %s\n", text);
utils::output_stderr(fmt::format("RPCS3: %s\n", text));
#ifdef __linux__
jit_announce(0, 0, "");
#endif
@ -174,7 +174,7 @@ LOG_CHANNEL(q_debug, "QDEBUG");
}
else
{
std::cerr << fmt::format("RPCS3: %s\n", text);
utils::output_stderr(fmt::format("RPCS3: %s\n", text));
}
static auto show_report = [is_html, include_help_text](std::string_view text)
@ -277,7 +277,7 @@ struct fatal_error_listener final : logs::listener
utils::attach_console(utils::console_stream::std_err, false);
// Output to error stream as is
std::cerr << _msg;
utils::output_stderr(_msg);
#ifdef _WIN32
if (IsDebuggerPresent())
@ -401,7 +401,7 @@ QCoreApplication* create_application(int& argc, char* argv[])
{
const std::string msg = fmt::format("The command line value %s for %s is not allowed. Please use a valid value for Qt::HighDpiScaleFactorRoundingPolicy.", arg_val, arg_rounding);
sys_log.error("%s", msg); // Don't exit with fatal error. The resulting dialog might be unreadable with dpi problems.
std::cerr << msg << std::endl;
utils::output_stderr(msg, true);
}
}
}

View File

@ -5,6 +5,8 @@
#include <stdio.h>
#endif
#include <iostream>
namespace utils
{
void attach_console([[maybe_unused]] int stream, [[maybe_unused]] bool open_console)
@ -34,6 +36,26 @@ namespace utils
{
[[maybe_unused]] const auto con_in = freopen("CONIN$", "r", stdin);
}
#endif
}
void output_stderr(std::string_view str, bool with_endline)
{
if (with_endline)
{
#ifdef _WIN32
std::clog << str;
#else
std::cerr << str;
#endif
str = "\n";
}
#ifdef _WIN32
// Flush seems broken on Windows (deadlocks)
std::clog << str;
#else
std::cerr << str;
#endif
}
}

View File

@ -1,5 +1,7 @@
#pragma once
#include <string_view>
namespace utils
{
enum console_stream
@ -10,4 +12,6 @@ namespace utils
};
void attach_console(int stream, bool open_console);
void output_stderr(std::string_view str, bool with_endline = false);
}