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
#[macro_use] mod log_macro;
// mod log_out;
mod log_config;
mod log_out;

pub use log_config::{set_level,set_prefix,show_time,show_file_line,output_to_file};
pub use log_config::{get_level,get_prefix,output};
pub use log_config::{Level,DEBUG,INFO,WARN,ERROR,PANIC};
pub use log_out::*;



#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use crate::{show_time, show_file_line, set_prefix, set_level, output_to_file, INFO, Level, log_prefix};
    use crate::log_config::close_output;

    #[test]
    fn log_config() {
        //Set log level, default:Debug
         set_level( INFO);
        //Set the log output prefix, default:"wd_log"
         set_prefix("WDLOG");
        //Whether to display the print time, default:true
         show_time(false);
        //Whether to display the location, default:true
         show_file_line(false);
        //Set output to a file, default:stdout
         output_to_file("./log.txt").expect("file open failed");
         log_debug_ln!("this is file output");
        //close output to std out
        close_output();
    }
    #[test]
    fn log_print(){
         log_debug!("{}","hello world");
         log_debug_ln!("{}","hello world");
         log_info!("{}","hello world");
         log_info_ln!("{}","hello world");
         log_warn!("{}","hello world");
         log_warn_ln!("{}","hello world");
         log_error!("{}","hello world");
         log_error_ln!("{}","hello world");
    }
    #[test]
    #[should_panic]
    fn log_panic(){
         log_panic!("{}","hello world");
    }
    #[test]
    fn error(){
        let result = Err("hello");
        let _res:Option<()> =  res_error!(result);
        let _res:Option<()> =  res_error_ln!(result);

        let _res:Option<()> =  res_error!(result;"this is {}","error");
        let _res:Option<()> =  res_error_ln!(result;"this is {}","error");

        let result:Result<&str,&str> = Ok("world");
        let res:Option<&str> =  res_error!(result);
        assert_eq!(res,Some("world"))
    }
    #[test]
    #[should_panic]
    fn res_panic(){
        let result:Result<&str,&str> = Ok("hello");
        let res =  res_panic!(result;"succes");
        println!("{}",res);
        let result = Err("hello");
        //It can lead to panic
        let _ =  res_panic!(result;"this is {}","error");
    }
    #[test]
    fn test_level(){
        let level = Level::from("inFO");
        println!("level {}",level.as_u8())
    }

    #[test]
    fn test_log_output(){
        log_prefix("hello world")
            .prefix("123")
            .prefix("2022-01-02 00:00:00")
            .prefix("74654313896493")
            .field("key","value")
            .field("count",1000)
            .fields(HashMap::from([
                ( "key1".to_string(),"value1".to_string()),
                ("key2".to_string(),"value2".to_string())]))
            .info("info content")
    }
}