1
0
mirror of synced 2024-11-28 01:20:51 +01:00

sys: Prevent libraries from printing garbage to the console through stderr

This commit is contained in:
WerWolv 2023-03-18 11:35:01 +01:00
parent 367bd76046
commit 7c1e33dde6
3 changed files with 19 additions and 3 deletions

View File

@ -10,6 +10,7 @@
#include <hex/helpers/logger.hpp> #include <hex/helpers/logger.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <cstdio>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
@ -24,6 +25,11 @@ namespace hex {
setenv("LD_LIBRARY_PATH", hex::format("{};{}", hex::getEnvironmentVariable("LD_LIBRARY_PATH").value_or(""), path.string().c_str()).c_str(), true); setenv("LD_LIBRARY_PATH", hex::format("{};{}", hex::getEnvironmentVariable("LD_LIBRARY_PATH").value_or(""), path.string().c_str()).c_str(), true);
} }
// Various libraries sadly directly print to stderr with no way to disable it
// We redirect stderr to /dev/null to prevent this
freopen("/dev/null", "w", stderr);
setvbuf(stderr, nullptr, _IONBF, 0);
// Redirect stdout to log file if we're not running in a terminal // Redirect stdout to log file if we're not running in a terminal
if (!isatty(STDOUT_FILENO)) { if (!isatty(STDOUT_FILENO)) {
log::redirectToFile(); log::redirectToFile();

View File

@ -10,6 +10,7 @@
#include <hex/helpers/logger.hpp> #include <hex/helpers/logger.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <cstdio>
#include <unistd.h> #include <unistd.h>
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
@ -23,6 +24,11 @@ namespace hex {
setenv("LD_LIBRARY_PATH", hex::format("{};{}", hex::getEnvironmentVariable("LD_LIBRARY_PATH").value_or(""), path.string().c_str()).c_str(), true); setenv("LD_LIBRARY_PATH", hex::format("{};{}", hex::getEnvironmentVariable("LD_LIBRARY_PATH").value_or(""), path.string().c_str()).c_str(), true);
} }
// Various libraries sadly directly print to stderr with no way to disable it
// We redirect stderr to /dev/null to prevent this
freopen("/dev/null", "w", stderr);
setvbuf(stderr, nullptr, _IONBF, 0);
// Redirect stdout to log file if we're not running in a terminal // Redirect stdout to log file if we're not running in a terminal
if (!isatty(STDOUT_FILENO)) { if (!isatty(STDOUT_FILENO)) {
log::redirectToFile(); log::redirectToFile();

View File

@ -24,6 +24,7 @@
#include <wrl/client.h> #include <wrl/client.h>
#include <csignal> #include <csignal>
#include <cstdio>
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
@ -203,15 +204,18 @@ namespace hex {
AddDllDirectory(path.c_str()); AddDllDirectory(path.c_str());
} }
// Various libraries sadly directly print to stderr with no way to disable it
// We redirect stderr to NUL to prevent this
freopen("NUL:", "w", stderr);
setvbuf(stderr, nullptr, _IONBF, 0);
// Attach to parent console if one exists // Attach to parent console if one exists
if (AttachConsole(ATTACH_PARENT_PROCESS)) { if (AttachConsole(ATTACH_PARENT_PROCESS)) {
// Redirect cin, cout and cerr to that console // Redirect stdin and stdout to that new console
freopen("CONIN$", "r", stdin); freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
setvbuf(stdin, nullptr, _IONBF, 0); setvbuf(stdin, nullptr, _IONBF, 0);
setvbuf(stdout, nullptr, _IONBF, 0); setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);
fmt::print("\n"); fmt::print("\n");