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
use std::error::Error;
use std::fmt;
use super::Context;
fn format_log<S: Into<String>>(msg: S, level: &str, context: Option<&Context>) -> String {
let context_display = match context {
None => "make:".to_string(),
Some(context) => match &context.path {
None => "make:".to_string(),
Some(path) => {
if context.line_number == 0 {
format!("{}:{}", path.display(), context.line_number)
} else {
format!("{}", path.display())
}
}
},
};
let level_display = format!("{:5} |", level.to_string());
format!("{context_display}{level_display}{}", msg.into())
}
fn format_warn<S: Into<String>>(msg: S, context: Option<&Context>) -> String {
format!("{}", format_log(msg, "WARN", context))
}
fn format_err<S: Into<String>>(msg: S, context: Option<&Context>) -> String {
format!("{}", format_log(msg, "ERROR", context))
}
pub fn log_warn<S: Into<String>>(msg: S, context: Option<&Context>) {
eprintln!("{}", format_warn(msg, context));
}
pub fn log_err<S: Into<String>>(msg: S, context: Option<&Context>) {
eprintln!("{}", format_err(msg, context));
}
#[derive(Debug)]
pub struct MakeError {
pub msg: String,
pub context: Context,
}
impl MakeError {
pub fn new<S: Into<String>>(msg: S, context: Context) -> Self {
Self {
msg: msg.into(),
context: context,
}
}
}
impl Error for MakeError {}
impl fmt::Display for MakeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", format_err(&self.msg, Some(&self.context)))
}
}