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. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
12 */
13
14/// A container which holds the formatted message and a range of slices
15/// which are suitable for the stderror output or syscons output.
16/// Normally, the `stderr_range` is a message without `PRI` and other headers
17///
18/// For example for the message type <34> Mon 12 ... the stderr_tange can be 1..
19/// which will exclude the <34>.
20#[derive(Debug)]
21pub struct SyslogFormatted
22{
23 /// A header
24 msg_header: Option<String>,
25
26 /// A msg which is sent to stderr if needed. Usually this is a message without PRI
27 msg_payload: String,
28
29 full_msg: Option<String>,
30}
31
32impl SyslogFormatted
33{
34 pub
35 fn new(msg_header: Option<String>, msg_payload: String) -> Self
36 {
37 return Self{ msg_header: msg_header, msg_payload: msg_payload, full_msg: None };
38 }
39
40 pub(crate)
41 fn get_stderr_output(&self) -> &str
42 {
43 return &self.msg_payload;
44 }
45
46 pub(crate)
47 fn get_full_msg(&mut self) -> &str
48 {
49 if self.full_msg.is_some() == true
50 {
51 return self.full_msg.as_ref().unwrap();
52 }
53 else
54 {
55 let header=
56 if self.msg_header.is_some() == true
57 {
58 let msg = [self.msg_header.as_ref().unwrap().as_str(), self.msg_payload.as_str()].concat();
59
60 self.full_msg.replace(msg);
61
62 self.full_msg.as_ref().unwrap().as_str()
63 }
64 else
65 {
66 self.msg_payload.as_ref()
67 };
68
69 return header;
70 }
71 }
72}
73
74/// A trait for the custom syslog formatter.
75pub trait SyslogFormatter: std::fmt::Debug + Send + Clone + From<String> + From<&'static str> + 'static
76{
77 /// Formats the message for the syslog server with specific protocol or
78 /// RFC.
79 ///
80 /// # Arguments
81 ///
82 /// * `tap_type` - [TapType] a type of the connection.
83 ///
84 /// * `max_msg_size` - a max msg size in bytes.
85 ///
86 /// * `pri` - a [Priority] a priority of the message.
87 ///
88 /// * `progname` - a ref to program name or identifier which is set during init.
89 ///
90 /// * `pid` - a ref to PID of the process.
91 ///
92 /// * `fmt` - generated message.
93 ///
94 /// # Returns
95 ///
96 /// A [SyslogFormatted] should be returned.
97 fn vsyslog1_format(&self, max_msg_size: usize, pri: Priority, progname: &str, pid: &str) -> SyslogFormatted;
98}
99
100/// RFC3146
101pub mod syslog_3146;
102
103/// RFC5424
104pub mod syslog_5424;
105
106/// Formatter for local file.
107pub mod syslog_file;
108
109pub use syslog_3146::FormatRfc3146;
110pub use syslog_5424::FormatRfc5424;
111pub use syslog_file::FormatFile;
112
113use crate::{Priority};
114
115/// A default syslog formatter. For GNU/Linux it is RFC3146
116#[cfg(target_os = "linux")]
117pub type DefaultSyslogFormatter = FormatRfc3146;
118
119/// A default syslog formatter. For xBSD it is RFC5424
120#[cfg(any(
121 target_os = "freebsd",
122 target_os = "dragonfly",
123 target_os = "openbsd",
124 target_os = "netbsd",
125 target_os = "macos"
126))]
127pub type DefaultSyslogFormatter = FormatRfc5424;
128
129/// A default syslog message formatter for local files.
130pub type DefaultSyslogFormatterFile = FormatFile;