tracelog/
lib.rs

1//! Easy logging and tracing
2
3use std::fmt::Display;
4
5pub struct Trace {
6    title : String,
7    info: String,
8    backtrace : String,
9}
10
11impl Trace {
12
13    /// Provides title to the trace logs
14    pub fn new<T : Display>(title: T) -> Trace {
15        Trace {
16            title: title.to_string(),
17            info: String::new(),
18            backtrace : String::new(),
19        }
20    }
21
22    /// Filters backtrace based on "|" separated string provided
23    pub fn trace_filter<T: Display>(&mut self, traces_of: T) -> &mut Trace {
24
25        let mut count = 0;
26
27        backtrace::trace(|frame| {
28
29            backtrace::resolve(frame.ip(), |symbol| {
30
31                let name = if let Some(name) = symbol.name() {
32                    name.to_string()
33                } else {
34                    " - <unknown caller name>".to_string()
35                };
36
37                let filename = if let Some(filename) = symbol.filename() {
38                    filename.display().to_string()
39                } else {
40                    " - <unknown file name>".to_string()
41                };
42
43                let line = if let Some(line) = symbol.lineno() {
44                    line
45                } else {
46                    0
47                };
48
49                let output = format!("{:<2} -- {} \n {:10} {}:{} ", count, name, "", filename, line);
50
51                let matches = traces_of.to_string();
52                let matches = matches.split("|");
53
54                for text_match in matches {
55                    if output.contains(text_match) {
56                        self.backtrace = format!("{}\n{}", self.backtrace, output);
57                        break;
58                    }
59                }
60            });
61
62            count += 1;
63
64            true // keep going
65        });
66
67        self
68    }
69
70    /// Any information to be shown inside the log block
71    pub fn info<T: Display>(&mut self, info: T) -> &mut Trace {
72        self.info = info.to_string();
73        self
74    }
75
76    /// Emits the trace log
77    pub fn emit(&self){
78
79        let output = format!("----------------------------------------------------------------------- BEGIN ------ {} \n", self.title);
80
81        let output = if self.info.len() > 0 {
82            format!("{}{} \n",output, self.info)
83        } else { output };
84        let output = if self.backtrace.len() > 0 {
85            format!("{}{} \n\n",output, self.backtrace)
86        } else { output };
87
88        let output = format!("{}----------------------------------------------------------------------- END -------- {} \n\n", output, self.title);
89
90        println!("{}", output);
91    }
92
93    /// Quick log handle
94    pub fn log<T: Display, U: Display, V: Display> (title: T, info: U, trace_filter: V){
95        Self::new(title).info(info).trace_filter(trace_filter).emit();
96    }
97
98}
99