syslog_rs/formatters/mod.rs
1/*-
2 * syslog-rs - a syslog client translated from libc to rust
3 *
4 * Copyright 2025 Aleksandr Morozov
5 *
6 * The syslog-rs crate can be redistributed and/or modified
7 * under the terms of either of the following licenses:
8 *
9 * 1. the Mozilla Public License Version 2.0 (the “MPL”) OR
10 *
11 * 2. The MIT License (MIT)
12 *
13 * 3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
14 */
15
16/// A container which holds the formatted message and a range of slices
17/// which are suitable for the stderror output or syscons output.
18/// Normally, the `stderr_range` is a message without `PRI` and other headers
19///
20/// For example for the message type <34> Mon 12 ... the stderr_tange can be 1..
21/// which will exclude the <34>.
22#[derive(Debug)]
23pub struct SyslogFormatted
24{
25 /// A header
26 msg_header: Option<String>,
27
28 /// A msg which is sent to stderr if needed. Usually this is a message without PRI
29 msg_payload: String,
30
31 /// A cache in case if it will be reqired to obtain a full message which consists
32 /// from header and payload.
33 full_msg: Option<String>,
34}
35
36impl SyslogFormatted
37{
38 /// Constructs new container.
39 pub
40 fn new(msg_header: Option<String>, msg_payload: String) -> Self
41 {
42 return Self{ msg_header: msg_header, msg_payload: msg_payload, full_msg: None };
43 }
44
45 /// Returns the payload without header.
46 pub(crate)
47 fn get_stderr_output(&self) -> &str
48 {
49 return &self.msg_payload;
50 }
51
52 /// Returns the concatinated message (headr+payload) or just a payload
53 /// if header does not present.
54 pub(crate)
55 fn get_full_msg(&mut self) -> &str
56 {
57 if self.full_msg.is_some() == true
58 {
59 return self.full_msg.as_ref().unwrap();
60 }
61 else
62 {
63 let header=
64 if self.msg_header.is_some() == true
65 {
66 let msg = [self.msg_header.as_ref().unwrap().as_str(), self.msg_payload.as_str()].concat();
67
68 self.full_msg.replace(msg);
69
70 self.full_msg.as_ref().unwrap().as_str()
71 }
72 else
73 {
74 self.msg_payload.as_ref()
75 };
76
77 return header;
78 }
79 }
80}
81
82/// A trait for the custom syslog formatter.
83pub trait SyslogFormatter: std::fmt::Debug + Send + Clone + From<String> + From<&'static str> + 'static
84{
85 /// Formats the message for the syslog server with specific protocol or
86 /// RFC.
87 ///
88 /// # Arguments
89 ///
90 /// * `max_msg_size` - a max msg size in bytes.
91 ///
92 /// * `prifac` - a [SyslogMsgPriFac] a prepared PRI header value.
93 ///
94 /// * `progname` - a ref to program name or identifier which is set during init.
95 ///
96 /// * `pid` - an [Option] value where inner constains a PID number already converted
97 /// into `string` form. If [Option::None] is provided, a [crate::LogStat::LOG_PID] was
98 /// not set during init. The formatter must exclude the `PID` from the report.
99 ///
100 /// # Returns
101 ///
102 /// A [SyslogFormatted] should be returned.
103 fn vsyslog1_format(&self, max_msg_size: usize, prifac: SyslogMsgPriFac, progname: &str, pid: Option<&str>) -> SyslogFormatted;
104}
105
106/// A windows specific formatter (which is not bounded to any standart).
107/// Used for Windows Event Log and not appliciable to the rest logging.
108#[cfg(target_os = "windows")]
109pub mod windows_10;
110
111/// RFC3146 based formatter.
112pub mod syslog_3146;
113
114/// RFC5424 based formatter.
115pub mod syslog_5424;
116
117/// Formatter for local file (which is not bounded to any standart).
118pub mod syslog_file;
119
120pub use syslog_3146::FormatRfc3146;
121pub use syslog_5424::FormatRfc5424;
122pub use syslog_file::FormatFile;
123
124#[cfg(target_os = "windows")]
125pub use windows_10::FormatWindows;
126
127use crate::{SyslogMsgPriFac};
128
129/// A default syslog formatter. For GNU/Linux it is RFC3146
130#[cfg(target_os = "linux")]
131pub type DefaultSyslogFormatter = FormatRfc3146;
132
133/// A default syslog formatter. For xBSD it is RFC5424
134#[cfg(any(
135 target_os = "freebsd",
136 target_os = "dragonfly",
137 target_os = "openbsd",
138 target_os = "netbsd",
139 target_os = "macos"
140))]
141pub type DefaultSyslogFormatter = FormatRfc5424;
142
143/// If the crate is compiled on the Windows OS, the value is set to
144/// the one which is for the Local Windows Event Log because by default
145/// the default syslog instance is `local`. You must override the value
146/// if working with remote or local syslog.
147#[cfg(target_os = "windows")]
148pub type DefaultSyslogFormatter = FormatWindows;
149
150/// A default syslog message formatter for local files.
151pub type DefaultSyslogFormatterFile = FormatFile;