salsa  0.3.0
Log.cc
1 #include <Log.hh>
2 
3 namespace Salsa {
4 uint64_t Log::msID = 0;
5 
6 Log::Log() : mName(std::to_string(msID++)) {}
7 
8 Log::~Log() {}
9 
10 int Log::add(std::string where)
11 {
12  if (where == "console" || where == "") {
13  // Console (STD out/err)
14  mSinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_st>());
15  }
16  else if (where.find("file://") == 0) {
17  // Simple file
18  // auto logger = spdlog::basic_logger_mt("mylogger", "log.txt");
19  mSinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(where.substr(7).c_str(), true));
20  }
21  else if (where.find("zmq://") == 0) {
22  // TODO implement ZMQ to SPDLog
23  }
24  else {
25  // throw std::runtime_error("Specified sink not found! sink: [" + where + "]");
26  return 1;
27  }
28  return 0;
29 }
30 
32 {
33  // Sanity check
34  // We could remove this to "reset" the logger, but there may be some risks to this
35  if (mpTarget == nullptr) {
36  // Create shared logger
37  // TODO find a way to drop sink?
38  if (mName == "") {
39  mName = fmt::format("salsa-runlog-{}", msID);
40  }
41  mpTarget = std::make_shared<spdlog::logger>(mName.c_str(), mSinks.begin(), mSinks.end());
42  mpTarget->set_pattern("%v[[--ENDL--]]");
43  // mpTarget->set_formatter(std::make_shared<spdlog::pattern_formatter>(
44  // logFormat, spdlog::pattern_time_type::local, ""));
45  }
46  return 0;
47 }
48 
49 int Log::write(char const * pContent)
50 {
51  // VERY self-explanatory
52  mpTarget->info("{}", pContent);
53  return 0;
54 }
55 } // namespace Salsa
static uint64_t msID
Static Job newName (holds index)
Definition: Log.hh:48
int write(char const *)
Write to logger.
Definition: Log.cc:49
std::string mName
newName (name) of current job
Definition: Log.hh:49
Definition: Actor.cc:2
std::vector< spdlog::sink_ptr > mSinks
Sinks for SPDLOG.
Definition: Log.hh:50
int create()
Create SPDLOG loger.
Definition: Log.cc:31
std::shared_ptr< spdlog::logger > mpTarget
SPDLOG logger handle.
Definition: Log.hh:51
int add(std::string)
Add output sink (file, console, zmq) for SPDLOG.
Definition: Log.cc:10