Write log output both to stderr and logfile at the same time (#7672)

* Write log output both to stderr and logfile at the same time

this uses tee sink from boost.iostreams
This commit is contained in:
Sergey Popov 2023-06-09 10:03:51 +03:00 committed by GitHub
parent 126e05f7f5
commit 25afcb3979
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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>
@ -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());