burn_train/logger/
file.rs1use super::Logger;
2use std::{fs::File, io::Write, path::Path};
3
4pub struct FileLogger {
6 file: File,
7}
8
9impl FileLogger {
10 pub fn new(path: impl AsRef<Path>) -> Self {
20 let path = path.as_ref();
21 let mut options = std::fs::File::options();
22 let file = options
23 .write(true)
24 .truncate(true)
25 .create(true)
26 .open(path)
27 .unwrap_or_else(|err| {
28 panic!(
29 "Should be able to create the new file '{}': {}",
30 path.display(),
31 err
32 )
33 });
34
35 Self { file }
36 }
37}
38
39impl<T> Logger<T> for FileLogger
40where
41 T: std::fmt::Display,
42{
43 fn log(&mut self, item: T) {
44 writeln!(&mut self.file, "{item}").expect("Can log an item.");
45 }
46}