syslog_rs/formatters/
syslog_file.rs1use std::borrow::Cow;
17
18use chrono::{Local, SecondsFormat};
19
20use crate::{NEXTLINE, NILVALUE, SyslogMsgPriFac, WSPACE, truncate, truncate_n};
21
22use super::{SyslogFormatted, SyslogFormatter};
23
24#[derive(Debug, Clone)]
25#[repr(transparent)]
26pub struct FormatFile(Cow<'static, str>);
27
28unsafe impl Send for FormatFile {}
29
30impl From<String> for FormatFile
31{
32 fn from(value: String) -> FormatFile
33 {
34 return Self(Cow::Owned(value));
35 }
36}
37
38impl From<&'static str> for FormatFile
39{
40 fn from(value: &'static str) -> FormatFile
41 {
42 return Self(Cow::Borrowed(value));
43 }
44}
45
46impl SyslogFormatter for FormatFile
47{
48 fn vsyslog1_format(&self, max_msg_size: usize, prifac: SyslogMsgPriFac, progname: &str, pid: Option<&str>) -> SyslogFormatted
49 {
50 let timedate = Local::now().to_rfc3339_opts(SecondsFormat::Secs, false);
51
52 let msg_payload = truncate_n(&self.0, max_msg_size);
53
54 let msg_payload_final =
55 if msg_payload.ends_with("\n") == true
56 {
57 truncate(msg_payload)
58 }
59 else
60 {
61 msg_payload
62 };
63
64 let msg_pkt =
65 [
66 "[", prifac.get_priority().to_string().as_str(), "]",
67 "[", prifac.get_log_facility().to_string().as_str(), "]",
68 WSPACE, timedate.as_str(),
69 WSPACE, progname,
70 WSPACE, pid.map_or(NILVALUE, |f| f),
71 WSPACE, msg_payload_final, NEXTLINE
72 ]
73 .concat();
74
75 return
76 SyslogFormatted
77 {
78 msg_header: None,
79 msg_payload: msg_pkt,
80 full_msg: None
81 };
82 }
83}
84
85impl FormatFile
86{
87 pub const FORMATTER_NAME: &'static str = "FILE";
88}