57 std::lock_guard<std::mutex> lock(_mtx);
58 if (freopen(file_path.string().c_str(),
"w", stdout) ==
nullptr)
73 std::lock_guard<std::mutex> lock(_mtx);
74 auto redirected_stdout = stdout;
78 HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
79 if (hStdout != INVALID_HANDLE_VALUE)
81 int fdStdout = _open_osfhandle((intptr_t)hStdout, _O_TEXT);
84 FILE* fpStdout = _fdopen(fdStdout,
"w");
88 std::setvbuf(stdout, NULL, _IONBF, 0);
92 CBEAM_LOG(
"cbeam::filesystem::stdout_redirector: Could not set stdout to default (failed to reopen stdout as FILE*)");
97 CBEAM_LOG(
"cbeam::filesystem::stdout_redirector: Could not set stdout to default (failed to open osfhandle as file descriptor)");
102 CBEAM_LOG(
"cbeam::filesystem::stdout_redirector: Could not set stdout to default (invalid standard output handle)");
105 done = freopen(
"/dev/tty",
"w", stdout) !=
nullptr;
109 CBEAM_LOG(
"cbeam::filesystem::stdout_redirector: Could not set stdout to default");
112 if (done && redirected_stdout != stdout)
114 assert(fclose(redirected_stdout) == 0);
117 catch (
const std::system_error& ex)
119 CBEAM_LOG(
"cbeam::filesystem::stdout_redirector: Could not lock the mutex. This indicates a serious (unexpected) bug that must be fixed during development phase.");
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
stdout_redirector(const std::filesystem::path &file_path)
Constructor that redirects stdout to the specified file.
Definition stdout_redirector.hpp:55
virtual ~stdout_redirector() noexcept
Destructor that resets stdout to the system's default state.
Definition stdout_redirector.hpp:69
#define CBEAM_LOG(s)
Logs a message using cbeam::logging::log_manager.
Definition log_manager.hpp:124
Facilitates file I/O, path normalization, and directory operations in a cross-platform manner....
Definition io.hpp:36
Header file to manage inclusion of windows.h with specific settings.