syslog_rs/formatters/
syslog_3146.rs1use std::borrow::Cow;
17
18use chrono::Local;
19
20use crate::{truncate, truncate_n, Priority, CBRACE_SEM, OBRACE, RFC3164_MAX_PAYLOAD_LEN, WSPACE};
21
22use super::{SyslogFormatted, SyslogFormatter};
23
24
25
26#[derive(Debug, Clone)]
27#[repr(transparent)]
28pub struct FormatRfc3146(Cow<'static, str>);
29
30unsafe impl Send for FormatRfc3146 {}
31
32impl From<String> for FormatRfc3146
33{
34 fn from(value: String) -> FormatRfc3146
35 {
36 return Self(Cow::Owned(value));
37 }
38}
39
40impl From<&'static str> for FormatRfc3146
41{
42 fn from(value: &'static str) -> FormatRfc3146
43 {
44 return FormatRfc3146(Cow::Borrowed(value));
45 }
46}
47
48
49
50impl SyslogFormatter for FormatRfc3146
51{
52 fn vsyslog1_format(&self, _max_msg_size: usize, pri: Priority, progname: &str, pid: &str) -> SyslogFormatted
53 {
54 let timedate = Local::now().format("%h %e %T").to_string();
56
57 let msg_payload = truncate_n(&self.0, RFC3164_MAX_PAYLOAD_LEN);
58
59 let msg_payload_final =
60 if msg_payload.ends_with("\n") == true
61 {
62 truncate(&msg_payload)
63 }
64 else
65 {
66 &msg_payload
67 };
68
69 let msg_pri =
71 [
72 "<", pri.bits().to_string().as_str(), ">"
73 ]
74 .concat();
75
76 let msg_pkt =
77 [
78 timedate.as_str(),
82 WSPACE, progname,
86 OBRACE, pid, CBRACE_SEM,
88 WSPACE, msg_payload_final
90 ]
91 .concat();
92
93
94 return
95 SyslogFormatted
96 {
97 msg_header: Some(msg_pri),
98 msg_payload: msg_pkt,
99 full_msg: None
100 };
101 }
102}
103