14 #include <fmt/format.h> 22 m_file = fopen(path.c_str(),
"we");
25 throw std::runtime_error(fmt::format(
26 "Could not open log file: {}", strerror(errno)
37 void Logger::log(
const std::string& source,
const std::string& msg)
40 memset(&tv, 0,
sizeof(tv));
41 gettimeofday(&tv,
nullptr);
44 memset(&btime, 0,
sizeof(tv));
45 localtime_r(&tv.tv_sec, &btime);
48 strftime(timeString,
sizeof(timeString),
"%a %F %T", &btime);
50 unsigned int len = msg.length();
51 while(len != 0 && (msg[len-1] ==
'\n' || msg[len-1] ==
'\r'))
54 fmt::print(
m_file,
"{}.{:03d}: {:>20}: ",
55 timeString, tv.tv_usec / 1000,
58 fwrite(msg.c_str(), 1, len,
m_file);
Logger(const std::string &path, bool flush=false)
Constructor.
void log(const std::string &source, const std::string &msg)
Log message.