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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::str::FromStr;
use std::fmt;
use log::{self, Log, LogLevel, LogRecord, LogMetadata};
use yansi::Paint;
struct RocketLogger(LoggingLevel);
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum LoggingLevel {
Critical,
Normal,
Debug,
}
impl LoggingLevel {
#[inline(always)]
fn max_log_level(&self) -> LogLevel {
match *self {
LoggingLevel::Critical => LogLevel::Warn,
LoggingLevel::Normal => LogLevel::Info,
LoggingLevel::Debug => LogLevel::Trace,
}
}
}
impl FromStr for LoggingLevel {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let level = match s {
"critical" => LoggingLevel::Critical,
"normal" => LoggingLevel::Normal,
"debug" => LoggingLevel::Debug,
_ => return Err("a log level (debug, normal, critical)")
};
Ok(level)
}
}
impl fmt::Display for LoggingLevel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let string = match *self {
LoggingLevel::Critical => "critical",
LoggingLevel::Normal => "normal",
LoggingLevel::Debug => "debug",
};
write!(f, "{}", string)
}
}
#[doc(hidden)] #[macro_export]
macro_rules! log_ {
($name:ident: $format:expr) => { log_!($name: $format,) };
($name:ident: $format:expr, $($args:expr),*) => {
$name!(target: "_", $format, $($args),*);
};
}
#[doc(hidden)] #[macro_export]
macro_rules! launch_info {
($format:expr, $($args:expr),*) => {
error!(target: "launch", $format, $($args),*)
}
}
#[doc(hidden)] #[macro_export]
macro_rules! error_ { ($($args:expr),+) => { log_!(error: $($args),+); }; }
#[doc(hidden)] #[macro_export]
macro_rules! info_ { ($($args:expr),+) => { log_!(info: $($args),+); }; }
#[doc(hidden)] #[macro_export]
macro_rules! trace_ { ($($args:expr),+) => { log_!(trace: $($args),+); }; }
#[doc(hidden)] #[macro_export]
macro_rules! debug_ { ($($args:expr),+) => { log_!(debug: $($args),+); }; }
#[doc(hidden)] #[macro_export]
macro_rules! warn_ { ($($args:expr),+) => { log_!(warn: $($args),+); }; }
impl Log for RocketLogger {
#[inline(always)]
fn enabled(&self, md: &LogMetadata) -> bool {
md.level() <= self.0.max_log_level()
}
fn log(&self, record: &LogRecord) {
if !self.enabled(record.metadata()) {
return;
}
let level = match record.target() {
"launch" => Info,
_ => record.level()
};
let from_hyper = record.location().module_path().starts_with("hyper::");
let from_rustls = record.location().module_path().starts_with("rustls::");
if self.0 != LoggingLevel::Debug && (from_hyper || from_rustls) {
return;
}
if record.target() == "_" && self.0 != LoggingLevel::Critical {
print!(" {} ", Paint::white("=>"));
}
use log::LogLevel::*;
match level {
Info => println!("{}", Paint::blue(record.args())),
Trace => println!("{}", Paint::purple(record.args())),
Error => {
println!("{} {}",
Paint::red("Error:").bold(),
Paint::red(record.args()))
}
Warn => {
println!("{} {}",
Paint::yellow("Warning:").bold(),
Paint::yellow(record.args()))
}
Debug => {
let loc = record.location();
print!("\n{} ", Paint::blue("-->").bold());
println!("{}:{}", Paint::blue(loc.file()), Paint::blue(loc.line()));
println!("{}", record.args());
}
}
}
}
#[doc(hidden)]
pub fn try_init(level: LoggingLevel, verbose: bool) {
if !::isatty::stdout_isatty() {
Paint::disable();
} else if cfg!(windows) {
Paint::enable_windows_ascii();
}
let result = log::set_logger(|max_log_level| {
max_log_level.set(level.max_log_level().to_log_level_filter());
Box::new(RocketLogger(level))
});
if let Err(err) = result {
if verbose {
println!("Logger failed to initialize: {}", err);
}
}
}
#[doc(hidden)]
pub fn init(level: LoggingLevel) {
try_init(level, true)
}