40 auto name = config.find(
"file_name");
41 if(name == config.end())
42 throw std::runtime_error(
"No output file provided to file logger");
43 file_name = name->second;
46 reopen_interval = std::chrono::seconds(300);
47 auto interval = config.find(
"reopen_interval");
48 if(interval != config.end()){
50 reopen_interval = std::chrono::seconds(
51 std::stoul(interval->second)
55 throw std::runtime_error(
56 interval->second +
" is not a valid reopen interval" 70 virtual void log(
const std::string& message,
const log_level level) {
71 if(level < LOG_LEVEL_CUTOFF)
return;
73 output.reserve(message.length() + 64);
76 output.append(message);
77 output.push_back(
'\n');
84 void log(
const std::string& message)
override{
98 auto now = std::chrono::system_clock::now();
100 if(now - last_reopen > reopen_interval) {
102 try{ file.close(); }
catch(...){}
104 file.open(file_name, std::ofstream::out | std::ofstream::app);
105 last_reopen = std::chrono::system_clock::now();
107 catch(std::exception& e) {
108 try{ file.close(); }
catch(...){}
std::ofstream file
Output file stream.
Definition: file_logger.hpp:19
std::chrono::seconds reopen_interval
Reopen interval in seconds.
Definition: file_logger.hpp:23
void log(const std::string &message) override
Definition: file_logger.hpp:84
Class providing the base for any logger.
Definition: logger.hpp:8
file_logger(const logging_config_t &config)
File logger constructor.
Definition: file_logger.hpp:38
void reopen()
Reopen the log file in a thread-safe fashion.
Definition: file_logger.hpp:95
std::chrono::system_clock::time_point last_reopen
Time point when last reopen took place.
Definition: file_logger.hpp:27
std::mutex lock
Mutex to handle concurrent log writes.
Definition: logger.hpp:15
std::string file_name
Name of output file.
Definition: file_logger.hpp:15
virtual void log(const std::string &message, const log_level level)
Definition: file_logger.hpp:70
Class representing a logger capable of writing to files.
Definition: file_logger.hpp:8