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 * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
7 * the European Commission - subsequent versions of the EUPL (the "Licence").
8 *
9 * You may not use this work except in compliance with the Licence.
10 *
11 * You may obtain a copy of the Licence at:
12 *
13 * https://joinup.ec.europa.eu/software/page/eupl
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 * Licence for the specific language governing permissions and limitations
19 * under the Licence.
20 */
21
22use std::{borrow::Cow, ops::Range};
23
24/// A container which holds the formatted message and a range of slices
25/// which are suitable for the stderror output or syscons output.
26/// Normally, the `stderr_range` is a message without `PRI`.
27pub struct SyslogFormatted<'f>
28{
29 /// A full (formated message)
30 msg: Vec<Cow<'f, str>>,
31
32 /// A range which is sent to stderr if needed. Usually this is a message without PRI
33 stderr_range: Range<usize>
34}
35
36impl<'f> SyslogFormatted<'f>
37{
38 pub
39 fn new(msg: Vec<Cow<'f, str>>, stderr_range: Range<usize>) -> Self
40 {
41 return Self{ msg, stderr_range };
42 }
43
44 pub(crate)
45 fn get_stderr_output(&self) -> &[Cow<'f, str>]
46 {
47 return &self.msg[self.stderr_range.start..self.stderr_range.end];
48 }
49
50 pub(crate)
51 fn concat(&self) -> String
52 {
53 return self.msg.concat();
54 }
55}
56
57/// A trait for the custom syslog formatter.
58pub trait SyslogFormatter: std::fmt::Debug + Send + Clone + 'static
59{
60 /// Formats the message for the syslog server with specific protocol or
61 /// RFC.
62 ///
63 /// # Arguments
64 ///
65 /// * `tap_type` - [TapType] a type of the connection. Can be used to control the
66 /// message size.
67 ///
68 /// * `pri` - a [Priority] a priority of the message.
69 ///
70 /// * `progname` - a program name or identifier which is set during init.
71 ///
72 /// * `pid` - a PID of the process.
73 ///
74 /// * `fmt` - generated message.
75 ///
76 /// # Returns
77 ///
78 /// A [SyslogFormatted] should be returned.
79 fn vsyslog1_format<'f>(tap_type: TapType, pri: Priority, progname: &'f str, pid: &'f str, fmt: &'f str) -> SyslogFormatted<'f>;
80}
81
82pub mod syslog_3146;
83pub mod syslog_5424;
84pub mod syslog_file;
85
86pub use syslog_3146::FormatRfc3146;
87pub use syslog_5424::FormatRfc5424;
88pub use syslog_file::FormatFile;
89
90use crate::{socket::TapType, Priority};
91
92/// A default syslog formatter. For GNU/Linux it is RFC3146
93#[cfg(target_os = "linux")]
94pub type DefaultSyslogFormatter = FormatRfc3146;
95
96/// A default syslog formatter. For xBSD it is RFC5424
97#[cfg(any(
98 target_os = "freebsd",
99 target_os = "dragonfly",
100 target_os = "openbsd",
101 target_os = "netbsd",
102 target_os = "macos"
103))]
104pub type DefaultSyslogFormatter = FormatRfc5424;
105
106/// A default syslog message formatter for local files.
107pub type DefaultSyslogFormatterFile = FormatFile;