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 full_msg: Option<String>,
32}
33
34impl SyslogFormatted
35{
36 pub
37 fn new(msg_header: Option<String>, msg_payload: String) -> Self
38 {
39 return Self{ msg_header: msg_header, msg_payload: msg_payload, full_msg: None };
40 }
41
42 pub(crate)
43 fn get_stderr_output(&self) -> &str
44 {
45 return &self.msg_payload;
46 }
47
48 pub(crate)
49 fn get_full_msg(&mut self) -> &str
50 {
51 if self.full_msg.is_some() == true
52 {
53 return self.full_msg.as_ref().unwrap();
54 }
55 else
56 {
57 let header=
58 if self.msg_header.is_some() == true
59 {
60 let msg = [self.msg_header.as_ref().unwrap().as_str(), self.msg_payload.as_str()].concat();
61
62 self.full_msg.replace(msg);
63
64 self.full_msg.as_ref().unwrap().as_str()
65 }
66 else
67 {
68 self.msg_payload.as_ref()
69 };
70
71 return header;
72 }
73 }
74}
75
76/// A trait for the custom syslog formatter.
77pub trait SyslogFormatter: std::fmt::Debug + Send + Clone + From<String> + From<&'static str> + 'static
78{
79 /// Formats the message for the syslog server with specific protocol or
80 /// RFC.
81 ///
82 /// # Arguments
83 ///
84 /// * `tap_type` - [TapType] a type of the connection.
85 ///
86 /// * `max_msg_size` - a max msg size in bytes.
87 ///
88 /// * `pri` - a [Priority] a priority of the message.
89 ///
90 /// * `progname` - a ref to program name or identifier which is set during init.
91 ///
92 /// * `pid` - a ref to PID of the process.
93 ///
94 /// * `fmt` - generated message.
95 ///
96 /// # Returns
97 ///
98 /// A [SyslogFormatted] should be returned.
99 fn vsyslog1_format(&self, max_msg_size: usize, pri: Priority, progname: &str, pid: &str) -> SyslogFormatted;
100}
101
102/// RFC3146
103pub mod syslog_3146;
104
105/// RFC5424
106pub mod syslog_5424;
107
108/// Formatter for local file.
109pub mod syslog_file;
110
111pub use syslog_3146::FormatRfc3146;
112pub use syslog_5424::FormatRfc5424;
113pub use syslog_file::FormatFile;
114
115use crate::{Priority};
116
117/// A default syslog formatter. For GNU/Linux it is RFC3146
118#[cfg(target_os = "linux")]
119pub type DefaultSyslogFormatter = FormatRfc3146;
120
121/// A default syslog formatter. For xBSD it is RFC5424
122#[cfg(any(
123 target_os = "freebsd",
124 target_os = "dragonfly",
125 target_os = "openbsd",
126 target_os = "netbsd",
127 target_os = "macos"
128))]
129pub type DefaultSyslogFormatter = FormatRfc5424;
130
131/// A default syslog message formatter for local files.
132pub type DefaultSyslogFormatterFile = FormatFile;