tui_logger/
file.rs

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
use std::fs::{File, OpenOptions};

use crate::TuiLoggerLevelOutput;

/// This closely follows the options of [``TuiLoggerSmartWidget``] but is used of logging to a file.
pub struct TuiLoggerFile {
    pub dump: File,
    pub format_separator: char,
    pub timestamp_fmt: Option<String>,
    pub format_output_target: bool,
    pub format_output_file: bool,
    pub format_output_line: bool,
    pub format_output_level: Option<TuiLoggerLevelOutput>,
}

impl TuiLoggerFile {
    pub fn new(fname: &str) -> Self {
        TuiLoggerFile {
            dump: OpenOptions::new()
                .create(true)
                .append(true)
                .open(fname)
                .expect("Failed to open dump File"),
            format_separator: ':',
            timestamp_fmt: Some("[%Y:%m:%d %H:%M:%S]".to_string()),
            format_output_file: true,
            format_output_line: true,
            format_output_target: true,
            format_output_level: Some(TuiLoggerLevelOutput::Long),
        }
    }
    pub fn output_target(mut self, enabled: bool) -> Self {
        self.format_output_target = enabled;
        self
    }
    pub fn output_file(mut self, enabled: bool) -> Self {
        self.format_output_file = enabled;
        self
    }
    pub fn output_line(mut self, enabled: bool) -> Self {
        self.format_output_line = enabled;
        self
    }
    pub fn output_timestamp(mut self, fmt: Option<String>) -> Self {
        self.timestamp_fmt = fmt;
        self
    }
    pub fn output_separator(mut self, sep: char) -> Self {
        self.format_separator = sep;
        self
    }
    pub fn output_level(mut self, level: Option<TuiLoggerLevelOutput>) -> Self {
        self.format_output_level = level;
        self
    }
}