blob: 0784ad18469ca6c3878b4f466d2c689e52314fd8 (
plain)
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
63
|
use std::path::Path;
use std::path::PathBuf;
use std::fs::File;
use std::fs::OpenOptions;
use std::convert::TryInto;
use std::io::Write;
use anyhow::Result;
use anyhow::Error;
use anyhow::anyhow;
use crate::log::LogSink;
use crate::log::LogItem;
use crate::job::Job;
pub struct FileSink {
file: File,
}
impl FileSink {
fn new(path: &Path) -> Result<Self> {
OpenOptions::new()
.create(true)
.append(true)
.write(false)
.open(path)
.map(|file| FileSink { file })
.map_err(Error::from)
}
}
impl LogSink for FileSink {
fn log_item(&mut self, item: &LogItem) -> Result<()> {
let s = item.display()?;
writeln!(self.file, "{}", s)?;
Ok(())
}
}
pub struct FileLogSinkFactory {
root: PathBuf
}
impl FileLogSinkFactory {
pub fn new(root: PathBuf) -> Self {
FileLogSinkFactory { root }
}
pub fn new_file_sink(&self, job: &Job) -> Result<FileSink> {
let now = chrono::offset::Local::now()
.naive_local()
.format("%Y-%m-%dT%H:%M:%S");
trace!("Got current time: {}", now);
let filename = format!("{}-{}", now, job.package().name());
trace!("Building path from {} and {}", self.root.display(), filename);
let p = self.root.join(filename);
FileSink::new(&p)
}
}
|