-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: allow dynamic selection of log fmt
- Loading branch information
1 parent
67485ba
commit c5979c5
Showing
10 changed files
with
141 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use std::sync::OnceLock; | ||
|
||
use tracing::{Dispatch, Event, Level, Metadata}; | ||
use tracing::subscriber::{Subscriber, Interest}; | ||
use tracing::span::{Attributes, Id, Record}; | ||
use tracing_subscriber::registry::{Registry, LookupSpan}; | ||
use tracing_subscriber::layer::{Context, Layer, Layered, SubscriberExt}; | ||
use tracing_subscriber::reload; | ||
use tracing_subscriber::util::SubscriberInitExt; | ||
|
||
use crate::config::{Config, CliColors}; | ||
use crate::trace::subscriber::{Compact, Pretty, RequestId, RequestIdLayer, RocketFmt}; | ||
use crate::trace::TraceFormat; | ||
|
||
pub struct RocketDynFmt { | ||
inner: either::Either<RocketFmt<Compact>, RocketFmt<Pretty>>, | ||
} | ||
|
||
impl From<RocketFmt<Compact>> for RocketDynFmt { | ||
fn from(value: RocketFmt<Compact>) -> Self { | ||
RocketDynFmt { inner: either::Either::Left(value) } | ||
} | ||
} | ||
|
||
impl From<RocketFmt<Pretty>> for RocketDynFmt { | ||
fn from(value: RocketFmt<Pretty>) -> Self { | ||
RocketDynFmt { inner: either::Either::Right(value) } | ||
} | ||
} | ||
|
||
impl RocketDynFmt { | ||
pub fn init(config: Option<&Config>) { | ||
type Handle = reload::Handle<RocketDynFmt, Layered<RequestIdLayer, Registry>>; | ||
|
||
static HANDLE: OnceLock<Handle> = OnceLock::new(); | ||
|
||
// Do nothing if there's no config and we've already initialized. | ||
if config.is_none() && HANDLE.get().is_some() { | ||
return; | ||
} | ||
|
||
let workers = config.map(|c| c.workers).unwrap_or(num_cpus::get()); | ||
let colors = config.map(|c| c.cli_colors).unwrap_or(CliColors::Auto); | ||
let level = config.map(|c| c.log_level).unwrap_or(Some(Level::INFO)); | ||
let format = config.map(|c| c.log_format).unwrap_or(TraceFormat::Pretty); | ||
|
||
let formatter = |format| match format { | ||
TraceFormat::Pretty => Self::from(RocketFmt::<Pretty>::new(workers, colors, level)), | ||
TraceFormat::Compact => Self::from(RocketFmt::<Compact>::new(workers, colors, level)), | ||
}; | ||
|
||
let (layer, reload_handle) = reload::Layer::new(formatter(format)); | ||
let result = tracing_subscriber::registry() | ||
.with(RequestId::layer()) | ||
.with(layer) | ||
.try_init(); | ||
|
||
if result.is_ok() { | ||
assert!(HANDLE.set(reload_handle).is_ok()); | ||
} if let Some(handle) = HANDLE.get() { | ||
assert!(handle.modify(|layer| *layer = formatter(format)).is_ok()); | ||
} | ||
} | ||
|
||
pub fn reset(&mut self, cli_colors: CliColors, level: Option<Level>) { | ||
either::for_both!(&mut self.inner, f => f.reset(cli_colors, level)) | ||
} | ||
} | ||
|
||
macro_rules! forward { | ||
($T:ident => $(& $r:tt)? $method:ident ( $($p:ident : $t:ty),* ) $(-> $R:ty)?) => { | ||
#[inline(always)] | ||
fn $method(& $($r)? self $(, $p : $t)*) $(-> $R)? { | ||
match & $($r)* self.inner { | ||
either::Either::Left(layer) => Layer::<$T>::$method(layer, $($p),*), | ||
either::Either::Right(layer) => Layer::<$T>::$method(layer, $($p),*), | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for RocketDynFmt { | ||
forward!(S => on_register_dispatch(subscriber: &Dispatch)); | ||
forward!(S => &mut on_layer(subscriber: &mut S)); | ||
forward!(S => register_callsite(metadata: &'static Metadata<'static>) -> Interest); | ||
forward!(S => enabled(metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool); | ||
forward!(S => on_new_span(attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)); | ||
forward!(S => on_record(_span: &Id, _values: &Record<'_>, _ctx: Context<'_, S>)); | ||
forward!(S => on_follows_from(_span: &Id, _follows: &Id, _ctx: Context<'_, S>)); | ||
forward!(S => event_enabled(_event: &Event<'_>, _ctx: Context<'_, S>) -> bool); | ||
forward!(S => on_event(_event: &Event<'_>, _ctx: Context<'_, S>)); | ||
forward!(S => on_enter(_id: &Id, _ctx: Context<'_, S>)); | ||
forward!(S => on_exit(_id: &Id, _ctx: Context<'_, S>)); | ||
forward!(S => on_close(_id: Id, _ctx: Context<'_, S>)); | ||
forward!(S => on_id_change(_old: &Id, _new: &Id, _ctx: Context<'_, S>)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
mod visit; | ||
mod pretty; | ||
mod compact; | ||
mod dynamic; | ||
mod common; | ||
mod request_id; | ||
|
||
pub use pretty::Pretty; | ||
pub use compact::Compact; | ||
pub use common::RocketFmt; | ||
pub use request_id::{RequestId, RequestIdLayer}; | ||
pub use dynamic::RocketDynFmt; | ||
|
||
pub(crate) use common::Handle; | ||
pub(crate) use visit::{RecordDisplay, Data}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters