libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
tee_stream.cpp
Go to the documentation of this file.
1/**
2 * @file tee_stream.cpp
3 * @brief Implementation of mx::TeeBuf, mx::TeeStream, and the redirect() helper.
4 */
5#include"tee_stream.hpp"
6#include<filesystem>
7
8namespace mx {
9 static std::ofstream log_file("system.log.txt", std::ios::out);
10 static std::ofstream error_file("error.log.txt", std::ios::out);
11 TeeStream system_out(std::cout, log_file);
13
14 void redirect() {
15 std::filesystem::path tempDir = std::filesystem::temp_directory_path();
16 if (!log_file.is_open()) {
17 std::filesystem::path logFilePath = tempDir / "system.log.txt";
18 log_file.open(logFilePath, std::ios::out);
19 if(!log_file.is_open()) {
20 std::cerr << "Error";
21 }
22 system_out << "Redirected to: " << logFilePath << "\n";
23 }
24 if (!error_file.is_open()) {
25 std::filesystem::path logFilePath = tempDir / "error.log.txt";
26 error_file.open(logFilePath, std::ios::out);
27 if(!log_file.is_open()) {
28 std::cerr << "Error";
29 }
30 system_out << "Redirected to: " << logFilePath << "\n";
31 }
32
33 std::cout.rdbuf(system_out.rdbuf());
34 std::cerr.rdbuf(system_err.rdbuf());
35 }
36}
An std::ostream that writes to two output streams simultaneously.
Definition cfg.cpp:11
static std::ofstream log_file("system.log.txt", std::ios::out)
void redirect()
Redirect std::cout and std::cerr through the tee streams.
TeeStream system_out(std::cout, log_file)
Global stdout + log tee stream.
static std::ofstream error_file("error.log.txt", std::ios::out)
TeeStream system_err(std::cerr, error_file)
Global stderr + log tee stream.
Tee stream utilities that duplicate output to two sinks simultaneously.