Skip to content

Commit

Permalink
Write log output both to stderr and logfile at the same time (#7672)
Browse files Browse the repository at this point in the history
* Write log output both to stderr and logfile at the same time

this uses tee sink from boost.iostreams
  • Loading branch information
loonycyborg committed Jun 9, 2023
1 parent 126e05f commit 25afcb3
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "mt_rng.hpp"

#include <boost/algorithm/string.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>

#include <map>
#include <sstream>
Expand Down Expand Up @@ -179,7 +181,11 @@ void set_log_to_file()
if(is_log_dir_writable_.value_or(false)) {
// get the log file stream and assign cerr+cout to it
output_file_path_ = filesystem::get_logs_dir()+"/"+unique_log_filename();
output_file_.reset(filesystem::ostream_file(output_file_path_).release());
static std::unique_ptr<std::ostream> logfile { filesystem::ostream_file(output_file_path_) };
static std::ostream cerr_stream{std::cerr.rdbuf()};
//static std::ostream cout_stream{std::cout.rdbuf()};
auto cerr_tee { boost::iostreams::tee(*logfile, cerr_stream) };
output_file_.reset(new boost::iostreams::stream<decltype(cerr_tee)>{cerr_tee, 4096, 0});
std::cerr.rdbuf(output_file_.get()->rdbuf());
std::cout.rdbuf(output_file_.get()->rdbuf());
rotate_logs(filesystem::get_logs_dir());
Expand Down

0 comments on commit 25afcb3

Please sign in to comment.