1 //! Tests!
2 #![cfg(all(not(windows), feature = "reopen-03"))]
3 use std::{fs, io, io::prelude::*};
4 
5 use log::Level::*;
6 
7 mod support;
8 
9 use support::manual_log;
10 
11 #[test]
test_basic_logging_reopen_logging()12 fn test_basic_logging_reopen_logging() {
13     // Create a temporary directory to put a log file into for testing
14     let temp_log_dir = tempdir::TempDir::new("fern").expect("Failed to set up temporary directory");
15     let log_file = temp_log_dir.path().join("test.log");
16 
17     {
18         // Create a basic logger configuration
19         let (_max_level, logger) = fern::Dispatch::new()
20             .format(|out, msg, record| out.finish(format_args!("[{}] {}", record.level(), msg)))
21             .level(log::LevelFilter::Info)
22             .chain(io::stdout())
23             .chain(fern::log_reopen(&log_file, None).expect("Failed to open log file"))
24             .into_log();
25 
26         let l = &*logger;
27         manual_log(l, Trace, "SHOULD NOT DISPLAY");
28         manual_log(l, Debug, "SHOULD NOT DISPLAY");
29         manual_log(l, Info, "Test information message");
30         manual_log(l, Warn, "Test warning message");
31         manual_log(l, Error, "Test error message");
32 
33         // ensure all File objects are dropped and OS buffers are flushed.
34         log::logger().flush();
35 
36         {
37             let result = {
38                 let mut log_read = fs::File::open(&temp_log_dir.path().join("test.log")).unwrap();
39                 let mut buf = String::new();
40                 log_read.read_to_string(&mut buf).unwrap();
41                 buf
42             };
43             assert!(
44                 !result.contains("SHOULD NOT DISPLAY"),
45                 "expected result not including \"SHOULD_NOT_DISPLAY\", found:\n```\n{}\n```\n",
46                 result
47             );
48             assert!(
49                 result.contains("[INFO] Test information message"),
50                 "expected result including \"[INFO] Test information message\", found:\n```\n{}\n```\n",
51                 result
52             );
53             assert!(
54                 result.contains("[WARN] Test warning message"),
55                 "expected result including \"[WARN] Test warning message\", found:\n```\n{}\n```\n",
56                 result
57             );
58             assert!(
59                 result.contains("[ERROR] Test error message"),
60                 "expected result to not include \"[ERROR] Test error message\", found:\n```\n{}\n```\n",
61                 result
62             );
63         }
64     } // ensure logger is dropped before temp dir
65 
66     temp_log_dir
67         .close()
68         .expect("Failed to clean up temporary directory");
69 }
70 
71 #[test]
test_custom_line_separators()72 fn test_custom_line_separators() {
73     // Create a temporary directory to put a log file into for testing
74     let temp_log_dir = tempdir::TempDir::new("fern").expect("Failed to set up temporary directory");
75     let log_file = temp_log_dir.path().join("test_custom_line_sep.log");
76 
77     {
78         // Create a basic logger configuration
79         let (_max_level, logger) = fern::Dispatch::new()
80             // default format is just the message if not specified
81             // default log level is 'trace' if not specified (logs all messages)
82             // output to the log file with the "\r\n" line separator.
83             .chain(fern::Output::reopen(
84                 fern::log_reopen(&log_file, None).expect("Failed to open log file"),
85                 "\r\n",
86             ))
87             .into_log();
88 
89         let l = &*logger;
90         manual_log(l, Info, "message1");
91         manual_log(l, Info, "message2");
92 
93         // ensure all File objects are dropped and OS buffers are flushed.
94         logger.flush();
95 
96         {
97             let result = {
98                 let mut log_read =
99                     fs::File::open(&temp_log_dir.path().join("test_custom_line_sep.log")).unwrap();
100                 let mut buf = String::new();
101                 log_read.read_to_string(&mut buf).unwrap();
102                 buf
103             };
104             assert_eq!(&result, "message1\r\nmessage2\r\n");
105         }
106     } // ensure logger is dropped before temp dir
107 
108     temp_log_dir
109         .close()
110         .expect("Failed to clean up temporary directory");
111 }
112