2016-09-30 08:25:07 +00:00
|
|
|
//! Rocket's logging infrastructure.
|
|
|
|
|
2016-10-03 10:39:56 +00:00
|
|
|
use std::str::FromStr;
|
2017-01-14 00:45:46 +00:00
|
|
|
use std::fmt;
|
2016-10-03 10:39:56 +00:00
|
|
|
|
2016-08-24 08:34:00 +00:00
|
|
|
use log::{self, Log, LogLevel, LogRecord, LogMetadata};
|
2017-06-02 04:44:31 +00:00
|
|
|
use yansi::Color::*;
|
2016-08-24 08:34:00 +00:00
|
|
|
|
2016-09-30 08:25:07 +00:00
|
|
|
struct RocketLogger(LoggingLevel);
|
2016-08-24 08:34:00 +00:00
|
|
|
|
2016-09-30 08:25:07 +00:00
|
|
|
/// Defines the different levels for log messages.
|
2016-10-03 10:39:56 +00:00
|
|
|
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
|
2016-08-27 01:37:28 +00:00
|
|
|
pub enum LoggingLevel {
|
2016-08-24 08:34:00 +00:00
|
|
|
/// Only shows errors and warning.
|
|
|
|
Critical,
|
|
|
|
/// Shows everything except debug and trace information.
|
|
|
|
Normal,
|
|
|
|
/// Shows everything.
|
|
|
|
Debug,
|
|
|
|
}
|
|
|
|
|
2016-08-27 01:37:28 +00:00
|
|
|
impl LoggingLevel {
|
2016-08-24 08:34:00 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn max_log_level(&self) -> LogLevel {
|
|
|
|
match *self {
|
2016-08-27 01:37:28 +00:00
|
|
|
LoggingLevel::Critical => LogLevel::Warn,
|
|
|
|
LoggingLevel::Normal => LogLevel::Info,
|
2016-09-30 22:20:11 +00:00
|
|
|
LoggingLevel::Debug => LogLevel::Trace,
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 10:39:56 +00:00
|
|
|
impl FromStr for LoggingLevel {
|
2017-04-13 07:18:31 +00:00
|
|
|
type Err = &'static str;
|
|
|
|
|
2016-10-03 10:39:56 +00:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let level = match s {
|
|
|
|
"critical" => LoggingLevel::Critical,
|
|
|
|
"normal" => LoggingLevel::Normal,
|
|
|
|
"debug" => LoggingLevel::Debug,
|
2017-04-13 07:18:31 +00:00
|
|
|
_ => return Err("a log level (debug, normal, critical)")
|
2016-10-03 10:39:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(level)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:45:46 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 14:38:06 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
2016-08-24 08:34:00 +00:00
|
|
|
macro_rules! log_ {
|
|
|
|
($name:ident: $format:expr) => { log_!($name: $format,) };
|
|
|
|
($name:ident: $format:expr, $($args:expr),*) => {
|
|
|
|
$name!(target: "_", $format, $($args),*);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-04-13 08:13:25 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
|
|
|
macro_rules! launch_info {
|
|
|
|
($format:expr, $($args:expr),*) => {
|
|
|
|
error!(target: "launch", $format, $($args),*)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-04 14:38:06 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
2016-08-24 08:34:00 +00:00
|
|
|
macro_rules! error_ { ($($args:expr),+) => { log_!(error: $($args),+); }; }
|
2016-11-04 14:38:06 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
2016-08-24 08:34:00 +00:00
|
|
|
macro_rules! info_ { ($($args:expr),+) => { log_!(info: $($args),+); }; }
|
2016-11-04 14:38:06 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
2016-08-24 08:34:00 +00:00
|
|
|
macro_rules! trace_ { ($($args:expr),+) => { log_!(trace: $($args),+); }; }
|
2016-11-04 14:38:06 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
2016-08-24 08:34:00 +00:00
|
|
|
macro_rules! debug_ { ($($args:expr),+) => { log_!(debug: $($args),+); }; }
|
2016-11-04 14:38:06 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
2016-08-24 08:34:00 +00:00
|
|
|
macro_rules! warn_ { ($($args:expr),+) => { log_!(warn: $($args),+); }; }
|
|
|
|
|
|
|
|
impl Log for RocketLogger {
|
2017-04-13 08:13:25 +00:00
|
|
|
#[inline(always)]
|
2016-08-24 08:34:00 +00:00
|
|
|
fn enabled(&self, md: &LogMetadata) -> bool {
|
|
|
|
md.level() <= self.0.max_log_level()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log(&self, record: &LogRecord) {
|
|
|
|
// Print nothing if this level isn't enabled.
|
|
|
|
if !self.enabled(record.metadata()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-13 08:13:25 +00:00
|
|
|
// We use the `launch_info` macro to "fake" a high priority info
|
|
|
|
// message. We want to print the message unless the user uses a custom
|
|
|
|
// drain, so we set it's status to critical, but reset it here to info.
|
|
|
|
let level = match record.target() {
|
|
|
|
"launch" => Info,
|
|
|
|
_ => record.level()
|
|
|
|
};
|
|
|
|
|
2017-04-13 07:18:31 +00:00
|
|
|
// Don't print Hyper or Rustls messages unless debug is enabled.
|
2016-10-09 11:29:02 +00:00
|
|
|
let from_hyper = record.location().module_path().starts_with("hyper::");
|
2017-04-13 07:18:31 +00:00
|
|
|
let from_rustls = record.location().module_path().starts_with("rustls::");
|
|
|
|
if self.0 != LoggingLevel::Debug && (from_hyper || from_rustls) {
|
2016-10-09 11:29:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-24 08:34:00 +00:00
|
|
|
// In Rocket, we abuse target with value "_" to indicate indentation.
|
2016-08-27 01:37:28 +00:00
|
|
|
if record.target() == "_" && self.0 != LoggingLevel::Critical {
|
2016-08-24 08:34:00 +00:00
|
|
|
print!(" {} ", White.paint("=>"));
|
|
|
|
}
|
|
|
|
|
|
|
|
use log::LogLevel::*;
|
2017-04-13 08:13:25 +00:00
|
|
|
match level {
|
2016-08-24 08:34:00 +00:00
|
|
|
Info => println!("{}", Blue.paint(record.args())),
|
2017-06-02 04:44:31 +00:00
|
|
|
Trace => println!("{}", Purple.paint(record.args())),
|
2016-08-24 08:34:00 +00:00
|
|
|
Error => {
|
|
|
|
println!("{} {}",
|
2017-06-02 04:44:31 +00:00
|
|
|
Red.paint("Error:").bold(),
|
2016-08-24 08:34:00 +00:00
|
|
|
Red.paint(record.args()))
|
|
|
|
}
|
|
|
|
Warn => {
|
|
|
|
println!("{} {}",
|
2017-06-02 04:44:31 +00:00
|
|
|
Yellow.paint("Warning:").bold(),
|
2016-08-24 08:34:00 +00:00
|
|
|
Yellow.paint(record.args()))
|
|
|
|
}
|
|
|
|
Debug => {
|
|
|
|
let loc = record.location();
|
2017-06-02 04:44:31 +00:00
|
|
|
print!("\n{} ", Blue.paint("-->").bold());
|
2016-08-26 08:55:11 +00:00
|
|
|
println!("{}:{}", Blue.paint(loc.file()), Blue.paint(loc.line()));
|
|
|
|
println!("{}", record.args());
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 04:44:27 +00:00
|
|
|
#[doc(hidden)]
|
2017-05-19 10:29:08 +00:00
|
|
|
pub fn try_init(level: LoggingLevel, verbose: bool) {
|
2016-08-24 08:34:00 +00:00
|
|
|
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 {
|
2017-05-19 10:29:08 +00:00
|
|
|
if verbose {
|
|
|
|
println!("Logger failed to initialize: {}", err);
|
|
|
|
}
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-19 10:29:08 +00:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn init(level: LoggingLevel) {
|
|
|
|
try_init(level, true)
|
|
|
|
}
|