CppLogging  1.0.4.0
C++ Logging Library
syslog_appender.cpp
Go to the documentation of this file.
1 
10 
11 #if defined(unix) || defined(__unix) || defined(__unix__)
12 #include <syslog.h>
13 #endif
14 
15 namespace CppLogging {
16 
18 {
19 #if defined(unix) || defined(__unix) || defined(__unix__)
20  openlog(nullptr, LOG_NDELAY | LOG_PID, LOG_USER);
21 #endif
22 }
23 
25 {
26 #if defined(unix) || defined(__unix) || defined(__unix__)
27  closelog();
28 #endif
29 }
30 
32 {
33  // Skip logging records without layout
34  if (record.raw.empty())
35  return;
36 
37 #if defined(unix) || defined(__unix) || defined(__unix__)
38  // Setup syslog priority depends on the logging level
39  int priority;
40  switch (record.level)
41  {
42  case Level::FATAL:
43  priority = LOG_CRIT;
44  break;
45  case Level::ERROR:
46  priority = LOG_ERR;
47  break;
48  case Level::WARN:
49  priority = LOG_WARNING;
50  break;
51  case Level::INFO:
52  priority = LOG_INFO;
53  break;
54  case Level::DEBUG:
55  priority = LOG_DEBUG;
56  break;
57  default:
58  priority = LOG_INFO;
59  break;
60  }
61 
62  // Append logging record content
63  syslog(priority, "%.*s", (int)record.raw.size() - 1, (char*)record.raw.data());
64 #endif
65 }
66 
67 } // namespace CppLogging
Logging record.
Definition: record.h:37
Level level
Level of the logging record.
Definition: record.h:44
std::vector< uint8_t > raw
Record content after layout.
Definition: record.h:53
void AppendRecord(Record &record) override
Append the given logging record.
C++ Logging project definitions.
Definition: appender.h:15
@ FATAL
Log fatal errors.
@ WARN
Log warnings.
@ INFO
Log information.
@ ERROR
Log errors.
@ DEBUG
Log debug.
Syslog appender definition.