summaryrefslogtreecommitdiff
path: root/src/logger_syslog.cc
blob: 8fa06db2ce77f16f3f521d687bad9cff39cc6f5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "common.hh"

#include "logger_base.hh"

#include <string>
#include <syslog.h>

namespace {

class LoggerSyslog : public LoggerBase {
public:
  explicit LoggerSyslog(std::string const& prgname)
    : ident_(prgname) {
    openlog(ident_.c_str(), LOG_PID, LOG_DAEMON);
  }

  ~LoggerSyslog() override {
    closelog();
  }

protected:
  void msg(Level lvl, std::string_view msg) override {
    int prio;
    switch (lvl) {
    case Level::ERR:
      prio = LOG_ERR;
      break;
    case Level::WARN:
      prio = LOG_WARNING;
      break;
    case Level::INFO:
      prio = LOG_INFO;
      break;
    case Level::DBG:
      prio = LOG_DEBUG;
      break;
    }
    syslog(prio, "%.*s", static_cast<int>(msg.length()), msg.data());
  }

private:
  std::string const ident_;
};

}  // namespace

std::unique_ptr<Logger> Logger::create_syslog(std::string const& prgname) {
  return std::make_unique<LoggerSyslog>(prgname);
}