salsa  0.4.15
Log.cc
1 #include <unistd.h>
2 #include <fstream>
3 #include "Log.hh"
4 
5 namespace Salsa {
6 uint64_t Log::msID = 0;
7 
8 Log::Log() : mName(std::to_string(msID++)) {}
9 
10 Log::~Log() {}
11 
12 int Log::add(std::string where)
13 {
14  if (where == "console" || where == "") {
15  // Console (STD out/err)
16  mSinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_st>());
17  }
18  else if (where.find("file://") == 0) {
19  // Simple file
20  // auto logger = spdlog::basic_logger_mt("mylogger", "log.txt");
21  // where.substr(7).c_str()
22  std::string p = where.substr(7);
23  SPD_TRACE("Testing file [{}] for write ...", p);
24  std::ofstream output(p.c_str());
25  if (output.is_open()) {
26  output.close();
27  SPD_TRACE("Testing file [{}] for write is OK ...", p);
28  mSinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(p.c_str(), true));
29  }
30  else {
31  SPD_WARN("Problem creating log file [{}]!!!", p);
32  output.close();
33  }
34  }
35  else if (where.find("zmq://") == 0) {
36  // TODO implement ZMQ to SPDLog
37  }
38  else {
39  // throw std::runtime_error("Specified sink not found! sink: [" + where + "]");
40  return 1;
41  }
42  return 0;
43 }
44 
45 int Log::create()
46 {
47  // Sanity check
48  // We could remove this to "reset" the logger, but there may be some risks to this
49  if (mpTarget == nullptr) {
50  // Create shared logger
51  // TODO find a way to drop sink?
52  if (mName == "") {
53  mName = fmt::format("salsa-runlog-{}", msID);
54  }
55  mpTarget = std::make_shared<spdlog::logger>(mName.c_str(), mSinks.begin(), mSinks.end());
56  // mpTarget->set_pattern("%v[[--ENDL--]]");
57  mpTarget->set_pattern("%v");
58  // mpTarget->set_formatter(std::make_shared<spdlog::pattern_formatter>(
59  // logFormat, spdlog::pattern_time_type::local, ""));
60  }
61  return 0;
62 }
63 
64 int Log::write(char const * pContent)
65 {
66  // VERY self-explanatory
67  mpTarget->info("{}", pContent);
68  return 0;
69 }
70 } // namespace Salsa
static uint64_t msID
Static Job newName (holds index)
Definition: Log.hh:50