40 auto name = config.find(
"file_name");
41 if(name == config.end())
42 throw std::runtime_error(
"No output file provided to file logger");
47 auto interval = config.find(
"reopen_interval");
48 if(interval != config.end()){
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();
102 try{
file.close(); }
catch(...){}
104 file.open(
file_name, std::ofstream::out | std::ofstream::app);
107 catch(std::exception& e) {
108 try{
file.close(); }
catch(...){}
Class representing a logger capable of writing to files.
Definition: file_logger.hpp:8
void log(const std::string &message) override
Definition: file_logger.hpp:84
std::chrono::seconds reopen_interval
Reopen interval in seconds.
Definition: file_logger.hpp:23
virtual void log(const std::string &message, const log_level level)
Definition: file_logger.hpp:70
std::chrono::system_clock::time_point last_reopen
Time point when last reopen took place.
Definition: file_logger.hpp:27
std::string file_name
Name of output file.
Definition: file_logger.hpp:15
file_logger(const logging_config_t &config)
File logger constructor.
Definition: file_logger.hpp:38
std::ofstream file
Output file stream.
Definition: file_logger.hpp:19
void reopen()
Reopen the log file in a thread-safe fashion.
Definition: file_logger.hpp:95
Class providing the base for any logger.
Definition: logger.hpp:8
std::mutex lock
Mutex to handle concurrent log writes.
Definition: logger.hpp:15