-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxlog_journal.cpp
88 lines (67 loc) · 2.35 KB
/
xlog_journal.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "xlog_journal.noexport.h"
#include "xlog.h"
#undef LOG_INFO
#undef LOG_DEBUG
#include <systemd/sd-journal.h>
#include <boost/log/attributes/value_extraction.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
// Keep up-to-date with SYSLOG_SEV_MAPPER in xlog.cpp
// TODO - Find a better way of keeping this all in-sync
int sev2priority(XLog::Severity sev)
{
switch (sev)
{
case XLog::Severity::INFO:
return LOG_DEBUG;
case XLog::Severity::DEBUG:
case XLog::Severity::DEBUG2:
return LOG_INFO;
case XLog::Severity::WARNING:
case XLog::Severity::WARNING2:
return LOG_WARNING;
case XLog::Severity::ERROR:
case XLog::Severity::ERROR2:
return LOG_ERR;
case XLog::Severity::FATAL:
return LOG_CRIT;
case XLog::Severity::INTERNAL:
return LOG_ALERT;
}
return LOG_EMERG;
}
#include <fmt/core.h>
#ifdef XLOG_LOGGING_USE_SOURCE_LOCATION
#define XLOG_GET_FILE fmt::format("CODE_FILE={0}", location.file_name())
#define XLOG_GET_LINE fmt::format("CODE_LINE={0}", location.line())
#define XLOG_GET_FUNC fmt::format("CODE_FUNC={0}", location.function_name())
#define XLOG_USE_STR(var) var.c_str()
#define XLOG_GET_SOURCE_LOCATION auto location = boost::log::extract<std::source_location>("SourceLocation", rec).get();
#else
#define XLOG_GET_FILE ""
#define XLOG_GET_LINE ""
#define XLOG_GET_FUNC ""
#define XLOG_USE_STR(var) ""
#define XLOG_GET_SOURCE_LOCATION
#endif // XLOG_LOGGING_USE_SOURCE_LOCATION
void xlog_journal_backend::consume(const boost::log::record_view& rec)
{
auto sev = boost::log::extract<XLog::Severity>("Severity", rec);
auto channel = boost::log::extract<std::string>("Channel", rec);
auto message = boost::log::extract<std::string>("Message", rec);
XLOG_GET_SOURCE_LOCATION
const std::string _file = XLOG_GET_FILE;
const std::string _line = XLOG_GET_LINE;
const std::string _func = XLOG_GET_FUNC;
auto result = sd_journal_send_with_location(
XLOG_USE_STR(_file),
XLOG_USE_STR(_line),
XLOG_USE_STR(_func),
"MESSAGE=%s", message.get().c_str(),
"CHANNEL=%s", channel.get().c_str(),
"PRIORITY=%i", sev2priority(sev.get()),
NULL);
if(result != 0)
{
// TODO - Do something with this information
}
}