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
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
use log;
|
2017-06-20 01:29:26 +00:00
|
|
|
use yansi::Paint;
|
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 {
|
2018-01-20 18:41:29 +00:00
|
|
|
/// Only shows errors, warnings, and launch information.
|
2016-08-24 08:34:00 +00:00
|
|
|
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)]
|
2018-01-29 21:16:04 +00:00
|
|
|
fn max_log_level(&self) -> log::Level {
|
2016-08-24 08:34:00 +00:00
|
|
|
match *self {
|
2018-01-29 21:16:04 +00:00
|
|
|
LoggingLevel::Critical => log::Level::Warn,
|
|
|
|
LoggingLevel::Normal => log::Level::Info,
|
|
|
|
LoggingLevel::Debug => log::Level::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]
|
2018-01-20 18:41:29 +00:00
|
|
|
macro_rules! log_ { ($name:ident: $($args:tt)*) => { $name!(target: "_", $($args)*) }; }
|
2017-04-13 08:13:25 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
2018-01-29 21:16:04 +00:00
|
|
|
macro_rules! launch_info { ($($args:tt)*) => { info!(target: "launch", $($args)*) } }
|
2018-01-20 18:41:29 +00:00
|
|
|
#[doc(hidden)] #[macro_export]
|
2018-01-29 21:16:04 +00:00
|
|
|
macro_rules! launch_info_ { ($($args:tt)*) => { info!(target: "launch_", $($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),+); }; }
|
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
impl log::Log for RocketLogger {
|
2017-04-13 08:13:25 +00:00
|
|
|
#[inline(always)]
|
2018-01-29 21:16:04 +00:00
|
|
|
fn enabled(&self, record: &log::Metadata) -> bool {
|
|
|
|
record.target().starts_with("launch") || record.level() <= self.0.max_log_level()
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
fn log(&self, record: &log::Record) {
|
|
|
|
// Print nothing if this level isn't enabled and this isn't launch info.
|
2016-08-24 08:34:00 +00:00
|
|
|
if !self.enabled(record.metadata()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-13 07:18:31 +00:00
|
|
|
// Don't print Hyper or Rustls messages unless debug is enabled.
|
2018-01-29 21:16:04 +00:00
|
|
|
let configged_level = self.0;
|
|
|
|
let from_hyper = record.module_path().map_or(false, |m| m.starts_with("hyper::"));
|
|
|
|
let from_rustls = record.module_path().map_or(false, |m| m.starts_with("rustls::"));
|
2018-01-20 18:41:29 +00:00
|
|
|
if configged_level != LoggingLevel::Debug && (from_hyper || from_rustls) {
|
2016-10-09 11:29:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
// In Rocket, we abuse targets with suffix "_" to indicate indentation.
|
|
|
|
if record.target().ends_with('_') {
|
|
|
|
if configged_level != LoggingLevel::Critical || record.target().starts_with("launch") {
|
|
|
|
print!(" {} ", Paint::white("=>"));
|
|
|
|
}
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
match record.level() {
|
|
|
|
log::Level::Info => println!("{}", Paint::blue(record.args())),
|
|
|
|
log::Level::Trace => println!("{}", Paint::purple(record.args())),
|
|
|
|
log::Level::Error => {
|
2016-08-24 08:34:00 +00:00
|
|
|
println!("{} {}",
|
2017-06-20 01:29:26 +00:00
|
|
|
Paint::red("Error:").bold(),
|
|
|
|
Paint::red(record.args()))
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
2018-01-29 21:16:04 +00:00
|
|
|
log::Level::Warn => {
|
2016-08-24 08:34:00 +00:00
|
|
|
println!("{} {}",
|
2017-06-20 01:29:26 +00:00
|
|
|
Paint::yellow("Warning:").bold(),
|
|
|
|
Paint::yellow(record.args()))
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
2018-01-29 21:16:04 +00:00
|
|
|
log::Level::Debug => {
|
2017-06-20 01:29:26 +00:00
|
|
|
print!("\n{} ", Paint::blue("-->").bold());
|
2018-01-29 21:16:04 +00:00
|
|
|
if let Some(file) = record.file() {
|
|
|
|
print!("{}", Paint::blue(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(line) = record.line() {
|
|
|
|
println!(":{}", Paint::blue(line));
|
|
|
|
}
|
|
|
|
|
2016-08-26 08:55:11 +00:00
|
|
|
println!("{}", record.args());
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 21:16:04 +00:00
|
|
|
|
|
|
|
fn flush(&self) {
|
|
|
|
// NOOP: We don't buffer any records.
|
|
|
|
}
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
pub(crate) fn try_init(level: LoggingLevel, verbose: bool) {
|
2017-06-20 01:29:26 +00:00
|
|
|
if !::isatty::stdout_isatty() {
|
|
|
|
Paint::disable();
|
2017-07-27 02:41:01 +00:00
|
|
|
} else if cfg!(windows) {
|
|
|
|
Paint::enable_windows_ascii();
|
2017-06-20 01:29:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
push_max_level(level);
|
|
|
|
if let Err(e) = log::set_boxed_logger(Box::new(RocketLogger(level))) {
|
2017-05-19 10:29:08 +00:00
|
|
|
if verbose {
|
2018-01-29 21:16:04 +00:00
|
|
|
eprintln!("Logger failed to initialize: {}", e);
|
2017-05-19 10:29:08 +00:00
|
|
|
}
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-19 10:29:08 +00:00
|
|
|
|
2018-01-29 21:25:33 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering};
|
2018-01-29 21:16:04 +00:00
|
|
|
|
2018-01-29 21:25:33 +00:00
|
|
|
static PUSHED: AtomicBool = AtomicBool::new(false);
|
2018-01-29 21:16:04 +00:00
|
|
|
static LAST_LOG_FILTER: AtomicUsize = AtomicUsize::new(filter_to_usize(log::LevelFilter::Off));
|
|
|
|
|
|
|
|
const fn filter_to_usize(filter: log::LevelFilter) -> usize {
|
|
|
|
filter as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usize_to_filter(num: usize) -> log::LevelFilter {
|
|
|
|
unsafe { ::std::mem::transmute(num) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn push_max_level(level: LoggingLevel) {
|
|
|
|
LAST_LOG_FILTER.store(filter_to_usize(log::max_level()), Ordering::Release);
|
2018-01-29 21:25:33 +00:00
|
|
|
PUSHED.store(true, Ordering::Release);
|
2018-01-29 21:16:04 +00:00
|
|
|
log::set_max_level(level.max_log_level().to_level_filter());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn pop_max_level() {
|
2018-01-29 21:25:33 +00:00
|
|
|
if PUSHED.load(Ordering::Acquire) {
|
|
|
|
log::set_max_level(usize_to_filter(LAST_LOG_FILTER.load(Ordering::Acquire)));
|
|
|
|
}
|
2018-01-29 21:16:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn init(level: LoggingLevel) {
|
|
|
|
try_init(level, true)
|
|
|
|
}
|