use std::borrow::Cow;
use crate::{CBRACE_SEM, OBRACE, SyslogMsgPriFac, WINDOWS_EVENT_REPORT_MAX_PAYLOAD_LEN, WSPACE, truncate, truncate_n};
use super::{SyslogFormatted, SyslogFormatter};
#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct FormatWindows(Cow<'static, str>);
unsafe impl Send for FormatWindows {}
impl From<String> for FormatWindows
{
fn from(value: String) -> FormatWindows
{
return Self(Cow::Owned(value));
}
}
impl From<&'static str> for FormatWindows
{
fn from(value: &'static str) -> FormatWindows
{
return FormatWindows(Cow::Borrowed(value));
}
}
impl SyslogFormatter for FormatWindows
{
fn vsyslog1_format(&self, _max_msg_size: usize, prifac: SyslogMsgPriFac, progname: &str, pid: Option<&str>) -> SyslogFormatted
{
let msg_payload = truncate_n(&self.0, WINDOWS_EVENT_REPORT_MAX_PAYLOAD_LEN);
let msg_payload_final =
if msg_payload.ends_with("\n") == true
{
truncate(&msg_payload)
}
else
{
&msg_payload
};
let msg_pri =
[
"|", prifac.get_val().to_string().as_str(), "|"
]
.concat();
let msg_pid =
if let Some(p) = pid
{
[OBRACE, p, CBRACE_SEM].concat()
}
else
{
[""].concat()
};
let msg_pkt =
[
WSPACE, progname,
msg_pid.as_str(),
WSPACE, msg_payload_final,
"\x00"
]
.concat();
return
SyslogFormatted
{
msg_header: Some(msg_pri),
msg_payload: msg_pkt,
full_msg: None
};
}
}
#[cfg(test)]
mod test_format_windows
{
use crate::{LogFacility, Priority};
use super::*;
#[test]
fn test_1()
{
let msg = FormatWindows::from("test string!");
let prifac = SyslogMsgPriFac::set_facility(Priority::LOG_ERR, LogFacility::LOG_DAEMON);
let mut res = msg.vsyslog1_format(1024, prifac, "testprog", Some("102"));
assert_eq!(res.msg_header.as_ref().map(|f| f.as_str()), Some("|27|"));
let fullmsg = res.get_full_msg();
println!("{}", fullmsg);
assert_eq!(fullmsg.find("testprog"), Some(5));
assert_eq!(fullmsg.find("[102]"), Some(13));
assert_eq!(fullmsg.find("]:"), Some(17));
assert_eq!(fullmsg.find("test string!"), Some(20));
}
}