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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::{io, os::raw::c_int};
use crate::{
formatter::JournaldFormatter,
sink::{helper, Sink},
Error, Level, Record, Result, StdResult, StringBuf,
};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
enum SyslogLevel {
_Emerg = 0,
_Alert = 1,
Crit = 2,
Err = 3,
Warning = 4,
_Notice = 5,
Info = 6,
Debug = 7,
}
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
struct SyslogLevels([SyslogLevel; Level::count()]);
impl SyslogLevels {
#[must_use]
const fn new() -> Self {
Self([
SyslogLevel::Crit, SyslogLevel::Err, SyslogLevel::Warning, SyslogLevel::Info, SyslogLevel::Debug, SyslogLevel::Debug, ])
}
#[must_use]
fn level(&self, level: Level) -> SyslogLevel {
self.0[level as usize]
}
}
impl Default for SyslogLevels {
fn default() -> Self {
Self::new()
}
}
fn journal_send(args: impl Iterator<Item = impl AsRef<str>>) -> StdResult<(), io::Error> {
#[cfg(target_os = "linux")] use libsystemd_sys::{const_iovec, journal as ffi};
let iovecs: Vec<_> = args.map(|a| unsafe { const_iovec::from_str(a) }).collect();
let result = unsafe { ffi::sd_journal_sendv(iovecs.as_ptr(), iovecs.len() as c_int) };
if result == 0 {
Ok(())
} else {
Err(io::Error::from_raw_os_error(result))
}
}
pub struct JournaldSink {
common_impl: helper::CommonImpl,
}
impl JournaldSink {
const SYSLOG_LEVELS: SyslogLevels = SyslogLevels::new();
#[must_use]
pub fn builder() -> JournaldSinkBuilder {
JournaldSinkBuilder {
common_builder_impl: helper::CommonBuilderImpl::new(),
}
}
}
impl Sink for JournaldSink {
fn log(&self, record: &Record) -> Result<()> {
if !self.should_log(record.level()) {
return Ok(());
}
let mut string_buf = StringBuf::new();
self.common_impl
.formatter
.read()
.format(record, &mut string_buf)?;
let kvs = [
format!("MESSAGE={}", string_buf),
format!(
"PRIORITY={}",
JournaldSink::SYSLOG_LEVELS.level(record.level()) as u32
),
];
let srcloc_kvs = match record.source_location() {
Some(srcloc) => [
Some(format!("CODE_FILE={}", srcloc.file_name())),
Some(format!("CODE_LINE={}", srcloc.line())),
],
None => [None, None],
};
journal_send(kvs.iter().chain(srcloc_kvs.iter().flatten())).map_err(Error::WriteRecord)
}
fn flush(&self) -> Result<()> {
Ok(())
}
helper::common_impl!(@Sink: common_impl);
}
pub struct JournaldSinkBuilder {
common_builder_impl: helper::CommonBuilderImpl,
}
impl JournaldSinkBuilder {
helper::common_impl!(@SinkBuilder: common_builder_impl);
pub fn build(self) -> Result<JournaldSink> {
let sink = JournaldSink {
common_impl: helper::CommonImpl::from_builder_with_formatter(
self.common_builder_impl,
|| Box::new(JournaldFormatter::new()),
),
};
Ok(sink)
}
}