22 #include <boost/regex.hpp> 32 std::string remove_ascii_format(
const std::string &in)
35 regex.assign(
"\033\\[.+m(.*)\033\\[0m");
36 std::ostringstream t(std::ios::out | std::ios::binary);
37 std::ostream_iterator<char, char> oi(t);
38 boost::regex_replace(oi, in.begin(), in.end(), regex,
"\\1",
39 boost::match_default | boost::format_sed);
47 void log(
const std::string &log_msg,
int ,
const char * ,
50 auto console = spdlog::get(
"console");
51 auto syslog = spdlog::get(
"syslog");
52 if (!console && !syslog)
54 std::cerr <<
"Logger not set-up yet ! Will display log message as is." 56 std::cerr << log_msg << std::endl;
63 std::string log_msg_with_thread_id = ss.str();
69 console->debug(log_msg_with_thread_id);
71 syslog->debug(remove_ascii_format(log_msg_with_thread_id));
75 console->info(log_msg_with_thread_id);
77 syslog->info(remove_ascii_format(log_msg_with_thread_id));
81 console->notice(log_msg_with_thread_id);
83 syslog->notice(remove_ascii_format(log_msg_with_thread_id));
87 console->warn(log_msg_with_thread_id);
89 syslog->warn(remove_ascii_format(log_msg_with_thread_id));
93 console->error(log_msg_with_thread_id);
95 syslog->error(remove_ascii_format(log_msg_with_thread_id));
99 console->critical(log_msg_with_thread_id);
101 syslog->critical(remove_ascii_format(log_msg_with_thread_id));
105 console->alert(log_msg_with_thread_id);
107 syslog->alert(remove_ascii_format(log_msg_with_thread_id));
111 console->emerg(log_msg_with_thread_id);
113 syslog->emerg(remove_ascii_format(log_msg_with_thread_id));
120 if (level ==
"DEBUG")
122 else if (level ==
"INFO")
124 else if (level ==
"NOTICE")
126 else if (level ==
"WARNING")
128 else if (level ==
"ERROR")
130 else if (level ==
"ALERT")
132 else if (level ==
"EMERGENCY")
134 throw std::runtime_error(
"Invalid log level: " + level);
uint64_t gettid()
Return the Linux thread ID.
void log(const std::string &log_msg, int, const char *, const char *, LogLevel level)
LogLevel log_level_from_string(const std::string &level)