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