Skip to content

Commit ba82c14

Browse files
committed
wip: fix remaining fallout
1 parent 7bc2fea commit ba82c14

File tree

18 files changed

+73
-54
lines changed

18 files changed

+73
-54
lines changed

benchmarks/src/routing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn generate_matching_requests<'c>(client: &'c Client, routes: &[Route]) -> Vec<L
8080
fn client(routes: Vec<Route>) -> Client {
8181
let config = Config {
8282
profile: Config::RELEASE_PROFILE,
83-
// log_level: rocket::config::LogLevel::Off,
83+
log_level: None,
8484
cli_colors: config::CliColors::Never,
8585
shutdown: config::ShutdownConfig {
8686
ctrlc: false,

contrib/db_pools/lib/Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ deadpool_redis = ["deadpool-redis", "deadpool"]
2323
# sqlx features
2424
sqlx_mysql = ["sqlx", "sqlx/mysql"]
2525
sqlx_postgres = ["sqlx", "sqlx/postgres"]
26-
sqlx_sqlite = ["sqlx", "sqlx/sqlite"]
26+
sqlx_sqlite = ["sqlx", "sqlx/sqlite", "log"]
2727
sqlx_macros = ["sqlx/macros"]
2828
# diesel features
2929
diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "diesel", "deadpool_09"]
@@ -86,6 +86,11 @@ default-features = false
8686
features = ["runtime-tokio-rustls"]
8787
optional = true
8888

89+
[dependencies.log]
90+
version = "0.4"
91+
default-features = false
92+
optional = true
93+
8994
[dev-dependencies.rocket]
9095
path = "../../../core/lib"
9196
default-features = false

contrib/db_pools/lib/src/pool.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ mod deadpool_old {
270270
mod sqlx {
271271
use sqlx::ConnectOptions;
272272
use super::{Duration, Error, Config, Figment};
273-
// use rocket::config::LogLevel;
273+
use rocket::tracing::level_filters::LevelFilter;
274274

275275
type Options<D> = <<D as sqlx::Database>::Connection as sqlx::Connection>::Options;
276276

@@ -302,12 +302,21 @@ mod sqlx {
302302
specialize(&mut opts, &config);
303303

304304
opts = opts.disable_statement_logging();
305-
// if let Ok(level) = figment.extract_inner::<LogLevel>(rocket::Config::LOG_LEVEL) {
306-
// if !matches!(level, LogLevel::Normal | LogLevel::Off) {
307-
// opts = opts.log_statements(level.into())
308-
// .log_slow_statements(level.into(), Duration::default());
309-
// }
310-
// }
305+
if let Ok(value) = figment.find_value(rocket::Config::LOG_LEVEL) {
306+
if let Some(level) = value.as_str().and_then(|v| v.parse().ok()) {
307+
let log_level = match level {
308+
LevelFilter::OFF => log::LevelFilter::Off,
309+
LevelFilter::ERROR => log::LevelFilter::Error,
310+
LevelFilter::WARN => log::LevelFilter::Warn,
311+
LevelFilter::INFO => log::LevelFilter::Info,
312+
LevelFilter::DEBUG => log::LevelFilter::Debug,
313+
LevelFilter::TRACE => log::LevelFilter::Trace,
314+
};
315+
316+
opts = opts.log_statements(log_level)
317+
.log_slow_statements(log_level, Duration::default());
318+
}
319+
}
311320

312321
sqlx::pool::PoolOptions::new()
313322
.max_connections(config.max_connections as u32)

contrib/dyn_templates/src/fairing.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rocket::{Rocket, Build, Orbit};
22
use rocket::fairing::{self, Fairing, Info, Kind};
33
use rocket::figment::{Source, value::magic::RelativePathBuf};
4+
use rocket::trace::Trace;
45

56
use crate::context::{Callback, Context, ContextManager};
67
use crate::template::DEFAULT_TEMPLATE_DIR;
@@ -40,7 +41,7 @@ impl Fairing for TemplateFairing {
4041
Ok(dir) => dir,
4142
Err(e) if e.missing() => DEFAULT_TEMPLATE_DIR.into(),
4243
Err(e) => {
43-
rocket::config::pretty_print_error(e);
44+
e.trace_error();
4445
return Err(rocket);
4546
}
4647
};

core/codegen/tests/typed-uris.rs

+16
Original file line numberDiff line numberDiff line change
@@ -709,3 +709,19 @@ fn test_vec_in_query() {
709709
uri!(h(v = &[1, 2, 3][..])) => "/?v=%01%02%03",
710710
}
711711
}
712+
713+
#[test]
714+
fn test_either() {
715+
use rocket::either::{Either, Left, Right};
716+
717+
#[get("/<_foo>")]
718+
fn f(_foo: Either<usize, &str>) { }
719+
720+
assert_uri_eq! {
721+
uri!(f(Left::<usize, &str>(123))) => "/123",
722+
uri!(f(_foo = Left::<usize, &str>(710))) => "/710",
723+
724+
uri!(f(Right::<usize, &str>("hello world"))) => "/hello%20world",
725+
uri!(f(_foo = Right::<usize, &str>("bye?"))) => "/bye%3F",
726+
}
727+
}

core/lib/fuzz/targets/collision-matching.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ type TestData<'a> = (
185185
fn fuzz((route_a, route_b, req): TestData<'_>) {
186186
let rocket = rocket::custom(rocket::Config {
187187
workers: 2,
188-
// log_level: rocket::log::LogLevel::Off,
188+
log_level: None,
189189
cli_colors: rocket::config::CliColors::Never,
190190
..rocket::Config::debug_default()
191191
});

core/lib/src/config/config.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ impl Config {
306306
/// let config = Config::from(figment);
307307
/// ```
308308
pub fn from<T: Provider>(provider: T) -> Self {
309-
Self::try_from(provider).unwrap_or_else(bail_with_config_error)
309+
Self::try_from(provider).unwrap_or_else(|e| {
310+
e.trace_error();
311+
panic!("aborting due to configuration error(s)")
312+
})
310313
}
311314
}
312315

@@ -433,15 +436,3 @@ impl<'r> FromRequest<'r> for &'r Config {
433436
request::Outcome::Success(req.rocket().config())
434437
}
435438
}
436-
437-
#[doc(hidden)]
438-
pub fn bail_with_config_error<T>(error: figment::Error) -> T {
439-
pretty_print_error(error);
440-
panic!("aborting due to configuration error(s)")
441-
}
442-
443-
#[doc(hidden)]
444-
// FIXME: Remove this function.
445-
pub fn pretty_print_error(error: figment::Error) {
446-
error.trace_error()
447-
}

core/lib/src/config/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,3 @@ pub use crate::shutdown::Sig;
139139

140140
#[cfg(feature = "secrets")]
141141
pub use secret_key::SecretKey;
142-
143-
#[doc(hidden)]
144-
pub use config::{pretty_print_error, bail_with_config_error};

core/lib/src/fairing/ad_hoc.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use futures::future::{Future, BoxFuture, FutureExt};
21
use parking_lot::Mutex;
2+
use futures::future::{Future, BoxFuture, FutureExt};
33

4-
use crate::route::RouteUri;
5-
use crate::fairing::{Fairing, Kind, Info, Result};
64
use crate::{Rocket, Request, Response, Data, Build, Orbit};
5+
use crate::fairing::{Fairing, Kind, Info, Result};
6+
use crate::route::RouteUri;
7+
use crate::trace::Trace;
78

89
/// A ad-hoc fairing that can be created from a function or closure.
910
///
@@ -235,7 +236,7 @@ impl AdHoc {
235236
let app_config = match rocket.figment().extract::<T>() {
236237
Ok(config) => config,
237238
Err(e) => {
238-
crate::config::pretty_print_error(e);
239+
e.trace_error();
239240
return Err(rocket);
240241
}
241242
};

core/lib/src/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ pub fn async_test<R>(fut: impl std::future::Future<Output = R>) -> R {
253253
/// WARNING: This is unstable! Do not use this method outside of Rocket!
254254
#[doc(hidden)]
255255
pub fn async_main<R>(fut: impl std::future::Future<Output = R> + Send) -> R {
256+
fn bail<T, E: crate::trace::Trace>(e: E) -> T {
257+
e.trace_error();
258+
panic!("aborting due to error")
259+
}
260+
256261
// FIXME: We need to run `fut` to get the user's `Figment` to properly set
257262
// up the async env, but we need the async env to run `fut`. So we're stuck.
258263
// Tokio doesn't let us take the state from one async env and migrate it to
@@ -262,8 +267,6 @@ pub fn async_main<R>(fut: impl std::future::Future<Output = R> + Send) -> R {
262267
// values won't reflect swaps of `Rocket` in attach fairings with different
263268
// config values, or values from non-Rocket configs. See tokio-rs/tokio#3329
264269
// for a necessary resolution in `tokio`.
265-
use config::bail_with_config_error as bail;
266-
267270
let fig = Config::figment();
268271
let workers = fig.extract_inner(Config::WORKERS).unwrap_or_else(bail);
269272
let max_blocking = fig.extract_inner(Config::MAX_BLOCKING).unwrap_or_else(bail);

core/lib/src/local/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ macro_rules! pub_client_impl {
138138
use crate::config;
139139

140140
let figment = rocket.figment().clone()
141-
// .merge((config::Config::LOG_LEVEL, config::LogLevel::Debug))
141+
.merge((config::Config::LOG_LEVEL, "debug"))
142142
.select(config::Config::DEBUG_PROFILE);
143143

144144
Self::tracked(rocket.reconfigure(figment)) $(.$suffix)?

core/lib/src/response/responder.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for Option<R> {
484484
match self {
485485
Some(r) => r.respond_to(req),
486486
None => {
487-
debug!("{} responder returned `None`", std::any::type_name::<Self>());
487+
let type_name = std::any::type_name::<Self>();
488+
debug!(type_name, "`Option` responder returned `None`");
488489
Err(Status::NotFound)
489490
},
490491
}

core/lib/src/rocket.rs

-13
Original file line numberDiff line numberDiff line change
@@ -590,19 +590,6 @@ impl Rocket<Build> {
590590
}
591591
}
592592

593-
#[tracing::instrument(name = "items", skip_all, fields(kind = kind))]
594-
fn log_items<T, I, B, O>(kind: &str, items: I, base: B, origin: O)
595-
where T: fmt::Display + Copy, I: Iterator<Item = T>,
596-
B: Fn(&T) -> &Origin<'_>, O: Fn(&T) -> &Origin<'_>
597-
{
598-
let mut items: Vec<_> = items.collect();
599-
items.sort_by_key(|i| origin(i).path().as_str().chars().count());
600-
items.sort_by_key(|i| origin(i).path().segments().count());
601-
items.sort_by_key(|i| base(i).path().as_str().chars().count());
602-
items.sort_by_key(|i| base(i).path().segments().count());
603-
items.iter().for_each(|item| info!(name: "item", %item));
604-
}
605-
606593
impl Rocket<Ignite> {
607594
/// Returns the finalized, active configuration. This is guaranteed to
608595
/// remain stable through ignition and into orbit.

core/lib/src/trace/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ pub mod subscriber;
88

99
pub(crate) mod level;
1010

11+
#[doc(inline)]
12+
pub use macros::*;
13+
1114
#[doc(inline)]
1215
pub use traceable::{Trace, TraceAll};
1316

1417
#[doc(inline)]
15-
pub use macros::*;
18+
pub use tracing::{Level, level_filters::LevelFilter};
1619

1720
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
1821
#[serde(crate = "rocket::serde")]

examples/config/Rocket.toml

+2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ port = 8000
1919
workers = 1
2020
keep_alive = 0
2121
log_level = "info"
22+
log_format = "pretty"
2223

2324
[release]
2425
address = "127.0.0.1"
2526
port = 8000
2627
workers = 12
2728
keep_alive = 5
2829
log_level = "error"
30+
log_format = "compact"
2931
# NOTE: Don't (!) use this key! Generate your own and keep it private!
3032
# e.g. via `head -c64 /dev/urandom | base64`
3133
secret_key = "hPRYyVRiMyxpw5sBB1XeCMN1kFsDCqKvBi2QJxBVHQk="

examples/config/src/tests.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use rocket::config::{Config, /* LogLevel */};
1+
use rocket::config::Config;
2+
use rocket::trace::{Level, TraceFormat};
23

34
async fn test_config(profile: &str) {
45
let provider = Config::figment().select(profile);
@@ -8,12 +9,14 @@ async fn test_config(profile: &str) {
89
"debug" => {
910
assert_eq!(config.workers, 1);
1011
assert_eq!(config.keep_alive, 0);
11-
// assert_eq!(config.log_level, LogLevel::Normal);
12+
assert_eq!(config.log_level, Some(Level::INFO));
13+
assert_eq!(config.log_format, TraceFormat::Compact);
1214
}
1315
"release" => {
1416
assert_eq!(config.workers, 12);
1517
assert_eq!(config.keep_alive, 5);
16-
// assert_eq!(config.log_level, LogLevel::Critical);
18+
assert_eq!(config.log_level, Some(Level::ERROR));
19+
assert_eq!(config.log_format, TraceFormat::Compact);
1720
assert!(!config.secret_key.is_zero());
1821
}
1922
_ => {

examples/hello/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ edition = "2021"
66
publish = false
77

88
[dependencies]
9-
rocket = { path = "../../core/lib", features = ["secrets"] }
9+
rocket = { path = "../../core/lib" }

examples/hello/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn wave(name: &str, age: u8) -> String {
5151
// http://127.0.0.1:8000/?name=Rocketeer&lang=en&emoji
5252
// http://127.0.0.1:8000/?lang=ru&emoji&name=Rocketeer
5353
#[get("/?<lang>&<opt..>")]
54-
async fn hello(lang: Option<Lang>, opt: Options<'_>) -> String {
54+
fn hello(lang: Option<Lang>, opt: Options<'_>) -> String {
5555
let mut greeting = String::new();
5656
if opt.emoji {
5757
greeting.push_str("👋 ");

0 commit comments

Comments
 (0)