File: test_file_writer.rs

package info (click to toggle)
rust-flexi-logger 0.29.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,084 kB
  • sloc: makefile: 2
file content (128 lines) | stat: -rw-r--r-- 4,043 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
mod test_utils;

use flexi_logger::{detailed_format, opt_format, Cleanup, Criterion, FileSpec, Logger, Naming};
use log::*;

const COUNT: u8 = 8;

#[test]
fn test_write_modes() {
    if let Some(value) = test_utils::dispatch(COUNT) {
        work(value)
    }
}

fn work(value: u8) {
    let link_name = "link_to_log".to_string();
    let mut logger = Logger::try_with_str("info").unwrap();

    match value {
        0 => {
            logger = logger.log_to_file(
                FileSpec::default()
                    .directory(self::test_utils::dir())
                    .basename("to_foo_or_not_to_foo"),
            );
        }
        1 => {
            logger = logger
                .log_to_file(
                    FileSpec::default()
                        .suppress_timestamp()
                        .directory(self::test_utils::dir()),
                )
                .rotate(Criterion::Size(2000), Naming::Numbers, Cleanup::Never);
        }
        2 => {
            logger = logger
                .format(detailed_format)
                .log_to_file(
                    FileSpec::default()
                        .directory(self::test_utils::dir())
                        .use_timestamp(false),
                )
                .rotate(Criterion::Size(2000), Naming::Numbers, Cleanup::Never);
        }
        3 => {
            logger = logger
                .format(detailed_format)
                .log_to_file(
                    FileSpec::default()
                        .suppress_timestamp()
                        .directory(self::test_utils::dir()),
                )
                .rotate(Criterion::Size(2000), Naming::Numbers, Cleanup::Never);
        }
        4 => {
            logger = logger
                .format(opt_format)
                .log_to_file(
                    FileSpec::default()
                        .suppress_timestamp()
                        .directory(self::test_utils::dir())
                        .discriminant("foo".to_string()),
                )
                .rotate(Criterion::Size(2000), Naming::Numbers, Cleanup::Never)
                .create_symlink(link_name.clone());
        }
        5 => {
            logger = logger.format(opt_format).log_to_file(
                FileSpec::default()
                    .suppress_timestamp()
                    .directory(self::test_utils::dir())
                    .discriminant("foo"),
            );
        }
        6 => {
            logger = logger.format(opt_format).log_to_file(
                FileSpec::default()
                    .directory(self::test_utils::dir())
                    .suppress_basename(),
            );
        }
        7 => {
            logger = logger.format(opt_format).log_to_file(
                FileSpec::default()
                    .directory(self::test_utils::dir())
                    .suppress_basename()
                    .discriminant("foo"),
            );
        }
        COUNT..=u8::MAX => {
            unreachable!("dtrtgfg")
        }
    };

    let handle = logger
        .start()
        .unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

    error!("This is an error message");
    warn!("This is a warning");
    info!("This is an info message");
    debug!("This is a debug message - you must not see it!");
    trace!("This is a trace message - you must not see it!");

    handle.validate_logs(&[
        ("ERROR", "test_file_writer", "error"),
        ("WARN", "test_file_writer", "warning"),
        ("INFO", "test_file_writer", "info"),
    ]);

    if value == 4 {
        self::platform::check_link(&link_name);
    }
}

mod platform {
    #[cfg(target_family = "unix")]
    pub fn check_link(link_name: &str) {
        match std::fs::symlink_metadata(link_name) {
            Err(e) => panic!("error with symlink: {e}"),
            Ok(metadata) => assert!(metadata.file_type().is_symlink(), "not a symlink"),
        }
    }

    #[cfg(not(target_family = "unix"))]
    pub fn check_link(_: &str) {}
}