2016-09-30 08:25:07 +00:00
|
|
|
//! Rocket's logging infrastructure.
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
use std::fmt;
|
2016-10-03 10:39:56 +00:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
use log;
|
2017-06-20 01:29:26 +00:00
|
|
|
use yansi::Paint;
|
2020-09-03 05:41:31 +00:00
|
|
|
use serde::{de, Serialize, Serializer, Deserialize, Deserializer};
|
2016-08-24 08:34:00 +00:00
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct RocketLogger(LogLevel);
|
2018-10-22 02:46:37 +00:00
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
/// Defines the maximum level of log messages to show.
|
2016-10-03 10:39:56 +00:00
|
|
|
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
|
2020-09-03 05:41:31 +00:00
|
|
|
pub enum LogLevel {
|
|
|
|
/// Only shows errors and warnings: `"critical"`.
|
2016-08-24 08:34:00 +00:00
|
|
|
Critical,
|
2020-09-03 05:41:31 +00:00
|
|
|
/// Shows everything except debug and trace information: `"normal"`.
|
2016-08-24 08:34:00 +00:00
|
|
|
Normal,
|
2020-09-03 05:41:31 +00:00
|
|
|
/// Shows everything: `"debug"`.
|
2016-08-24 08:34:00 +00:00
|
|
|
Debug,
|
2020-09-03 05:41:31 +00:00
|
|
|
/// Shows nothing: "`"off"`".
|
2018-07-03 20:47:17 +00:00
|
|
|
Off,
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl LogLevel {
|
|
|
|
fn as_str(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
LogLevel::Critical => "critical",
|
|
|
|
LogLevel::Normal => "normal",
|
|
|
|
LogLevel::Debug => "debug",
|
|
|
|
LogLevel::Off => "off",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 08:34:00 +00:00
|
|
|
#[inline(always)]
|
2018-07-18 16:45:20 +00:00
|
|
|
fn to_level_filter(self) -> log::LevelFilter {
|
|
|
|
match self {
|
2020-09-03 05:41:31 +00:00
|
|
|
LogLevel::Critical => log::LevelFilter::Warn,
|
|
|
|
LogLevel::Normal => log::LevelFilter::Info,
|
|
|
|
LogLevel::Debug => log::LevelFilter::Trace,
|
|
|
|
LogLevel::Off => log::LevelFilter::Off
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl FromStr for LogLevel {
|
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> {
|
2020-09-03 05:41:31 +00:00
|
|
|
let level = match &*s.to_ascii_lowercase() {
|
|
|
|
"critical" => LogLevel::Critical,
|
|
|
|
"normal" => LogLevel::Normal,
|
|
|
|
"debug" => LogLevel::Debug,
|
|
|
|
"off" => LogLevel::Off,
|
2018-07-03 20:47:17 +00:00
|
|
|
_ => return Err("a log level (off, debug, normal, critical)")
|
2016-10-03 10:39:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(level)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl fmt::Display for LogLevel {
|
2019-06-13 01:48:02 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-09-03 05:41:31 +00:00
|
|
|
write!(f, "{}", self.as_str())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for LogLevel {
|
|
|
|
fn serialize<S: Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
|
|
|
|
ser.serialize_str(self.as_str())
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 00:45:46 +00:00
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
impl<'de> Deserialize<'de> for LogLevel {
|
|
|
|
fn deserialize<D: Deserializer<'de>>(de: D) -> Result<Self, D::Error> {
|
|
|
|
let string = String::deserialize(de)?;
|
|
|
|
LogLevel::from_str(&string).map_err(|_| de::Error::invalid_value(
|
|
|
|
de::Unexpected::Str(&string),
|
|
|
|
&figment::error::OneOf( &["critical", "normal", "debug", "off"])
|
|
|
|
))
|
2017-01-14 00:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)]
|
2019-06-13 01:48:02 +00:00
|
|
|
fn enabled(&self, record: &log::Metadata<'_>) -> bool {
|
2018-07-03 20:47:17 +00:00
|
|
|
match self.0.to_level_filter().to_level() {
|
|
|
|
Some(max) => record.level() <= max || record.target().starts_with("launch"),
|
|
|
|
None => false
|
|
|
|
}
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
fn log(&self, record: &log::Record<'_>) {
|
2018-01-29 21:16:04 +00:00
|
|
|
// 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::"));
|
2020-09-03 05:41:31 +00:00
|
|
|
if configged_level != LogLevel::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.
|
2018-11-19 10:11:38 +00:00
|
|
|
let is_launch = record.target().starts_with("launch");
|
2018-01-29 21:16:04 +00:00
|
|
|
if record.target().ends_with('_') {
|
2020-09-03 05:41:31 +00:00
|
|
|
if configged_level != LogLevel::Critical || is_launch {
|
2018-11-19 10:11:38 +00:00
|
|
|
print!(" {} ", Paint::default("=>").bold());
|
2018-01-29 21:16:04 +00:00
|
|
|
}
|
2016-08-24 08:34:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:16:04 +00:00
|
|
|
match record.level() {
|
2018-11-19 10:11:38 +00:00
|
|
|
log::Level::Info => println!("{}", Paint::blue(record.args()).wrap()),
|
|
|
|
log::Level::Trace => println!("{}", Paint::magenta(record.args()).wrap()),
|
2018-01-29 21:16:04 +00:00
|
|
|
log::Level::Error => {
|
2016-08-24 08:34:00 +00:00
|
|
|
println!("{} {}",
|
2017-06-20 01:29:26 +00:00
|
|
|
Paint::red("Error:").bold(),
|
2018-11-19 10:11:38 +00:00
|
|
|
Paint::red(record.args()).wrap())
|
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(),
|
2018-11-19 10:11:38 +00:00
|
|
|
Paint::yellow(record.args()).wrap())
|
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
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
pub(crate) fn try_init(level: LogLevel, colors: bool, verbose: bool) -> bool {
|
|
|
|
if level == LogLevel::Off {
|
2018-07-03 20:47:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
if !atty::is(atty::Stream::Stdout)
|
2018-10-22 02:46:37 +00:00
|
|
|
|| (cfg!(windows) && !Paint::enable_windows_ascii())
|
2020-09-03 05:41:31 +00:00
|
|
|
|| !colors
|
2018-10-22 02:46:37 +00:00
|
|
|
{
|
2017-06-20 01:29:26 +00:00
|
|
|
Paint::disable();
|
|
|
|
}
|
|
|
|
|
2019-06-30 16:45:17 +00:00
|
|
|
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
|
|
|
}
|
2018-07-06 00:54:19 +00:00
|
|
|
|
2018-07-03 20:47:17 +00:00
|
|
|
return false;
|
2019-06-30 16:45:17 +00:00
|
|
|
}
|
2018-07-03 20:47:17 +00:00
|
|
|
|
|
|
|
log::set_max_level(level.to_level_filter());
|
2020-09-03 05:41:31 +00:00
|
|
|
true
|
2018-01-29 21:16:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 05:41:31 +00:00
|
|
|
pub trait PaintExt {
|
2020-04-14 12:28:36 +00:00
|
|
|
fn emoji(item: &str) -> Paint<&str>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PaintExt for Paint<&str> {
|
|
|
|
/// Paint::masked(), but hidden on Windows due to broken output. See #1122.
|
2020-09-03 05:41:31 +00:00
|
|
|
fn emoji(_item: &str) -> Paint<&str> {
|
|
|
|
#[cfg(windows)] { Paint::masked("") }
|
|
|
|
#[cfg(not(windows))] { Paint::masked(_item) }
|
2020-04-14 12:28:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
#[doc(hidden)]
|
2020-09-03 05:41:31 +00:00
|
|
|
pub fn init(level: LogLevel) -> bool {
|
|
|
|
try_init(level, true, true)
|
2017-05-19 10:29:08 +00:00
|
|
|
}
|
2018-07-21 22:11:08 +00:00
|
|
|
|
2018-09-20 04:14:30 +00:00
|
|
|
// Expose logging macros as (hidden) funcions for use by core/contrib codegen.
|
|
|
|
macro_rules! external_log_function {
|
|
|
|
($fn_name:ident: $macro_name:ident) => (
|
|
|
|
#[doc(hidden)] #[inline(always)]
|
|
|
|
pub fn $fn_name(msg: &str) { $macro_name!("{}", msg); }
|
|
|
|
)
|
2018-07-21 22:11:08 +00:00
|
|
|
}
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2018-11-12 21:08:39 +00:00
|
|
|
external_log_function!(error: error);
|
|
|
|
external_log_function!(error_: error_);
|
|
|
|
external_log_function!(warn: warn);
|
|
|
|
external_log_function!(warn_: warn_);
|