File: test_external_rename.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 (105 lines) | stat: -rw-r--r-- 3,189 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
mod test_utils;

use flexi_logger::{FileSpec, Logger, WriteMode};
use log::*;
use std::path::Path;

#[cfg(feature = "async")]
const COUNT: u8 = 3;
#[cfg(not(feature = "async"))]
const COUNT: u8 = 2;

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

fn work(value: u8) {
    let mut logger = Logger::try_with_str("info").unwrap();
    let file_spec = FileSpec::default()
        .directory(self::test_utils::dir())
        .suppress_timestamp()
        .basename("myprog");
    let file_path = file_spec.as_pathbuf(None);
    logger = logger.log_to_file(file_spec);

    // ToDo: test with all write modes, with and without rotation
    match value {
        0 => {
            logger = logger.write_mode(WriteMode::Direct);
        }
        1 => {
            logger = logger.write_mode(WriteMode::BufferAndFlush);
        }
        #[cfg(feature = "async")]
        2 => {
            logger = logger.write_mode(WriteMode::Async);
        }
        COUNT..=u8::MAX => {
            unreachable!("dtrtgfg")
        }
    };

    // create the "moved" folder
    let mut mv_dir = file_path.clone();
    mv_dir.pop();
    mv_dir.push("moved");
    std::fs::create_dir_all(mv_dir.clone()).unwrap();
    let target_filespec = FileSpec::try_from(&file_path)
        .unwrap()
        .directory(mv_dir.clone());
    {
        let logger = logger
            .start()
            .unwrap_or_else(|e| panic!("Logger initialization failed with {e}"));

        // write some log lines to initialize the file
        info!("XXX 1 AAA");
        info!("XXX 2 AAA");
        info!("XXX 3 AAA");

        // write log lines in a slow loop, and rename the log file intermittently
        for i in 0..100 {
            if i % 25 == 20 {
                let target_path = target_filespec.as_pathbuf(Some(&i.to_string()));
                match std::fs::rename(file_path.clone(), target_path.clone()) {
                    Ok(()) => {
                        println!("Renamed the log file {:?} to {:?}", file_path, &target_path);
                        logger.reopen_output().unwrap();
                    }
                    Err(e) => {
                        panic!(
                            "Cannot rename log file {file_path:?} to {target_path:?} due to {e:?}",
                        )
                    }
                }
            }

            std::thread::sleep(std::time::Duration::from_millis(10));
            info!("YYY {} AAA", i);
        }
    }

    // verify that all log lines are written and are found in moved files
    let mut files = 1;
    let mut sum = count_lines(&file_path);
    for entry in std::fs::read_dir(mv_dir).unwrap() {
        let entry = entry.unwrap();
        let lines = count_lines(&entry.path());
        sum += lines;
        if lines > 0 {
            files += 1;
        }
    }
    assert_eq!(files, 5, "wrong number of files");
    assert_eq!(sum, 103, "wrong number of log lines");
}

fn count_lines(path: &Path) -> usize {
    match std::fs::read_to_string(path) {
        Ok(s) => s.lines().filter(|line| line.contains("AAA")).count(),
        Err(_e) => 0,
    }
}