2 #include "Mu2e-STMDAQ/utils/async_logger.hh"
5 AsyncLogger::AsyncLogger(
const Config& cfg,
const std::shared_ptr<cpu_utils>& cpu_)
6 : done(false), cfg(cfg), cpu(cpu_)
13 std::string date_time = Timer::run_start_timestamp();
16 std::string log_dir = EnvVars::expand(
"${LOG_DIR}");
18 std::string host = EnvVars::expand(
"${HOSTNAME}");
19 std::string channel =
"LaBr";
20 if(host == cfg.getValue<std::string>(
"stm.ch0_host"))
23 std::string log_file = cfg.getValue<std::string>(
"stm.write_data.logfile") +
"_" +
24 channel +
"_" + date_time +
".log";
26 size_t max_shm_alarms = cfg.getValue<
int>(
"stm.dqm.max_num_alarms");
28 bool make_shm = cfg.getValue<
bool>(
"stm.operations.DQM");
31 LoggerSTM::Instance(LoggerSTM::DEBUG);
32 LoggerSTM::Instance()->setStylePlain();
33 LoggerSTM::Instance()->initSHM(make_shm, max_shm_alarms);
34 LoggerSTM::Instance()->LogToFile(log_dir + log_file);
35 LoggerSTM::Instance()->write(1,
"STM DAQ started: " + date_time);
36 LoggerSTM::Instance()->write(1,
"Loaded configuration file: " + cfg.getXMLpath());
37 LoggerSTM::Instance()->write(1,
"LoggerSTM initialised");
40 printerThread = std::thread(&AsyncLogger::printerThreadFunc,
this);
44 void AsyncLogger::printerThreadFunc()
47 cpu->get_next_core(
"AsyncLogger");
53 std::unique_lock<std::mutex> lock(queueMutex);
54 cv.wait(lock, [
this] {
return !messageQueue.empty() || done; });
57 while(!messageQueue.empty())
60 Message msg = std::move(messageQueue.front());
65 LoggerSTM::Instance()->write(msg.second, msg.first);
72 if(done && messageQueue.empty())
80 void AsyncLogger::log(
const std::string& msg,
unsigned int level)
85 stop::trigger_critical_stop();
88 std::lock_guard<std::mutex> lock(queueMutex);
91 auto now = std::chrono::system_clock::now();
92 std::time_t now_c = std::chrono::system_clock::to_time_t(now);
95 std::ostringstream timeStream;
96 timeStream << std::put_time(std::localtime(&now_c),
"%Y-%m-%d %H:%M:%S");
99 std::string logEntry =
"[" + timeStream.str() +
"] " + msg;
102 messageQueue.emplace(logEntry, level);