From 6f29696b4fcd460c9dff7c15a480dd42469839c7 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Thu, 13 Apr 2017 01:13:25 -0700 Subject: [PATCH] Make TLS misconfig an error. Always print launch message. --- examples/hello_tls/Cargo.toml | 2 +- lib/src/logger.rs | 18 +++++++++++++++++- lib/src/rocket.rs | 6 +++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/examples/hello_tls/Cargo.toml b/examples/hello_tls/Cargo.toml index d691440e..24d37d21 100644 --- a/examples/hello_tls/Cargo.toml +++ b/examples/hello_tls/Cargo.toml @@ -4,7 +4,7 @@ version = "0.0.0" workspace = "../../" [dependencies] -rocket = { path = "../../lib", features = ["tls"] } +rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } [dev-dependencies] diff --git a/lib/src/logger.rs b/lib/src/logger.rs index 2550dd7c..e0569c3b 100644 --- a/lib/src/logger.rs +++ b/lib/src/logger.rs @@ -66,6 +66,13 @@ macro_rules! log_ { }; } +#[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] @@ -78,6 +85,7 @@ macro_rules! debug_ { ($($args:expr),+) => { log_!(debug: $($args),+); }; } 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() } @@ -88,6 +96,14 @@ impl Log for RocketLogger { return; } + // 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() + }; + // Don't print Hyper or Rustls messages unless debug is enabled. let from_hyper = record.location().module_path().starts_with("hyper::"); let from_rustls = record.location().module_path().starts_with("rustls::"); @@ -101,7 +117,7 @@ impl Log for RocketLogger { } use log::LogLevel::*; - match record.level() { + match level { Info => println!("{}", Blue.paint(record.args())), Trace => println!("{}", Magenta.paint(record.args())), Error => { diff --git a/lib/src/rocket.rs b/lib/src/rocket.rs index 0dae48b9..ac41fbdb 100644 --- a/lib/src/rocket.rs +++ b/lib/src/rocket.rs @@ -387,9 +387,9 @@ impl Rocket { if tls_configured && cfg!(feature = "tls") { info_!("tls: {}", White.paint("enabled")); } else { - info_!("tls: {}", White.paint("disabled")); + error_!("tls: {}", White.paint("disabled")); if tls_configured { - warn_!("tls is configured, but the tls feature is disabled"); + error_!("tls is configured, but the tls feature is disabled"); } } @@ -602,7 +602,7 @@ impl Rocket { Err(e) => return LaunchError::from(e) }; - info!("🚀 {} {}{}", + launch_info!("🚀 {} {}{}", White.paint("Rocket has launched from"), White.bold().paint(proto), White.bold().paint(&full_addr));