1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::error::Error;
use std::fs::File;
use std::path::PathBuf;
use serde::{Serialize, Deserialize};
use std::fmt::{Debug, Write};
use std::fs;
use std::io::Write as IoWrite;
use chrono::{Local, SecondsFormat, TimeZone};
use crate::message::Message;
use super::MessageDestination;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct FileDestination {
path: PathBuf,
}
impl MessageDestination for FileDestination {
fn send(&self, message: &Message) -> Result<(), Box<dyn Error>> {
if let Some(parent) = self.path.parent() {
if !parent.exists() {
fs::create_dir_all(parent)?;
}
}
let s = self.format_message(message);
let mut file = File::options()
.create(true)
.append(true)
.open(&self.path)?;
writeln!(&mut file, "{}", s)?;
Ok(())
}
}
impl FileDestination {
pub fn new(path: PathBuf) -> Self {
Self {
path
}
}
fn format_message(&self, message: &Message) -> String {
let mut s = String::new();
let timestamp = Local::timestamp_millis(&Local, message.get_unix_timestamp_millis());
write!(s, "{} - {:?}: ", timestamp.to_rfc3339_opts(SecondsFormat::Millis, true), message.get_level()).unwrap();
if message.get_component().is_some() {
write!(s, "[{}] ", message.get_component().as_ref().unwrap()).unwrap();
}
if message.get_title().is_some() {
write!(s, "{} - ", message.get_title().as_ref().unwrap()).unwrap();
}
write!(s, "'{}'", inline(message.get_message_detail().raw())).unwrap();
write!(s, " @ {}", message.get_author()).unwrap();
s
}
}
fn inline(s: &str) -> String {
let vec: Vec<_> = s.lines().collect();
vec.join("\\n")
}