diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:11:38 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:13:23 +0000 |
commit | 20431706a863f92cb37dc512fef6e48d192aaf2c (patch) | |
tree | 2867f13f5fd5437ba628c67d7f87309ccadcd286 /vendor/tracing-subscriber/src/fmt | |
parent | Releasing progress-linux version 1.65.0+dfsg1-2~progress7.99u1. (diff) | |
download | rustc-20431706a863f92cb37dc512fef6e48d192aaf2c.tar.xz rustc-20431706a863f92cb37dc512fef6e48d192aaf2c.zip |
Merging upstream version 1.66.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tracing-subscriber/src/fmt')
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/fmt_layer.rs | 1368 | ||||
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/format/json.rs | 885 | ||||
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/format/mod.rs | 2161 | ||||
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/format/pretty.rs | 511 | ||||
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/mod.rs | 1324 | ||||
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/time/datetime.rs | 410 | ||||
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/time/mod.rs | 138 | ||||
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/time/time_crate.rs | 470 | ||||
-rw-r--r-- | vendor/tracing-subscriber/src/fmt/writer.rs | 1464 |
9 files changed, 0 insertions, 8731 deletions
diff --git a/vendor/tracing-subscriber/src/fmt/fmt_layer.rs b/vendor/tracing-subscriber/src/fmt/fmt_layer.rs deleted file mode 100644 index 21992e780..000000000 --- a/vendor/tracing-subscriber/src/fmt/fmt_layer.rs +++ /dev/null @@ -1,1368 +0,0 @@ -use crate::{ - field::RecordFields, - fmt::{format, FormatEvent, FormatFields, MakeWriter, TestWriter}, - layer::{self, Context}, - registry::{self, LookupSpan, SpanRef}, -}; -use format::{FmtSpan, TimingDisplay}; -use std::{any::TypeId, cell::RefCell, fmt, io, marker::PhantomData, ops::Deref, time::Instant}; -use tracing_core::{ - field, - span::{Attributes, Current, Id, Record}, - Event, Metadata, Subscriber, -}; - -/// A [`Layer`] that logs formatted representations of `tracing` events. -/// -/// ## Examples -/// -/// Constructing a layer with the default configuration: -/// -/// ```rust -/// use tracing_subscriber::{fmt, Registry}; -/// use tracing_subscriber::prelude::*; -/// -/// let subscriber = Registry::default() -/// .with(fmt::Layer::default()); -/// -/// tracing::subscriber::set_global_default(subscriber).unwrap(); -/// ``` -/// -/// Overriding the layer's behavior: -/// -/// ```rust -/// use tracing_subscriber::{fmt, Registry}; -/// use tracing_subscriber::prelude::*; -/// -/// let fmt_layer = fmt::layer() -/// .with_target(false) // don't include event targets when logging -/// .with_level(false); // don't include event levels when logging -/// -/// let subscriber = Registry::default().with(fmt_layer); -/// # tracing::subscriber::set_global_default(subscriber).unwrap(); -/// ``` -/// -/// Setting a custom event formatter: -/// -/// ```rust -/// use tracing_subscriber::fmt::{self, format, time}; -/// use tracing_subscriber::prelude::*; -/// -/// let fmt = format().with_timer(time::Uptime::default()); -/// let fmt_layer = fmt::layer() -/// .event_format(fmt) -/// .with_target(false); -/// # let subscriber = fmt_layer.with_subscriber(tracing_subscriber::registry::Registry::default()); -/// # tracing::subscriber::set_global_default(subscriber).unwrap(); -/// ``` -/// -/// [`Layer`]: super::layer::Layer -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -#[derive(Debug)] -pub struct Layer< - S, - N = format::DefaultFields, - E = format::Format<format::Full>, - W = fn() -> io::Stdout, -> { - make_writer: W, - fmt_fields: N, - fmt_event: E, - fmt_span: format::FmtSpanConfig, - is_ansi: bool, - _inner: PhantomData<fn(S)>, -} - -impl<S> Layer<S> { - /// Returns a new [`Layer`][self::Layer] with the default configuration. - pub fn new() -> Self { - Self::default() - } -} - -// This needs to be a seperate impl block because they place different bounds on the type parameters. -impl<S, N, E, W> Layer<S, N, E, W> -where - S: Subscriber + for<'a> LookupSpan<'a>, - N: for<'writer> FormatFields<'writer> + 'static, - W: for<'writer> MakeWriter<'writer> + 'static, -{ - /// Sets the [event formatter][`FormatEvent`] that the layer being built will - /// use to format events. - /// - /// The event formatter may be any type implementing the [`FormatEvent`] - /// trait, which is implemented for all functions taking a [`FmtContext`], a - /// [`Writer`], and an [`Event`]. - /// - /// # Examples - /// - /// Setting a type implementing [`FormatEvent`] as the formatter: - /// ```rust - /// use tracing_subscriber::fmt::{self, format}; - /// - /// let layer = fmt::layer() - /// .event_format(format().compact()); - /// # // this is necessary for type inference. - /// # use tracing_subscriber::Layer as _; - /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); - /// ``` - /// [`FormatEvent`]: format::FormatEvent - /// [`Event`]: tracing::Event - /// [`Writer`]: format::Writer - pub fn event_format<E2>(self, e: E2) -> Layer<S, N, E2, W> - where - E2: FormatEvent<S, N> + 'static, - { - Layer { - fmt_fields: self.fmt_fields, - fmt_event: e, - fmt_span: self.fmt_span, - make_writer: self.make_writer, - is_ansi: self.is_ansi, - _inner: self._inner, - } - } - - /// Updates the event formatter by applying a function to the existing event formatter. - /// - /// This sets the event formatter that the layer being built will use to record fields. - /// - /// # Examples - /// - /// Updating an event formatter: - /// - /// ```rust - /// let layer = tracing_subscriber::fmt::layer() - /// .map_event_format(|e| e.compact()); - /// # // this is necessary for type inference. - /// # use tracing_subscriber::Layer as _; - /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); - /// ``` - pub fn map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> Layer<S, N, E2, W> - where - E2: FormatEvent<S, N> + 'static, - { - Layer { - fmt_fields: self.fmt_fields, - fmt_event: f(self.fmt_event), - fmt_span: self.fmt_span, - make_writer: self.make_writer, - is_ansi: self.is_ansi, - _inner: self._inner, - } - } -} - -// This needs to be a seperate impl block because they place different bounds on the type parameters. -impl<S, N, E, W> Layer<S, N, E, W> { - /// Sets the [`MakeWriter`] that the layer being built will use to write events. - /// - /// # Examples - /// - /// Using `stderr` rather than `stdout`: - /// - /// ```rust - /// use std::io; - /// use tracing_subscriber::fmt; - /// - /// let layer = fmt::layer() - /// .with_writer(io::stderr); - /// # // this is necessary for type inference. - /// # use tracing_subscriber::Layer as _; - /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); - /// ``` - pub fn with_writer<W2>(self, make_writer: W2) -> Layer<S, N, E, W2> - where - W2: for<'writer> MakeWriter<'writer> + 'static, - { - Layer { - fmt_fields: self.fmt_fields, - fmt_event: self.fmt_event, - fmt_span: self.fmt_span, - is_ansi: self.is_ansi, - make_writer, - _inner: self._inner, - } - } - - /// Borrows the [writer] for this [`Layer`]. - /// - /// [writer]: MakeWriter - pub fn writer(&self) -> &W { - &self.make_writer - } - - /// Mutably borrows the [writer] for this [`Layer`]. - /// - /// This method is primarily expected to be used with the - /// [`reload::Handle::modify`](crate::reload::Handle::modify) method. - /// - /// # Examples - /// - /// ``` - /// # use tracing::info; - /// # use tracing_subscriber::{fmt,reload,Registry,prelude::*}; - /// # fn non_blocking<T: std::io::Write>(writer: T) -> (fn() -> std::io::Stdout) { - /// # std::io::stdout - /// # } - /// # fn main() { - /// let layer = fmt::layer().with_writer(non_blocking(std::io::stderr())); - /// let (layer, reload_handle) = reload::Layer::new(layer); - /// # - /// # // specifying the Registry type is required - /// # let _: &reload::Handle<fmt::Layer<Registry, _, _, _>, Registry> = &reload_handle; - /// # - /// info!("This will be logged to stderr"); - /// reload_handle.modify(|layer| *layer.writer_mut() = non_blocking(std::io::stdout())); - /// info!("This will be logged to stdout"); - /// # } - /// ``` - /// - /// [writer]: MakeWriter - pub fn writer_mut(&mut self) -> &mut W { - &mut self.make_writer - } - - /// Sets whether this layer should use ANSI terminal formatting - /// escape codes (such as colors). - /// - /// This method is primarily expected to be used with the - /// [`reload::Handle::modify`](crate::reload::Handle::modify) method when changing - /// the writer. - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] - pub fn set_ansi(&mut self, ansi: bool) { - self.is_ansi = ansi; - } - - /// Configures the layer to support [`libtest`'s output capturing][capturing] when used in - /// unit tests. - /// - /// See [`TestWriter`] for additional details. - /// - /// # Examples - /// - /// Using [`TestWriter`] to let `cargo test` capture test output: - /// - /// ```rust - /// use std::io; - /// use tracing_subscriber::fmt; - /// - /// let layer = fmt::layer() - /// .with_test_writer(); - /// # // this is necessary for type inference. - /// # use tracing_subscriber::Layer as _; - /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); - /// ``` - /// [capturing]: - /// https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output - /// [`TestWriter`]: super::writer::TestWriter - pub fn with_test_writer(self) -> Layer<S, N, E, TestWriter> { - Layer { - fmt_fields: self.fmt_fields, - fmt_event: self.fmt_event, - fmt_span: self.fmt_span, - is_ansi: self.is_ansi, - make_writer: TestWriter::default(), - _inner: self._inner, - } - } - - /// Enable ANSI terminal colors for formatted output. - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] - pub fn with_ansi(self, ansi: bool) -> Self { - Self { - is_ansi: ansi, - ..self - } - } - - /// Updates the [`MakeWriter`] by applying a function to the existing [`MakeWriter`]. - /// - /// This sets the [`MakeWriter`] that the layer being built will use to write events. - /// - /// # Examples - /// - /// Redirect output to stderr if level is <= WARN: - /// - /// ```rust - /// use tracing::Level; - /// use tracing_subscriber::fmt::{self, writer::MakeWriterExt}; - /// - /// let stderr = std::io::stderr.with_max_level(Level::WARN); - /// let layer = fmt::layer() - /// .map_writer(move |w| stderr.or_else(w)); - /// # // this is necessary for type inference. - /// # use tracing_subscriber::Layer as _; - /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); - /// ``` - pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> Layer<S, N, E, W2> - where - W2: for<'writer> MakeWriter<'writer> + 'static, - { - Layer { - fmt_fields: self.fmt_fields, - fmt_event: self.fmt_event, - fmt_span: self.fmt_span, - is_ansi: self.is_ansi, - make_writer: f(self.make_writer), - _inner: self._inner, - } - } -} - -impl<S, N, L, T, W> Layer<S, N, format::Format<L, T>, W> -where - N: for<'writer> FormatFields<'writer> + 'static, -{ - /// Use the given [`timer`] for span and event timestamps. - /// - /// See the [`time` module] for the provided timer implementations. - /// - /// Note that using the `"time`"" feature flag enables the - /// additional time formatters [`UtcTime`] and [`LocalTime`], which use the - /// [`time` crate] to provide more sophisticated timestamp formatting - /// options. - /// - /// [`timer`]: super::time::FormatTime - /// [`time` module]: mod@super::time - /// [`UtcTime`]: super::time::UtcTime - /// [`LocalTime`]: super::time::LocalTime - /// [`time` crate]: https://docs.rs/time/0.3 - pub fn with_timer<T2>(self, timer: T2) -> Layer<S, N, format::Format<L, T2>, W> { - Layer { - fmt_event: self.fmt_event.with_timer(timer), - fmt_fields: self.fmt_fields, - fmt_span: self.fmt_span, - make_writer: self.make_writer, - is_ansi: self.is_ansi, - _inner: self._inner, - } - } - - /// Do not emit timestamps with spans and event. - pub fn without_time(self) -> Layer<S, N, format::Format<L, ()>, W> { - Layer { - fmt_event: self.fmt_event.without_time(), - fmt_fields: self.fmt_fields, - fmt_span: self.fmt_span.without_time(), - make_writer: self.make_writer, - is_ansi: self.is_ansi, - _inner: self._inner, - } - } - - /// Configures how synthesized events are emitted at points in the [span - /// lifecycle][lifecycle]. - /// - /// The following options are available: - /// - /// - `FmtSpan::NONE`: No events will be synthesized when spans are - /// created, entered, exited, or closed. Data from spans will still be - /// included as the context for formatted events. This is the default. - /// - `FmtSpan::NEW`: An event will be synthesized when spans are created. - /// - `FmtSpan::ENTER`: An event will be synthesized when spans are entered. - /// - `FmtSpan::EXIT`: An event will be synthesized when spans are exited. - /// - `FmtSpan::CLOSE`: An event will be synthesized when a span closes. If - /// [timestamps are enabled][time] for this formatter, the generated - /// event will contain fields with the span's _busy time_ (the total - /// time for which it was entered) and _idle time_ (the total time that - /// the span existed but was not entered). - /// - `FmtSpan::ACTIVE`: Events will be synthesized when spans are entered - /// or exited. - /// - `FmtSpan::FULL`: Events will be synthesized whenever a span is - /// created, entered, exited, or closed. If timestamps are enabled, the - /// close event will contain the span's busy and idle time, as - /// described above. - /// - /// The options can be enabled in any combination. For instance, the following - /// will synthesize events whenever spans are created and closed: - /// - /// ```rust - /// use tracing_subscriber::fmt; - /// use tracing_subscriber::fmt::format::FmtSpan; - /// - /// let subscriber = fmt() - /// .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) - /// .finish(); - /// ``` - /// - /// Note that the generated events will only be part of the log output by - /// this formatter; they will not be recorded by other `Subscriber`s or by - /// `Layer`s added to this subscriber. - /// - /// [lifecycle]: https://docs.rs/tracing/latest/tracing/span/index.html#the-span-lifecycle - /// [time]: Layer::without_time() - pub fn with_span_events(self, kind: FmtSpan) -> Self { - Layer { - fmt_span: self.fmt_span.with_kind(kind), - ..self - } - } - - /// Sets whether or not an event's target is displayed. - pub fn with_target(self, display_target: bool) -> Layer<S, N, format::Format<L, T>, W> { - Layer { - fmt_event: self.fmt_event.with_target(display_target), - ..self - } - } - /// Sets whether or not an event's [source code file path][file] is - /// displayed. - /// - /// [file]: tracing_core::Metadata::file - pub fn with_file(self, display_filename: bool) -> Layer<S, N, format::Format<L, T>, W> { - Layer { - fmt_event: self.fmt_event.with_file(display_filename), - ..self - } - } - - /// Sets whether or not an event's [source code line number][line] is - /// displayed. - /// - /// [line]: tracing_core::Metadata::line - pub fn with_line_number( - self, - display_line_number: bool, - ) -> Layer<S, N, format::Format<L, T>, W> { - Layer { - fmt_event: self.fmt_event.with_line_number(display_line_number), - ..self - } - } - - /// Sets whether or not an event's level is displayed. - pub fn with_level(self, display_level: bool) -> Layer<S, N, format::Format<L, T>, W> { - Layer { - fmt_event: self.fmt_event.with_level(display_level), - ..self - } - } - - /// Sets whether or not the [thread ID] of the current thread is displayed - /// when formatting events. - /// - /// [thread ID]: std::thread::ThreadId - pub fn with_thread_ids(self, display_thread_ids: bool) -> Layer<S, N, format::Format<L, T>, W> { - Layer { - fmt_event: self.fmt_event.with_thread_ids(display_thread_ids), - ..self - } - } - - /// Sets whether or not the [name] of the current thread is displayed - /// when formatting events. - /// - /// [name]: std::thread#naming-threads - pub fn with_thread_names( - self, - display_thread_names: bool, - ) -> Layer<S, N, format::Format<L, T>, W> { - Layer { - fmt_event: self.fmt_event.with_thread_names(display_thread_names), - ..self - } - } - - /// Sets the layer being built to use a [less verbose formatter][super::format::Compact]. - pub fn compact(self) -> Layer<S, N, format::Format<format::Compact, T>, W> - where - N: for<'writer> FormatFields<'writer> + 'static, - { - Layer { - fmt_event: self.fmt_event.compact(), - fmt_fields: self.fmt_fields, - fmt_span: self.fmt_span, - make_writer: self.make_writer, - is_ansi: self.is_ansi, - _inner: self._inner, - } - } - - /// Sets the layer being built to use an [excessively pretty, human-readable formatter](crate::fmt::format::Pretty). - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] - pub fn pretty(self) -> Layer<S, format::Pretty, format::Format<format::Pretty, T>, W> { - Layer { - fmt_event: self.fmt_event.pretty(), - fmt_fields: format::Pretty::default(), - fmt_span: self.fmt_span, - make_writer: self.make_writer, - is_ansi: self.is_ansi, - _inner: self._inner, - } - } - - /// Sets the layer being built to use a [JSON formatter][super::format::Json]. - /// - /// The full format includes fields from all entered spans. - /// - /// # Example Output - /// - /// ```ignore,json - /// {"timestamp":"Feb 20 11:28:15.096","level":"INFO","target":"mycrate","fields":{"message":"some message", "key": "value"}} - /// ``` - /// - /// # Options - /// - /// - [`Layer::flatten_event`] can be used to enable flattening event fields into the root - /// object. - /// - /// [`Layer::flatten_event`]: Layer::flatten_event() - #[cfg(feature = "json")] - #[cfg_attr(docsrs, doc(cfg(feature = "json")))] - pub fn json(self) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { - Layer { - fmt_event: self.fmt_event.json(), - fmt_fields: format::JsonFields::new(), - fmt_span: self.fmt_span, - make_writer: self.make_writer, - // always disable ANSI escapes in JSON mode! - is_ansi: false, - _inner: self._inner, - } - } -} - -#[cfg(feature = "json")] -#[cfg_attr(docsrs, doc(cfg(feature = "json")))] -impl<S, T, W> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { - /// Sets the JSON layer being built to flatten event metadata. - /// - /// See [`format::Json`][super::format::Json] - pub fn flatten_event( - self, - flatten_event: bool, - ) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { - Layer { - fmt_event: self.fmt_event.flatten_event(flatten_event), - fmt_fields: format::JsonFields::new(), - ..self - } - } - - /// Sets whether or not the formatter will include the current span in - /// formatted events. - /// - /// See [`format::Json`][super::format::Json] - pub fn with_current_span( - self, - display_current_span: bool, - ) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { - Layer { - fmt_event: self.fmt_event.with_current_span(display_current_span), - fmt_fields: format::JsonFields::new(), - ..self - } - } - - /// Sets whether or not the formatter will include a list (from root to leaf) - /// of all currently entered spans in formatted events. - /// - /// See [`format::Json`][super::format::Json] - pub fn with_span_list( - self, - display_span_list: bool, - ) -> Layer<S, format::JsonFields, format::Format<format::Json, T>, W> { - Layer { - fmt_event: self.fmt_event.with_span_list(display_span_list), - fmt_fields: format::JsonFields::new(), - ..self - } - } -} - -impl<S, N, E, W> Layer<S, N, E, W> { - /// Sets the field formatter that the layer being built will use to record - /// fields. - pub fn fmt_fields<N2>(self, fmt_fields: N2) -> Layer<S, N2, E, W> - where - N2: for<'writer> FormatFields<'writer> + 'static, - { - Layer { - fmt_event: self.fmt_event, - fmt_fields, - fmt_span: self.fmt_span, - make_writer: self.make_writer, - is_ansi: self.is_ansi, - _inner: self._inner, - } - } - - /// Updates the field formatter by applying a function to the existing field formatter. - /// - /// This sets the field formatter that the layer being built will use to record fields. - /// - /// # Examples - /// - /// Updating a field formatter: - /// - /// ```rust - /// use tracing_subscriber::field::MakeExt; - /// let layer = tracing_subscriber::fmt::layer() - /// .map_fmt_fields(|f| f.debug_alt()); - /// # // this is necessary for type inference. - /// # use tracing_subscriber::Layer as _; - /// # let _ = layer.with_subscriber(tracing_subscriber::registry::Registry::default()); - /// ``` - pub fn map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> Layer<S, N2, E, W> - where - N2: for<'writer> FormatFields<'writer> + 'static, - { - Layer { - fmt_event: self.fmt_event, - fmt_fields: f(self.fmt_fields), - fmt_span: self.fmt_span, - make_writer: self.make_writer, - is_ansi: self.is_ansi, - _inner: self._inner, - } - } -} - -impl<S> Default for Layer<S> { - fn default() -> Self { - Layer { - fmt_fields: format::DefaultFields::default(), - fmt_event: format::Format::default(), - fmt_span: format::FmtSpanConfig::default(), - make_writer: io::stdout, - is_ansi: cfg!(feature = "ansi"), - _inner: PhantomData, - } - } -} - -impl<S, N, E, W> Layer<S, N, E, W> -where - S: Subscriber + for<'a> LookupSpan<'a>, - N: for<'writer> FormatFields<'writer> + 'static, - E: FormatEvent<S, N> + 'static, - W: for<'writer> MakeWriter<'writer> + 'static, -{ - #[inline] - fn make_ctx<'a>(&'a self, ctx: Context<'a, S>, event: &'a Event<'a>) -> FmtContext<'a, S, N> { - FmtContext { - ctx, - fmt_fields: &self.fmt_fields, - event, - } - } -} - -/// A formatted representation of a span's fields stored in its [extensions]. -/// -/// Because `FormattedFields` is generic over the type of the formatter that -/// produced it, multiple versions of a span's formatted fields can be stored in -/// the [`Extensions`][extensions] type-map. This means that when multiple -/// formatters are in use, each can store its own formatted representation -/// without conflicting. -/// -/// [extensions]: crate::registry::Extensions -#[derive(Default)] -pub struct FormattedFields<E: ?Sized> { - _format_fields: PhantomData<fn(E)>, - was_ansi: bool, - /// The formatted fields of a span. - pub fields: String, -} - -impl<E: ?Sized> FormattedFields<E> { - /// Returns a new `FormattedFields`. - pub fn new(fields: String) -> Self { - Self { - fields, - was_ansi: false, - _format_fields: PhantomData, - } - } - - /// Returns a new [`format::Writer`] for writing to this `FormattedFields`. - /// - /// The returned [`format::Writer`] can be used with the - /// [`FormatFields::format_fields`] method. - pub fn as_writer(&mut self) -> format::Writer<'_> { - format::Writer::new(&mut self.fields).with_ansi(self.was_ansi) - } -} - -impl<E: ?Sized> fmt::Debug for FormattedFields<E> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("FormattedFields") - .field("fields", &self.fields) - .field("formatter", &format_args!("{}", std::any::type_name::<E>())) - .field("was_ansi", &self.was_ansi) - .finish() - } -} - -impl<E: ?Sized> fmt::Display for FormattedFields<E> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&self.fields, f) - } -} - -impl<E: ?Sized> Deref for FormattedFields<E> { - type Target = String; - fn deref(&self) -> &Self::Target { - &self.fields - } -} - -// === impl FmtLayer === - -macro_rules! with_event_from_span { - ($id:ident, $span:ident, $($field:literal = $value:expr),*, |$event:ident| $code:block) => { - let meta = $span.metadata(); - let cs = meta.callsite(); - let fs = field::FieldSet::new(&[$($field),*], cs); - #[allow(unused)] - let mut iter = fs.iter(); - let v = [$( - (&iter.next().unwrap(), Some(&$value as &dyn field::Value)), - )*]; - let vs = fs.value_set(&v); - let $event = Event::new_child_of($id, meta, &vs); - $code - }; -} - -impl<S, N, E, W> layer::Layer<S> for Layer<S, N, E, W> -where - S: Subscriber + for<'a> LookupSpan<'a>, - N: for<'writer> FormatFields<'writer> + 'static, - E: FormatEvent<S, N> + 'static, - W: for<'writer> MakeWriter<'writer> + 'static, -{ - fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) { - let span = ctx.span(id).expect("Span not found, this is a bug"); - let mut extensions = span.extensions_mut(); - - if extensions.get_mut::<FormattedFields<N>>().is_none() { - let mut fields = FormattedFields::<N>::new(String::new()); - if self - .fmt_fields - .format_fields(fields.as_writer().with_ansi(self.is_ansi), attrs) - .is_ok() - { - fields.was_ansi = self.is_ansi; - extensions.insert(fields); - } - } - - if self.fmt_span.fmt_timing - && self.fmt_span.trace_close() - && extensions.get_mut::<Timings>().is_none() - { - extensions.insert(Timings::new()); - } - - if self.fmt_span.trace_new() { - with_event_from_span!(id, span, "message" = "new", |event| { - drop(extensions); - drop(span); - self.on_event(&event, ctx); - }); - } - } - - fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) { - let span = ctx.span(id).expect("Span not found, this is a bug"); - let mut extensions = span.extensions_mut(); - if let Some(fields) = extensions.get_mut::<FormattedFields<N>>() { - let _ = self.fmt_fields.add_fields(fields, values); - return; - } - - let mut fields = FormattedFields::<N>::new(String::new()); - if self - .fmt_fields - .format_fields(fields.as_writer().with_ansi(self.is_ansi), values) - .is_ok() - { - fields.was_ansi = self.is_ansi; - extensions.insert(fields); - } - } - - fn on_enter(&self, id: &Id, ctx: Context<'_, S>) { - if self.fmt_span.trace_enter() || self.fmt_span.trace_close() && self.fmt_span.fmt_timing { - let span = ctx.span(id).expect("Span not found, this is a bug"); - let mut extensions = span.extensions_mut(); - if let Some(timings) = extensions.get_mut::<Timings>() { - let now = Instant::now(); - timings.idle += (now - timings.last).as_nanos() as u64; - timings.last = now; - } - - if self.fmt_span.trace_enter() { - with_event_from_span!(id, span, "message" = "enter", |event| { - drop(extensions); - drop(span); - self.on_event(&event, ctx); - }); - } - } - } - - fn on_exit(&self, id: &Id, ctx: Context<'_, S>) { - if self.fmt_span.trace_exit() || self.fmt_span.trace_close() && self.fmt_span.fmt_timing { - let span = ctx.span(id).expect("Span not found, this is a bug"); - let mut extensions = span.extensions_mut(); - if let Some(timings) = extensions.get_mut::<Timings>() { - let now = Instant::now(); - timings.busy += (now - timings.last).as_nanos() as u64; - timings.last = now; - } - - if self.fmt_span.trace_exit() { - with_event_from_span!(id, span, "message" = "exit", |event| { - drop(extensions); - drop(span); - self.on_event(&event, ctx); - }); - } - } - } - - fn on_close(&self, id: Id, ctx: Context<'_, S>) { - if self.fmt_span.trace_close() { - let span = ctx.span(&id).expect("Span not found, this is a bug"); - let extensions = span.extensions(); - if let Some(timing) = extensions.get::<Timings>() { - let Timings { - busy, - mut idle, - last, - } = *timing; - idle += (Instant::now() - last).as_nanos() as u64; - - let t_idle = field::display(TimingDisplay(idle)); - let t_busy = field::display(TimingDisplay(busy)); - - with_event_from_span!( - id, - span, - "message" = "close", - "time.busy" = t_busy, - "time.idle" = t_idle, - |event| { - drop(extensions); - drop(span); - self.on_event(&event, ctx); - } - ); - } else { - with_event_from_span!(id, span, "message" = "close", |event| { - drop(extensions); - drop(span); - self.on_event(&event, ctx); - }); - } - } - } - - fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) { - thread_local! { - static BUF: RefCell<String> = RefCell::new(String::new()); - } - - BUF.with(|buf| { - let borrow = buf.try_borrow_mut(); - let mut a; - let mut b; - let mut buf = match borrow { - Ok(buf) => { - a = buf; - &mut *a - } - _ => { - b = String::new(); - &mut b - } - }; - - let ctx = self.make_ctx(ctx, event); - if self - .fmt_event - .format_event( - &ctx, - format::Writer::new(&mut buf).with_ansi(self.is_ansi), - event, - ) - .is_ok() - { - let mut writer = self.make_writer.make_writer_for(event.metadata()); - let _ = io::Write::write_all(&mut writer, buf.as_bytes()); - } - - buf.clear(); - }); - } - - unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { - // This `downcast_raw` impl allows downcasting a `fmt` layer to any of - // its components (event formatter, field formatter, and `MakeWriter`) - // as well as to the layer's type itself. The potential use-cases for - // this *may* be somewhat niche, though... - match () { - _ if id == TypeId::of::<Self>() => Some(self as *const Self as *const ()), - _ if id == TypeId::of::<E>() => Some(&self.fmt_event as *const E as *const ()), - _ if id == TypeId::of::<N>() => Some(&self.fmt_fields as *const N as *const ()), - _ if id == TypeId::of::<W>() => Some(&self.make_writer as *const W as *const ()), - _ => None, - } - } -} - -/// Provides the current span context to a formatter. -pub struct FmtContext<'a, S, N> { - pub(crate) ctx: Context<'a, S>, - pub(crate) fmt_fields: &'a N, - pub(crate) event: &'a Event<'a>, -} - -impl<'a, S, N> fmt::Debug for FmtContext<'a, S, N> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("FmtContext").finish() - } -} - -impl<'cx, 'writer, S, N> FormatFields<'writer> for FmtContext<'cx, S, N> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: FormatFields<'writer> + 'static, -{ - fn format_fields<R: RecordFields>( - &self, - writer: format::Writer<'writer>, - fields: R, - ) -> fmt::Result { - self.fmt_fields.format_fields(writer, fields) - } -} - -impl<'a, S, N> FmtContext<'a, S, N> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - /// Visits every span in the current context with a closure. - /// - /// The provided closure will be called first with the current span, - /// and then with that span's parent, and then that span's parent, - /// and so on until a root span is reached. - pub fn visit_spans<E, F>(&self, mut f: F) -> Result<(), E> - where - F: FnMut(&SpanRef<'_, S>) -> Result<(), E>, - { - // visit all the current spans - if let Some(scope) = self.event_scope() { - for span in scope.from_root() { - f(&span)?; - } - } - Ok(()) - } - - /// Returns metadata for the span with the given `id`, if it exists. - /// - /// If this returns `None`, then no span exists for that ID (either it has - /// closed or the ID is invalid). - #[inline] - pub fn metadata(&self, id: &Id) -> Option<&'static Metadata<'static>> - where - S: for<'lookup> LookupSpan<'lookup>, - { - self.ctx.metadata(id) - } - - /// Returns [stored data] for the span with the given `id`, if it exists. - /// - /// If this returns `None`, then no span exists for that ID (either it has - /// closed or the ID is invalid). - /// - /// [stored data]: crate::registry::SpanRef - #[inline] - pub fn span(&self, id: &Id) -> Option<SpanRef<'_, S>> - where - S: for<'lookup> LookupSpan<'lookup>, - { - self.ctx.span(id) - } - - /// Returns `true` if an active span exists for the given `Id`. - #[inline] - pub fn exists(&self, id: &Id) -> bool - where - S: for<'lookup> LookupSpan<'lookup>, - { - self.ctx.exists(id) - } - - /// Returns [stored data] for the span that the wrapped subscriber considers - /// to be the current. - /// - /// If this returns `None`, then we are not currently within a span. - /// - /// [stored data]: crate::registry::SpanRef - #[inline] - pub fn lookup_current(&self) -> Option<SpanRef<'_, S>> - where - S: for<'lookup> LookupSpan<'lookup>, - { - self.ctx.lookup_current() - } - - /// Returns the current span for this formatter. - pub fn current_span(&self) -> Current { - self.ctx.current_span() - } - - /// Returns [stored data] for the parent span of the event currently being - /// formatted. - /// - /// If the event has a contextual parent, this will return the current span. If - /// the event has an explicit parent span, this will return that span. If - /// the event does not have a parent span, this will return `None`. - /// - /// [stored data]: SpanRef - pub fn parent_span(&self) -> Option<SpanRef<'_, S>> { - self.ctx.event_span(self.event) - } - - /// Returns an iterator over the [stored data] for all the spans in the - /// current context, starting with the specified span and ending with the - /// root of the trace tree and ending with the current span. - /// - /// This is equivalent to the [`Context::span_scope`] method. - /// - /// <div class="information"> - /// <div class="tooltip ignore" style="">ⓘ<span class="tooltiptext">Note</span></div> - /// </div> - /// <div class="example-wrap" style="display:inline-block"> - /// <pre class="ignore" style="white-space:normal;font:inherit;"> - /// <strong>Note</strong>: Compared to <a href="#method.scope"><code>scope</code></a> this - /// returns the spans in reverse order (from leaf to root). Use - /// <a href="../registry/struct.Scope.html#method.from_root"><code>Scope::from_root</code></a> - /// in case root-to-leaf ordering is desired. - /// </pre></div> - /// - /// <div class="example-wrap" style="display:inline-block"> - /// <pre class="ignore" style="white-space:normal;font:inherit;"> - /// <strong>Note</strong>: This requires the wrapped subscriber to implement the - /// <a href="../registry/trait.LookupSpan.html"><code>LookupSpan</code></a> trait. - /// See the documentation on <a href="./struct.Context.html"><code>Context</code>'s - /// declaration</a> for details. - /// </pre></div> - /// - /// [stored data]: crate::registry::SpanRef - pub fn span_scope(&self, id: &Id) -> Option<registry::Scope<'_, S>> - where - S: for<'lookup> LookupSpan<'lookup>, - { - self.ctx.span_scope(id) - } - - /// Returns an iterator over the [stored data] for all the spans in the - /// event's span context, starting with its parent span and ending with the - /// root of the trace tree. - /// - /// This is equivalent to calling the [`Context::event_scope`] method and - /// passing the event currently being formatted. - /// - /// <div class="example-wrap" style="display:inline-block"> - /// <pre class="ignore" style="white-space:normal;font:inherit;"> - /// <strong>Note</strong>: Compared to <a href="#method.scope"><code>scope</code></a> this - /// returns the spans in reverse order (from leaf to root). Use - /// <a href="../registry/struct.Scope.html#method.from_root"><code>Scope::from_root</code></a> - /// in case root-to-leaf ordering is desired. - /// </pre></div> - /// - /// <div class="example-wrap" style="display:inline-block"> - /// <pre class="ignore" style="white-space:normal;font:inherit;"> - /// <strong>Note</strong>: This requires the wrapped subscriber to implement the - /// <a href="../registry/trait.LookupSpan.html"><code>LookupSpan</code></a> trait. - /// See the documentation on <a href="./struct.Context.html"><code>Context</code>'s - /// declaration</a> for details. - /// </pre></div> - /// - /// [stored data]: crate::registry::SpanRef - pub fn event_scope(&self) -> Option<registry::Scope<'_, S>> - where - S: for<'lookup> registry::LookupSpan<'lookup>, - { - self.ctx.event_scope(self.event) - } - - /// Returns the [field formatter] configured by the subscriber invoking - /// `format_event`. - /// - /// The event formatter may use the returned field formatter to format the - /// fields of any events it records. - /// - /// [field formatter]: FormatFields - pub fn field_format(&self) -> &N { - self.fmt_fields - } -} - -struct Timings { - idle: u64, - busy: u64, - last: Instant, -} - -impl Timings { - fn new() -> Self { - Self { - idle: 0, - busy: 0, - last: Instant::now(), - } - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::fmt::{ - self, - format::{self, test::MockTime, Format}, - layer::Layer as _, - test::{MockMakeWriter, MockWriter}, - time, - }; - use crate::Registry; - use format::FmtSpan; - use regex::Regex; - use tracing::subscriber::with_default; - use tracing_core::dispatcher::Dispatch; - - #[test] - fn impls() { - let f = Format::default().with_timer(time::Uptime::default()); - let fmt = fmt::Layer::default().event_format(f); - let subscriber = fmt.with_subscriber(Registry::default()); - let _dispatch = Dispatch::new(subscriber); - - let f = format::Format::default(); - let fmt = fmt::Layer::default().event_format(f); - let subscriber = fmt.with_subscriber(Registry::default()); - let _dispatch = Dispatch::new(subscriber); - - let f = format::Format::default().compact(); - let fmt = fmt::Layer::default().event_format(f); - let subscriber = fmt.with_subscriber(Registry::default()); - let _dispatch = Dispatch::new(subscriber); - } - - #[test] - fn fmt_layer_downcasts() { - let f = format::Format::default(); - let fmt = fmt::Layer::default().event_format(f); - let subscriber = fmt.with_subscriber(Registry::default()); - - let dispatch = Dispatch::new(subscriber); - assert!(dispatch.downcast_ref::<fmt::Layer<Registry>>().is_some()); - } - - #[test] - fn fmt_layer_downcasts_to_parts() { - let f = format::Format::default(); - let fmt = fmt::Layer::default().event_format(f); - let subscriber = fmt.with_subscriber(Registry::default()); - let dispatch = Dispatch::new(subscriber); - assert!(dispatch.downcast_ref::<format::DefaultFields>().is_some()); - assert!(dispatch.downcast_ref::<format::Format>().is_some()) - } - - #[test] - fn is_lookup_span() { - fn assert_lookup_span<T: for<'a> crate::registry::LookupSpan<'a>>(_: T) {} - let fmt = fmt::Layer::default(); - let subscriber = fmt.with_subscriber(Registry::default()); - assert_lookup_span(subscriber) - } - - fn sanitize_timings(s: String) -> String { - let re = Regex::new("time\\.(idle|busy)=([0-9.]+)[mµn]s").unwrap(); - re.replace_all(s.as_str(), "timing").to_string() - } - - #[test] - fn synthesize_span_none() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime) - // check that FmtSpan::NONE is the default - .finish(); - - with_default(subscriber, || { - let span1 = tracing::info_span!("span1", x = 42); - let _e = span1.enter(); - }); - let actual = sanitize_timings(make_writer.get_string()); - assert_eq!("", actual.as_str()); - } - - #[test] - fn synthesize_span_active() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime) - .with_span_events(FmtSpan::ACTIVE) - .finish(); - - with_default(subscriber, || { - let span1 = tracing::info_span!("span1", x = 42); - let _e = span1.enter(); - }); - let actual = sanitize_timings(make_writer.get_string()); - assert_eq!( - "fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: enter\n\ - fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: exit\n", - actual.as_str() - ); - } - - #[test] - fn synthesize_span_close() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime) - .with_span_events(FmtSpan::CLOSE) - .finish(); - - with_default(subscriber, || { - let span1 = tracing::info_span!("span1", x = 42); - let _e = span1.enter(); - }); - let actual = sanitize_timings(make_writer.get_string()); - assert_eq!( - "fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: close timing timing\n", - actual.as_str() - ); - } - - #[test] - fn synthesize_span_close_no_timing() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime) - .without_time() - .with_span_events(FmtSpan::CLOSE) - .finish(); - - with_default(subscriber, || { - let span1 = tracing::info_span!("span1", x = 42); - let _e = span1.enter(); - }); - let actual = sanitize_timings(make_writer.get_string()); - assert_eq!( - "span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: close\n", - actual.as_str() - ); - } - - #[test] - fn synthesize_span_full() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime) - .with_span_events(FmtSpan::FULL) - .finish(); - - with_default(subscriber, || { - let span1 = tracing::info_span!("span1", x = 42); - let _e = span1.enter(); - }); - let actual = sanitize_timings(make_writer.get_string()); - assert_eq!( - "fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: new\n\ - fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: enter\n\ - fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: exit\n\ - fake time span1{x=42}: tracing_subscriber::fmt::fmt_layer::test: close timing timing\n", - actual.as_str() - ); - } - - #[test] - fn make_writer_based_on_meta() { - struct MakeByTarget { - make_writer1: MockMakeWriter, - make_writer2: MockMakeWriter, - } - - impl<'a> MakeWriter<'a> for MakeByTarget { - type Writer = MockWriter; - - fn make_writer(&'a self) -> Self::Writer { - self.make_writer1.make_writer() - } - - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - if meta.target() == "writer2" { - return self.make_writer2.make_writer(); - } - self.make_writer() - } - } - - let make_writer1 = MockMakeWriter::default(); - let make_writer2 = MockMakeWriter::default(); - - let make_writer = MakeByTarget { - make_writer1: make_writer1.clone(), - make_writer2: make_writer2.clone(), - }; - - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer) - .with_level(false) - .with_target(false) - .with_ansi(false) - .with_timer(MockTime) - .with_span_events(FmtSpan::CLOSE) - .finish(); - - with_default(subscriber, || { - let span1 = tracing::info_span!("writer1_span", x = 42); - let _e = span1.enter(); - tracing::info!(target: "writer2", "hello writer2!"); - let span2 = tracing::info_span!(target: "writer2", "writer2_span"); - let _e = span2.enter(); - tracing::warn!(target: "writer1", "hello writer1!"); - }); - - let actual = sanitize_timings(make_writer1.get_string()); - assert_eq!( - "fake time writer1_span{x=42}:writer2_span: hello writer1!\n\ - fake time writer1_span{x=42}: close timing timing\n", - actual.as_str() - ); - let actual = sanitize_timings(make_writer2.get_string()); - assert_eq!( - "fake time writer1_span{x=42}: hello writer2!\n\ - fake time writer1_span{x=42}:writer2_span: close timing timing\n", - actual.as_str() - ); - } -} diff --git a/vendor/tracing-subscriber/src/fmt/format/json.rs b/vendor/tracing-subscriber/src/fmt/format/json.rs deleted file mode 100644 index c2f4d3755..000000000 --- a/vendor/tracing-subscriber/src/fmt/format/json.rs +++ /dev/null @@ -1,885 +0,0 @@ -use super::{Format, FormatEvent, FormatFields, FormatTime, Writer}; -use crate::{ - field::{RecordFields, VisitOutput}, - fmt::{ - fmt_layer::{FmtContext, FormattedFields}, - writer::WriteAdaptor, - }, - registry::LookupSpan, -}; -use serde::ser::{SerializeMap, Serializer as _}; -use serde_json::Serializer; -use std::{ - collections::BTreeMap, - fmt::{self, Write}, -}; -use tracing_core::{ - field::{self, Field}, - span::Record, - Event, Subscriber, -}; -use tracing_serde::AsSerde; - -#[cfg(feature = "tracing-log")] -use tracing_log::NormalizeEvent; - -/// Marker for [`Format`] that indicates that the newline-delimited JSON log -/// format should be used. -/// -/// This formatter is intended for production use with systems where structured -/// logs are consumed as JSON by analysis and viewing tools. The JSON output is -/// not optimized for human readability; instead, it should be pretty-printed -/// using external JSON tools such as `jq`, or using a JSON log viewer. -/// -/// # Example Output -/// -/// <pre><font color="#4E9A06"><b>:;</b></font> <font color="#4E9A06">cargo</font> run --example fmt-json -/// <font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 0.08s -/// <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt-json` -/// {"timestamp":"2022-02-15T18:47:10.821315Z","level":"INFO","fields":{"message":"preparing to shave yaks","number_of_yaks":3},"target":"fmt_json"} -/// {"timestamp":"2022-02-15T18:47:10.821422Z","level":"INFO","fields":{"message":"shaving yaks"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} -/// {"timestamp":"2022-02-15T18:47:10.821495Z","level":"TRACE","fields":{"message":"hello! I'm gonna shave a yak","excitement":"yay!"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":1,"name":"shave"}]} -/// {"timestamp":"2022-02-15T18:47:10.821546Z","level":"TRACE","fields":{"message":"yak shaved successfully"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":1,"name":"shave"}]} -/// {"timestamp":"2022-02-15T18:47:10.821598Z","level":"DEBUG","fields":{"yak":1,"shaved":true},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} -/// {"timestamp":"2022-02-15T18:47:10.821637Z","level":"TRACE","fields":{"yaks_shaved":1},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} -/// {"timestamp":"2022-02-15T18:47:10.821684Z","level":"TRACE","fields":{"message":"hello! I'm gonna shave a yak","excitement":"yay!"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":2,"name":"shave"}]} -/// {"timestamp":"2022-02-15T18:47:10.821727Z","level":"TRACE","fields":{"message":"yak shaved successfully"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":2,"name":"shave"}]} -/// {"timestamp":"2022-02-15T18:47:10.821773Z","level":"DEBUG","fields":{"yak":2,"shaved":true},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} -/// {"timestamp":"2022-02-15T18:47:10.821806Z","level":"TRACE","fields":{"yaks_shaved":2},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} -/// {"timestamp":"2022-02-15T18:47:10.821909Z","level":"TRACE","fields":{"message":"hello! I'm gonna shave a yak","excitement":"yay!"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":3,"name":"shave"}]} -/// {"timestamp":"2022-02-15T18:47:10.821956Z","level":"WARN","fields":{"message":"could not locate yak"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"},{"yak":3,"name":"shave"}]} -/// {"timestamp":"2022-02-15T18:47:10.822006Z","level":"DEBUG","fields":{"yak":3,"shaved":false},"target":"yak_events","spans":[{"yaks":3,"name":"shaving_yaks"}]} -/// {"timestamp":"2022-02-15T18:47:10.822041Z","level":"ERROR","fields":{"message":"failed to shave yak","yak":3,"error":"missing yak"},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} -/// {"timestamp":"2022-02-15T18:47:10.822079Z","level":"TRACE","fields":{"yaks_shaved":2},"target":"fmt_json::yak_shave","spans":[{"yaks":3,"name":"shaving_yaks"}]} -/// {"timestamp":"2022-02-15T18:47:10.822117Z","level":"INFO","fields":{"message":"yak shaving completed","all_yaks_shaved":false},"target":"fmt_json"} -/// </pre> -/// -/// # Options -/// -/// This formatter exposes additional options to configure the structure of the -/// output JSON objects: -/// -/// - [`Json::flatten_event`] can be used to enable flattening event fields into -/// the root -/// - [`Json::with_current_span`] can be used to control logging of the current -/// span -/// - [`Json::with_span_list`] can be used to control logging of the span list -/// object. -/// -/// By default, event fields are not flattened, and both current span and span -/// list are logged. -/// -/// # Valuable Support -/// -/// Experimental support is available for using the [`valuable`] crate to record -/// user-defined values as structured JSON. When the ["valuable" unstable -/// feature][unstable] is enabled, types implementing [`valuable::Valuable`] will -/// be recorded as structured JSON, rather than -/// using their [`std::fmt::Debug`] implementations. -/// -/// **Note**: This is an experimental feature. [Unstable features][unstable] -/// must be enabled in order to use `valuable` support. -/// -/// [`Json::flatten_event`]: Json::flatten_event() -/// [`Json::with_current_span`]: Json::with_current_span() -/// [`Json::with_span_list`]: Json::with_span_list() -/// [`valuable`]: https://crates.io/crates/valuable -/// [unstable]: crate#unstable-features -/// [`valuable::Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html -#[derive(Debug, Copy, Clone, Eq, PartialEq)] -pub struct Json { - pub(crate) flatten_event: bool, - pub(crate) display_current_span: bool, - pub(crate) display_span_list: bool, -} - -impl Json { - /// If set to `true` event metadata will be flattened into the root object. - pub fn flatten_event(&mut self, flatten_event: bool) { - self.flatten_event = flatten_event; - } - - /// If set to `false`, formatted events won't contain a field for the current span. - pub fn with_current_span(&mut self, display_current_span: bool) { - self.display_current_span = display_current_span; - } - - /// If set to `false`, formatted events won't contain a list of all currently - /// entered spans. Spans are logged in a list from root to leaf. - pub fn with_span_list(&mut self, display_span_list: bool) { - self.display_span_list = display_span_list; - } -} - -struct SerializableContext<'a, 'b, Span, N>( - &'b crate::layer::Context<'a, Span>, - std::marker::PhantomData<N>, -) -where - Span: Subscriber + for<'lookup> crate::registry::LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static; - -impl<'a, 'b, Span, N> serde::ser::Serialize for SerializableContext<'a, 'b, Span, N> -where - Span: Subscriber + for<'lookup> crate::registry::LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - fn serialize<Ser>(&self, serializer_o: Ser) -> Result<Ser::Ok, Ser::Error> - where - Ser: serde::ser::Serializer, - { - use serde::ser::SerializeSeq; - let mut serializer = serializer_o.serialize_seq(None)?; - - if let Some(leaf_span) = self.0.lookup_current() { - for span in leaf_span.scope().from_root() { - serializer.serialize_element(&SerializableSpan(&span, self.1))?; - } - } - - serializer.end() - } -} - -struct SerializableSpan<'a, 'b, Span, N>( - &'b crate::registry::SpanRef<'a, Span>, - std::marker::PhantomData<N>, -) -where - Span: for<'lookup> crate::registry::LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static; - -impl<'a, 'b, Span, N> serde::ser::Serialize for SerializableSpan<'a, 'b, Span, N> -where - Span: for<'lookup> crate::registry::LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error> - where - Ser: serde::ser::Serializer, - { - let mut serializer = serializer.serialize_map(None)?; - - let ext = self.0.extensions(); - let data = ext - .get::<FormattedFields<N>>() - .expect("Unable to find FormattedFields in extensions; this is a bug"); - - // TODO: let's _not_ do this, but this resolves - // https://github.com/tokio-rs/tracing/issues/391. - // We should probably rework this to use a `serde_json::Value` or something - // similar in a JSON-specific layer, but I'd (david) - // rather have a uglier fix now rather than shipping broken JSON. - match serde_json::from_str::<serde_json::Value>(data) { - Ok(serde_json::Value::Object(fields)) => { - for field in fields { - serializer.serialize_entry(&field.0, &field.1)?; - } - } - // We have fields for this span which are valid JSON but not an object. - // This is probably a bug, so panic if we're in debug mode - Ok(_) if cfg!(debug_assertions) => panic!( - "span '{}' had malformed fields! this is a bug.\n error: invalid JSON object\n fields: {:?}", - self.0.metadata().name(), - data - ), - // If we *aren't* in debug mode, it's probably best not to - // crash the program, let's log the field found but also an - // message saying it's type is invalid - Ok(value) => { - serializer.serialize_entry("field", &value)?; - serializer.serialize_entry("field_error", "field was no a valid object")? - } - // We have previously recorded fields for this span - // should be valid JSON. However, they appear to *not* - // be valid JSON. This is almost certainly a bug, so - // panic if we're in debug mode - Err(e) if cfg!(debug_assertions) => panic!( - "span '{}' had malformed fields! this is a bug.\n error: {}\n fields: {:?}", - self.0.metadata().name(), - e, - data - ), - // If we *aren't* in debug mode, it's probably best not - // crash the program, but let's at least make sure it's clear - // that the fields are not supposed to be missing. - Err(e) => serializer.serialize_entry("field_error", &format!("{}", e))?, - }; - serializer.serialize_entry("name", self.0.metadata().name())?; - serializer.end() - } -} - -impl<S, N, T> FormatEvent<S, N> for Format<Json, T> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, - T: FormatTime, -{ - fn format_event( - &self, - ctx: &FmtContext<'_, S, N>, - mut writer: Writer<'_>, - event: &Event<'_>, - ) -> fmt::Result - where - S: Subscriber + for<'a> LookupSpan<'a>, - { - let mut timestamp = String::new(); - self.timer.format_time(&mut Writer::new(&mut timestamp))?; - - #[cfg(feature = "tracing-log")] - let normalized_meta = event.normalized_metadata(); - #[cfg(feature = "tracing-log")] - let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); - #[cfg(not(feature = "tracing-log"))] - let meta = event.metadata(); - - let mut visit = || { - let mut serializer = Serializer::new(WriteAdaptor::new(&mut writer)); - - let mut serializer = serializer.serialize_map(None)?; - - if self.display_timestamp { - serializer.serialize_entry("timestamp", ×tamp)?; - } - - if self.display_level { - serializer.serialize_entry("level", &meta.level().as_serde())?; - } - - let format_field_marker: std::marker::PhantomData<N> = std::marker::PhantomData; - - let current_span = if self.format.display_current_span || self.format.display_span_list - { - event - .parent() - .and_then(|id| ctx.span(id)) - .or_else(|| ctx.lookup_current()) - } else { - None - }; - - if self.format.flatten_event { - let mut visitor = tracing_serde::SerdeMapVisitor::new(serializer); - event.record(&mut visitor); - - serializer = visitor.take_serializer()?; - } else { - use tracing_serde::fields::AsMap; - serializer.serialize_entry("fields", &event.field_map())?; - }; - - if self.display_target { - serializer.serialize_entry("target", meta.target())?; - } - - if self.display_filename { - if let Some(filename) = meta.file() { - serializer.serialize_entry("filename", filename)?; - } - } - - if self.display_line_number { - if let Some(line_number) = meta.line() { - serializer.serialize_entry("line_number", &line_number)?; - } - } - - if self.format.display_current_span { - if let Some(ref span) = current_span { - serializer - .serialize_entry("span", &SerializableSpan(span, format_field_marker)) - .unwrap_or(()); - } - } - - if self.format.display_span_list && current_span.is_some() { - serializer.serialize_entry( - "spans", - &SerializableContext(&ctx.ctx, format_field_marker), - )?; - } - - if self.display_thread_name { - let current_thread = std::thread::current(); - match current_thread.name() { - Some(name) => { - serializer.serialize_entry("threadName", name)?; - } - // fall-back to thread id when name is absent and ids are not enabled - None if !self.display_thread_id => { - serializer - .serialize_entry("threadName", &format!("{:?}", current_thread.id()))?; - } - _ => {} - } - } - - if self.display_thread_id { - serializer - .serialize_entry("threadId", &format!("{:?}", std::thread::current().id()))?; - } - - serializer.end() - }; - - visit().map_err(|_| fmt::Error)?; - writeln!(writer) - } -} - -impl Default for Json { - fn default() -> Json { - Json { - flatten_event: false, - display_current_span: true, - display_span_list: true, - } - } -} - -/// The JSON [`FormatFields`] implementation. -/// -#[derive(Debug)] -pub struct JsonFields { - // reserve the ability to add fields to this without causing a breaking - // change in the future. - _private: (), -} - -impl JsonFields { - /// Returns a new JSON [`FormatFields`] implementation. - /// - pub fn new() -> Self { - Self { _private: () } - } -} - -impl Default for JsonFields { - fn default() -> Self { - Self::new() - } -} - -impl<'a> FormatFields<'a> for JsonFields { - /// Format the provided `fields` to the provided `writer`, returning a result. - fn format_fields<R: RecordFields>(&self, mut writer: Writer<'_>, fields: R) -> fmt::Result { - let mut v = JsonVisitor::new(&mut writer); - fields.record(&mut v); - v.finish() - } - - /// Record additional field(s) on an existing span. - /// - /// By default, this appends a space to the current set of fields if it is - /// non-empty, and then calls `self.format_fields`. If different behavior is - /// required, the default implementation of this method can be overridden. - fn add_fields( - &self, - current: &'a mut FormattedFields<Self>, - fields: &Record<'_>, - ) -> fmt::Result { - if current.is_empty() { - // If there are no previously recorded fields, we can just reuse the - // existing string. - let mut writer = current.as_writer(); - let mut v = JsonVisitor::new(&mut writer); - fields.record(&mut v); - v.finish()?; - return Ok(()); - } - - // If fields were previously recorded on this span, we need to parse - // the current set of fields as JSON, add the new fields, and - // re-serialize them. Otherwise, if we just appended the new fields - // to a previously serialized JSON object, we would end up with - // malformed JSON. - // - // XXX(eliza): this is far from efficient, but unfortunately, it is - // necessary as long as the JSON formatter is implemented on top of - // an interface that stores all formatted fields as strings. - // - // We should consider reimplementing the JSON formatter as a - // separate layer, rather than a formatter for the `fmt` layer — - // then, we could store fields as JSON values, and add to them - // without having to parse and re-serialize. - let mut new = String::new(); - let map: BTreeMap<&'_ str, serde_json::Value> = - serde_json::from_str(current).map_err(|_| fmt::Error)?; - let mut v = JsonVisitor::new(&mut new); - v.values = map; - fields.record(&mut v); - v.finish()?; - current.fields = new; - - Ok(()) - } -} - -/// The [visitor] produced by [`JsonFields`]'s [`MakeVisitor`] implementation. -/// -/// [visitor]: crate::field::Visit -/// [`MakeVisitor`]: crate::field::MakeVisitor -pub struct JsonVisitor<'a> { - values: BTreeMap<&'a str, serde_json::Value>, - writer: &'a mut dyn Write, -} - -impl<'a> fmt::Debug for JsonVisitor<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_fmt(format_args!("JsonVisitor {{ values: {:?} }}", self.values)) - } -} - -impl<'a> JsonVisitor<'a> { - /// Returns a new default visitor that formats to the provided `writer`. - /// - /// # Arguments - /// - `writer`: the writer to format to. - /// - `is_empty`: whether or not any fields have been previously written to - /// that writer. - pub fn new(writer: &'a mut dyn Write) -> Self { - Self { - values: BTreeMap::new(), - writer, - } - } -} - -impl<'a> crate::field::VisitFmt for JsonVisitor<'a> { - fn writer(&mut self) -> &mut dyn fmt::Write { - self.writer - } -} - -impl<'a> crate::field::VisitOutput<fmt::Result> for JsonVisitor<'a> { - fn finish(self) -> fmt::Result { - let inner = || { - let mut serializer = Serializer::new(WriteAdaptor::new(self.writer)); - let mut ser_map = serializer.serialize_map(None)?; - - for (k, v) in self.values { - ser_map.serialize_entry(k, &v)?; - } - - ser_map.end() - }; - - if inner().is_err() { - Err(fmt::Error) - } else { - Ok(()) - } - } -} - -impl<'a> field::Visit for JsonVisitor<'a> { - #[cfg(all(tracing_unstable, feature = "valuable"))] - fn record_value(&mut self, field: &Field, value: valuable_crate::Value<'_>) { - let value = match serde_json::to_value(valuable_serde::Serializable::new(value)) { - Ok(value) => value, - Err(_e) => { - #[cfg(debug_assertions)] - unreachable!( - "`valuable::Valuable` implementations should always serialize \ - successfully, but an error occurred: {}", - _e, - ); - - #[cfg(not(debug_assertions))] - return; - } - }; - - self.values.insert(field.name(), value); - } - - /// Visit a double precision floating point value. - fn record_f64(&mut self, field: &Field, value: f64) { - self.values - .insert(field.name(), serde_json::Value::from(value)); - } - - /// Visit a signed 64-bit integer value. - fn record_i64(&mut self, field: &Field, value: i64) { - self.values - .insert(field.name(), serde_json::Value::from(value)); - } - - /// Visit an unsigned 64-bit integer value. - fn record_u64(&mut self, field: &Field, value: u64) { - self.values - .insert(field.name(), serde_json::Value::from(value)); - } - - /// Visit a boolean value. - fn record_bool(&mut self, field: &Field, value: bool) { - self.values - .insert(field.name(), serde_json::Value::from(value)); - } - - /// Visit a string value. - fn record_str(&mut self, field: &Field, value: &str) { - self.values - .insert(field.name(), serde_json::Value::from(value)); - } - - fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { - match field.name() { - // Skip fields that are actually log metadata that have already been handled - #[cfg(feature = "tracing-log")] - name if name.starts_with("log.") => (), - name if name.starts_with("r#") => { - self.values - .insert(&name[2..], serde_json::Value::from(format!("{:?}", value))); - } - name => { - self.values - .insert(name, serde_json::Value::from(format!("{:?}", value))); - } - }; - } -} -#[cfg(test)] -mod test { - use super::*; - use crate::fmt::{format::FmtSpan, test::MockMakeWriter, time::FormatTime, SubscriberBuilder}; - use tracing::{self, subscriber::with_default}; - - use std::fmt; - use std::path::Path; - - struct MockTime; - impl FormatTime for MockTime { - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - write!(w, "fake time") - } - } - - fn subscriber() -> SubscriberBuilder<JsonFields, Format<Json>> { - SubscriberBuilder::default().json() - } - - #[test] - fn json() { - let expected = - "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; - let subscriber = subscriber() - .flatten_event(false) - .with_current_span(true) - .with_span_list(true); - test_json(expected, subscriber, || { - let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); - let _guard = span.enter(); - tracing::info!("some json test"); - }); - } - - #[test] - fn json_filename() { - let current_path = Path::new("tracing-subscriber") - .join("src") - .join("fmt") - .join("format") - .join("json.rs") - .to_str() - .expect("path must be valid unicode") - // escape windows backslashes - .replace('\\', "\\\\"); - let expected = - &format!("{}{}{}", - "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"filename\":\"", - current_path, - "\",\"fields\":{\"message\":\"some json test\"}}\n"); - let subscriber = subscriber() - .flatten_event(false) - .with_current_span(true) - .with_file(true) - .with_span_list(true); - test_json(expected, subscriber, || { - let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); - let _guard = span.enter(); - tracing::info!("some json test"); - }); - } - - #[test] - fn json_line_number() { - let expected = - "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"line_number\":42,\"fields\":{\"message\":\"some json test\"}}\n"; - let subscriber = subscriber() - .flatten_event(false) - .with_current_span(true) - .with_line_number(true) - .with_span_list(true); - test_json_with_line_number(expected, subscriber, || { - let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); - let _guard = span.enter(); - tracing::info!("some json test"); - }); - } - - #[test] - fn json_flattened_event() { - let expected = - "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"message\":\"some json test\"}\n"; - - let subscriber = subscriber() - .flatten_event(true) - .with_current_span(true) - .with_span_list(true); - test_json(expected, subscriber, || { - let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); - let _guard = span.enter(); - tracing::info!("some json test"); - }); - } - - #[test] - fn json_disabled_current_span_event() { - let expected = - "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; - let subscriber = subscriber() - .flatten_event(false) - .with_current_span(false) - .with_span_list(true); - test_json(expected, subscriber, || { - let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); - let _guard = span.enter(); - tracing::info!("some json test"); - }); - } - - #[test] - fn json_disabled_span_list_event() { - let expected = - "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":42,\"name\":\"json_span\",\"number\":3},\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; - let subscriber = subscriber() - .flatten_event(false) - .with_current_span(true) - .with_span_list(false); - test_json(expected, subscriber, || { - let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); - let _guard = span.enter(); - tracing::info!("some json test"); - }); - } - - #[test] - fn json_nested_span() { - let expected = - "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"span\":{\"answer\":43,\"name\":\"nested_json_span\",\"number\":4},\"spans\":[{\"answer\":42,\"name\":\"json_span\",\"number\":3},{\"answer\":43,\"name\":\"nested_json_span\",\"number\":4}],\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; - let subscriber = subscriber() - .flatten_event(false) - .with_current_span(true) - .with_span_list(true); - test_json(expected, subscriber, || { - let span = tracing::span!(tracing::Level::INFO, "json_span", answer = 42, number = 3); - let _guard = span.enter(); - let span = tracing::span!( - tracing::Level::INFO, - "nested_json_span", - answer = 43, - number = 4 - ); - let _guard = span.enter(); - tracing::info!("some json test"); - }); - } - - #[test] - fn json_no_span() { - let expected = - "{\"timestamp\":\"fake time\",\"level\":\"INFO\",\"target\":\"tracing_subscriber::fmt::format::json::test\",\"fields\":{\"message\":\"some json test\"}}\n"; - let subscriber = subscriber() - .flatten_event(false) - .with_current_span(true) - .with_span_list(true); - test_json(expected, subscriber, || { - tracing::info!("some json test"); - }); - } - - #[test] - fn record_works() { - // This test reproduces issue #707, where using `Span::record` causes - // any events inside the span to be ignored. - - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt() - .json() - .with_writer(make_writer.clone()) - .finish(); - - with_default(subscriber, || { - tracing::info!("an event outside the root span"); - assert_eq!( - parse_as_json(&make_writer)["fields"]["message"], - "an event outside the root span" - ); - - let span = tracing::info_span!("the span", na = tracing::field::Empty); - span.record("na", &"value"); - let _enter = span.enter(); - - tracing::info!("an event inside the root span"); - assert_eq!( - parse_as_json(&make_writer)["fields"]["message"], - "an event inside the root span" - ); - }); - } - - #[test] - fn json_span_event_show_correct_context() { - let buffer = MockMakeWriter::default(); - let subscriber = subscriber() - .with_writer(buffer.clone()) - .flatten_event(false) - .with_current_span(true) - .with_span_list(false) - .with_span_events(FmtSpan::FULL) - .finish(); - - with_default(subscriber, || { - let context = "parent"; - let parent_span = tracing::info_span!("parent_span", context); - - let event = parse_as_json(&buffer); - assert_eq!(event["fields"]["message"], "new"); - assert_eq!(event["span"]["context"], "parent"); - - let _parent_enter = parent_span.enter(); - let event = parse_as_json(&buffer); - assert_eq!(event["fields"]["message"], "enter"); - assert_eq!(event["span"]["context"], "parent"); - - let context = "child"; - let child_span = tracing::info_span!("child_span", context); - let event = parse_as_json(&buffer); - assert_eq!(event["fields"]["message"], "new"); - assert_eq!(event["span"]["context"], "child"); - - let _child_enter = child_span.enter(); - let event = parse_as_json(&buffer); - assert_eq!(event["fields"]["message"], "enter"); - assert_eq!(event["span"]["context"], "child"); - - drop(_child_enter); - let event = parse_as_json(&buffer); - assert_eq!(event["fields"]["message"], "exit"); - assert_eq!(event["span"]["context"], "child"); - - drop(child_span); - let event = parse_as_json(&buffer); - assert_eq!(event["fields"]["message"], "close"); - assert_eq!(event["span"]["context"], "child"); - - drop(_parent_enter); - let event = parse_as_json(&buffer); - assert_eq!(event["fields"]["message"], "exit"); - assert_eq!(event["span"]["context"], "parent"); - - drop(parent_span); - let event = parse_as_json(&buffer); - assert_eq!(event["fields"]["message"], "close"); - assert_eq!(event["span"]["context"], "parent"); - }); - } - - #[test] - fn json_span_event_with_no_fields() { - // Check span events serialize correctly. - // Discussion: https://github.com/tokio-rs/tracing/issues/829#issuecomment-661984255 - let buffer = MockMakeWriter::default(); - let subscriber = subscriber() - .with_writer(buffer.clone()) - .flatten_event(false) - .with_current_span(false) - .with_span_list(false) - .with_span_events(FmtSpan::FULL) - .finish(); - - with_default(subscriber, || { - let span = tracing::info_span!("valid_json"); - assert_eq!(parse_as_json(&buffer)["fields"]["message"], "new"); - - let _enter = span.enter(); - assert_eq!(parse_as_json(&buffer)["fields"]["message"], "enter"); - - drop(_enter); - assert_eq!(parse_as_json(&buffer)["fields"]["message"], "exit"); - - drop(span); - assert_eq!(parse_as_json(&buffer)["fields"]["message"], "close"); - }); - } - - fn parse_as_json(buffer: &MockMakeWriter) -> serde_json::Value { - let buf = String::from_utf8(buffer.buf().to_vec()).unwrap(); - let json = buf - .lines() - .last() - .expect("expected at least one line to be written!"); - match serde_json::from_str(json) { - Ok(v) => v, - Err(e) => panic!( - "assertion failed: JSON shouldn't be malformed\n error: {}\n json: {}", - e, json - ), - } - } - - fn test_json<T>( - expected: &str, - builder: crate::fmt::SubscriberBuilder<JsonFields, Format<Json>>, - producer: impl FnOnce() -> T, - ) { - let make_writer = MockMakeWriter::default(); - let subscriber = builder - .with_writer(make_writer.clone()) - .with_timer(MockTime) - .finish(); - - with_default(subscriber, producer); - - let buf = make_writer.buf(); - let actual = std::str::from_utf8(&buf[..]).unwrap(); - assert_eq!( - serde_json::from_str::<std::collections::HashMap<&str, serde_json::Value>>(expected) - .unwrap(), - serde_json::from_str(actual).unwrap() - ); - } - - fn test_json_with_line_number<T>( - expected: &str, - builder: crate::fmt::SubscriberBuilder<JsonFields, Format<Json>>, - producer: impl FnOnce() -> T, - ) { - let make_writer = MockMakeWriter::default(); - let subscriber = builder - .with_writer(make_writer.clone()) - .with_timer(MockTime) - .finish(); - - with_default(subscriber, producer); - - let buf = make_writer.buf(); - let actual = std::str::from_utf8(&buf[..]).unwrap(); - let mut expected = - serde_json::from_str::<std::collections::HashMap<&str, serde_json::Value>>(expected) - .unwrap(); - let expect_line_number = expected.remove("line_number").is_some(); - let mut actual: std::collections::HashMap<&str, serde_json::Value> = - serde_json::from_str(actual).unwrap(); - let line_number = actual.remove("line_number"); - if expect_line_number { - assert_eq!(line_number.map(|x| x.is_number()), Some(true)); - } else { - assert!(line_number.is_none()); - } - assert_eq!(actual, expected); - } -} diff --git a/vendor/tracing-subscriber/src/fmt/format/mod.rs b/vendor/tracing-subscriber/src/fmt/format/mod.rs deleted file mode 100644 index ec79ac140..000000000 --- a/vendor/tracing-subscriber/src/fmt/format/mod.rs +++ /dev/null @@ -1,2161 +0,0 @@ -//! Formatters for logging `tracing` events. -//! -//! This module provides several formatter implementations, as well as utilities -//! for implementing custom formatters. -//! -//! # Formatters -//! This module provides a number of formatter implementations: -//! -//! * [`Full`]: The default formatter. This emits human-readable, -//! single-line logs for each event that occurs, with the current span context -//! displayed before the formatted representation of the event. See -//! [here](Full#example-output) for sample output. -//! -//! * [`Compact`]: A variant of the default formatter, optimized for -//! short line lengths. Fields from the current span context are appended to -//! the fields of the formatted event, and span names are not shown; the -//! verbosity level is abbreviated to a single character. See -//! [here](Compact#example-output) for sample output. -//! -//! * [`Pretty`]: Emits excessively pretty, multi-line logs, optimized -//! for human readability. This is primarily intended to be used in local -//! development and debugging, or for command-line applications, where -//! automated analysis and compact storage of logs is less of a priority than -//! readability and visual appeal. See [here](Pretty#example-output) -//! for sample output. -//! -//! * [`Json`]: Outputs newline-delimited JSON logs. This is intended -//! for production use with systems where structured logs are consumed as JSON -//! by analysis and viewing tools. The JSON output is not optimized for human -//! readability. See [here](Json#example-output) for sample output. -use super::time::{FormatTime, SystemTime}; -use crate::{ - field::{MakeOutput, MakeVisitor, RecordFields, VisitFmt, VisitOutput}, - fmt::fmt_layer::FmtContext, - fmt::fmt_layer::FormattedFields, - registry::LookupSpan, -}; - -use std::fmt::{self, Debug, Display, Write}; -use tracing_core::{ - field::{self, Field, Visit}, - span, Event, Level, Subscriber, -}; - -#[cfg(feature = "tracing-log")] -use tracing_log::NormalizeEvent; - -#[cfg(feature = "ansi")] -use ansi_term::{Colour, Style}; - -#[cfg(feature = "json")] -mod json; -#[cfg(feature = "json")] -#[cfg_attr(docsrs, doc(cfg(feature = "json")))] -pub use json::*; - -#[cfg(feature = "ansi")] -mod pretty; -#[cfg(feature = "ansi")] -#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] -pub use pretty::*; - -/// A type that can format a tracing [`Event`] to a [`Writer`]. -/// -/// `FormatEvent` is primarily used in the context of [`fmt::Subscriber`] or -/// [`fmt::Layer`]. Each time an event is dispatched to [`fmt::Subscriber`] or -/// [`fmt::Layer`], the subscriber or layer -/// forwards it to its associated `FormatEvent` to emit a log message. -/// -/// This trait is already implemented for function pointers with the same -/// signature as `format_event`. -/// -/// # Arguments -/// -/// The following arguments are passed to `FormatEvent::format_event`: -/// -/// * A [`FmtContext`]. This is an extension of the [`layer::Context`] type, -/// which can be used for accessing stored information such as the current -/// span context an event occurred in. -/// -/// In addition, [`FmtContext`] exposes access to the [`FormatFields`] -/// implementation that the subscriber was configured to use via the -/// [`FmtContext::field_format`] method. This can be used when the -/// [`FormatEvent`] implementation needs to format the event's fields. -/// -/// For convenience, [`FmtContext`] also [implements `FormatFields`], -/// forwarding to the configured [`FormatFields`] type. -/// -/// * A [`Writer`] to which the formatted representation of the event is -/// written. This type implements the [`std::fmt::Write`] trait, and therefore -/// can be used with the [`std::write!`] and [`std::writeln!`] macros, as well -/// as calling [`std::fmt::Write`] methods directly. -/// -/// The [`Writer`] type also implements additional methods that provide -/// information about how the event should be formatted. The -/// [`Writer::has_ansi_escapes`] method indicates whether [ANSI terminal -/// escape codes] are supported by the underlying I/O writer that the event -/// will be written to. If this returns `true`, the formatter is permitted to -/// use ANSI escape codes to add colors and other text formatting to its -/// output. If it returns `false`, the event will be written to an output that -/// does not support ANSI escape codes (such as a log file), and they should -/// not be emitted. -/// -/// Crates like [`ansi_term`] and [`owo-colors`] can be used to add ANSI -/// escape codes to formatted output. -/// -/// * The actual [`Event`] to be formatted. -/// -/// # Examples -/// -/// This example re-implements a simiplified version of this crate's [default -/// formatter]: -/// -/// ```rust -/// use std::fmt; -/// use tracing_core::{Subscriber, Event}; -/// use tracing_subscriber::fmt::{ -/// format::{self, FormatEvent, FormatFields}, -/// FmtContext, -/// FormattedFields, -/// }; -/// use tracing_subscriber::registry::LookupSpan; -/// -/// struct MyFormatter; -/// -/// impl<S, N> FormatEvent<S, N> for MyFormatter -/// where -/// S: Subscriber + for<'a> LookupSpan<'a>, -/// N: for<'a> FormatFields<'a> + 'static, -/// { -/// fn format_event( -/// &self, -/// ctx: &FmtContext<'_, S, N>, -/// mut writer: format::Writer<'_>, -/// event: &Event<'_>, -/// ) -> fmt::Result { -/// // Format values from the event's's metadata: -/// let metadata = event.metadata(); -/// write!(&mut writer, "{} {}: ", metadata.level(), metadata.target())?; -/// -/// // Format all the spans in the event's span context. -/// if let Some(scope) = ctx.event_scope() { -/// for span in scope.from_root() { -/// write!(writer, "{}", span.name())?; -/// -/// // `FormattedFields` is a formatted representation of the span's -/// // fields, which is stored in its extensions by the `fmt` layer's -/// // `new_span` method. The fields will have been formatted -/// // by the same field formatter that's provided to the event -/// // formatter in the `FmtContext`. -/// let ext = span.extensions(); -/// let fields = &ext -/// .get::<FormattedFields<N>>() -/// .expect("will never be `None`"); -/// -/// // Skip formatting the fields if the span had no fields. -/// if !fields.is_empty() { -/// write!(writer, "{{{}}}", fields)?; -/// } -/// write!(writer, ": ")?; -/// } -/// } -/// -/// // Write fields on the event -/// ctx.field_format().format_fields(writer.by_ref(), event)?; -/// -/// writeln!(writer) -/// } -/// } -/// -/// let _subscriber = tracing_subscriber::fmt() -/// .event_format(MyFormatter) -/// .init(); -/// -/// let _span = tracing::info_span!("my_span", answer = 42).entered(); -/// tracing::info!(question = "life, the universe, and everything", "hello world"); -/// ``` -/// -/// This formatter will print events like this: -/// -/// ```text -/// DEBUG yak_shaving::shaver: some-span{field-on-span=foo}: started shaving yak -/// ``` -/// -/// [`layer::Context`]: crate::layer::Context -/// [`fmt::Layer`]: super::Layer -/// [`fmt::Subscriber`]: super::Subscriber -/// [`Event`]: tracing::Event -/// [implements `FormatFields`]: super::FmtContext#impl-FormatFields<'writer> -/// [ANSI terminal escape codes]: https://en.wikipedia.org/wiki/ANSI_escape_code -/// [`Writer::has_ansi_escapes`]: Writer::has_ansi_escapes -/// [`ansi_term`]: https://crates.io/crates/ansi_term -/// [`owo-colors`]: https://crates.io/crates/owo-colors -/// [default formatter]: Full -pub trait FormatEvent<S, N> -where - S: Subscriber + for<'a> LookupSpan<'a>, - N: for<'a> FormatFields<'a> + 'static, -{ - /// Write a log message for `Event` in `Context` to the given [`Writer`]. - fn format_event( - &self, - ctx: &FmtContext<'_, S, N>, - writer: Writer<'_>, - event: &Event<'_>, - ) -> fmt::Result; -} - -impl<S, N> FormatEvent<S, N> - for fn(ctx: &FmtContext<'_, S, N>, Writer<'_>, &Event<'_>) -> fmt::Result -where - S: Subscriber + for<'a> LookupSpan<'a>, - N: for<'a> FormatFields<'a> + 'static, -{ - fn format_event( - &self, - ctx: &FmtContext<'_, S, N>, - writer: Writer<'_>, - event: &Event<'_>, - ) -> fmt::Result { - (*self)(ctx, writer, event) - } -} -/// A type that can format a [set of fields] to a [`Writer`]. -/// -/// `FormatFields` is primarily used in the context of [`FmtSubscriber`]. Each -/// time a span or event with fields is recorded, the subscriber will format -/// those fields with its associated `FormatFields` implementation. -/// -/// [set of fields]: crate::field::RecordFields -/// [`FmtSubscriber`]: super::Subscriber -pub trait FormatFields<'writer> { - /// Format the provided `fields` to the provided [`Writer`], returning a result. - fn format_fields<R: RecordFields>(&self, writer: Writer<'writer>, fields: R) -> fmt::Result; - - /// Record additional field(s) on an existing span. - /// - /// By default, this appends a space to the current set of fields if it is - /// non-empty, and then calls `self.format_fields`. If different behavior is - /// required, the default implementation of this method can be overridden. - fn add_fields( - &self, - current: &'writer mut FormattedFields<Self>, - fields: &span::Record<'_>, - ) -> fmt::Result { - if !current.fields.is_empty() { - current.fields.push(' '); - } - self.format_fields(current.as_writer(), fields) - } -} - -/// Returns the default configuration for an [event formatter]. -/// -/// Methods on the returned event formatter can be used for further -/// configuration. For example: -/// -/// ```rust -/// let format = tracing_subscriber::fmt::format() -/// .without_time() // Don't include timestamps -/// .with_target(false) // Don't include event targets. -/// .with_level(false) // Don't include event levels. -/// .compact(); // Use a more compact, abbreviated format. -/// -/// // Use the configured formatter when building a new subscriber. -/// tracing_subscriber::fmt() -/// .event_format(format) -/// .init(); -/// ``` -pub fn format() -> Format { - Format::default() -} - -/// Returns the default configuration for a JSON [event formatter]. -#[cfg(feature = "json")] -#[cfg_attr(docsrs, doc(cfg(feature = "json")))] -pub fn json() -> Format<Json> { - format().json() -} - -/// Returns a [`FormatFields`] implementation that formats fields using the -/// provided function or closure. -/// -pub fn debug_fn<F>(f: F) -> FieldFn<F> -where - F: Fn(&mut Writer<'_>, &Field, &dyn fmt::Debug) -> fmt::Result + Clone, -{ - FieldFn(f) -} - -/// A writer to which formatted representations of spans and events are written. -/// -/// This type is provided as input to the [`FormatEvent::format_event`] and -/// [`FormatFields::format_fields`] methods, which will write formatted -/// representations of [`Event`]s and [fields] to the `Writer`. -/// -/// This type implements the [`std::fmt::Write`] trait, allowing it to be used -/// with any function that takes an instance of [`std::fmt::Write`]. -/// Additionally, it can be used with the standard library's [`std::write!`] and -/// [`std::writeln!`] macros. -/// -/// Additionally, a `Writer` may expose additional `tracing`-specific -/// information to the formatter implementation. -/// -/// [fields]: tracing_core::field -pub struct Writer<'writer> { - writer: &'writer mut dyn fmt::Write, - // TODO(eliza): add ANSI support - is_ansi: bool, -} - -/// A [`FormatFields`] implementation that formats fields by calling a function -/// or closure. -/// -#[derive(Debug, Clone)] -pub struct FieldFn<F>(F); -/// The [visitor] produced by [`FieldFn`]'s [`MakeVisitor`] implementation. -/// -/// [visitor]: super::super::field::Visit -/// [`MakeVisitor`]: super::super::field::MakeVisitor -pub struct FieldFnVisitor<'a, F> { - f: F, - writer: Writer<'a>, - result: fmt::Result, -} -/// Marker for [`Format`] that indicates that the compact log format should be used. -/// -/// The compact format includes fields from all currently entered spans, after -/// the event's fields. Span names are listed in order before fields are -/// displayed. -/// -/// # Example Output -/// -/// <pre><font color="#4E9A06"><b>:;</b></font> <font color="#4E9A06">cargo</font> run --example fmt-compact -/// <font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 0.08s -/// <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt-compact` -/// <font color="#AAAAAA">2022-02-17T19:51:05.809287Z </font><font color="#4E9A06"> INFO</font> <b>fmt_compact</b><font color="#AAAAAA">: preparing to shave yaks </font><i>number_of_yaks</i><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809367Z </font><font color="#4E9A06"> INFO</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: shaving yaks </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809414Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!" </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=1</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809443Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: yak shaved successfully </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=1</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809477Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks</b>: <b>yak_events</b><font color="#AAAAAA">: </font><i>yak</i><font color="#AAAAAA">=1 </font><i>shaved</i><font color="#AAAAAA">=true </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809500Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: </font><i>yaks_shaved</i><font color="#AAAAAA">=1 </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809531Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!" </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=2</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809554Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: yak shaved successfully </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=2</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809581Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks</b>: <b>yak_events</b><font color="#AAAAAA">: </font><i>yak</i><font color="#AAAAAA">=2 </font><i>shaved</i><font color="#AAAAAA">=true </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809606Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: </font><i>yaks_shaved</i><font color="#AAAAAA">=2 </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809635Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!" </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809664Z </font><font color="#C4A000"> WARN</font> <b>shaving_yaks</b>:<b>shave</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: could not locate yak </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3 </font><font color="#AAAAAA"><i>yak</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809693Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks</b>: <b>yak_events</b><font color="#AAAAAA">: </font><i>yak</i><font color="#AAAAAA">=3 </font><i>shaved</i><font color="#AAAAAA">=false </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809717Z </font><font color="#CC0000">ERROR</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: failed to shave yak </font><i>yak</i><font color="#AAAAAA">=3 </font><i>error</i><font color="#AAAAAA">=missing yak </font><i>error.sources</i><font color="#AAAAAA">=[out of space, out of cash] </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809743Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks</b>: <b>fmt_compact::yak_shave</b><font color="#AAAAAA">: </font><i>yaks_shaved</i><font color="#AAAAAA">=2 </font><font color="#AAAAAA"><i>yaks</i></font><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-17T19:51:05.809768Z </font><font color="#4E9A06"> INFO</font> <b>fmt_compact</b><font color="#AAAAAA">: yak shaving completed </font><i>all_yaks_shaved</i><font color="#AAAAAA">=false</font> -/// -/// </pre> -#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)] -pub struct Compact; - -/// Marker for [`Format`] that indicates that the default log format should be used. -/// -/// This formatter shows the span context before printing event data. Spans are -/// displayed including their names and fields. -/// -/// # Example Output -/// -/// <pre><font color="#4E9A06"><b>:;</b></font> <font color="#4E9A06">cargo</font> run --example fmt -/// <font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 0.08s -/// <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt` -/// <font color="#AAAAAA">2022-02-15T18:40:14.289898Z </font><font color="#4E9A06"> INFO</font> fmt: preparing to shave yaks <i>number_of_yaks</i><font color="#AAAAAA">=3</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.289974Z </font><font color="#4E9A06"> INFO</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: shaving yaks</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290011Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=1</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!"</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290038Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=1</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: yak shaved successfully</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290070Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: yak_events: </font><i>yak</i><font color="#AAAAAA">=1 </font><i>shaved</i><font color="#AAAAAA">=true</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290089Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: </font><i>yaks_shaved</i><font color="#AAAAAA">=1</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290114Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=2</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!"</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290134Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=2</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: yak shaved successfully</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290157Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: yak_events: </font><i>yak</i><font color="#AAAAAA">=2 </font><i>shaved</i><font color="#AAAAAA">=true</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290174Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: </font><i>yaks_shaved</i><font color="#AAAAAA">=2</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290198Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: hello! I'm gonna shave a yak </font><i>excitement</i><font color="#AAAAAA">="yay!"</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290222Z </font><font color="#C4A000"> WARN</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">:</font><b>shave{</b><i>yak</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: could not locate yak</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290247Z </font><font color="#3465A4">DEBUG</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: yak_events: </font><i>yak</i><font color="#AAAAAA">=3 </font><i>shaved</i><font color="#AAAAAA">=false</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290268Z </font><font color="#CC0000">ERROR</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: failed to shave yak </font><i>yak</i><font color="#AAAAAA">=3 </font><i>error</i><font color="#AAAAAA">=missing yak </font><i>error.sources</i><font color="#AAAAAA">=[out of space, out of cash]</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290287Z </font><font color="#75507B">TRACE</font> <b>shaving_yaks{</b><i>yaks</i><font color="#AAAAAA">=3</font><b>}</b><font color="#AAAAAA">: fmt::yak_shave: </font><i>yaks_shaved</i><font color="#AAAAAA">=2</font> -/// <font color="#AAAAAA">2022-02-15T18:40:14.290309Z </font><font color="#4E9A06"> INFO</font> fmt: yak shaving completed. <i>all_yaks_shaved</i><font color="#AAAAAA">=false</font> -/// </pre> -#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)] -pub struct Full; - -/// A pre-configured event formatter. -/// -/// You will usually want to use this as the `FormatEvent` for a `FmtSubscriber`. -/// -/// The default logging format, [`Full`] includes all fields in each event and its containing -/// spans. The [`Compact`] logging format is intended to produce shorter log -/// lines; it displays each event's fields, along with fields from the current -/// span context, but other information is abbreviated. The [`Pretty`] logging -/// format is an extra-verbose, multi-line human-readable logging format -/// intended for use in development. -#[derive(Debug, Clone)] -pub struct Format<F = Full, T = SystemTime> { - format: F, - pub(crate) timer: T, - pub(crate) ansi: Option<bool>, - pub(crate) display_timestamp: bool, - pub(crate) display_target: bool, - pub(crate) display_level: bool, - pub(crate) display_thread_id: bool, - pub(crate) display_thread_name: bool, - pub(crate) display_filename: bool, - pub(crate) display_line_number: bool, -} - -// === impl Writer === - -impl<'writer> Writer<'writer> { - // TODO(eliza): consider making this a public API? - // We may not want to do that if we choose to expose specialized - // constructors instead (e.g. `from_string` that stores whether the string - // is empty...?) - pub(crate) fn new(writer: &'writer mut impl fmt::Write) -> Self { - Self { - writer: writer as &mut dyn fmt::Write, - is_ansi: false, - } - } - - // TODO(eliza): consider making this a public API? - pub(crate) fn with_ansi(self, is_ansi: bool) -> Self { - Self { is_ansi, ..self } - } - - /// Return a new `Writer` that mutably borrows `self`. - /// - /// This can be used to temporarily borrow a `Writer` to pass a new `Writer` - /// to a function that takes a `Writer` by value, allowing the original writer - /// to still be used once that function returns. - pub fn by_ref(&mut self) -> Writer<'_> { - let is_ansi = self.is_ansi; - Writer { - writer: self as &mut dyn fmt::Write, - is_ansi, - } - } - - /// Writes a string slice into this `Writer`, returning whether the write succeeded. - /// - /// This method can only succeed if the entire string slice was successfully - /// written, and this method will not return until all data has been written - /// or an error occurs. - /// - /// This is identical to calling the [`write_str` method] from the `Writer`'s - /// [`std::fmt::Write`] implementation. However, it is also provided as an - /// inherent method, so that `Writer`s can be used without needing to import the - /// [`std::fmt::Write`] trait. - /// - /// # Errors - /// - /// This function will return an instance of [`std::fmt::Error`] on error. - /// - /// [`write_str` method]: std::fmt::Write::write_str - #[inline] - pub fn write_str(&mut self, s: &str) -> fmt::Result { - self.writer.write_str(s) - } - - /// Writes a [`char`] into this writer, returning whether the write succeeded. - /// - /// A single [`char`] may be encoded as more than one byte. - /// This method can only succeed if the entire byte sequence was successfully - /// written, and this method will not return until all data has been - /// written or an error occurs. - /// - /// This is identical to calling the [`write_char` method] from the `Writer`'s - /// [`std::fmt::Write`] implementation. However, it is also provided as an - /// inherent method, so that `Writer`s can be used without needing to import the - /// [`std::fmt::Write`] trait. - /// - /// # Errors - /// - /// This function will return an instance of [`std::fmt::Error`] on error. - /// - /// [`write_char` method]: std::fmt::Write::write_char - #[inline] - pub fn write_char(&mut self, c: char) -> fmt::Result { - self.writer.write_char(c) - } - - /// Glue for usage of the [`write!`] macro with `Writer`s. - /// - /// This method should generally not be invoked manually, but rather through - /// the [`write!`] macro itself. - /// - /// This is identical to calling the [`write_fmt` method] from the `Writer`'s - /// [`std::fmt::Write`] implementation. However, it is also provided as an - /// inherent method, so that `Writer`s can be used with the [`write!` macro] - /// without needing to import the - /// [`std::fmt::Write`] trait. - /// - /// [`write_fmt` method]: std::fmt::Write::write_fmt - pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result { - self.writer.write_fmt(args) - } - - /// Returns `true` if [ANSI escape codes] may be used to add colors - /// and other formatting when writing to this `Writer`. - /// - /// If this returns `false`, formatters should not emit ANSI escape codes. - /// - /// [ANSI escape codes]: https://en.wikipedia.org/wiki/ANSI_escape_code - pub fn has_ansi_escapes(&self) -> bool { - self.is_ansi - } - - pub(in crate::fmt::format) fn bold(&self) -> Style { - #[cfg(feature = "ansi")] - { - if self.is_ansi { - return Style::new().bold(); - } - } - - Style::new() - } - - pub(in crate::fmt::format) fn dimmed(&self) -> Style { - #[cfg(feature = "ansi")] - { - if self.is_ansi { - return Style::new().dimmed(); - } - } - - Style::new() - } - - pub(in crate::fmt::format) fn italic(&self) -> Style { - #[cfg(feature = "ansi")] - { - if self.is_ansi { - return Style::new().italic(); - } - } - - Style::new() - } -} - -impl fmt::Write for Writer<'_> { - #[inline] - fn write_str(&mut self, s: &str) -> fmt::Result { - Writer::write_str(self, s) - } - - #[inline] - fn write_char(&mut self, c: char) -> fmt::Result { - Writer::write_char(self, c) - } - - #[inline] - fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result { - Writer::write_fmt(self, args) - } -} - -impl fmt::Debug for Writer<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("Writer") - .field("writer", &format_args!("<&mut dyn fmt::Write>")) - .field("is_ansi", &self.is_ansi) - .finish() - } -} - -// === impl Format === - -impl Default for Format<Full, SystemTime> { - fn default() -> Self { - Format { - format: Full, - timer: SystemTime, - ansi: None, - display_timestamp: true, - display_target: true, - display_level: true, - display_thread_id: false, - display_thread_name: false, - display_filename: false, - display_line_number: false, - } - } -} - -impl<F, T> Format<F, T> { - /// Use a less verbose output format. - /// - /// See [`Compact`]. - pub fn compact(self) -> Format<Compact, T> { - Format { - format: Compact, - timer: self.timer, - ansi: self.ansi, - display_target: self.display_target, - display_timestamp: self.display_timestamp, - display_level: self.display_level, - display_thread_id: self.display_thread_id, - display_thread_name: self.display_thread_name, - display_filename: self.display_filename, - display_line_number: self.display_line_number, - } - } - - /// Use an excessively pretty, human-readable output format. - /// - /// See [`Pretty`]. - /// - /// Note that this requires the "ansi" feature to be enabled. - /// - /// # Options - /// - /// [`Format::with_ansi`] can be used to disable ANSI terminal escape codes (which enable - /// formatting such as colors, bold, italic, etc) in event formatting. However, a field - /// formatter must be manually provided to avoid ANSI in the formatting of parent spans, like - /// so: - /// - /// ``` - /// # use tracing_subscriber::fmt::format; - /// tracing_subscriber::fmt() - /// .pretty() - /// .with_ansi(false) - /// .fmt_fields(format::PrettyFields::new().with_ansi(false)) - /// // ... other settings ... - /// .init(); - /// ``` - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] - pub fn pretty(self) -> Format<Pretty, T> { - Format { - format: Pretty::default(), - timer: self.timer, - ansi: self.ansi, - display_target: self.display_target, - display_timestamp: self.display_timestamp, - display_level: self.display_level, - display_thread_id: self.display_thread_id, - display_thread_name: self.display_thread_name, - display_filename: true, - display_line_number: true, - } - } - - /// Use the full JSON format. - /// - /// The full format includes fields from all entered spans. - /// - /// # Example Output - /// - /// ```ignore,json - /// {"timestamp":"Feb 20 11:28:15.096","level":"INFO","target":"mycrate","fields":{"message":"some message", "key": "value"}} - /// ``` - /// - /// # Options - /// - /// - [`Format::flatten_event`] can be used to enable flattening event fields into the root - /// object. - #[cfg(feature = "json")] - #[cfg_attr(docsrs, doc(cfg(feature = "json")))] - pub fn json(self) -> Format<Json, T> { - Format { - format: Json::default(), - timer: self.timer, - ansi: self.ansi, - display_target: self.display_target, - display_timestamp: self.display_timestamp, - display_level: self.display_level, - display_thread_id: self.display_thread_id, - display_thread_name: self.display_thread_name, - display_filename: self.display_filename, - display_line_number: self.display_line_number, - } - } - - /// Use the given [`timer`] for log message timestamps. - /// - /// See [`time` module] for the provided timer implementations. - /// - /// Note that using the `"time"` feature flag enables the - /// additional time formatters [`UtcTime`] and [`LocalTime`], which use the - /// [`time` crate] to provide more sophisticated timestamp formatting - /// options. - /// - /// [`timer`]: super::time::FormatTime - /// [`time` module]: mod@super::time - /// [`UtcTime`]: super::time::UtcTime - /// [`LocalTime`]: super::time::LocalTime - /// [`time` crate]: https://docs.rs/time/0.3 - pub fn with_timer<T2>(self, timer: T2) -> Format<F, T2> { - Format { - format: self.format, - timer, - ansi: self.ansi, - display_target: self.display_target, - display_timestamp: self.display_timestamp, - display_level: self.display_level, - display_thread_id: self.display_thread_id, - display_thread_name: self.display_thread_name, - display_filename: self.display_filename, - display_line_number: self.display_line_number, - } - } - - /// Do not emit timestamps with log messages. - pub fn without_time(self) -> Format<F, ()> { - Format { - format: self.format, - timer: (), - ansi: self.ansi, - display_timestamp: false, - display_target: self.display_target, - display_level: self.display_level, - display_thread_id: self.display_thread_id, - display_thread_name: self.display_thread_name, - display_filename: self.display_filename, - display_line_number: self.display_line_number, - } - } - - /// Enable ANSI terminal colors for formatted output. - pub fn with_ansi(self, ansi: bool) -> Format<F, T> { - Format { - ansi: Some(ansi), - ..self - } - } - - /// Sets whether or not an event's target is displayed. - pub fn with_target(self, display_target: bool) -> Format<F, T> { - Format { - display_target, - ..self - } - } - - /// Sets whether or not an event's level is displayed. - pub fn with_level(self, display_level: bool) -> Format<F, T> { - Format { - display_level, - ..self - } - } - - /// Sets whether or not the [thread ID] of the current thread is displayed - /// when formatting events. - /// - /// [thread ID]: std::thread::ThreadId - pub fn with_thread_ids(self, display_thread_id: bool) -> Format<F, T> { - Format { - display_thread_id, - ..self - } - } - - /// Sets whether or not the [name] of the current thread is displayed - /// when formatting events. - /// - /// [name]: std::thread#naming-threads - pub fn with_thread_names(self, display_thread_name: bool) -> Format<F, T> { - Format { - display_thread_name, - ..self - } - } - - /// Sets whether or not an event's [source code file path][file] is - /// displayed. - /// - /// [file]: tracing_core::Metadata::file - pub fn with_file(self, display_filename: bool) -> Format<F, T> { - Format { - display_filename, - ..self - } - } - - /// Sets whether or not an event's [source code line number][line] is - /// displayed. - /// - /// [line]: tracing_core::Metadata::line - pub fn with_line_number(self, display_line_number: bool) -> Format<F, T> { - Format { - display_line_number, - ..self - } - } - - /// Sets whether or not the source code location from which an event - /// originated is displayed. - /// - /// This is equivalent to calling [`Format::with_file`] and - /// [`Format::with_line_number`] with the same value. - pub fn with_source_location(self, display_location: bool) -> Self { - self.with_line_number(display_location) - .with_file(display_location) - } - - #[inline] - fn format_timestamp(&self, writer: &mut Writer<'_>) -> fmt::Result - where - T: FormatTime, - { - // If timestamps are disabled, do nothing. - if !self.display_timestamp { - return Ok(()); - } - - // If ANSI color codes are enabled, format the timestamp with ANSI - // colors. - #[cfg(feature = "ansi")] - { - if writer.has_ansi_escapes() { - let style = Style::new().dimmed(); - write!(writer, "{}", style.prefix())?; - - // If getting the timestamp failed, don't bail --- only bail on - // formatting errors. - if self.timer.format_time(writer).is_err() { - writer.write_str("<unknown time>")?; - } - - write!(writer, "{} ", style.suffix())?; - return Ok(()); - } - } - - // Otherwise, just format the timestamp without ANSI formatting. - // If getting the timestamp failed, don't bail --- only bail on - // formatting errors. - if self.timer.format_time(writer).is_err() { - writer.write_str("<unknown time>")?; - } - writer.write_char(' ') - } -} - -#[cfg(feature = "json")] -#[cfg_attr(docsrs, doc(cfg(feature = "json")))] -impl<T> Format<Json, T> { - /// Use the full JSON format with the event's event fields flattened. - /// - /// # Example Output - /// - /// ```ignore,json - /// {"timestamp":"Feb 20 11:28:15.096","level":"INFO","target":"mycrate", "message":"some message", "key": "value"} - /// ``` - /// See [`Json`][super::format::Json]. - #[cfg(feature = "json")] - #[cfg_attr(docsrs, doc(cfg(feature = "json")))] - pub fn flatten_event(mut self, flatten_event: bool) -> Format<Json, T> { - self.format.flatten_event(flatten_event); - self - } - - /// Sets whether or not the formatter will include the current span in - /// formatted events. - /// - /// See [`format::Json`][Json] - #[cfg(feature = "json")] - #[cfg_attr(docsrs, doc(cfg(feature = "json")))] - pub fn with_current_span(mut self, display_current_span: bool) -> Format<Json, T> { - self.format.with_current_span(display_current_span); - self - } - - /// Sets whether or not the formatter will include a list (from root to - /// leaf) of all currently entered spans in formatted events. - /// - /// See [`format::Json`][Json] - #[cfg(feature = "json")] - #[cfg_attr(docsrs, doc(cfg(feature = "json")))] - pub fn with_span_list(mut self, display_span_list: bool) -> Format<Json, T> { - self.format.with_span_list(display_span_list); - self - } -} - -impl<S, N, T> FormatEvent<S, N> for Format<Full, T> -where - S: Subscriber + for<'a> LookupSpan<'a>, - N: for<'a> FormatFields<'a> + 'static, - T: FormatTime, -{ - fn format_event( - &self, - ctx: &FmtContext<'_, S, N>, - mut writer: Writer<'_>, - event: &Event<'_>, - ) -> fmt::Result { - #[cfg(feature = "tracing-log")] - let normalized_meta = event.normalized_metadata(); - #[cfg(feature = "tracing-log")] - let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); - #[cfg(not(feature = "tracing-log"))] - let meta = event.metadata(); - - // if the `Format` struct *also* has an ANSI color configuration, - // override the writer...the API for configuring ANSI color codes on the - // `Format` struct is deprecated, but we still need to honor those - // configurations. - if let Some(ansi) = self.ansi { - writer = writer.with_ansi(ansi); - } - - self.format_timestamp(&mut writer)?; - - if self.display_level { - let fmt_level = { - #[cfg(feature = "ansi")] - { - FmtLevel::new(meta.level(), writer.has_ansi_escapes()) - } - #[cfg(not(feature = "ansi"))] - { - FmtLevel::new(meta.level()) - } - }; - write!(writer, "{} ", fmt_level)?; - } - - if self.display_thread_name { - let current_thread = std::thread::current(); - match current_thread.name() { - Some(name) => { - write!(writer, "{} ", FmtThreadName::new(name))?; - } - // fall-back to thread id when name is absent and ids are not enabled - None if !self.display_thread_id => { - write!(writer, "{:0>2?} ", current_thread.id())?; - } - _ => {} - } - } - - if self.display_thread_id { - write!(writer, "{:0>2?} ", std::thread::current().id())?; - } - - let dimmed = writer.dimmed(); - - if let Some(scope) = ctx.event_scope() { - let bold = writer.bold(); - - let mut seen = false; - - for span in scope.from_root() { - write!(writer, "{}", bold.paint(span.metadata().name()))?; - seen = true; - - let ext = span.extensions(); - if let Some(fields) = &ext.get::<FormattedFields<N>>() { - if !fields.is_empty() { - write!(writer, "{}{}{}", bold.paint("{"), fields, bold.paint("}"))?; - } - } - write!(writer, "{}", dimmed.paint(":"))?; - } - - if seen { - writer.write_char(' ')?; - } - }; - - if self.display_target { - write!( - writer, - "{}{} ", - dimmed.paint(meta.target()), - dimmed.paint(":") - )?; - } - - let line_number = if self.display_line_number { - meta.line() - } else { - None - }; - - if self.display_filename { - if let Some(filename) = meta.file() { - write!( - writer, - "{}{}{}", - dimmed.paint(filename), - dimmed.paint(":"), - if line_number.is_some() { "" } else { " " } - )?; - } - } - - if let Some(line_number) = line_number { - write!( - writer, - "{}{}:{} ", - dimmed.prefix(), - line_number, - dimmed.suffix() - )?; - } - - ctx.format_fields(writer.by_ref(), event)?; - writeln!(writer) - } -} - -impl<S, N, T> FormatEvent<S, N> for Format<Compact, T> -where - S: Subscriber + for<'a> LookupSpan<'a>, - N: for<'a> FormatFields<'a> + 'static, - T: FormatTime, -{ - fn format_event( - &self, - ctx: &FmtContext<'_, S, N>, - mut writer: Writer<'_>, - event: &Event<'_>, - ) -> fmt::Result { - #[cfg(feature = "tracing-log")] - let normalized_meta = event.normalized_metadata(); - #[cfg(feature = "tracing-log")] - let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); - #[cfg(not(feature = "tracing-log"))] - let meta = event.metadata(); - - // if the `Format` struct *also* has an ANSI color configuration, - // override the writer...the API for configuring ANSI color codes on the - // `Format` struct is deprecated, but we still need to honor those - // configurations. - if let Some(ansi) = self.ansi { - writer = writer.with_ansi(ansi); - } - - self.format_timestamp(&mut writer)?; - - if self.display_level { - let fmt_level = { - #[cfg(feature = "ansi")] - { - FmtLevel::new(meta.level(), writer.has_ansi_escapes()) - } - #[cfg(not(feature = "ansi"))] - { - FmtLevel::new(meta.level()) - } - }; - write!(writer, "{} ", fmt_level)?; - } - - if self.display_thread_name { - let current_thread = std::thread::current(); - match current_thread.name() { - Some(name) => { - write!(writer, "{} ", FmtThreadName::new(name))?; - } - // fall-back to thread id when name is absent and ids are not enabled - None if !self.display_thread_id => { - write!(writer, "{:0>2?} ", current_thread.id())?; - } - _ => {} - } - } - - if self.display_thread_id { - write!(writer, "{:0>2?} ", std::thread::current().id())?; - } - - let fmt_ctx = { - #[cfg(feature = "ansi")] - { - FmtCtx::new(ctx, event.parent(), writer.has_ansi_escapes()) - } - #[cfg(not(feature = "ansi"))] - { - FmtCtx::new(&ctx, event.parent()) - } - }; - write!(writer, "{}", fmt_ctx)?; - - let bold = writer.bold(); - let dimmed = writer.dimmed(); - - let mut needs_space = false; - if self.display_target { - write!(writer, "{}{}", bold.paint(meta.target()), dimmed.paint(":"))?; - needs_space = true; - } - - if self.display_filename { - if let Some(filename) = meta.file() { - if self.display_target { - writer.write_char(' ')?; - } - write!(writer, "{}{}", bold.paint(filename), dimmed.paint(":"))?; - needs_space = true; - } - } - - if self.display_line_number { - if let Some(line_number) = meta.line() { - write!( - writer, - "{}{}{}{}", - bold.prefix(), - line_number, - bold.suffix(), - dimmed.paint(":") - )?; - needs_space = true; - } - } - - if needs_space { - writer.write_char(' ')?; - } - - ctx.format_fields(writer.by_ref(), event)?; - - for span in ctx - .event_scope() - .into_iter() - .flat_map(crate::registry::Scope::from_root) - { - let exts = span.extensions(); - if let Some(fields) = exts.get::<FormattedFields<N>>() { - if !fields.is_empty() { - write!(writer, " {}", dimmed.paint(&fields.fields))?; - } - } - } - writeln!(writer) - } -} - -// === impl FormatFields === -impl<'writer, M> FormatFields<'writer> for M -where - M: MakeOutput<Writer<'writer>, fmt::Result>, - M::Visitor: VisitFmt + VisitOutput<fmt::Result>, -{ - fn format_fields<R: RecordFields>(&self, writer: Writer<'writer>, fields: R) -> fmt::Result { - let mut v = self.make_visitor(writer); - fields.record(&mut v); - v.finish() - } -} - -/// The default [`FormatFields`] implementation. -/// -#[derive(Debug)] -pub struct DefaultFields { - // reserve the ability to add fields to this without causing a breaking - // change in the future. - _private: (), -} - -/// The [visitor] produced by [`DefaultFields`]'s [`MakeVisitor`] implementation. -/// -/// [visitor]: super::super::field::Visit -/// [`MakeVisitor`]: super::super::field::MakeVisitor -#[derive(Debug)] -pub struct DefaultVisitor<'a> { - writer: Writer<'a>, - is_empty: bool, - result: fmt::Result, -} - -impl DefaultFields { - /// Returns a new default [`FormatFields`] implementation. - /// - pub fn new() -> Self { - Self { _private: () } - } -} - -impl Default for DefaultFields { - fn default() -> Self { - Self::new() - } -} - -impl<'a> MakeVisitor<Writer<'a>> for DefaultFields { - type Visitor = DefaultVisitor<'a>; - - #[inline] - fn make_visitor(&self, target: Writer<'a>) -> Self::Visitor { - DefaultVisitor::new(target, true) - } -} - -// === impl DefaultVisitor === - -impl<'a> DefaultVisitor<'a> { - /// Returns a new default visitor that formats to the provided `writer`. - /// - /// # Arguments - /// - `writer`: the writer to format to. - /// - `is_empty`: whether or not any fields have been previously written to - /// that writer. - pub fn new(writer: Writer<'a>, is_empty: bool) -> Self { - Self { - writer, - is_empty, - result: Ok(()), - } - } - - fn maybe_pad(&mut self) { - if self.is_empty { - self.is_empty = false; - } else { - self.result = write!(self.writer, " "); - } - } -} - -impl<'a> field::Visit for DefaultVisitor<'a> { - fn record_str(&mut self, field: &Field, value: &str) { - if self.result.is_err() { - return; - } - - if field.name() == "message" { - self.record_debug(field, &format_args!("{}", value)) - } else { - self.record_debug(field, &value) - } - } - - fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { - if let Some(source) = value.source() { - let italic = self.writer.italic(); - self.record_debug( - field, - &format_args!( - "{} {}{}{}{}", - value, - italic.paint(field.name()), - italic.paint(".sources"), - self.writer.dimmed().paint("="), - ErrorSourceList(source) - ), - ) - } else { - self.record_debug(field, &format_args!("{}", value)) - } - } - - fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { - if self.result.is_err() { - return; - } - - self.maybe_pad(); - self.result = match field.name() { - "message" => write!(self.writer, "{:?}", value), - // Skip fields that are actually log metadata that have already been handled - #[cfg(feature = "tracing-log")] - name if name.starts_with("log.") => Ok(()), - name if name.starts_with("r#") => write!( - self.writer, - "{}{}{:?}", - self.writer.italic().paint(&name[2..]), - self.writer.dimmed().paint("="), - value - ), - name => write!( - self.writer, - "{}{}{:?}", - self.writer.italic().paint(name), - self.writer.dimmed().paint("="), - value - ), - }; - } -} - -impl<'a> crate::field::VisitOutput<fmt::Result> for DefaultVisitor<'a> { - fn finish(self) -> fmt::Result { - self.result - } -} - -impl<'a> crate::field::VisitFmt for DefaultVisitor<'a> { - fn writer(&mut self) -> &mut dyn fmt::Write { - &mut self.writer - } -} - -/// Renders an error into a list of sources, *including* the error -struct ErrorSourceList<'a>(&'a (dyn std::error::Error + 'static)); - -impl<'a> Display for ErrorSourceList<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut list = f.debug_list(); - let mut curr = Some(self.0); - while let Some(curr_err) = curr { - list.entry(&format_args!("{}", curr_err)); - curr = curr_err.source(); - } - list.finish() - } -} - -struct FmtCtx<'a, S, N> { - ctx: &'a FmtContext<'a, S, N>, - span: Option<&'a span::Id>, - #[cfg(feature = "ansi")] - ansi: bool, -} - -impl<'a, S, N: 'a> FmtCtx<'a, S, N> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - #[cfg(feature = "ansi")] - pub(crate) fn new( - ctx: &'a FmtContext<'_, S, N>, - span: Option<&'a span::Id>, - ansi: bool, - ) -> Self { - Self { ctx, span, ansi } - } - - #[cfg(not(feature = "ansi"))] - pub(crate) fn new(ctx: &'a FmtContext<'_, S, N>, span: Option<&'a span::Id>) -> Self { - Self { ctx, span } - } - - fn bold(&self) -> Style { - #[cfg(feature = "ansi")] - { - if self.ansi { - return Style::new().bold(); - } - } - - Style::new() - } -} - -impl<'a, S, N: 'a> fmt::Display for FmtCtx<'a, S, N> -where - S: Subscriber + for<'lookup> LookupSpan<'lookup>, - N: for<'writer> FormatFields<'writer> + 'static, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let bold = self.bold(); - let mut seen = false; - - let span = self - .span - .and_then(|id| self.ctx.ctx.span(id)) - .or_else(|| self.ctx.ctx.lookup_current()); - - let scope = span.into_iter().flat_map(|span| span.scope().from_root()); - - for span in scope { - seen = true; - write!(f, "{}:", bold.paint(span.metadata().name()))?; - } - - if seen { - f.write_char(' ')?; - } - Ok(()) - } -} - -#[cfg(not(feature = "ansi"))] -struct Style; - -#[cfg(not(feature = "ansi"))] -impl Style { - fn new() -> Self { - Style - } - - fn bold(self) -> Self { - self - } - - fn paint(&self, d: impl fmt::Display) -> impl fmt::Display { - d - } - - fn prefix(&self) -> impl fmt::Display { - "" - } - - fn suffix(&self) -> impl fmt::Display { - "" - } -} - -struct FmtThreadName<'a> { - name: &'a str, -} - -impl<'a> FmtThreadName<'a> { - pub(crate) fn new(name: &'a str) -> Self { - Self { name } - } -} - -impl<'a> fmt::Display for FmtThreadName<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use std::sync::atomic::{ - AtomicUsize, - Ordering::{AcqRel, Acquire, Relaxed}, - }; - - // Track the longest thread name length we've seen so far in an atomic, - // so that it can be updated by any thread. - static MAX_LEN: AtomicUsize = AtomicUsize::new(0); - let len = self.name.len(); - // Snapshot the current max thread name length. - let mut max_len = MAX_LEN.load(Relaxed); - - while len > max_len { - // Try to set a new max length, if it is still the value we took a - // snapshot of. - match MAX_LEN.compare_exchange(max_len, len, AcqRel, Acquire) { - // We successfully set the new max value - Ok(_) => break, - // Another thread set a new max value since we last observed - // it! It's possible that the new length is actually longer than - // ours, so we'll loop again and check whether our length is - // still the longest. If not, we'll just use the newer value. - Err(actual) => max_len = actual, - } - } - - // pad thread name using `max_len` - write!(f, "{:>width$}", self.name, width = max_len) - } -} - -struct FmtLevel<'a> { - level: &'a Level, - #[cfg(feature = "ansi")] - ansi: bool, -} - -impl<'a> FmtLevel<'a> { - #[cfg(feature = "ansi")] - pub(crate) fn new(level: &'a Level, ansi: bool) -> Self { - Self { level, ansi } - } - - #[cfg(not(feature = "ansi"))] - pub(crate) fn new(level: &'a Level) -> Self { - Self { level } - } -} - -const TRACE_STR: &str = "TRACE"; -const DEBUG_STR: &str = "DEBUG"; -const INFO_STR: &str = " INFO"; -const WARN_STR: &str = " WARN"; -const ERROR_STR: &str = "ERROR"; - -#[cfg(not(feature = "ansi"))] -impl<'a> fmt::Display for FmtLevel<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self.level { - Level::TRACE => f.pad(TRACE_STR), - Level::DEBUG => f.pad(DEBUG_STR), - Level::INFO => f.pad(INFO_STR), - Level::WARN => f.pad(WARN_STR), - Level::ERROR => f.pad(ERROR_STR), - } - } -} - -#[cfg(feature = "ansi")] -impl<'a> fmt::Display for FmtLevel<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.ansi { - match *self.level { - Level::TRACE => write!(f, "{}", Colour::Purple.paint(TRACE_STR)), - Level::DEBUG => write!(f, "{}", Colour::Blue.paint(DEBUG_STR)), - Level::INFO => write!(f, "{}", Colour::Green.paint(INFO_STR)), - Level::WARN => write!(f, "{}", Colour::Yellow.paint(WARN_STR)), - Level::ERROR => write!(f, "{}", Colour::Red.paint(ERROR_STR)), - } - } else { - match *self.level { - Level::TRACE => f.pad(TRACE_STR), - Level::DEBUG => f.pad(DEBUG_STR), - Level::INFO => f.pad(INFO_STR), - Level::WARN => f.pad(WARN_STR), - Level::ERROR => f.pad(ERROR_STR), - } - } - } -} - -// === impl FieldFn === - -impl<'a, F> MakeVisitor<Writer<'a>> for FieldFn<F> -where - F: Fn(&mut Writer<'a>, &Field, &dyn fmt::Debug) -> fmt::Result + Clone, -{ - type Visitor = FieldFnVisitor<'a, F>; - - fn make_visitor(&self, writer: Writer<'a>) -> Self::Visitor { - FieldFnVisitor { - writer, - f: self.0.clone(), - result: Ok(()), - } - } -} - -impl<'a, F> Visit for FieldFnVisitor<'a, F> -where - F: Fn(&mut Writer<'a>, &Field, &dyn fmt::Debug) -> fmt::Result, -{ - fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { - if self.result.is_ok() { - self.result = (self.f)(&mut self.writer, field, value) - } - } -} - -impl<'a, F> VisitOutput<fmt::Result> for FieldFnVisitor<'a, F> -where - F: Fn(&mut Writer<'a>, &Field, &dyn fmt::Debug) -> fmt::Result, -{ - fn finish(self) -> fmt::Result { - self.result - } -} - -impl<'a, F> VisitFmt for FieldFnVisitor<'a, F> -where - F: Fn(&mut Writer<'a>, &Field, &dyn fmt::Debug) -> fmt::Result, -{ - fn writer(&mut self) -> &mut dyn fmt::Write { - &mut self.writer - } -} - -impl<'a, F> fmt::Debug for FieldFnVisitor<'a, F> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("FieldFnVisitor") - .field("f", &format_args!("{}", std::any::type_name::<F>())) - .field("writer", &self.writer) - .field("result", &self.result) - .finish() - } -} - -// === printing synthetic Span events === - -/// Configures what points in the span lifecycle are logged as events. -/// -/// See also [`with_span_events`](super::SubscriberBuilder.html::with_span_events). -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)] -pub struct FmtSpan(u8); - -impl FmtSpan { - /// one event when span is created - pub const NEW: FmtSpan = FmtSpan(1 << 0); - /// one event per enter of a span - pub const ENTER: FmtSpan = FmtSpan(1 << 1); - /// one event per exit of a span - pub const EXIT: FmtSpan = FmtSpan(1 << 2); - /// one event when the span is dropped - pub const CLOSE: FmtSpan = FmtSpan(1 << 3); - - /// spans are ignored (this is the default) - pub const NONE: FmtSpan = FmtSpan(0); - /// one event per enter/exit of a span - pub const ACTIVE: FmtSpan = FmtSpan(FmtSpan::ENTER.0 | FmtSpan::EXIT.0); - /// events at all points (new, enter, exit, drop) - pub const FULL: FmtSpan = - FmtSpan(FmtSpan::NEW.0 | FmtSpan::ENTER.0 | FmtSpan::EXIT.0 | FmtSpan::CLOSE.0); - - /// Check whether or not a certain flag is set for this [`FmtSpan`] - fn contains(&self, other: FmtSpan) -> bool { - self.clone() & other.clone() == other - } -} - -macro_rules! impl_fmt_span_bit_op { - ($trait:ident, $func:ident, $op:tt) => { - impl std::ops::$trait for FmtSpan { - type Output = FmtSpan; - - fn $func(self, rhs: Self) -> Self::Output { - FmtSpan(self.0 $op rhs.0) - } - } - }; -} - -macro_rules! impl_fmt_span_bit_assign_op { - ($trait:ident, $func:ident, $op:tt) => { - impl std::ops::$trait for FmtSpan { - fn $func(&mut self, rhs: Self) { - *self = FmtSpan(self.0 $op rhs.0) - } - } - }; -} - -impl_fmt_span_bit_op!(BitAnd, bitand, &); -impl_fmt_span_bit_op!(BitOr, bitor, |); -impl_fmt_span_bit_op!(BitXor, bitxor, ^); - -impl_fmt_span_bit_assign_op!(BitAndAssign, bitand_assign, &); -impl_fmt_span_bit_assign_op!(BitOrAssign, bitor_assign, |); -impl_fmt_span_bit_assign_op!(BitXorAssign, bitxor_assign, ^); - -impl Debug for FmtSpan { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut wrote_flag = false; - let mut write_flags = |flag, flag_str| -> fmt::Result { - if self.contains(flag) { - if wrote_flag { - f.write_str(" | ")?; - } - - f.write_str(flag_str)?; - wrote_flag = true; - } - - Ok(()) - }; - - if FmtSpan::NONE | self.clone() == FmtSpan::NONE { - f.write_str("FmtSpan::NONE")?; - } else { - write_flags(FmtSpan::NEW, "FmtSpan::NEW")?; - write_flags(FmtSpan::ENTER, "FmtSpan::ENTER")?; - write_flags(FmtSpan::EXIT, "FmtSpan::EXIT")?; - write_flags(FmtSpan::CLOSE, "FmtSpan::CLOSE")?; - } - - Ok(()) - } -} - -pub(super) struct FmtSpanConfig { - pub(super) kind: FmtSpan, - pub(super) fmt_timing: bool, -} - -impl FmtSpanConfig { - pub(super) fn without_time(self) -> Self { - Self { - kind: self.kind, - fmt_timing: false, - } - } - pub(super) fn with_kind(self, kind: FmtSpan) -> Self { - Self { - kind, - fmt_timing: self.fmt_timing, - } - } - pub(super) fn trace_new(&self) -> bool { - self.kind.contains(FmtSpan::NEW) - } - pub(super) fn trace_enter(&self) -> bool { - self.kind.contains(FmtSpan::ENTER) - } - pub(super) fn trace_exit(&self) -> bool { - self.kind.contains(FmtSpan::EXIT) - } - pub(super) fn trace_close(&self) -> bool { - self.kind.contains(FmtSpan::CLOSE) - } -} - -impl Debug for FmtSpanConfig { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.kind.fmt(f) - } -} - -impl Default for FmtSpanConfig { - fn default() -> Self { - Self { - kind: FmtSpan::NONE, - fmt_timing: true, - } - } -} - -pub(super) struct TimingDisplay(pub(super) u64); -impl Display for TimingDisplay { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut t = self.0 as f64; - for unit in ["ns", "µs", "ms", "s"].iter() { - if t < 10.0 { - return write!(f, "{:.2}{}", t, unit); - } else if t < 100.0 { - return write!(f, "{:.1}{}", t, unit); - } else if t < 1000.0 { - return write!(f, "{:.0}{}", t, unit); - } - t /= 1000.0; - } - write!(f, "{:.0}s", t * 1000.0) - } -} - -#[cfg(test)] -pub(super) mod test { - use crate::fmt::{test::MockMakeWriter, time::FormatTime}; - use tracing::{ - self, - dispatcher::{set_default, Dispatch}, - subscriber::with_default, - }; - - use super::*; - - use regex::Regex; - use std::{fmt, path::Path}; - - pub(crate) struct MockTime; - impl FormatTime for MockTime { - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - write!(w, "fake time") - } - } - - #[test] - fn disable_everything() { - // This test reproduces https://github.com/tokio-rs/tracing/issues/1354 - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .without_time() - .with_level(false) - .with_target(false) - .with_thread_ids(false) - .with_thread_names(false); - #[cfg(feature = "ansi")] - let subscriber = subscriber.with_ansi(false); - assert_info_hello(subscriber, make_writer, "hello\n") - } - - fn test_ansi<T>( - is_ansi: bool, - expected: &str, - builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, - ) where - Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, - T: Send + Sync + 'static, - { - let make_writer = MockMakeWriter::default(); - let subscriber = builder - .with_writer(make_writer.clone()) - .with_ansi(is_ansi) - .with_timer(MockTime); - run_test(subscriber, make_writer, expected) - } - - #[cfg(not(feature = "ansi"))] - fn test_without_ansi<T>( - expected: &str, - builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, - ) where - Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, - T: Send + Sync, - { - let make_writer = MockMakeWriter::default(); - let subscriber = builder.with_writer(make_writer).with_timer(MockTime); - run_test(subscriber, make_writer, expected) - } - - fn test_without_level<T>( - expected: &str, - builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, - ) where - Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, - T: Send + Sync + 'static, - { - let make_writer = MockMakeWriter::default(); - let subscriber = builder - .with_writer(make_writer.clone()) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime); - run_test(subscriber, make_writer, expected); - } - - #[test] - fn with_line_number_and_file_name() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_file(true) - .with_line_number(true) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime); - - let expected = Regex::new(&format!( - "^fake time tracing_subscriber::fmt::format::test: {}:[0-9]+: hello\n$", - current_path() - // if we're on Windows, the path might contain backslashes, which - // have to be escpaed before compiling the regex. - .replace('\\', "\\\\") - )) - .unwrap(); - let _default = set_default(&subscriber.into()); - tracing::info!("hello"); - let res = make_writer.get_string(); - assert!(expected.is_match(&res)); - } - - #[test] - fn with_line_number() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_line_number(true) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime); - - let expected = - Regex::new("^fake time tracing_subscriber::fmt::format::test: [0-9]+: hello\n$") - .unwrap(); - let _default = set_default(&subscriber.into()); - tracing::info!("hello"); - let res = make_writer.get_string(); - assert!(expected.is_match(&res)); - } - - #[test] - fn with_filename() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_file(true) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime); - let expected = &format!( - "fake time tracing_subscriber::fmt::format::test: {}: hello\n", - current_path(), - ); - assert_info_hello(subscriber, make_writer, expected); - } - - #[test] - fn with_thread_ids() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_thread_ids(true) - .with_ansi(false) - .with_timer(MockTime); - let expected = - "fake time INFO ThreadId(NUMERIC) tracing_subscriber::fmt::format::test: hello\n"; - - assert_info_hello_ignore_numeric(subscriber, make_writer, expected); - } - - #[test] - fn pretty_default() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .pretty() - .with_writer(make_writer.clone()) - .with_ansi(false) - .with_timer(MockTime); - let expected = format!( - r#" fake time INFO tracing_subscriber::fmt::format::test: hello - at {}:NUMERIC - -"#, - file!() - ); - - assert_info_hello_ignore_numeric(subscriber, make_writer, &expected) - } - - fn assert_info_hello(subscriber: impl Into<Dispatch>, buf: MockMakeWriter, expected: &str) { - let _default = set_default(&subscriber.into()); - tracing::info!("hello"); - let result = buf.get_string(); - - assert_eq!(expected, result) - } - - // When numeric characters are used they often form a non-deterministic value as they usually represent things like a thread id or line number. - // This assert method should be used when non-deterministic numeric characters are present. - fn assert_info_hello_ignore_numeric( - subscriber: impl Into<Dispatch>, - buf: MockMakeWriter, - expected: &str, - ) { - let _default = set_default(&subscriber.into()); - tracing::info!("hello"); - - let regex = Regex::new("[0-9]+").unwrap(); - let result = buf.get_string(); - let result_cleaned = regex.replace_all(&result, "NUMERIC"); - - assert_eq!(expected, result_cleaned) - } - - fn test_overridden_parents<T>( - expected: &str, - builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, - ) where - Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, - T: Send + Sync + 'static, - { - let make_writer = MockMakeWriter::default(); - let subscriber = builder - .with_writer(make_writer.clone()) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime) - .finish(); - - with_default(subscriber, || { - let span1 = tracing::info_span!("span1", span = 1); - let span2 = tracing::info_span!(parent: &span1, "span2", span = 2); - tracing::info!(parent: &span2, "hello"); - }); - assert_eq!(expected, make_writer.get_string()); - } - - fn test_overridden_parents_in_scope<T>( - expected1: &str, - expected2: &str, - builder: crate::fmt::SubscriberBuilder<DefaultFields, Format<T>>, - ) where - Format<T, MockTime>: FormatEvent<crate::Registry, DefaultFields>, - T: Send + Sync + 'static, - { - let make_writer = MockMakeWriter::default(); - let subscriber = builder - .with_writer(make_writer.clone()) - .with_level(false) - .with_ansi(false) - .with_timer(MockTime) - .finish(); - - with_default(subscriber, || { - let span1 = tracing::info_span!("span1", span = 1); - let span2 = tracing::info_span!(parent: &span1, "span2", span = 2); - let span3 = tracing::info_span!("span3", span = 3); - let _e3 = span3.enter(); - - tracing::info!("hello"); - assert_eq!(expected1, make_writer.get_string().as_str()); - - tracing::info!(parent: &span2, "hello"); - assert_eq!(expected2, make_writer.get_string().as_str()); - }); - } - - fn run_test(subscriber: impl Into<Dispatch>, buf: MockMakeWriter, expected: &str) { - let _default = set_default(&subscriber.into()); - tracing::info!("hello"); - assert_eq!(expected, buf.get_string()) - } - - mod default { - use super::*; - - #[test] - fn with_thread_ids() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .with_writer(make_writer.clone()) - .with_thread_ids(true) - .with_ansi(false) - .with_timer(MockTime); - let expected = - "fake time INFO ThreadId(NUMERIC) tracing_subscriber::fmt::format::test: hello\n"; - - assert_info_hello_ignore_numeric(subscriber, make_writer, expected); - } - - #[cfg(feature = "ansi")] - #[test] - fn with_ansi_true() { - let expected = "\u{1b}[2mfake time\u{1b}[0m \u{1b}[32m INFO\u{1b}[0m \u{1b}[2mtracing_subscriber::fmt::format::test\u{1b}[0m\u{1b}[2m:\u{1b}[0m hello\n"; - test_ansi(true, expected, crate::fmt::Subscriber::builder()); - } - - #[cfg(feature = "ansi")] - #[test] - fn with_ansi_false() { - let expected = "fake time INFO tracing_subscriber::fmt::format::test: hello\n"; - test_ansi(false, expected, crate::fmt::Subscriber::builder()); - } - - #[cfg(not(feature = "ansi"))] - #[test] - fn without_ansi() { - let expected = "fake time INFO tracing_subscriber::fmt::format::test: hello\n"; - test_without_ansi(expected, crate::fmt::Subscriber::builder()) - } - - #[test] - fn without_level() { - let expected = "fake time tracing_subscriber::fmt::format::test: hello\n"; - test_without_level(expected, crate::fmt::Subscriber::builder()) - } - - #[test] - fn overridden_parents() { - let expected = "fake time span1{span=1}:span2{span=2}: tracing_subscriber::fmt::format::test: hello\n"; - test_overridden_parents(expected, crate::fmt::Subscriber::builder()) - } - - #[test] - fn overridden_parents_in_scope() { - test_overridden_parents_in_scope( - "fake time span3{span=3}: tracing_subscriber::fmt::format::test: hello\n", - "fake time span1{span=1}:span2{span=2}: tracing_subscriber::fmt::format::test: hello\n", - crate::fmt::Subscriber::builder(), - ) - } - } - - mod compact { - use super::*; - - #[cfg(feature = "ansi")] - #[test] - fn with_ansi_true() { - let expected = "\u{1b}[2mfake time\u{1b}[0m \u{1b}[32m INFO\u{1b}[0m \u{1b}[1mtracing_subscriber::fmt::format::test\u{1b}[0m\u{1b}[2m:\u{1b}[0m hello\n"; - test_ansi(true, expected, crate::fmt::Subscriber::builder().compact()) - } - - #[cfg(feature = "ansi")] - #[test] - fn with_ansi_false() { - let expected = "fake time INFO tracing_subscriber::fmt::format::test: hello\n"; - test_ansi(false, expected, crate::fmt::Subscriber::builder().compact()); - } - - #[cfg(not(feature = "ansi"))] - #[test] - fn without_ansi() { - let expected = "fake time INFO tracing_subscriber::fmt::format::test: hello\n"; - test_without_ansi(expected, crate::fmt::Subscriber::builder().compact()) - } - - #[test] - fn without_level() { - let expected = "fake time tracing_subscriber::fmt::format::test: hello\n"; - test_without_level(expected, crate::fmt::Subscriber::builder().compact()); - } - - #[test] - fn overridden_parents() { - let expected = "fake time span1:span2: tracing_subscriber::fmt::format::test: hello span=1 span=2\n"; - test_overridden_parents(expected, crate::fmt::Subscriber::builder().compact()) - } - - #[test] - fn overridden_parents_in_scope() { - test_overridden_parents_in_scope( - "fake time span3: tracing_subscriber::fmt::format::test: hello span=3\n", - "fake time span1:span2: tracing_subscriber::fmt::format::test: hello span=1 span=2\n", - crate::fmt::Subscriber::builder().compact(), - ) - } - } - - mod pretty { - use super::*; - - #[test] - fn pretty_default() { - let make_writer = MockMakeWriter::default(); - let subscriber = crate::fmt::Subscriber::builder() - .pretty() - .with_writer(make_writer.clone()) - .with_ansi(false) - .with_timer(MockTime); - let expected = format!( - " fake time INFO tracing_subscriber::fmt::format::test: hello\n at {}:NUMERIC\n\n", - file!() - ); - - assert_info_hello_ignore_numeric(subscriber, make_writer, &expected) - } - } - - #[test] - fn format_nanos() { - fn fmt(t: u64) -> String { - TimingDisplay(t).to_string() - } - - assert_eq!(fmt(1), "1.00ns"); - assert_eq!(fmt(12), "12.0ns"); - assert_eq!(fmt(123), "123ns"); - assert_eq!(fmt(1234), "1.23µs"); - assert_eq!(fmt(12345), "12.3µs"); - assert_eq!(fmt(123456), "123µs"); - assert_eq!(fmt(1234567), "1.23ms"); - assert_eq!(fmt(12345678), "12.3ms"); - assert_eq!(fmt(123456789), "123ms"); - assert_eq!(fmt(1234567890), "1.23s"); - assert_eq!(fmt(12345678901), "12.3s"); - assert_eq!(fmt(123456789012), "123s"); - assert_eq!(fmt(1234567890123), "1235s"); - } - - #[test] - fn fmt_span_combinations() { - let f = FmtSpan::NONE; - assert!(!f.contains(FmtSpan::NEW)); - assert!(!f.contains(FmtSpan::ENTER)); - assert!(!f.contains(FmtSpan::EXIT)); - assert!(!f.contains(FmtSpan::CLOSE)); - - let f = FmtSpan::ACTIVE; - assert!(!f.contains(FmtSpan::NEW)); - assert!(f.contains(FmtSpan::ENTER)); - assert!(f.contains(FmtSpan::EXIT)); - assert!(!f.contains(FmtSpan::CLOSE)); - - let f = FmtSpan::FULL; - assert!(f.contains(FmtSpan::NEW)); - assert!(f.contains(FmtSpan::ENTER)); - assert!(f.contains(FmtSpan::EXIT)); - assert!(f.contains(FmtSpan::CLOSE)); - - let f = FmtSpan::NEW | FmtSpan::CLOSE; - assert!(f.contains(FmtSpan::NEW)); - assert!(!f.contains(FmtSpan::ENTER)); - assert!(!f.contains(FmtSpan::EXIT)); - assert!(f.contains(FmtSpan::CLOSE)); - } - - /// Returns the test's module path. - fn current_path() -> String { - Path::new("tracing-subscriber") - .join("src") - .join("fmt") - .join("format") - .join("mod.rs") - .to_str() - .expect("path must not contain invalid unicode") - .to_owned() - } -} diff --git a/vendor/tracing-subscriber/src/fmt/format/pretty.rs b/vendor/tracing-subscriber/src/fmt/format/pretty.rs deleted file mode 100644 index a50d08ba7..000000000 --- a/vendor/tracing-subscriber/src/fmt/format/pretty.rs +++ /dev/null @@ -1,511 +0,0 @@ -use super::*; -use crate::{ - field::{VisitFmt, VisitOutput}, - fmt::fmt_layer::{FmtContext, FormattedFields}, - registry::LookupSpan, -}; - -use std::fmt; -use tracing_core::{ - field::{self, Field}, - Event, Level, Subscriber, -}; - -#[cfg(feature = "tracing-log")] -use tracing_log::NormalizeEvent; - -use ansi_term::{Colour, Style}; - -/// An excessively pretty, human-readable event formatter. -/// -/// Unlike the [`Full`], [`Compact`], and [`Json`] formatters, this is a -/// multi-line output format. Each individual event may output multiple lines of -/// text. -/// -/// # Example Output -/// -/// <pre><font color="#4E9A06"><b>:;</b></font> <font color="#4E9A06">cargo</font> run --example fmt-pretty -/// <font color="#4E9A06"><b> Finished</b></font> dev [unoptimized + debuginfo] target(s) in 0.08s -/// <font color="#4E9A06"><b> Running</b></font> `target/debug/examples/fmt-pretty` -/// 2022-02-15T18:44:24.535324Z <font color="#4E9A06"> INFO</font> <font color="#4E9A06"><b>fmt_pretty</b></font><font color="#4E9A06">: preparing to shave yaks, </font><font color="#4E9A06"><b>number_of_yaks</b></font><font color="#4E9A06">: 3</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt-pretty.rs:16 <font color="#AAAAAA"><i>on</i></font> main -/// -/// 2022-02-15T18:44:24.535403Z <font color="#4E9A06"> INFO</font> <font color="#4E9A06"><b>fmt_pretty::yak_shave</b></font><font color="#4E9A06">: shaving yaks</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:41 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535442Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: hello! I'm gonna shave a yak, </font><font color="#75507B"><b>excitement</b></font><font color="#75507B">: "yay!"</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:16 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 1 -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535469Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: yak shaved successfully</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:25 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 1 -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535502Z <font color="#3465A4">DEBUG</font> <font color="#3465A4"><b>yak_events</b></font><font color="#3465A4">: </font><font color="#3465A4"><b>yak</b></font><font color="#3465A4">: 1, </font><font color="#3465A4"><b>shaved</b></font><font color="#3465A4">: true</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:46 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535524Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: </font><font color="#75507B"><b>yaks_shaved</b></font><font color="#75507B">: 1</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:55 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535551Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: hello! I'm gonna shave a yak, </font><font color="#75507B"><b>excitement</b></font><font color="#75507B">: "yay!"</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:16 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 2 -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535573Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: yak shaved successfully</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:25 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 2 -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535600Z <font color="#3465A4">DEBUG</font> <font color="#3465A4"><b>yak_events</b></font><font color="#3465A4">: </font><font color="#3465A4"><b>yak</b></font><font color="#3465A4">: 2, </font><font color="#3465A4"><b>shaved</b></font><font color="#3465A4">: true</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:46 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535618Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: </font><font color="#75507B"><b>yaks_shaved</b></font><font color="#75507B">: 2</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:55 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535644Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: hello! I'm gonna shave a yak, </font><font color="#75507B"><b>excitement</b></font><font color="#75507B">: "yay!"</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:16 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 3 -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535670Z <font color="#C4A000"> WARN</font> <font color="#C4A000"><b>fmt_pretty::yak_shave</b></font><font color="#C4A000">: could not locate yak</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:18 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shave</b> <font color="#AAAAAA"><i>with</i></font> <b>yak</b>: 3 -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535698Z <font color="#3465A4">DEBUG</font> <font color="#3465A4"><b>yak_events</b></font><font color="#3465A4">: </font><font color="#3465A4"><b>yak</b></font><font color="#3465A4">: 3, </font><font color="#3465A4"><b>shaved</b></font><font color="#3465A4">: false</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:46 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535720Z <font color="#CC0000">ERROR</font> <font color="#CC0000"><b>fmt_pretty::yak_shave</b></font><font color="#CC0000">: failed to shave yak, </font><font color="#CC0000"><b>yak</b></font><font color="#CC0000">: 3, </font><font color="#CC0000"><b>error</b></font><font color="#CC0000">: missing yak, </font><font color="#CC0000"><b>error.sources</b></font><font color="#CC0000">: [out of space, out of cash]</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:51 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535742Z <font color="#75507B">TRACE</font> <font color="#75507B"><b>fmt_pretty::yak_shave</b></font><font color="#75507B">: </font><font color="#75507B"><b>yaks_shaved</b></font><font color="#75507B">: 2</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt/yak_shave.rs:55 <font color="#AAAAAA"><i>on</i></font> main -/// <font color="#AAAAAA"><i>in</i></font> fmt_pretty::yak_shave::<b>shaving_yaks</b> <font color="#AAAAAA"><i>with</i></font> <b>yaks</b>: 3 -/// -/// 2022-02-15T18:44:24.535765Z <font color="#4E9A06"> INFO</font> <font color="#4E9A06"><b>fmt_pretty</b></font><font color="#4E9A06">: yak shaving completed, </font><font color="#4E9A06"><b>all_yaks_shaved</b></font><font color="#4E9A06">: false</font> -/// <font color="#AAAAAA"><i>at</i></font> examples/examples/fmt-pretty.rs:19 <font color="#AAAAAA"><i>on</i></font> main -/// </pre> -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct Pretty { - display_location: bool, -} - -/// The [visitor] produced by [`Pretty`]'s [`MakeVisitor`] implementation. -/// -/// [visitor]: field::Visit -/// [`MakeVisitor`]: crate::field::MakeVisitor -#[derive(Debug)] -pub struct PrettyVisitor<'a> { - writer: Writer<'a>, - is_empty: bool, - style: Style, - result: fmt::Result, -} - -/// An excessively pretty, human-readable [`MakeVisitor`] implementation. -/// -/// [`MakeVisitor`]: crate::field::MakeVisitor -#[derive(Debug)] -pub struct PrettyFields { - /// A value to override the provided `Writer`'s ANSI formatting - /// configuration. - /// - /// If this is `Some`, we override the `Writer`'s ANSI setting. This is - /// necessary in order to continue supporting the deprecated - /// `PrettyFields::with_ansi` method. If it is `None`, we don't override the - /// ANSI formatting configuration (because the deprecated method was not - /// called). - // TODO: when `PrettyFields::with_ansi` is removed, we can get rid - // of this entirely. - ansi: Option<bool>, -} - -// === impl Pretty === - -impl Default for Pretty { - fn default() -> Self { - Self { - display_location: true, - } - } -} - -impl Pretty { - fn style_for(level: &Level) -> Style { - match *level { - Level::TRACE => Style::new().fg(Colour::Purple), - Level::DEBUG => Style::new().fg(Colour::Blue), - Level::INFO => Style::new().fg(Colour::Green), - Level::WARN => Style::new().fg(Colour::Yellow), - Level::ERROR => Style::new().fg(Colour::Red), - } - } - - /// Sets whether the event's source code location is displayed. - /// - /// This defaults to `true`. - #[deprecated( - since = "0.3.6", - note = "all formatters now support configurable source locations. Use `Format::with_source_location` instead." - )] - pub fn with_source_location(self, display_location: bool) -> Self { - Self { - display_location, - ..self - } - } -} - -impl<C, N, T> FormatEvent<C, N> for Format<Pretty, T> -where - C: Subscriber + for<'a> LookupSpan<'a>, - N: for<'a> FormatFields<'a> + 'static, - T: FormatTime, -{ - fn format_event( - &self, - ctx: &FmtContext<'_, C, N>, - mut writer: Writer<'_>, - event: &Event<'_>, - ) -> fmt::Result { - #[cfg(feature = "tracing-log")] - let normalized_meta = event.normalized_metadata(); - #[cfg(feature = "tracing-log")] - let meta = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); - #[cfg(not(feature = "tracing-log"))] - let meta = event.metadata(); - write!(&mut writer, " ")?; - - // if the `Format` struct *also* has an ANSI color configuration, - // override the writer...the API for configuring ANSI color codes on the - // `Format` struct is deprecated, but we still need to honor those - // configurations. - if let Some(ansi) = self.ansi { - writer = writer.with_ansi(ansi); - } - - self.format_timestamp(&mut writer)?; - - let style = if self.display_level && writer.has_ansi_escapes() { - Pretty::style_for(meta.level()) - } else { - Style::new() - }; - - if self.display_level { - write!( - writer, - "{} ", - super::FmtLevel::new(meta.level(), writer.has_ansi_escapes()) - )?; - } - - if self.display_target { - let target_style = if writer.has_ansi_escapes() { - style.bold() - } else { - style - }; - write!( - writer, - "{}{}{}:", - target_style.prefix(), - meta.target(), - target_style.infix(style) - )?; - } - let line_number = if self.display_line_number { - meta.line() - } else { - None - }; - - // If the file name is disabled, format the line number right after the - // target. Otherwise, if we also display the file, it'll go on a - // separate line. - if let (Some(line_number), false, true) = ( - line_number, - self.display_filename, - self.format.display_location, - ) { - write!( - writer, - "{}{}{}:", - style.prefix(), - line_number, - style.infix(style) - )?; - } - - writer.write_char(' ')?; - - let mut v = PrettyVisitor::new(writer.by_ref(), true).with_style(style); - event.record(&mut v); - v.finish()?; - writer.write_char('\n')?; - - let dimmed = if writer.has_ansi_escapes() { - Style::new().dimmed().italic() - } else { - Style::new() - }; - let thread = self.display_thread_name || self.display_thread_id; - - if let (Some(file), true, true) = ( - meta.file(), - self.format.display_location, - self.display_filename, - ) { - write!(writer, " {} {}", dimmed.paint("at"), file,)?; - - if let Some(line) = line_number { - write!(writer, ":{}", line)?; - } - writer.write_char(if thread { ' ' } else { '\n' })?; - } else if thread { - write!(writer, " ")?; - }; - - if thread { - write!(writer, "{} ", dimmed.paint("on"))?; - let thread = std::thread::current(); - if self.display_thread_name { - if let Some(name) = thread.name() { - write!(writer, "{}", name)?; - if self.display_thread_id { - writer.write_char(' ')?; - } - } - } - if self.display_thread_id { - write!(writer, "{:?}", thread.id())?; - } - writer.write_char('\n')?; - } - - let bold = writer.bold(); - let span = event - .parent() - .and_then(|id| ctx.span(id)) - .or_else(|| ctx.lookup_current()); - - let scope = span.into_iter().flat_map(|span| span.scope()); - - for span in scope { - let meta = span.metadata(); - if self.display_target { - write!( - writer, - " {} {}::{}", - dimmed.paint("in"), - meta.target(), - bold.paint(meta.name()), - )?; - } else { - write!( - writer, - " {} {}", - dimmed.paint("in"), - bold.paint(meta.name()), - )?; - } - - let ext = span.extensions(); - let fields = &ext - .get::<FormattedFields<N>>() - .expect("Unable to find FormattedFields in extensions; this is a bug"); - if !fields.is_empty() { - write!(writer, " {} {}", dimmed.paint("with"), fields)?; - } - writer.write_char('\n')?; - } - - writer.write_char('\n') - } -} - -impl<'writer> FormatFields<'writer> for Pretty { - fn format_fields<R: RecordFields>(&self, writer: Writer<'writer>, fields: R) -> fmt::Result { - let mut v = PrettyVisitor::new(writer, true); - fields.record(&mut v); - v.finish() - } - - fn add_fields( - &self, - current: &'writer mut FormattedFields<Self>, - fields: &span::Record<'_>, - ) -> fmt::Result { - let empty = current.is_empty(); - let writer = current.as_writer(); - let mut v = PrettyVisitor::new(writer, empty); - fields.record(&mut v); - v.finish() - } -} - -// === impl PrettyFields === - -impl Default for PrettyFields { - fn default() -> Self { - Self::new() - } -} - -impl PrettyFields { - /// Returns a new default [`PrettyFields`] implementation. - pub fn new() -> Self { - // By default, don't override the `Writer`'s ANSI colors - // configuration. We'll only do this if the user calls the - // deprecated `PrettyFields::with_ansi` method. - Self { ansi: None } - } - - /// Enable ANSI encoding for formatted fields. - #[deprecated( - since = "0.3.3", - note = "Use `fmt::Subscriber::with_ansi` or `fmt::Layer::with_ansi` instead." - )] - pub fn with_ansi(self, ansi: bool) -> Self { - Self { - ansi: Some(ansi), - ..self - } - } -} - -impl<'a> MakeVisitor<Writer<'a>> for PrettyFields { - type Visitor = PrettyVisitor<'a>; - - #[inline] - fn make_visitor(&self, mut target: Writer<'a>) -> Self::Visitor { - if let Some(ansi) = self.ansi { - target = target.with_ansi(ansi); - } - PrettyVisitor::new(target, true) - } -} - -// === impl PrettyVisitor === - -impl<'a> PrettyVisitor<'a> { - /// Returns a new default visitor that formats to the provided `writer`. - /// - /// # Arguments - /// - `writer`: the writer to format to. - /// - `is_empty`: whether or not any fields have been previously written to - /// that writer. - pub fn new(writer: Writer<'a>, is_empty: bool) -> Self { - Self { - writer, - is_empty, - style: Style::default(), - result: Ok(()), - } - } - - pub(crate) fn with_style(self, style: Style) -> Self { - Self { style, ..self } - } - - fn write_padded(&mut self, value: &impl fmt::Debug) { - let padding = if self.is_empty { - self.is_empty = false; - "" - } else { - ", " - }; - self.result = write!(self.writer, "{}{:?}", padding, value); - } - - fn bold(&self) -> Style { - if self.writer.has_ansi_escapes() { - self.style.bold() - } else { - Style::new() - } - } -} - -impl<'a> field::Visit for PrettyVisitor<'a> { - fn record_str(&mut self, field: &Field, value: &str) { - if self.result.is_err() { - return; - } - - if field.name() == "message" { - self.record_debug(field, &format_args!("{}", value)) - } else { - self.record_debug(field, &value) - } - } - - fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { - if let Some(source) = value.source() { - let bold = self.bold(); - self.record_debug( - field, - &format_args!( - "{}, {}{}.sources{}: {}", - value, - bold.prefix(), - field, - bold.infix(self.style), - ErrorSourceList(source), - ), - ) - } else { - self.record_debug(field, &format_args!("{}", value)) - } - } - - fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) { - if self.result.is_err() { - return; - } - let bold = self.bold(); - match field.name() { - "message" => self.write_padded(&format_args!("{}{:?}", self.style.prefix(), value,)), - // Skip fields that are actually log metadata that have already been handled - #[cfg(feature = "tracing-log")] - name if name.starts_with("log.") => self.result = Ok(()), - name if name.starts_with("r#") => self.write_padded(&format_args!( - "{}{}{}: {:?}", - bold.prefix(), - &name[2..], - bold.infix(self.style), - value - )), - name => self.write_padded(&format_args!( - "{}{}{}: {:?}", - bold.prefix(), - name, - bold.infix(self.style), - value - )), - }; - } -} - -impl<'a> VisitOutput<fmt::Result> for PrettyVisitor<'a> { - fn finish(mut self) -> fmt::Result { - write!(&mut self.writer, "{}", self.style.suffix())?; - self.result - } -} - -impl<'a> VisitFmt for PrettyVisitor<'a> { - fn writer(&mut self) -> &mut dyn fmt::Write { - &mut self.writer - } -} diff --git a/vendor/tracing-subscriber/src/fmt/mod.rs b/vendor/tracing-subscriber/src/fmt/mod.rs deleted file mode 100644 index 3c6a6ac40..000000000 --- a/vendor/tracing-subscriber/src/fmt/mod.rs +++ /dev/null @@ -1,1324 +0,0 @@ -//! A `Subscriber` for formatting and logging `tracing` data. -//! -//! # Overview -//! -//! [`tracing`] is a framework for instrumenting Rust programs with context-aware, -//! structured, event-based diagnostic information. This crate provides an -//! implementation of the [`Subscriber`] trait that records `tracing`'s `Event`s -//! and `Span`s by formatting them as text and logging them to stdout. -//! -//! # Usage -//! -//! First, add this to your `Cargo.toml` file: -//! -//! ```toml -//! [dependencies] -//! tracing-subscriber = "0.3" -//! ``` -//! -//! *Compiler support: [requires `rustc` 1.49+][msrv]* -//! -//! [msrv]: super#supported-rust-versions -//! -//! Add the following to your executable to initialize the default subscriber: -//! ```rust -//! use tracing_subscriber; -//! -//! tracing_subscriber::fmt::init(); -//! ``` -//! -//! ## Filtering Events with Environment Variables -//! -//! The default subscriber installed by `init` enables you to filter events -//! at runtime using environment variables (using the [`EnvFilter`]). -//! -//! The filter syntax is a superset of the [`env_logger`] syntax. -//! -//! For example: -//! - Setting `RUST_LOG=debug` enables all `Span`s and `Event`s -//! set to the log level `DEBUG` or higher -//! - Setting `RUST_LOG=my_crate=trace` enables `Span`s and `Event`s -//! in `my_crate` at all log levels -//! -//! **Note**: This should **not** be called by libraries. Libraries should use -//! [`tracing`] to publish `tracing` `Event`s. -//! -//! # Configuration -//! -//! You can configure a subscriber instead of using the defaults with -//! the following functions: -//! -//! ### Subscriber -//! -//! The [`FmtSubscriber`] formats and records `tracing` events as line-oriented logs. -//! You can create one by calling: -//! -//! ```rust -//! let subscriber = tracing_subscriber::fmt() -//! // ... add configuration -//! .finish(); -//! ``` -//! -//! You can find the configuration methods for [`FmtSubscriber`] in -//! [`SubscriberBuilder`]. -//! -//! ## Formatters -//! -//! The output format used by the layer and subscriber in this module is -//! represented by implementing the [`FormatEvent`] trait, and can be -//! customized. This module provides a number of formatter implementations: -//! -//! * [`format::Full`]: The default formatter. This emits human-readable, -//! single-line logs for each event that occurs, with the current span context -//! displayed before the formatted representation of the event. See -//! [here](format::Full#example-output) for sample output. -//! -//! * [`format::Compact`]: A variant of the default formatter, optimized for -//! short line lengths. Fields from the current span context are appended to -//! the fields of the formatted event. See -//! [here](format::Compact#example-output) for sample output. -//! -//! * [`format::Pretty`]: Emits excessively pretty, multi-line logs, optimized -//! for human readability. This is primarily intended to be used in local -//! development and debugging, or for command-line applications, where -//! automated analysis and compact storage of logs is less of a priority than -//! readability and visual appeal. See [here](format::Pretty#example-output) -//! for sample output. -//! -//! * [`format::Json`]: Outputs newline-delimited JSON logs. This is intended -//! for production use with systems where structured logs are consumed as JSON -//! by analysis and viewing tools. The JSON output is not optimized for human -//! readability. See [here](format::Json#example-output) for sample output. -//! -//! ### Customizing Formatters -//! -//! The formatting of log lines for spans and events is controlled by two -//! traits, [`FormatEvent`] and [`FormatFields`]. The [`FormatEvent`] trait -//! determines the overall formatting of the log line, such as what information -//! from the event's metadata and span context is included and in what order. -//! The [`FormatFields`] trait determines how fields — both the event's -//! fields and fields on spans — are formatted. -//! -//! The [`fmt::format`] module provides several types which implement these traits, -//! many of which expose additional configuration options to customize their -//! output. The [`format::Format`] type implements common configuration used by -//! all the formatters provided in this crate, and can be used as a builder to -//! set specific formatting settings. For example: -//! -//! ``` -//! use tracing_subscriber::fmt; -//! -//! // Configure a custom event formatter -//! let format = fmt::format() -//! .with_level(false) // don't include levels in formatted output -//! .with_target(false) // don't include targets -//! .with_thread_ids(true) // include the thread ID of the current thread -//! .with_thread_names(true) // include the name of the current thread -//! .compact(); // use the `Compact` formatting style. -//! -//! // Create a `fmt` subscriber that uses our custom event format, and set it -//! // as the default. -//! tracing_subscriber::fmt() -//! .event_format(format) -//! .init(); -//! ``` -//! -//! However, if a specific output format is needed, other crates can -//! also implement [`FormatEvent`] and [`FormatFields`]. See those traits' -//! documentation for details on how to implement them. -//! -//! ## Filters -//! -//! If you want to filter the `tracing` `Events` based on environment -//! variables, you can use the [`EnvFilter`] as follows: -//! -//! ```rust -//! use tracing_subscriber::EnvFilter; -//! -//! let filter = EnvFilter::from_default_env(); -//! ``` -//! -//! As mentioned above, the [`EnvFilter`] allows `Span`s and `Event`s to -//! be filtered at runtime by setting the `RUST_LOG` environment variable. -//! -//! You can find the other available [`filter`]s in the documentation. -//! -//! ### Using Your Subscriber -//! -//! Finally, once you have configured your `Subscriber`, you need to -//! configure your executable to use it. -//! -//! A subscriber can be installed globally using: -//! ```rust -//! use tracing; -//! use tracing_subscriber::FmtSubscriber; -//! -//! let subscriber = FmtSubscriber::new(); -//! -//! tracing::subscriber::set_global_default(subscriber) -//! .map_err(|_err| eprintln!("Unable to set global default subscriber")); -//! // Note this will only fail if you try to set the global default -//! // subscriber multiple times -//! ``` -//! -//! ### Composing Layers -//! -//! Composing an [`EnvFilter`] `Layer` and a [format `Layer`][super::fmt::Layer]: -//! -//! ```rust -//! use tracing_subscriber::{fmt, EnvFilter}; -//! use tracing_subscriber::prelude::*; -//! -//! let fmt_layer = fmt::layer() -//! .with_target(false); -//! let filter_layer = EnvFilter::try_from_default_env() -//! .or_else(|_| EnvFilter::try_new("info")) -//! .unwrap(); -//! -//! tracing_subscriber::registry() -//! .with(filter_layer) -//! .with(fmt_layer) -//! .init(); -//! ``` -//! -//! [`EnvFilter`]: super::filter::EnvFilter -//! [`env_logger`]: https://docs.rs/env_logger/ -//! [`filter`]: super::filter -//! [`FmtSubscriber`]: Subscriber -//! [`Subscriber`]: -//! https://docs.rs/tracing/latest/tracing/trait.Subscriber.html -//! [`tracing`]: https://crates.io/crates/tracing -//! [`fmt::format`]: mod@crate::fmt::format -use std::{any::TypeId, error::Error, io}; -use tracing_core::{span, subscriber::Interest, Event, Metadata}; - -mod fmt_layer; -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -pub mod format; -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -pub mod time; -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -pub mod writer; - -pub use fmt_layer::{FmtContext, FormattedFields, Layer}; - -use crate::layer::Layer as _; -use crate::util::SubscriberInitExt; -use crate::{ - filter::LevelFilter, - layer, - registry::{LookupSpan, Registry}, -}; - -#[doc(inline)] -pub use self::{ - format::{format, FormatEvent, FormatFields}, - time::time, - writer::{MakeWriter, TestWriter}, -}; - -/// A `Subscriber` that logs formatted representations of `tracing` events. -/// -/// This consists of an inner `Formatter` wrapped in a layer that performs filtering. -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -#[derive(Debug)] -pub struct Subscriber< - N = format::DefaultFields, - E = format::Format<format::Full>, - F = LevelFilter, - W = fn() -> io::Stdout, -> { - inner: layer::Layered<F, Formatter<N, E, W>>, -} - -/// A `Subscriber` that logs formatted representations of `tracing` events. -/// This type only logs formatted events; it does not perform any filtering. -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -pub type Formatter< - N = format::DefaultFields, - E = format::Format<format::Full>, - W = fn() -> io::Stdout, -> = layer::Layered<fmt_layer::Layer<Registry, N, E, W>, Registry>; - -/// Configures and constructs `Subscriber`s. -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -#[derive(Debug)] -pub struct SubscriberBuilder< - N = format::DefaultFields, - E = format::Format<format::Full>, - F = LevelFilter, - W = fn() -> io::Stdout, -> { - filter: F, - inner: Layer<Registry, N, E, W>, -} - -/// Returns a new [`SubscriberBuilder`] for configuring a [formatting subscriber]. -/// -/// This is essentially shorthand for [`SubscriberBuilder::default()]`. -/// -/// # Examples -/// -/// Using [`init`] to set the default subscriber: -/// -/// ```rust -/// tracing_subscriber::fmt().init(); -/// ``` -/// -/// Configuring the output format: -/// -/// ```rust -/// -/// tracing_subscriber::fmt() -/// // Configure formatting settings. -/// .with_target(false) -/// .with_timer(tracing_subscriber::fmt::time::uptime()) -/// .with_level(true) -/// // Set the subscriber as the default. -/// .init(); -/// ``` -/// -/// [`try_init`] returns an error if the default subscriber could not be set: -/// -/// ```rust -/// use std::error::Error; -/// -/// fn init_subscriber() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { -/// tracing_subscriber::fmt() -/// // Configure the subscriber to emit logs in JSON format. -/// .json() -/// // Configure the subscriber to flatten event fields in the output JSON objects. -/// .flatten_event(true) -/// // Set the subscriber as the default, returning an error if this fails. -/// .try_init()?; -/// -/// Ok(()) -/// } -/// ``` -/// -/// Rather than setting the subscriber as the default, [`finish`] _returns_ the -/// constructed subscriber, which may then be passed to other functions: -/// -/// ```rust -/// let subscriber = tracing_subscriber::fmt() -/// .with_max_level(tracing::Level::DEBUG) -/// .compact() -/// .finish(); -/// -/// tracing::subscriber::with_default(subscriber, || { -/// // the subscriber will only be set as the default -/// // inside this closure... -/// }) -/// ``` -/// -/// [formatting subscriber]: Subscriber -/// [`SubscriberBuilder::default()`]: SubscriberBuilder::default -/// [`init`]: SubscriberBuilder::init() -/// [`try_init`]: SubscriberBuilder::try_init() -/// [`finish`]: SubscriberBuilder::finish() -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -pub fn fmt() -> SubscriberBuilder { - SubscriberBuilder::default() -} - -/// Returns a new [formatting layer] that can be [composed] with other layers to -/// construct a [`Subscriber`]. -/// -/// This is a shorthand for the equivalent [`Layer::default()`] function. -/// -/// [formatting layer]: Layer -/// [composed]: crate::layer -/// [`Layer::default()`]: Layer::default -#[cfg_attr(docsrs, doc(cfg(all(feature = "fmt", feature = "std"))))] -pub fn layer<S>() -> Layer<S> { - Layer::default() -} - -impl Subscriber { - /// The maximum [verbosity level] that is enabled by a `Subscriber` by - /// default. - /// - /// This can be overridden with the [`SubscriberBuilder::with_max_level`] method. - /// - /// [verbosity level]: tracing_core::Level - /// [`SubscriberBuilder::with_max_level`]: SubscriberBuilder::with_max_level - pub const DEFAULT_MAX_LEVEL: LevelFilter = LevelFilter::INFO; - - /// Returns a new `SubscriberBuilder` for configuring a format subscriber. - pub fn builder() -> SubscriberBuilder { - SubscriberBuilder::default() - } - - /// Returns a new format subscriber with the default configuration. - pub fn new() -> Self { - Default::default() - } -} - -impl Default for Subscriber { - fn default() -> Self { - SubscriberBuilder::default().finish() - } -} - -// === impl Subscriber === - -impl<N, E, F, W> tracing_core::Subscriber for Subscriber<N, E, F, W> -where - N: for<'writer> FormatFields<'writer> + 'static, - E: FormatEvent<Registry, N> + 'static, - F: layer::Layer<Formatter<N, E, W>> + 'static, - W: for<'writer> MakeWriter<'writer> + 'static, - layer::Layered<F, Formatter<N, E, W>>: tracing_core::Subscriber, - fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry>, -{ - #[inline] - fn register_callsite(&self, meta: &'static Metadata<'static>) -> Interest { - self.inner.register_callsite(meta) - } - - #[inline] - fn enabled(&self, meta: &Metadata<'_>) -> bool { - self.inner.enabled(meta) - } - - #[inline] - fn new_span(&self, attrs: &span::Attributes<'_>) -> span::Id { - self.inner.new_span(attrs) - } - - #[inline] - fn record(&self, span: &span::Id, values: &span::Record<'_>) { - self.inner.record(span, values) - } - - #[inline] - fn record_follows_from(&self, span: &span::Id, follows: &span::Id) { - self.inner.record_follows_from(span, follows) - } - - #[inline] - fn event_enabled(&self, event: &Event<'_>) -> bool { - self.inner.event_enabled(event) - } - - #[inline] - fn event(&self, event: &Event<'_>) { - self.inner.event(event); - } - - #[inline] - fn enter(&self, id: &span::Id) { - // TODO: add on_enter hook - self.inner.enter(id); - } - - #[inline] - fn exit(&self, id: &span::Id) { - self.inner.exit(id); - } - - #[inline] - fn current_span(&self) -> span::Current { - self.inner.current_span() - } - - #[inline] - fn clone_span(&self, id: &span::Id) -> span::Id { - self.inner.clone_span(id) - } - - #[inline] - fn try_close(&self, id: span::Id) -> bool { - self.inner.try_close(id) - } - - #[inline] - fn max_level_hint(&self) -> Option<tracing_core::LevelFilter> { - self.inner.max_level_hint() - } - - unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> { - if id == TypeId::of::<Self>() { - Some(self as *const Self as *const ()) - } else { - self.inner.downcast_raw(id) - } - } -} - -impl<'a, N, E, F, W> LookupSpan<'a> for Subscriber<N, E, F, W> -where - layer::Layered<F, Formatter<N, E, W>>: LookupSpan<'a>, -{ - type Data = <layer::Layered<F, Formatter<N, E, W>> as LookupSpan<'a>>::Data; - - fn span_data(&'a self, id: &span::Id) -> Option<Self::Data> { - self.inner.span_data(id) - } -} - -// ===== impl SubscriberBuilder ===== - -impl Default for SubscriberBuilder { - fn default() -> Self { - SubscriberBuilder { - filter: Subscriber::DEFAULT_MAX_LEVEL, - inner: Default::default(), - } - } -} - -impl<N, E, F, W> SubscriberBuilder<N, E, F, W> -where - N: for<'writer> FormatFields<'writer> + 'static, - E: FormatEvent<Registry, N> + 'static, - W: for<'writer> MakeWriter<'writer> + 'static, - F: layer::Layer<Formatter<N, E, W>> + Send + Sync + 'static, - fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry> + Send + Sync + 'static, -{ - /// Finish the builder, returning a new `FmtSubscriber`. - pub fn finish(self) -> Subscriber<N, E, F, W> { - let subscriber = self.inner.with_subscriber(Registry::default()); - Subscriber { - inner: self.filter.with_subscriber(subscriber), - } - } - - /// Install this Subscriber as the global default if one is - /// not already set. - /// - /// If the `tracing-log` feature is enabled, this will also install - /// the LogTracer to convert `Log` records into `tracing` `Event`s. - /// - /// # Errors - /// Returns an Error if the initialization was unsuccessful, likely - /// because a global subscriber was already installed by another - /// call to `try_init`. - pub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>> { - use crate::util::SubscriberInitExt; - self.finish().try_init()?; - - Ok(()) - } - - /// Install this Subscriber as the global default. - /// - /// If the `tracing-log` feature is enabled, this will also install - /// the LogTracer to convert `Log` records into `tracing` `Event`s. - /// - /// # Panics - /// Panics if the initialization was unsuccessful, likely because a - /// global subscriber was already installed by another call to `try_init`. - pub fn init(self) { - self.try_init() - .expect("Unable to install global subscriber") - } -} - -impl<N, E, F, W> From<SubscriberBuilder<N, E, F, W>> for tracing_core::Dispatch -where - N: for<'writer> FormatFields<'writer> + 'static, - E: FormatEvent<Registry, N> + 'static, - W: for<'writer> MakeWriter<'writer> + 'static, - F: layer::Layer<Formatter<N, E, W>> + Send + Sync + 'static, - fmt_layer::Layer<Registry, N, E, W>: layer::Layer<Registry> + Send + Sync + 'static, -{ - fn from(builder: SubscriberBuilder<N, E, F, W>) -> tracing_core::Dispatch { - tracing_core::Dispatch::new(builder.finish()) - } -} - -impl<N, L, T, F, W> SubscriberBuilder<N, format::Format<L, T>, F, W> -where - N: for<'writer> FormatFields<'writer> + 'static, -{ - /// Use the given [`timer`] for log message timestamps. - /// - /// See the [`time` module] for the provided timer implementations. - /// - /// Note that using the `"time`"" feature flag enables the - /// additional time formatters [`UtcTime`] and [`LocalTime`], which use the - /// [`time` crate] to provide more sophisticated timestamp formatting - /// options. - /// - /// [`timer`]: time::FormatTime - /// [`time` module]: mod@time - /// [`UtcTime`]: time::UtcTime - /// [`LocalTime`]: time::LocalTime - /// [`time` crate]: https://docs.rs/time/0.3 - pub fn with_timer<T2>(self, timer: T2) -> SubscriberBuilder<N, format::Format<L, T2>, F, W> { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.with_timer(timer), - } - } - - /// Do not emit timestamps with log messages. - pub fn without_time(self) -> SubscriberBuilder<N, format::Format<L, ()>, F, W> { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.without_time(), - } - } - - /// Configures how synthesized events are emitted at points in the [span - /// lifecycle][lifecycle]. - /// - /// The following options are available: - /// - /// - `FmtSpan::NONE`: No events will be synthesized when spans are - /// created, entered, exited, or closed. Data from spans will still be - /// included as the context for formatted events. This is the default. - /// - `FmtSpan::NEW`: An event will be synthesized when spans are created. - /// - `FmtSpan::ENTER`: An event will be synthesized when spans are entered. - /// - `FmtSpan::EXIT`: An event will be synthesized when spans are exited. - /// - `FmtSpan::CLOSE`: An event will be synthesized when a span closes. If - /// [timestamps are enabled][time] for this formatter, the generated - /// event will contain fields with the span's _busy time_ (the total - /// time for which it was entered) and _idle time_ (the total time that - /// the span existed but was not entered). - /// - `FmtSpan::ACTIVE`: An event will be synthesized when spans are entered - /// or exited. - /// - `FmtSpan::FULL`: Events will be synthesized whenever a span is - /// created, entered, exited, or closed. If timestamps are enabled, the - /// close event will contain the span's busy and idle time, as - /// described above. - /// - /// The options can be enabled in any combination. For instance, the following - /// will synthesize events whenever spans are created and closed: - /// - /// ```rust - /// use tracing_subscriber::fmt::format::FmtSpan; - /// use tracing_subscriber::fmt; - /// - /// let subscriber = fmt() - /// .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) - /// .finish(); - /// ``` - /// - /// Note that the generated events will only be part of the log output by - /// this formatter; they will not be recorded by other `Subscriber`s or by - /// `Layer`s added to this subscriber. - /// - /// [lifecycle]: https://docs.rs/tracing/latest/tracing/span/index.html#the-span-lifecycle - /// [time]: SubscriberBuilder::without_time() - pub fn with_span_events(self, kind: format::FmtSpan) -> Self { - SubscriberBuilder { - inner: self.inner.with_span_events(kind), - ..self - } - } - - /// Enable ANSI encoding for formatted events. - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] - pub fn with_ansi(self, ansi: bool) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { - SubscriberBuilder { - inner: self.inner.with_ansi(ansi), - ..self - } - } - - /// Sets whether or not an event's target is displayed. - pub fn with_target( - self, - display_target: bool, - ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { - SubscriberBuilder { - inner: self.inner.with_target(display_target), - ..self - } - } - - /// Sets whether or not an event's [source code file path][file] is - /// displayed. - /// - /// [file]: tracing_core::Metadata::file - pub fn with_file( - self, - display_filename: bool, - ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { - SubscriberBuilder { - inner: self.inner.with_file(display_filename), - ..self - } - } - - /// Sets whether or not an event's [source code line number][line] is - /// displayed. - /// - /// [line]: tracing_core::Metadata::line - pub fn with_line_number( - self, - display_line_number: bool, - ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { - SubscriberBuilder { - inner: self.inner.with_line_number(display_line_number), - ..self - } - } - - /// Sets whether or not an event's level is displayed. - pub fn with_level( - self, - display_level: bool, - ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { - SubscriberBuilder { - inner: self.inner.with_level(display_level), - ..self - } - } - - /// Sets whether or not the [name] of the current thread is displayed - /// when formatting events. - /// - /// [name]: std::thread#naming-threads - pub fn with_thread_names( - self, - display_thread_names: bool, - ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { - SubscriberBuilder { - inner: self.inner.with_thread_names(display_thread_names), - ..self - } - } - - /// Sets whether or not the [thread ID] of the current thread is displayed - /// when formatting events. - /// - /// [thread ID]: std::thread::ThreadId - pub fn with_thread_ids( - self, - display_thread_ids: bool, - ) -> SubscriberBuilder<N, format::Format<L, T>, F, W> { - SubscriberBuilder { - inner: self.inner.with_thread_ids(display_thread_ids), - ..self - } - } - - /// Sets the subscriber being built to use a less verbose formatter. - /// - /// See [`format::Compact`]. - pub fn compact(self) -> SubscriberBuilder<N, format::Format<format::Compact, T>, F, W> - where - N: for<'writer> FormatFields<'writer> + 'static, - { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.compact(), - } - } - - /// Sets the subscriber being built to use an [excessively pretty, human-readable formatter](crate::fmt::format::Pretty). - #[cfg(feature = "ansi")] - #[cfg_attr(docsrs, doc(cfg(feature = "ansi")))] - pub fn pretty( - self, - ) -> SubscriberBuilder<format::Pretty, format::Format<format::Pretty, T>, F, W> { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.pretty(), - } - } - - /// Sets the subscriber being built to use a JSON formatter. - /// - /// See [`format::Json`][super::fmt::format::Json] - #[cfg(feature = "json")] - #[cfg_attr(docsrs, doc(cfg(feature = "json")))] - pub fn json( - self, - ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> - where - N: for<'writer> FormatFields<'writer> + 'static, - { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.json(), - } - } -} - -#[cfg(feature = "json")] -#[cfg_attr(docsrs, doc(cfg(feature = "json")))] -impl<T, F, W> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { - /// Sets the json subscriber being built to flatten event metadata. - /// - /// See [`format::Json`][super::fmt::format::Json] - pub fn flatten_event( - self, - flatten_event: bool, - ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.flatten_event(flatten_event), - } - } - - /// Sets whether or not the JSON subscriber being built will include the current span - /// in formatted events. - /// - /// See [`format::Json`][super::fmt::format::Json] - pub fn with_current_span( - self, - display_current_span: bool, - ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.with_current_span(display_current_span), - } - } - - /// Sets whether or not the JSON subscriber being built will include a list (from - /// root to leaf) of all currently entered spans in formatted events. - /// - /// See [`format::Json`][super::fmt::format::Json] - pub fn with_span_list( - self, - display_span_list: bool, - ) -> SubscriberBuilder<format::JsonFields, format::Format<format::Json, T>, F, W> { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.with_span_list(display_span_list), - } - } -} - -#[cfg(feature = "env-filter")] -#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] -impl<N, E, W> SubscriberBuilder<N, E, crate::EnvFilter, W> -where - Formatter<N, E, W>: tracing_core::Subscriber + 'static, -{ - /// Configures the subscriber being built to allow filter reloading at - /// runtime. - pub fn with_filter_reloading( - self, - ) -> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W> - { - let (filter, _) = crate::reload::Layer::new(self.filter); - SubscriberBuilder { - filter, - inner: self.inner, - } - } -} - -#[cfg(feature = "env-filter")] -#[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] -impl<N, E, W> SubscriberBuilder<N, E, crate::reload::Layer<crate::EnvFilter, Formatter<N, E, W>>, W> -where - Formatter<N, E, W>: tracing_core::Subscriber + 'static, -{ - /// Returns a `Handle` that may be used to reload the constructed subscriber's - /// filter. - pub fn reload_handle(&self) -> crate::reload::Handle<crate::EnvFilter, Formatter<N, E, W>> { - self.filter.handle() - } -} - -impl<N, E, F, W> SubscriberBuilder<N, E, F, W> { - /// Sets the field formatter that the subscriber being built will use to record - /// fields. - /// - /// For example: - /// ```rust - /// use tracing_subscriber::fmt::format; - /// use tracing_subscriber::prelude::*; - /// - /// let formatter = - /// // Construct a custom formatter for `Debug` fields - /// format::debug_fn(|writer, field, value| write!(writer, "{}: {:?}", field, value)) - /// // Use the `tracing_subscriber::MakeFmtExt` trait to wrap the - /// // formatter so that a delimiter is added between fields. - /// .delimited(", "); - /// - /// let subscriber = tracing_subscriber::fmt() - /// .fmt_fields(formatter) - /// .finish(); - /// # drop(subscriber) - /// ``` - pub fn fmt_fields<N2>(self, fmt_fields: N2) -> SubscriberBuilder<N2, E, F, W> - where - N2: for<'writer> FormatFields<'writer> + 'static, - { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.fmt_fields(fmt_fields), - } - } - - /// Sets the [`EnvFilter`] that the subscriber will use to determine if - /// a span or event is enabled. - /// - /// Note that this method requires the "env-filter" feature flag to be enabled. - /// - /// If a filter was previously set, or a maximum level was set by the - /// [`with_max_level`] method, that value is replaced by the new filter. - /// - /// # Examples - /// - /// Setting a filter based on the value of the `RUST_LOG` environment - /// variable: - /// ```rust - /// use tracing_subscriber::{fmt, EnvFilter}; - /// - /// fmt() - /// .with_env_filter(EnvFilter::from_default_env()) - /// .init(); - /// ``` - /// - /// Setting a filter based on a pre-set filter directive string: - /// ```rust - /// use tracing_subscriber::fmt; - /// - /// fmt() - /// .with_env_filter("my_crate=info,my_crate::my_mod=debug,[my_span]=trace") - /// .init(); - /// ``` - /// - /// Adding additional directives to a filter constructed from an env var: - /// ```rust - /// use tracing_subscriber::{fmt, filter::{EnvFilter, LevelFilter}}; - /// - /// # fn filter() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { - /// let filter = EnvFilter::try_from_env("MY_CUSTOM_FILTER_ENV_VAR")? - /// // Set the base level when not matched by other directives to WARN. - /// .add_directive(LevelFilter::WARN.into()) - /// // Set the max level for `my_crate::my_mod` to DEBUG, overriding - /// // any directives parsed from the env variable. - /// .add_directive("my_crate::my_mod=debug".parse()?); - /// - /// fmt() - /// .with_env_filter(filter) - /// .try_init()?; - /// # Ok(())} - /// ``` - /// [`EnvFilter`]: super::filter::EnvFilter - /// [`with_max_level`]: SubscriberBuilder::with_max_level() - #[cfg(feature = "env-filter")] - #[cfg_attr(docsrs, doc(cfg(feature = "env-filter")))] - pub fn with_env_filter( - self, - filter: impl Into<crate::EnvFilter>, - ) -> SubscriberBuilder<N, E, crate::EnvFilter, W> - where - Formatter<N, E, W>: tracing_core::Subscriber + 'static, - { - let filter = filter.into(); - SubscriberBuilder { - filter, - inner: self.inner, - } - } - - /// Sets the maximum [verbosity level] that will be enabled by the - /// subscriber. - /// - /// If the max level has already been set, or a [`EnvFilter`] was added by - /// [`with_env_filter`], this replaces that configuration with the new - /// maximum level. - /// - /// # Examples - /// - /// Enable up to the `DEBUG` verbosity level: - /// ```rust - /// use tracing_subscriber::fmt; - /// use tracing::Level; - /// - /// fmt() - /// .with_max_level(Level::DEBUG) - /// .init(); - /// ``` - /// This subscriber won't record any spans or events! - /// ```rust - /// use tracing_subscriber::{fmt, filter::LevelFilter}; - /// - /// let subscriber = fmt() - /// .with_max_level(LevelFilter::OFF) - /// .finish(); - /// ``` - /// [verbosity level]: tracing_core::Level - /// [`EnvFilter`]: struct@crate::filter::EnvFilter - /// [`with_env_filter`]: fn@Self::with_env_filter - pub fn with_max_level( - self, - filter: impl Into<LevelFilter>, - ) -> SubscriberBuilder<N, E, LevelFilter, W> { - let filter = filter.into(); - SubscriberBuilder { - filter, - inner: self.inner, - } - } - - /// Sets the [event formatter][`FormatEvent`] that the subscriber being built - /// will use to format events that occur. - /// - /// The event formatter may be any type implementing the [`FormatEvent`] - /// trait, which is implemented for all functions taking a [`FmtContext`], a - /// [`Writer`], and an [`Event`]. - /// - /// # Examples - /// - /// Setting a type implementing [`FormatEvent`] as the formatter: - /// - /// ```rust - /// use tracing_subscriber::fmt::format; - /// - /// let subscriber = tracing_subscriber::fmt() - /// .event_format(format().compact()) - /// .finish(); - /// ``` - /// - /// [`Writer`]: struct@self::format::Writer - pub fn event_format<E2>(self, fmt_event: E2) -> SubscriberBuilder<N, E2, F, W> - where - E2: FormatEvent<Registry, N> + 'static, - N: for<'writer> FormatFields<'writer> + 'static, - W: for<'writer> MakeWriter<'writer> + 'static, - { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.event_format(fmt_event), - } - } - - /// Sets the [`MakeWriter`] that the subscriber being built will use to write events. - /// - /// # Examples - /// - /// Using `stderr` rather than `stdout`: - /// - /// ```rust - /// use tracing_subscriber::fmt; - /// use std::io; - /// - /// fmt() - /// .with_writer(io::stderr) - /// .init(); - /// ``` - pub fn with_writer<W2>(self, make_writer: W2) -> SubscriberBuilder<N, E, F, W2> - where - W2: for<'writer> MakeWriter<'writer> + 'static, - { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.with_writer(make_writer), - } - } - - /// Configures the subscriber to support [`libtest`'s output capturing][capturing] when used in - /// unit tests. - /// - /// See [`TestWriter`] for additional details. - /// - /// # Examples - /// - /// Using [`TestWriter`] to let `cargo test` capture test output. Note that we do not install it - /// globally as it may cause conflicts. - /// - /// ```rust - /// use tracing_subscriber::fmt; - /// use tracing::subscriber; - /// - /// subscriber::set_default( - /// fmt() - /// .with_test_writer() - /// .finish() - /// ); - /// ``` - /// - /// [capturing]: - /// https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output - /// [`TestWriter`]: writer::TestWriter - pub fn with_test_writer(self) -> SubscriberBuilder<N, E, F, TestWriter> { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.with_writer(TestWriter::default()), - } - } - - /// Updates the event formatter by applying a function to the existing event formatter. - /// - /// This sets the event formatter that the subscriber being built will use to record fields. - /// - /// # Examples - /// - /// Updating an event formatter: - /// - /// ```rust - /// let subscriber = tracing_subscriber::fmt() - /// .map_event_format(|e| e.compact()) - /// .finish(); - /// ``` - pub fn map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> SubscriberBuilder<N, E2, F, W> - where - E2: FormatEvent<Registry, N> + 'static, - N: for<'writer> FormatFields<'writer> + 'static, - W: for<'writer> MakeWriter<'writer> + 'static, - { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.map_event_format(f), - } - } - - /// Updates the field formatter by applying a function to the existing field formatter. - /// - /// This sets the field formatter that the subscriber being built will use to record fields. - /// - /// # Examples - /// - /// Updating a field formatter: - /// - /// ```rust - /// use tracing_subscriber::field::MakeExt; - /// let subscriber = tracing_subscriber::fmt() - /// .map_fmt_fields(|f| f.debug_alt()) - /// .finish(); - /// ``` - pub fn map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> SubscriberBuilder<N2, E, F, W> - where - N2: for<'writer> FormatFields<'writer> + 'static, - { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.map_fmt_fields(f), - } - } - - /// Updates the [`MakeWriter`] by applying a function to the existing [`MakeWriter`]. - /// - /// This sets the [`MakeWriter`] that the subscriber being built will use to write events. - /// - /// # Examples - /// - /// Redirect output to stderr if level is <= WARN: - /// - /// ```rust - /// use tracing::Level; - /// use tracing_subscriber::fmt::{self, writer::MakeWriterExt}; - /// - /// let stderr = std::io::stderr.with_max_level(Level::WARN); - /// let layer = tracing_subscriber::fmt() - /// .map_writer(move |w| stderr.or_else(w)) - /// .finish(); - /// ``` - pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> SubscriberBuilder<N, E, F, W2> - where - W2: for<'writer> MakeWriter<'writer> + 'static, - { - SubscriberBuilder { - filter: self.filter, - inner: self.inner.map_writer(f), - } - } -} - -/// Install a global tracing subscriber that listens for events and -/// filters based on the value of the [`RUST_LOG` environment variable], -/// if one is not already set. -/// -/// If the `tracing-log` feature is enabled, this will also install -/// the [`LogTracer`] to convert `log` records into `tracing` `Event`s. -/// -/// This is shorthand for -/// -/// ```rust -/// # fn doc() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { -/// tracing_subscriber::fmt().try_init() -/// # } -/// ``` -/// -/// -/// # Errors -/// -/// Returns an Error if the initialization was unsuccessful, -/// likely because a global subscriber was already installed by another -/// call to `try_init`. -/// -/// [`LogTracer`]: -/// https://docs.rs/tracing-log/0.1.0/tracing_log/struct.LogTracer.html -/// [`RUST_LOG` environment variable]: crate::filter::EnvFilter::DEFAULT_ENV -pub fn try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { - let builder = Subscriber::builder(); - - #[cfg(feature = "env-filter")] - let builder = builder.with_env_filter(crate::EnvFilter::from_default_env()); - - // If `env-filter` is disabled, remove the default max level filter from the - // subscriber; it will be added to the `Targets` filter instead if no filter - // is set in `RUST_LOG`. - // Replacing the default `LevelFilter` with an `EnvFilter` would imply this, - // but we can't replace the builder's filter with a `Targets` filter yet. - #[cfg(not(feature = "env-filter"))] - let builder = builder.with_max_level(LevelFilter::TRACE); - - let subscriber = builder.finish(); - #[cfg(not(feature = "env-filter"))] - let subscriber = { - use crate::{filter::Targets, layer::SubscriberExt}; - use std::{env, str::FromStr}; - let targets = match env::var("RUST_LOG") { - Ok(var) => Targets::from_str(&var) - .map_err(|e| { - eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e); - }) - .unwrap_or_default(), - Err(env::VarError::NotPresent) => { - Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL) - } - Err(e) => { - eprintln!("Ignoring `RUST_LOG`: {}", e); - Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL) - } - }; - subscriber.with(targets) - }; - - subscriber.try_init().map_err(Into::into) -} - -/// Install a global tracing subscriber that listens for events and -/// filters based on the value of the [`RUST_LOG` environment variable]. -/// -/// If the `tracing-log` feature is enabled, this will also install -/// the LogTracer to convert `Log` records into `tracing` `Event`s. -/// -/// This is shorthand for -/// -/// ```rust -/// tracing_subscriber::fmt().init() -/// ``` -/// -/// # Panics -/// Panics if the initialization was unsuccessful, likely because a -/// global subscriber was already installed by another call to `try_init`. -/// -/// [`RUST_LOG` environment variable]: crate::filter::EnvFilter::DEFAULT_ENV -pub fn init() { - try_init().expect("Unable to install global subscriber") -} - -#[cfg(test)] -mod test { - use crate::{ - filter::LevelFilter, - fmt::{ - format::{self, Format}, - time, - writer::MakeWriter, - Subscriber, - }, - }; - use std::{ - io, - sync::{Arc, Mutex, MutexGuard, TryLockError}, - }; - use tracing_core::dispatcher::Dispatch; - - pub(crate) struct MockWriter { - buf: Arc<Mutex<Vec<u8>>>, - } - - impl MockWriter { - pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self { - Self { buf } - } - - pub(crate) fn map_error<Guard>(err: TryLockError<Guard>) -> io::Error { - match err { - TryLockError::WouldBlock => io::Error::from(io::ErrorKind::WouldBlock), - TryLockError::Poisoned(_) => io::Error::from(io::ErrorKind::Other), - } - } - - pub(crate) fn buf(&self) -> io::Result<MutexGuard<'_, Vec<u8>>> { - self.buf.try_lock().map_err(Self::map_error) - } - } - - impl io::Write for MockWriter { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - self.buf()?.write(buf) - } - - fn flush(&mut self) -> io::Result<()> { - self.buf()?.flush() - } - } - - #[derive(Clone, Default)] - pub(crate) struct MockMakeWriter { - buf: Arc<Mutex<Vec<u8>>>, - } - - impl MockMakeWriter { - pub(crate) fn new(buf: Arc<Mutex<Vec<u8>>>) -> Self { - Self { buf } - } - - #[cfg(feature = "json")] - pub(crate) fn buf(&self) -> MutexGuard<'_, Vec<u8>> { - self.buf.lock().unwrap() - } - - pub(crate) fn get_string(&self) -> String { - let mut buf = self.buf.lock().expect("lock shouldn't be poisoned"); - let string = std::str::from_utf8(&buf[..]) - .expect("formatter should not have produced invalid utf-8") - .to_owned(); - buf.clear(); - string - } - } - - impl<'a> MakeWriter<'a> for MockMakeWriter { - type Writer = MockWriter; - - fn make_writer(&'a self) -> Self::Writer { - MockWriter::new(self.buf.clone()) - } - } - - #[test] - fn impls() { - let f = Format::default().with_timer(time::Uptime::default()); - let subscriber = Subscriber::builder().event_format(f).finish(); - let _dispatch = Dispatch::new(subscriber); - - let f = format::Format::default(); - let subscriber = Subscriber::builder().event_format(f).finish(); - let _dispatch = Dispatch::new(subscriber); - - let f = format::Format::default().compact(); - let subscriber = Subscriber::builder().event_format(f).finish(); - let _dispatch = Dispatch::new(subscriber); - } - - #[test] - fn subscriber_downcasts() { - let subscriber = Subscriber::builder().finish(); - let dispatch = Dispatch::new(subscriber); - assert!(dispatch.downcast_ref::<Subscriber>().is_some()); - } - - #[test] - fn subscriber_downcasts_to_parts() { - let subscriber = Subscriber::new(); - let dispatch = Dispatch::new(subscriber); - assert!(dispatch.downcast_ref::<format::DefaultFields>().is_some()); - assert!(dispatch.downcast_ref::<LevelFilter>().is_some()); - assert!(dispatch.downcast_ref::<format::Format>().is_some()) - } - - #[test] - fn is_lookup_span() { - fn assert_lookup_span<T: for<'a> crate::registry::LookupSpan<'a>>(_: T) {} - let subscriber = Subscriber::new(); - assert_lookup_span(subscriber) - } -} diff --git a/vendor/tracing-subscriber/src/fmt/time/datetime.rs b/vendor/tracing-subscriber/src/fmt/time/datetime.rs deleted file mode 100644 index 531331687..000000000 --- a/vendor/tracing-subscriber/src/fmt/time/datetime.rs +++ /dev/null @@ -1,410 +0,0 @@ -// musl as a whole is licensed under the following standard MIT license: -// -// ---------------------------------------------------------------------- -// Copyright © 2005-2020 Rich Felker, et al. -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// ---------------------------------------------------------------------- -// -// Authors/contributors include: -// -// A. Wilcox -// Ada Worcester -// Alex Dowad -// Alex Suykov -// Alexander Monakov -// Andre McCurdy -// Andrew Kelley -// Anthony G. Basile -// Aric Belsito -// Arvid Picciani -// Bartosz Brachaczek -// Benjamin Peterson -// Bobby Bingham -// Boris Brezillon -// Brent Cook -// Chris Spiegel -// Clément Vasseur -// Daniel Micay -// Daniel Sabogal -// Daurnimator -// David Carlier -// David Edelsohn -// Denys Vlasenko -// Dmitry Ivanov -// Dmitry V. Levin -// Drew DeVault -// Emil Renner Berthing -// Fangrui Song -// Felix Fietkau -// Felix Janda -// Gianluca Anzolin -// Hauke Mehrtens -// He X -// Hiltjo Posthuma -// Isaac Dunham -// Jaydeep Patil -// Jens Gustedt -// Jeremy Huntwork -// Jo-Philipp Wich -// Joakim Sindholt -// John Spencer -// Julien Ramseier -// Justin Cormack -// Kaarle Ritvanen -// Khem Raj -// Kylie McClain -// Leah Neukirchen -// Luca Barbato -// Luka Perkov -// M Farkas-Dyck (Strake) -// Mahesh Bodapati -// Markus Wichmann -// Masanori Ogino -// Michael Clark -// Michael Forney -// Mikhail Kremnyov -// Natanael Copa -// Nicholas J. Kain -// orc -// Pascal Cuoq -// Patrick Oppenlander -// Petr Hosek -// Petr Skocik -// Pierre Carrier -// Reini Urban -// Rich Felker -// Richard Pennington -// Ryan Fairfax -// Samuel Holland -// Segev Finer -// Shiz -// sin -// Solar Designer -// Stefan Kristiansson -// Stefan O'Rear -// Szabolcs Nagy -// Timo Teräs -// Trutz Behn -// Valentin Ochs -// Will Dietz -// William Haddon -// William Pitcock -// -// Portions of this software are derived from third-party works licensed -// under terms compatible with the above MIT license: -// -// The TRE regular expression implementation (src/regex/reg* and -// src/regex/tre*) is Copyright © 2001-2008 Ville Laurikari and licensed -// under a 2-clause BSD license (license text in the source files). The -// included version has been heavily modified by Rich Felker in 2012, in -// the interests of size, simplicity, and namespace cleanliness. -// -// Much of the math library code (src/math/* and src/complex/*) is -// Copyright © 1993,2004 Sun Microsystems or -// Copyright © 2003-2011 David Schultz or -// Copyright © 2003-2009 Steven G. Kargl or -// Copyright © 2003-2009 Bruce D. Evans or -// Copyright © 2008 Stephen L. Moshier or -// Copyright © 2017-2018 Arm Limited -// and labelled as such in comments in the individual source files. All -// have been licensed under extremely permissive terms. -// -// The ARM memcpy code (src/string/arm/memcpy.S) is Copyright © 2008 -// The Android Open Source Project and is licensed under a two-clause BSD -// license. It was taken from Bionic libc, used on Android. -// -// The AArch64 memcpy and memset code (src/string/aarch64/*) are -// Copyright © 1999-2019, Arm Limited. -// -// The implementation of DES for crypt (src/crypt/crypt_des.c) is -// Copyright © 1994 David Burren. It is licensed under a BSD license. -// -// The implementation of blowfish crypt (src/crypt/crypt_blowfish.c) was -// originally written by Solar Designer and placed into the public -// domain. The code also comes with a fallback permissive license for use -// in jurisdictions that may not recognize the public domain. -// -// The smoothsort implementation (src/stdlib/qsort.c) is Copyright © 2011 -// Valentin Ochs and is licensed under an MIT-style license. -// -// The x86_64 port was written by Nicholas J. Kain and is licensed under -// the standard MIT terms. -// -// The mips and microblaze ports were originally written by Richard -// Pennington for use in the ellcc project. The original code was adapted -// by Rich Felker for build system and code conventions during upstream -// integration. It is licensed under the standard MIT terms. -// -// The mips64 port was contributed by Imagination Technologies and is -// licensed under the standard MIT terms. -// -// The powerpc port was also originally written by Richard Pennington, -// and later supplemented and integrated by John Spencer. It is licensed -// under the standard MIT terms. -// -// All other files which have no copyright comments are original works -// produced specifically for use as part of this library, written either -// by Rich Felker, the main author of the library, or by one or more -// contibutors listed above. Details on authorship of individual files -// can be found in the git version control history of the project. The -// omission of copyright and license comments in each file is in the -// interest of source tree size. -// -// In addition, permission is hereby granted for all public header files -// (include/* and arch/*/bits/*) and crt files intended to be linked into -// applications (crt/*, ldso/dlstart.c, and arch/*/crt_arch.h) to omit -// the copyright notice and permission notice otherwise required by the -// license, and to use these files without any requirement of -// attribution. These files include substantial contributions from: -// -// Bobby Bingham -// John Spencer -// Nicholas J. Kain -// Rich Felker -// Richard Pennington -// Stefan Kristiansson -// Szabolcs Nagy -// -// all of whom have explicitly granted such permission. -// -// This file previously contained text expressing a belief that most of -// the files covered by the above exception were sufficiently trivial not -// to be subject to copyright, resulting in confusion over whether it -// negated the permissions granted in the license. In the spirit of -// permissive licensing, and of not having licensing issues being an -// obstacle to adoption, that text has been removed. - - -use std::fmt; - -/// A date/time type which exists primarily to convert `SystemTime` timestamps into an ISO 8601 -/// formatted string. -/// -/// Yes, this exists. Before you have a heart attack, understand that the meat of this is musl's -/// [`__secs_to_tm`][1] converted to Rust via [c2rust][2] and then cleaned up by hand as part of -/// the [kudu-rs project][3], [released under MIT][4]. -/// -/// [1] http://git.musl-libc.org/cgit/musl/tree/src/time/__secs_to_tm.c -/// [2] https://c2rust.com/ -/// [3] https://github.com/danburkert/kudu-rs/blob/c9660067e5f4c1a54143f169b5eeb49446f82e54/src/timestamp.rs#L5-L18 -/// [4] https://github.com/tokio-rs/tracing/issues/1644#issuecomment-963888244 -/// -/// All existing `strftime`-like APIs I found were unable to handle the full range of timestamps representable -/// by `SystemTime`, including `strftime` itself, since tm.tm_year is an int. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub(crate) struct DateTime { - year: i64, - month: u8, - day: u8, - hour: u8, - minute: u8, - second: u8, - nanos: u32, -} - -impl fmt::Display for DateTime { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.year > 9999 { - write!(f, "+{}", self.year)?; - } else if self.year < 0 { - write!(f, "{:05}", self.year)?; - } else { - write!(f, "{:04}", self.year)?; - } - - write!( - f, - "-{:02}-{:02}T{:02}:{:02}:{:02}.{:06}Z", - self.month, - self.day, - self.hour, - self.minute, - self.second, - self.nanos / 1_000 - ) - } -} - -impl From<std::time::SystemTime> for DateTime { - fn from(timestamp: std::time::SystemTime) -> DateTime { - let (t, nanos) = match timestamp.duration_since(std::time::UNIX_EPOCH) { - Ok(duration) => { - debug_assert!(duration.as_secs() <= std::i64::MAX as u64); - (duration.as_secs() as i64, duration.subsec_nanos()) - } - Err(error) => { - let duration = error.duration(); - debug_assert!(duration.as_secs() <= std::i64::MAX as u64); - let (secs, nanos) = (duration.as_secs() as i64, duration.subsec_nanos()); - if nanos == 0 { - (-secs, 0) - } else { - (-secs - 1, 1_000_000_000 - nanos) - } - } - }; - - // 2000-03-01 (mod 400 year, immediately after feb29 - const LEAPOCH: i64 = 946_684_800 + 86400 * (31 + 29); - const DAYS_PER_400Y: i32 = 365 * 400 + 97; - const DAYS_PER_100Y: i32 = 365 * 100 + 24; - const DAYS_PER_4Y: i32 = 365 * 4 + 1; - static DAYS_IN_MONTH: [i8; 12] = [31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29]; - - // Note(dcb): this bit is rearranged slightly to avoid integer overflow. - let mut days: i64 = (t / 86_400) - (LEAPOCH / 86_400); - let mut remsecs: i32 = (t % 86_400) as i32; - if remsecs < 0i32 { - remsecs += 86_400; - days -= 1 - } - - let mut qc_cycles: i32 = (days / i64::from(DAYS_PER_400Y)) as i32; - let mut remdays: i32 = (days % i64::from(DAYS_PER_400Y)) as i32; - if remdays < 0 { - remdays += DAYS_PER_400Y; - qc_cycles -= 1; - } - - let mut c_cycles: i32 = remdays / DAYS_PER_100Y; - if c_cycles == 4 { - c_cycles -= 1; - } - remdays -= c_cycles * DAYS_PER_100Y; - - let mut q_cycles: i32 = remdays / DAYS_PER_4Y; - if q_cycles == 25 { - q_cycles -= 1; - } - remdays -= q_cycles * DAYS_PER_4Y; - - let mut remyears: i32 = remdays / 365; - if remyears == 4 { - remyears -= 1; - } - remdays -= remyears * 365; - - let mut years: i64 = i64::from(remyears) - + 4 * i64::from(q_cycles) - + 100 * i64::from(c_cycles) - + 400 * i64::from(qc_cycles); - - let mut months: i32 = 0; - while i32::from(DAYS_IN_MONTH[months as usize]) <= remdays { - remdays -= i32::from(DAYS_IN_MONTH[months as usize]); - months += 1 - } - - if months >= 10 { - months -= 12; - years += 1; - } - - DateTime { - year: years + 2000, - month: (months + 3) as u8, - day: (remdays + 1) as u8, - hour: (remsecs / 3600) as u8, - minute: (remsecs / 60 % 60) as u8, - second: (remsecs % 60) as u8, - nanos, - } - } -} - -#[cfg(test)] -mod tests { - use std::i32; - use std::time::{Duration, UNIX_EPOCH}; - - use super::*; - - #[test] - fn test_datetime() { - let case = |expected: &str, secs: i64, micros: u32| { - let timestamp = if secs >= 0 { - UNIX_EPOCH + Duration::new(secs as u64, micros * 1_000) - } else { - (UNIX_EPOCH - Duration::new(!secs as u64 + 1, 0)) + Duration::new(0, micros * 1_000) - }; - assert_eq!( - expected, - format!("{}", DateTime::from(timestamp)), - "secs: {}, micros: {}", - secs, - micros - ) - }; - - // Mostly generated with: - // - date -jur <secs> +"%Y-%m-%dT%H:%M:%S.000000Z" - // - http://unixtimestamp.50x.eu/ - - case("1970-01-01T00:00:00.000000Z", 0, 0); - - case("1970-01-01T00:00:00.000001Z", 0, 1); - case("1970-01-01T00:00:00.500000Z", 0, 500_000); - case("1970-01-01T00:00:01.000001Z", 1, 1); - case("1970-01-01T00:01:01.000001Z", 60 + 1, 1); - case("1970-01-01T01:01:01.000001Z", 60 * 60 + 60 + 1, 1); - case( - "1970-01-02T01:01:01.000001Z", - 24 * 60 * 60 + 60 * 60 + 60 + 1, - 1, - ); - - case("1969-12-31T23:59:59.000000Z", -1, 0); - case("1969-12-31T23:59:59.000001Z", -1, 1); - case("1969-12-31T23:59:59.500000Z", -1, 500_000); - case("1969-12-31T23:58:59.000001Z", -60 - 1, 1); - case("1969-12-31T22:58:59.000001Z", -60 * 60 - 60 - 1, 1); - case( - "1969-12-30T22:58:59.000001Z", - -24 * 60 * 60 - 60 * 60 - 60 - 1, - 1, - ); - - case("2038-01-19T03:14:07.000000Z", std::i32::MAX as i64, 0); - case("2038-01-19T03:14:08.000000Z", std::i32::MAX as i64 + 1, 0); - case("1901-12-13T20:45:52.000000Z", i32::MIN as i64, 0); - case("1901-12-13T20:45:51.000000Z", i32::MIN as i64 - 1, 0); - - // Skipping these tests on windows as std::time::SysteTime range is low - // on Windows compared with that of Unix which can cause the following - // high date value tests to panic - #[cfg(not(target_os = "windows"))] - { - case("+292277026596-12-04T15:30:07.000000Z", std::i64::MAX, 0); - case("+292277026596-12-04T15:30:06.000000Z", std::i64::MAX - 1, 0); - case("-292277022657-01-27T08:29:53.000000Z", i64::MIN + 1, 0); - } - - case("1900-01-01T00:00:00.000000Z", -2208988800, 0); - case("1899-12-31T23:59:59.000000Z", -2208988801, 0); - case("0000-01-01T00:00:00.000000Z", -62167219200, 0); - case("-0001-12-31T23:59:59.000000Z", -62167219201, 0); - - case("1234-05-06T07:08:09.000000Z", -23215049511, 0); - case("-1234-05-06T07:08:09.000000Z", -101097651111, 0); - case("2345-06-07T08:09:01.000000Z", 11847456541, 0); - case("-2345-06-07T08:09:01.000000Z", -136154620259, 0); - } -} diff --git a/vendor/tracing-subscriber/src/fmt/time/mod.rs b/vendor/tracing-subscriber/src/fmt/time/mod.rs deleted file mode 100644 index e5b7c83b0..000000000 --- a/vendor/tracing-subscriber/src/fmt/time/mod.rs +++ /dev/null @@ -1,138 +0,0 @@ -//! Formatters for event timestamps. -use crate::fmt::format::Writer; -use std::fmt; -use std::time::Instant; - -mod datetime; - -#[cfg(feature = "time")] -mod time_crate; -#[cfg(feature = "time")] -#[cfg_attr(docsrs, doc(cfg(feature = "time")))] -pub use time_crate::UtcTime; - -#[cfg(feature = "local-time")] -#[cfg_attr(docsrs, doc(cfg(unsound_local_offset, feature = "local-time")))] -pub use time_crate::LocalTime; - -#[cfg(feature = "time")] -#[cfg_attr(docsrs, doc(cfg(feature = "time")))] -pub use time_crate::OffsetTime; - -/// A type that can measure and format the current time. -/// -/// This trait is used by `Format` to include a timestamp with each `Event` when it is logged. -/// -/// Notable default implementations of this trait are `SystemTime` and `()`. The former prints the -/// current time as reported by `std::time::SystemTime`, and the latter does not print the current -/// time at all. `FormatTime` is also automatically implemented for any function pointer with the -/// appropriate signature. -/// -/// The full list of provided implementations can be found in [`time`]. -/// -/// [`time`]: self -pub trait FormatTime { - /// Measure and write out the current time. - /// - /// When `format_time` is called, implementors should get the current time using their desired - /// mechanism, and write it out to the given `fmt::Write`. Implementors must insert a trailing - /// space themselves if they wish to separate the time from subsequent log message text. - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result; -} - -/// Returns a new `SystemTime` timestamp provider. -/// -/// This can then be configured further to determine how timestamps should be -/// configured. -/// -/// This is equivalent to calling -/// ```rust -/// # fn timer() -> tracing_subscriber::fmt::time::SystemTime { -/// tracing_subscriber::fmt::time::SystemTime::default() -/// # } -/// ``` -pub fn time() -> SystemTime { - SystemTime::default() -} - -/// Returns a new `Uptime` timestamp provider. -/// -/// With this timer, timestamps will be formatted with the amount of time -/// elapsed since the timestamp provider was constructed. -/// -/// This can then be configured further to determine how timestamps should be -/// configured. -/// -/// This is equivalent to calling -/// ```rust -/// # fn timer() -> tracing_subscriber::fmt::time::Uptime { -/// tracing_subscriber::fmt::time::Uptime::default() -/// # } -/// ``` -pub fn uptime() -> Uptime { - Uptime::default() -} - -impl<'a, F> FormatTime for &'a F -where - F: FormatTime, -{ - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - (*self).format_time(w) - } -} - -impl FormatTime for () { - fn format_time(&self, _: &mut Writer<'_>) -> fmt::Result { - Ok(()) - } -} - -impl FormatTime for fn(&mut Writer<'_>) -> fmt::Result { - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - (*self)(w) - } -} - -/// Retrieve and print the current wall-clock time. -#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] -pub struct SystemTime; - -/// Retrieve and print the relative elapsed wall-clock time since an epoch. -/// -/// The `Default` implementation for `Uptime` makes the epoch the current time. -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub struct Uptime { - epoch: Instant, -} - -impl Default for Uptime { - fn default() -> Self { - Uptime { - epoch: Instant::now(), - } - } -} - -impl From<Instant> for Uptime { - fn from(epoch: Instant) -> Self { - Uptime { epoch } - } -} - -impl FormatTime for SystemTime { - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - write!( - w, - "{}", - datetime::DateTime::from(std::time::SystemTime::now()) - ) - } -} - -impl FormatTime for Uptime { - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - let e = self.epoch.elapsed(); - write!(w, "{:4}.{:09}s", e.as_secs(), e.subsec_nanos()) - } -} diff --git a/vendor/tracing-subscriber/src/fmt/time/time_crate.rs b/vendor/tracing-subscriber/src/fmt/time/time_crate.rs deleted file mode 100644 index 60d57fd0b..000000000 --- a/vendor/tracing-subscriber/src/fmt/time/time_crate.rs +++ /dev/null @@ -1,470 +0,0 @@ -use crate::fmt::{format::Writer, time::FormatTime, writer::WriteAdaptor}; -use std::fmt; -use time::{format_description::well_known, formatting::Formattable, OffsetDateTime, UtcOffset}; - -/// Formats the current [local time] using a [formatter] from the [`time` crate]. -/// -/// To format the current [UTC time] instead, use the [`UtcTime`] type. -/// -/// <div class="example-wrap" style="display:inline-block"> -/// <pre class="compile_fail" style="white-space:normal;font:inherit;"> -/// <strong>Warning</strong>: The <a href = "https://docs.rs/time/0.3/time/"><code>time</code> -/// crate</a> must be compiled with <code>--cfg unsound_local_offset</code> in order to use -/// local timestamps. When this cfg is not enabled, local timestamps cannot be recorded, and -/// events will be logged without timestamps. -/// -/// Alternatively, [`OffsetTime`] can log with a local offset if it is initialized early. -/// -/// See the <a href="https://docs.rs/time/0.3.4/time/#feature-flags"><code>time</code> -/// documentation</a> for more details. -/// </pre></div> -/// -/// [local time]: time::OffsetDateTime::now_local -/// [UTC time]: time::OffsetDateTime::now_utc -/// [formatter]: time::formatting::Formattable -/// [`time` crate]: time -#[derive(Clone, Debug)] -#[cfg_attr( - docsrs, - doc(cfg(all(unsound_local_offset, feature = "time", feature = "local-time"))) -)] -#[cfg(feature = "local-time")] -pub struct LocalTime<F> { - format: F, -} - -/// Formats the current [UTC time] using a [formatter] from the [`time` crate]. -/// -/// To format the current [local time] instead, use the [`LocalTime`] type. -/// -/// [local time]: time::OffsetDateTime::now_local -/// [UTC time]: time::OffsetDateTime::now_utc -/// [formatter]: time::formatting::Formattable -/// [`time` crate]: time -#[cfg_attr(docsrs, doc(cfg(feature = "time")))] -#[derive(Clone, Debug)] -pub struct UtcTime<F> { - format: F, -} - -/// Formats the current time using a fixed offset and a [formatter] from the [`time` crate]. -/// -/// This is typically used as an alternative to [`LocalTime`]. `LocalTime` determines the offset -/// every time it formats a message, which may be unsound or fail. With `OffsetTime`, the offset is -/// determined once. This makes it possible to do so while the program is still single-threaded and -/// handle any errors. However, this also means the offset cannot change while the program is -/// running (the offset will not change across DST changes). -/// -/// [formatter]: time::formatting::Formattable -/// [`time` crate]: time -#[derive(Clone, Debug)] -#[cfg_attr(docsrs, doc(cfg(feature = "time")))] -pub struct OffsetTime<F> { - offset: time::UtcOffset, - format: F, -} - -// === impl LocalTime === - -#[cfg(feature = "local-time")] -impl LocalTime<well_known::Rfc3339> { - /// Returns a formatter that formats the current [local time] in the - /// [RFC 3339] format (a subset of the [ISO 8601] timestamp format). - /// - /// # Examples - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time}; - /// - /// let collector = tracing_subscriber::fmt() - /// .with_timer(time::LocalTime::rfc_3339()); - /// # drop(collector); - /// ``` - /// - /// [local time]: time::OffsetDateTime::now_local - /// [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339 - /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 - pub fn rfc_3339() -> Self { - Self::new(well_known::Rfc3339) - } -} - -#[cfg(feature = "local-time")] -impl<F: Formattable> LocalTime<F> { - /// Returns a formatter that formats the current [local time] using the - /// [`time` crate] with the provided provided format. The format may be any - /// type that implements the [`Formattable`] trait. - /// - /// - /// <div class="example-wrap" style="display:inline-block"> - /// <pre class="compile_fail" style="white-space:normal;font:inherit;"> - /// <strong>Warning</strong>: The <a href = "https://docs.rs/time/0.3/time/"> - /// <code>time</code> crate</a> must be compiled with <code>--cfg - /// unsound_local_offset</code> in order to use local timestamps. When this - /// cfg is not enabled, local timestamps cannot be recorded, and - /// events will be logged without timestamps. - /// - /// See the <a href="https://docs.rs/time/0.3.4/time/#feature-flags"> - /// <code>time</code> documentation</a> for more details. - /// </pre></div> - /// - /// Typically, the format will be a format description string, or one of the - /// `time` crate's [well-known formats]. - /// - /// If the format description is statically known, then the - /// [`format_description!`] macro should be used. This is identical to the - /// [`time::format_description::parse`] method, but runs at compile-time, - /// throwing an error if the format description is invalid. If the desired format - /// is not known statically (e.g., a user is providing a format string), then the - /// [`time::format_description::parse`] method should be used. Note that this - /// method is fallible. - /// - /// See the [`time` book] for details on the format description syntax. - /// - /// # Examples - /// - /// Using the [`format_description!`] macro: - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::LocalTime}; - /// use time::macros::format_description; - /// - /// let timer = LocalTime::new(format_description!("[hour]:[minute]:[second]")); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// Using [`time::format_description::parse`]: - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::LocalTime}; - /// - /// let time_format = time::format_description::parse("[hour]:[minute]:[second]") - /// .expect("format string should be valid!"); - /// let timer = LocalTime::new(time_format); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// Using the [`format_description!`] macro requires enabling the `time` - /// crate's "macros" feature flag. - /// - /// Using a [well-known format][well-known formats] (this is equivalent to - /// [`LocalTime::rfc_3339`]): - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::LocalTime}; - /// - /// let timer = LocalTime::new(time::format_description::well_known::Rfc3339); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// [local time]: time::OffsetDateTime::now_local() - /// [`time` crate]: time - /// [`Formattable`]: time::formatting::Formattable - /// [well-known formats]: time::format_description::well_known - /// [`format_description!`]: time::macros::format_description! - /// [`time::format_description::parse`]: time::format_description::parse() - /// [`time` book]: https://time-rs.github.io/book/api/format-description.html - pub fn new(format: F) -> Self { - Self { format } - } -} - -#[cfg(feature = "local-time")] -impl<F> FormatTime for LocalTime<F> -where - F: Formattable, -{ - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - let now = OffsetDateTime::now_local().map_err(|_| fmt::Error)?; - format_datetime(now, w, &self.format) - } -} - -#[cfg(feature = "local-time")] -impl<F> Default for LocalTime<F> -where - F: Formattable + Default, -{ - fn default() -> Self { - Self::new(F::default()) - } -} - -// === impl UtcTime === - -impl UtcTime<well_known::Rfc3339> { - /// Returns a formatter that formats the current [UTC time] in the - /// [RFC 3339] format, which is a subset of the [ISO 8601] timestamp format. - /// - /// # Examples - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time}; - /// - /// let collector = tracing_subscriber::fmt() - /// .with_timer(time::UtcTime::rfc_3339()); - /// # drop(collector); - /// ``` - /// - /// [local time]: time::OffsetDateTime::now_utc - /// [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339 - /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 - pub fn rfc_3339() -> Self { - Self::new(well_known::Rfc3339) - } -} - -impl<F: Formattable> UtcTime<F> { - /// Returns a formatter that formats the current [UTC time] using the - /// [`time` crate], with the provided provided format. The format may be any - /// type that implements the [`Formattable`] trait. - /// - /// Typically, the format will be a format description string, or one of the - /// `time` crate's [well-known formats]. - /// - /// If the format description is statically known, then the - /// [`format_description!`] macro should be used. This is identical to the - /// [`time::format_description::parse`] method, but runs at compile-time, - /// failing an error if the format description is invalid. If the desired format - /// is not known statically (e.g., a user is providing a format string), then the - /// [`time::format_description::parse`] method should be used. Note that this - /// method is fallible. - /// - /// See the [`time` book] for details on the format description syntax. - /// - /// # Examples - /// - /// Using the [`format_description!`] macro: - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::UtcTime}; - /// use time::macros::format_description; - /// - /// let timer = UtcTime::new(format_description!("[hour]:[minute]:[second]")); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// Using the [`format_description!`] macro requires enabling the `time` - /// crate's "macros" feature flag. - /// - /// Using [`time::format_description::parse`]: - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::UtcTime}; - /// - /// let time_format = time::format_description::parse("[hour]:[minute]:[second]") - /// .expect("format string should be valid!"); - /// let timer = UtcTime::new(time_format); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// Using a [well-known format][well-known formats] (this is equivalent to - /// [`UtcTime::rfc_3339`]): - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::UtcTime}; - /// - /// let timer = UtcTime::new(time::format_description::well_known::Rfc3339); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// [UTC time]: time::OffsetDateTime::now_utc() - /// [`time` crate]: time - /// [`Formattable`]: time::formatting::Formattable - /// [well-known formats]: time::format_description::well_known - /// [`format_description!`]: time::macros::format_description! - /// [`time::format_description::parse`]: time::format_description::parse - /// [`time` book]: https://time-rs.github.io/book/api/format-description.html - pub fn new(format: F) -> Self { - Self { format } - } -} - -impl<F> FormatTime for UtcTime<F> -where - F: Formattable, -{ - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - format_datetime(OffsetDateTime::now_utc(), w, &self.format) - } -} - -impl<F> Default for UtcTime<F> -where - F: Formattable + Default, -{ - fn default() -> Self { - Self::new(F::default()) - } -} - -// === impl OffsetTime === - -#[cfg(feature = "local-time")] -impl OffsetTime<well_known::Rfc3339> { - /// Returns a formatter that formats the current time using the [local time offset] in the [RFC - /// 3339] format (a subset of the [ISO 8601] timestamp format). - /// - /// Returns an error if the local time offset cannot be determined. This typically occurs in - /// multithreaded programs. To avoid this problem, initialize `OffsetTime` before forking - /// threads. When using Tokio, this means initializing `OffsetTime` before the Tokio runtime. - /// - /// # Examples - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time}; - /// - /// let collector = tracing_subscriber::fmt() - /// .with_timer(time::OffsetTime::local_rfc_3339().expect("could not get local offset!")); - /// # drop(collector); - /// ``` - /// - /// Using `OffsetTime` with Tokio: - /// - /// ``` - /// use tracing_subscriber::fmt::time::OffsetTime; - /// - /// #[tokio::main] - /// async fn run() { - /// tracing::info!("runtime initialized"); - /// - /// // At this point the Tokio runtime is initialized, and we can use both Tokio and Tracing - /// // normally. - /// } - /// - /// fn main() { - /// // Because we need to get the local offset before Tokio spawns any threads, our `main` - /// // function cannot use `tokio::main`. - /// tracing_subscriber::fmt() - /// .with_timer(OffsetTime::local_rfc_3339().expect("could not get local time offset")) - /// .init(); - /// - /// // Even though `run` is written as an `async fn`, because we used `tokio::main` on it - /// // we can call it as a synchronous function. - /// run(); - /// } - /// ``` - /// - /// [local time offset]: time::UtcOffset::current_local_offset - /// [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339 - /// [ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 - pub fn local_rfc_3339() -> Result<Self, time::error::IndeterminateOffset> { - Ok(Self::new( - UtcOffset::current_local_offset()?, - well_known::Rfc3339, - )) - } -} - -impl<F: time::formatting::Formattable> OffsetTime<F> { - /// Returns a formatter that formats the current time using the [`time` crate] with the provided - /// provided format and [timezone offset]. The format may be any type that implements the - /// [`Formattable`] trait. - /// - /// - /// Typically, the offset will be the [local offset], and format will be a format description - /// string, or one of the `time` crate's [well-known formats]. - /// - /// If the format description is statically known, then the - /// [`format_description!`] macro should be used. This is identical to the - /// [`time::format_description::parse`] method, but runs at compile-time, - /// throwing an error if the format description is invalid. If the desired format - /// is not known statically (e.g., a user is providing a format string), then the - /// [`time::format_description::parse`] method should be used. Note that this - /// method is fallible. - /// - /// See the [`time` book] for details on the format description syntax. - /// - /// # Examples - /// - /// Using the [`format_description!`] macro: - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::OffsetTime}; - /// use time::macros::format_description; - /// use time::UtcOffset; - /// - /// let offset = UtcOffset::current_local_offset().expect("should get local offset!"); - /// let timer = OffsetTime::new(offset, format_description!("[hour]:[minute]:[second]")); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// Using [`time::format_description::parse`]: - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::OffsetTime}; - /// use time::UtcOffset; - /// - /// let offset = UtcOffset::current_local_offset().expect("should get local offset!"); - /// let time_format = time::format_description::parse("[hour]:[minute]:[second]") - /// .expect("format string should be valid!"); - /// let timer = OffsetTime::new(offset, time_format); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// Using the [`format_description!`] macro requires enabling the `time` - /// crate's "macros" feature flag. - /// - /// Using a [well-known format][well-known formats] (this is equivalent to - /// [`OffsetTime::local_rfc_3339`]): - /// - /// ``` - /// use tracing_subscriber::fmt::{self, time::OffsetTime}; - /// use time::UtcOffset; - /// - /// let offset = UtcOffset::current_local_offset().expect("should get local offset!"); - /// let timer = OffsetTime::new(offset, time::format_description::well_known::Rfc3339); - /// let collector = tracing_subscriber::fmt() - /// .with_timer(timer); - /// # drop(collector); - /// ``` - /// - /// [`time` crate]: time - /// [timezone offset]: time::UtcOffset - /// [`Formattable`]: time::formatting::Formattable - /// [local offset]: time::UtcOffset::current_local_offset() - /// [well-known formats]: time::format_description::well_known - /// [`format_description!`]: time::macros::format_description - /// [`time::format_description::parse`]: time::format_description::parse - /// [`time` book]: https://time-rs.github.io/book/api/format-description.html - pub fn new(offset: time::UtcOffset, format: F) -> Self { - Self { offset, format } - } -} - -impl<F> FormatTime for OffsetTime<F> -where - F: time::formatting::Formattable, -{ - fn format_time(&self, w: &mut Writer<'_>) -> fmt::Result { - let now = OffsetDateTime::now_utc().to_offset(self.offset); - format_datetime(now, w, &self.format) - } -} - -fn format_datetime( - now: OffsetDateTime, - into: &mut Writer<'_>, - fmt: &impl Formattable, -) -> fmt::Result { - let mut into = WriteAdaptor::new(into); - now.format_into(&mut into, fmt) - .map_err(|_| fmt::Error) - .map(|_| ()) -} diff --git a/vendor/tracing-subscriber/src/fmt/writer.rs b/vendor/tracing-subscriber/src/fmt/writer.rs deleted file mode 100644 index 4aacd6d54..000000000 --- a/vendor/tracing-subscriber/src/fmt/writer.rs +++ /dev/null @@ -1,1464 +0,0 @@ -//! Abstractions for creating [`io::Write`] instances. -//! -//! [`io::Write`]: std::io::Write -use std::{ - fmt, - io::{self, Write}, - sync::{Arc, Mutex, MutexGuard}, -}; -use tracing_core::Metadata; - -/// A type that can create [`io::Write`] instances. -/// -/// `MakeWriter` is used by [`fmt::Layer`] or [`fmt::Subscriber`] to print -/// formatted text representations of [`Event`]s. -/// -/// This trait is already implemented for function pointers and -/// immutably-borrowing closures that return an instance of [`io::Write`], such -/// as [`io::stdout`] and [`io::stderr`]. Additionally, it is implemented for -/// [`std::sync::Mutex`][mutex] when the tyoe inside the mutex implements -/// [`io::Write`]. -/// -/// # Examples -/// -/// The simplest usage is to pass in a named function that returns a writer. For -/// example, to log all events to stderr, we could write: -/// ``` -/// let subscriber = tracing_subscriber::fmt() -/// .with_writer(std::io::stderr) -/// .finish(); -/// # drop(subscriber); -/// ``` -/// -/// Any function that returns a writer can be used: -/// -/// ``` -/// fn make_my_great_writer() -> impl std::io::Write { -/// // ... -/// # std::io::stdout() -/// } -/// -/// let subscriber = tracing_subscriber::fmt() -/// .with_writer(make_my_great_writer) -/// .finish(); -/// # drop(subscriber); -/// ``` -/// -/// A closure can be used to introduce arbitrary logic into how the writer is -/// created. Consider the (admittedly rather silly) example of sending every 5th -/// event to stderr, and all other events to stdout: -/// -/// ``` -/// use std::io; -/// use std::sync::atomic::{AtomicUsize, Ordering::Relaxed}; -/// -/// let n = AtomicUsize::new(0); -/// let subscriber = tracing_subscriber::fmt() -/// .with_writer(move || -> Box<dyn io::Write> { -/// if n.fetch_add(1, Relaxed) % 5 == 0 { -/// Box::new(io::stderr()) -/// } else { -/// Box::new(io::stdout()) -/// } -/// }) -/// .finish(); -/// # drop(subscriber); -/// ``` -/// -/// A single instance of a type implementing [`io::Write`] may be used as a -/// `MakeWriter` by wrapping it in a [`Mutex`][mutex]. For example, we could -/// write to a file like so: -/// -/// ``` -/// use std::{fs::File, sync::Mutex}; -/// -/// # fn docs() -> Result<(), Box<dyn std::error::Error>> { -/// let log_file = File::create("my_cool_trace.log")?; -/// let subscriber = tracing_subscriber::fmt() -/// .with_writer(Mutex::new(log_file)) -/// .finish(); -/// # drop(subscriber); -/// # Ok(()) -/// # } -/// ``` -/// -/// [`io::Write`]: std::io::Write -/// [`fmt::Layer`]: crate::fmt::Layer -/// [`fmt::Subscriber`]: crate::fmt::Subscriber -/// [`Event`]: tracing_core::event::Event -/// [`io::stdout`]: std::io::stdout() -/// [`io::stderr`]: std::io::stderr() -/// [mutex]: std::sync::Mutex -/// [`MakeWriter::make_writer_for`]: MakeWriter::make_writer_for -/// [`Metadata`]: tracing_core::Metadata -/// [levels]: tracing_core::Level -/// [targets]: tracing_core::Metadata::target -pub trait MakeWriter<'a> { - /// The concrete [`io::Write`] implementation returned by [`make_writer`]. - /// - /// [`io::Write`]: std::io::Write - /// [`make_writer`]: MakeWriter::make_writer - type Writer: io::Write; - - /// Returns an instance of [`Writer`]. - /// - /// # Implementer notes - /// - /// [`fmt::Layer`] or [`fmt::Subscriber`] will call this method each time an event is recorded. Ensure any state - /// that must be saved across writes is not lost when the [`Writer`] instance is dropped. If - /// creating a [`io::Write`] instance is expensive, be sure to cache it when implementing - /// [`MakeWriter`] to improve performance. - /// - /// [`Writer`]: MakeWriter::Writer - /// [`fmt::Layer`]: crate::fmt::Layer - /// [`fmt::Subscriber`]: crate::fmt::Subscriber - /// [`io::Write`]: std::io::Write - fn make_writer(&'a self) -> Self::Writer; - - /// Returns a [`Writer`] for writing data from the span or event described - /// by the provided [`Metadata`]. - /// - /// By default, this calls [`self.make_writer()`][make_writer], ignoring - /// the provided metadata, but implementations can override this to provide - /// metadata-specific behaviors. - /// - /// This method allows `MakeWriter` implementations to implement different - /// behaviors based on the span or event being written. The `MakeWriter` - /// type might return different writers based on the provided metadata, or - /// might write some values to the writer before or after providing it to - /// the caller. - /// - /// For example, we might want to write data from spans and events at the - /// [`ERROR`] and [`WARN`] levels to `stderr`, and data from spans or events - /// at lower levels to stdout: - /// - /// ``` - /// use std::io::{self, Stdout, Stderr, StdoutLock, StderrLock}; - /// use tracing_subscriber::fmt::writer::MakeWriter; - /// use tracing_core::{Metadata, Level}; - /// - /// pub struct MyMakeWriter { - /// stdout: Stdout, - /// stderr: Stderr, - /// } - /// - /// /// A lock on either stdout or stderr, depending on the verbosity level - /// /// of the event being written. - /// pub enum StdioLock<'a> { - /// Stdout(StdoutLock<'a>), - /// Stderr(StderrLock<'a>), - /// } - /// - /// impl<'a> io::Write for StdioLock<'a> { - /// fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - /// match self { - /// StdioLock::Stdout(lock) => lock.write(buf), - /// StdioLock::Stderr(lock) => lock.write(buf), - /// } - /// } - /// - /// fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - /// // ... - /// # match self { - /// # StdioLock::Stdout(lock) => lock.write_all(buf), - /// # StdioLock::Stderr(lock) => lock.write_all(buf), - /// # } - /// } - /// - /// fn flush(&mut self) -> io::Result<()> { - /// // ... - /// # match self { - /// # StdioLock::Stdout(lock) => lock.flush(), - /// # StdioLock::Stderr(lock) => lock.flush(), - /// # } - /// } - /// } - /// - /// impl<'a> MakeWriter<'a> for MyMakeWriter { - /// type Writer = StdioLock<'a>; - /// - /// fn make_writer(&'a self) -> Self::Writer { - /// // We must have an implementation of `make_writer` that makes - /// // a "default" writer without any configuring metadata. Let's - /// // just return stdout in that case. - /// StdioLock::Stdout(self.stdout.lock()) - /// } - /// - /// fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - /// // Here's where we can implement our special behavior. We'll - /// // check if the metadata's verbosity level is WARN or ERROR, - /// // and return stderr in that case. - /// if meta.level() <= &Level::WARN { - /// return StdioLock::Stderr(self.stderr.lock()); - /// } - /// - /// // Otherwise, we'll return stdout. - /// StdioLock::Stdout(self.stdout.lock()) - /// } - /// } - /// ``` - /// - /// [`Writer`]: MakeWriter::Writer - /// [`Metadata`]: tracing_core::Metadata - /// [make_writer]: MakeWriter::make_writer - /// [`WARN`]: tracing_core::Level::WARN - /// [`ERROR`]: tracing_core::Level::ERROR - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - let _ = meta; - self.make_writer() - } -} - -/// Extension trait adding combinators for working with types implementing -/// [`MakeWriter`]. -/// -/// This is not intended to be implemented directly for user-defined -/// [`MakeWriter`]s; instead, it should be imported when the desired methods are -/// used. -pub trait MakeWriterExt<'a>: MakeWriter<'a> { - /// Wraps `self` and returns a [`MakeWriter`] that will only write output - /// for events at or below the provided verbosity [`Level`]. For instance, - /// `Level::TRACE` is considered to be _more verbose` than `Level::INFO`. - /// - /// Events whose level is more verbose than `level` will be ignored, and no - /// output will be written. - /// - /// # Examples - /// - /// ``` - /// use tracing::Level; - /// use tracing_subscriber::fmt::writer::MakeWriterExt; - /// - /// // Construct a writer that outputs events to `stderr` only if the span or - /// // event's level is >= WARN (WARN and ERROR). - /// let mk_writer = std::io::stderr.with_max_level(Level::WARN); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// ``` - /// - /// Writing the `ERROR` and `WARN` levels to `stderr`, and everything else - /// to `stdout`: - /// - /// ``` - /// # use tracing::Level; - /// # use tracing_subscriber::fmt::writer::MakeWriterExt; - /// - /// let mk_writer = std::io::stderr - /// .with_max_level(Level::WARN) - /// .or_else(std::io::stdout); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// ``` - /// - /// Writing the `ERROR` level to `stderr`, the `INFO` and `WARN` levels to - /// `stdout`, and the `INFO` and DEBUG` levels to a file: - /// - /// ``` - /// # use tracing::Level; - /// # use tracing_subscriber::fmt::writer::MakeWriterExt; - /// use std::{sync::Arc, fs::File}; - /// # // don't actually create the file when running the tests. - /// # fn docs() -> std::io::Result<()> { - /// let debug_log = Arc::new(File::create("debug.log")?); - /// - /// let mk_writer = std::io::stderr - /// .with_max_level(Level::ERROR) - /// .or_else(std::io::stdout - /// .with_max_level(Level::INFO) - /// .and(debug_log.with_max_level(Level::DEBUG)) - /// ); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// # Ok(()) } - /// ``` - /// - /// [`Level`]: tracing_core::Level - /// [`io::Write`]: std::io::Write - fn with_max_level(self, level: tracing_core::Level) -> WithMaxLevel<Self> - where - Self: Sized, - { - WithMaxLevel::new(self, level) - } - - /// Wraps `self` and returns a [`MakeWriter`] that will only write output - /// for events at or above the provided verbosity [`Level`]. - /// - /// Events whose level is less verbose than `level` will be ignored, and no - /// output will be written. - /// - /// # Examples - /// - /// ``` - /// use tracing::Level; - /// use tracing_subscriber::fmt::writer::MakeWriterExt; - /// - /// // Construct a writer that outputs events to `stdout` only if the span or - /// // event's level is <= DEBUG (DEBUG and TRACE). - /// let mk_writer = std::io::stdout.with_min_level(Level::DEBUG); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// ``` - /// This can be combined with [`MakeWriterExt::with_max_level`] to write - /// only within a range of levels: - /// - /// ``` - /// # use tracing::Level; - /// # use tracing_subscriber::fmt::writer::MakeWriterExt; - /// // Only write the `DEBUG` and `INFO` levels to stdout. - /// let mk_writer = std::io::stdout - /// .with_max_level(Level::DEBUG) - /// .with_min_level(Level::INFO) - /// // Write the `WARN` and `ERROR` levels to stderr. - /// .and(std::io::stderr.with_min_level(Level::WARN)); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// ``` - /// [`Level`]: tracing_core::Level - /// [`io::Write`]: std::io::Write - fn with_min_level(self, level: tracing_core::Level) -> WithMinLevel<Self> - where - Self: Sized, - { - WithMinLevel::new(self, level) - } - - /// Wraps `self` with a predicate that takes a span or event's [`Metadata`] - /// and returns a `bool`. The returned [`MakeWriter`]'s - /// [`MakeWriter::make_writer_for`][mwf] method will check the predicate to - /// determine if a writer should be produced for a given span or event. - /// - /// If the predicate returns `false`, the wrapped [`MakeWriter`]'s - /// [`make_writer_for`][mwf] will return [`OptionalWriter::none`][own]. - /// Otherwise, it calls the wrapped [`MakeWriter`]'s - /// [`make_writer_for`][mwf] method, and returns the produced writer. - /// - /// This can be used to filter an output based on arbitrary [`Metadata`] - /// parameters. - /// - /// # Examples - /// - /// Writing events with a specific target to an HTTP access log, and other - /// events to stdout: - /// - /// ``` - /// use tracing_subscriber::fmt::writer::MakeWriterExt; - /// use std::{sync::Arc, fs::File}; - /// # // don't actually create the file when running the tests. - /// # fn docs() -> std::io::Result<()> { - /// let access_log = Arc::new(File::create("access.log")?); - /// - /// let mk_writer = access_log - /// // Only write events with the target "http::access_log" to the - /// // access log file. - /// .with_filter(|meta| meta.target() == "http::access_log") - /// // Write events with all other targets to stdout. - /// .or_else(std::io::stdout); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// # Ok(()) - /// # } - /// ``` - /// - /// Conditionally enabling or disabling a log file: - /// ``` - /// use tracing_subscriber::fmt::writer::MakeWriterExt; - /// use std::{ - /// sync::{Arc, atomic::{AtomicBool, Ordering}}, - /// fs::File, - /// }; - /// - /// static DEBUG_LOG_ENABLED: AtomicBool = AtomicBool::new(false); - /// - /// # // don't actually create the file when running the tests. - /// # fn docs() -> std::io::Result<()> { - /// // Create the debug log file - /// let debug_file = Arc::new(File::create("debug.log")?) - /// // Enable the debug log only if the flag is enabled. - /// .with_filter(|_| DEBUG_LOG_ENABLED.load(Ordering::Acquire)); - /// - /// // Always write to stdout - /// let mk_writer = std::io::stdout - /// // Write to the debug file if it's enabled - /// .and(debug_file); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// - /// // ... - /// - /// // Later, we can toggle on or off the debug log file. - /// DEBUG_LOG_ENABLED.store(true, Ordering::Release); - /// # Ok(()) - /// # } - /// ``` - /// - /// [`Metadata`]: tracing_core::Metadata - /// [mwf]: MakeWriter::make_writer_for - /// [own]: EitherWriter::none - fn with_filter<F>(self, filter: F) -> WithFilter<Self, F> - where - Self: Sized, - F: Fn(&Metadata<'_>) -> bool, - { - WithFilter::new(self, filter) - } - - /// Combines `self` with another type implementing [`MakeWriter`], returning - /// a new [`MakeWriter`] that produces [writers] that write to *both* - /// outputs. - /// - /// If writing to either writer returns an error, the returned writer will - /// return that error. However, both writers will still be written to before - /// the error is returned, so it is possible for one writer to fail while - /// the other is written to successfully. - /// - /// # Examples - /// - /// ``` - /// use tracing_subscriber::fmt::writer::MakeWriterExt; - /// - /// // Construct a writer that outputs events to `stdout` *and* `stderr`. - /// let mk_writer = std::io::stdout.and(std::io::stderr); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// ``` - /// - /// `and` can be used in conjunction with filtering combinators. For - /// example, if we want to write to a number of outputs depending on the - /// level of an event, we could write: - /// - /// ``` - /// use tracing::Level; - /// # use tracing_subscriber::fmt::writer::MakeWriterExt; - /// use std::{sync::Arc, fs::File}; - /// # // don't actually create the file when running the tests. - /// # fn docs() -> std::io::Result<()> { - /// let debug_log = Arc::new(File::create("debug.log")?); - /// - /// // Write everything to the debug log. - /// let mk_writer = debug_log - /// // Write the `ERROR` and `WARN` levels to stderr. - /// .and(std::io::stderr.with_max_level(Level::WARN)) - /// // Write `INFO` to `stdout`. - /// .and(std::io::stdout - /// .with_max_level(Level::INFO) - /// .with_min_level(Level::INFO) - /// ); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// # Ok(()) } - /// ``` - /// - /// [writers]: std::io::Write - fn and<B>(self, other: B) -> Tee<Self, B> - where - Self: Sized, - B: MakeWriter<'a> + Sized, - { - Tee::new(self, other) - } - - /// Combines `self` with another type implementing [`MakeWriter`], returning - /// a new [`MakeWriter`] that calls `other`'s [`make_writer`] if `self`'s - /// `make_writer` returns [`OptionalWriter::none`][own]. - /// - /// # Examples - /// - /// ``` - /// use tracing::Level; - /// use tracing_subscriber::fmt::writer::MakeWriterExt; - /// - /// // Produces a writer that writes to `stderr` if the level is >= WARN, - /// // or returns `OptionalWriter::none()` otherwise. - /// let stderr = std::io::stderr.with_max_level(Level::WARN); - /// - /// // If the `stderr` `MakeWriter` is disabled by the max level filter, - /// // write to stdout instead: - /// let mk_writer = stderr.or_else(std::io::stdout); - /// - /// tracing_subscriber::fmt().with_writer(mk_writer).init(); - /// ``` - /// - /// [`make_writer`]: MakeWriter::make_writer - /// [own]: EitherWriter::none - fn or_else<W, B>(self, other: B) -> OrElse<Self, B> - where - Self: MakeWriter<'a, Writer = OptionalWriter<W>> + Sized, - B: MakeWriter<'a> + Sized, - W: Write, - { - OrElse::new(self, other) - } -} - -/// A writer intended to support [`libtest`'s output capturing][capturing] for use in unit tests. -/// -/// `TestWriter` is used by [`fmt::Subscriber`] or [`fmt::Layer`] to enable capturing support. -/// -/// `cargo test` can only capture output from the standard library's [`print!`] macro. See -/// [`libtest`'s output capturing][capturing] for more details about output capturing. -/// -/// Writing to [`io::stdout`] and [`io::stderr`] produces the same results as using -/// [`libtest`'s `--nocapture` option][nocapture] which may make the results look unreadable. -/// -/// [`fmt::Subscriber`]: super::Subscriber -/// [`fmt::Layer`]: super::Layer -/// [capturing]: https://doc.rust-lang.org/book/ch11-02-running-tests.html#showing-function-output -/// [nocapture]: https://doc.rust-lang.org/cargo/commands/cargo-test.html -/// [`io::stdout`]: std::io::stdout -/// [`io::stderr`]: std::io::stderr -/// [`print!`]: std::print! -#[derive(Default, Debug)] -pub struct TestWriter { - _p: (), -} - -/// A writer that erases the specific [`io::Write`] and [`MakeWriter`] types being used. -/// -/// This is useful in cases where the concrete type of the writer cannot be known -/// until runtime. -/// -/// # Examples -/// -/// A function that returns a [`Subscriber`] that will write to either stdout or stderr: -/// -/// ```rust -/// # use tracing::Subscriber; -/// # use tracing_subscriber::fmt::writer::BoxMakeWriter; -/// -/// fn dynamic_writer(use_stderr: bool) -> impl Subscriber { -/// let writer = if use_stderr { -/// BoxMakeWriter::new(std::io::stderr) -/// } else { -/// BoxMakeWriter::new(std::io::stdout) -/// }; -/// -/// tracing_subscriber::fmt().with_writer(writer).finish() -/// } -/// ``` -/// -/// [`Subscriber`]: tracing::Subscriber -/// [`io::Write`]: std::io::Write -pub struct BoxMakeWriter { - inner: Box<dyn for<'a> MakeWriter<'a, Writer = Box<dyn Write + 'a>> + Send + Sync>, - name: &'static str, -} - -/// A [writer] that is one of two types implementing [`io::Write`][writer]. -/// -/// This may be used by [`MakeWriter`] implementations that may conditionally -/// return one of two writers. -/// -/// [writer]: std::io::Write -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub enum EitherWriter<A, B> { - /// A writer of type `A`. - A(A), - /// A writer of type `B`. - B(B), -} - -/// A [writer] which may or may not be enabled. -/// -/// This may be used by [`MakeWriter`] implementations that wish to -/// conditionally enable or disable the returned writer based on a span or -/// event's [`Metadata`]. -/// -/// [writer]: std::io::Write -pub type OptionalWriter<T> = EitherWriter<T, std::io::Sink>; - -/// A [`MakeWriter`] combinator that only returns an enabled [writer] for spans -/// and events with metadata at or below a specified verbosity [`Level`]. -/// -/// This is returned by the [`MakeWriterExt::with_max_level`] method. See the -/// method documentation for details. -/// -/// [writer]: std::io::Write -/// [`Level`]: tracing_core::Level -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct WithMaxLevel<M> { - make: M, - level: tracing_core::Level, -} - -/// A [`MakeWriter`] combinator that only returns an enabled [writer] for spans -/// and events with metadata at or above a specified verbosity [`Level`]. -/// -/// This is returned by the [`MakeWriterExt::with_min_level`] method. See the -/// method documentation for details. -/// -/// [writer]: std::io::Write -/// [`Level`]: tracing_core::Level -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct WithMinLevel<M> { - make: M, - level: tracing_core::Level, -} - -/// A [`MakeWriter`] combinator that wraps a [`MakeWriter`] with a predicate for -/// span and event [`Metadata`], so that the [`MakeWriter::make_writer_for`] -/// method returns [`OptionalWriter::some`][ows] when the predicate returns `true`, -/// and [`OptionalWriter::none`][own] when the predicate returns `false`. -/// -/// This is returned by the [`MakeWriterExt::with_filter`] method. See the -/// method documentation for details. -/// -/// [`Metadata`]: tracing_core::Metadata -/// [ows]: EitherWriter::some -/// [own]: EitherWriter::none -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct WithFilter<M, F> { - make: M, - filter: F, -} - -/// Combines a [`MakeWriter`] that returns an [`OptionalWriter`] with another -/// [`MakeWriter`], so that the second [`MakeWriter`] is used when the first -/// [`MakeWriter`] returns [`OptionalWriter::none`][own]. -/// -/// This is returned by the [`MakeWriterExt::or_else] method. See the -/// method documentation for details. -/// -/// [own]: EitherWriter::none -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct OrElse<A, B> { - inner: A, - or_else: B, -} - -/// Combines two types implementing [`MakeWriter`] (or [`std::io::Write`]) to -/// produce a writer that writes to both [`MakeWriter`]'s returned writers. -/// -/// This is returned by the [`MakeWriterExt::and`] method. See the method -/// documentation for details. -#[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub struct Tee<A, B> { - a: A, - b: B, -} - -/// A type implementing [`io::Write`] for a [`MutexGuard`] where the type -/// inside the [`Mutex`] implements [`io::Write`]. -/// -/// This is used by the [`MakeWriter`] implementation for [`Mutex`], because -/// [`MutexGuard`] itself will not implement [`io::Write`] — instead, it -/// _dereferences_ to a type implementing [`io::Write`]. Because [`MakeWriter`] -/// requires the `Writer` type to implement [`io::Write`], it's necessary to add -/// a newtype that forwards the trait implementation. -/// -/// [`io::Write`]: std::io::Write -/// [`MutexGuard`]: std::sync::MutexGuard -/// [`Mutex`]: std::sync::Mutex -#[derive(Debug)] -pub struct MutexGuardWriter<'a, W>(MutexGuard<'a, W>); - -/// Implements [`std::io::Write`] for an [`Arc`]<W> where `&W: Write`. -/// -/// This is an implementation detail of the [`MakeWriter`] impl for [`Arc`]. -#[derive(Clone, Debug)] -pub struct ArcWriter<W>(Arc<W>); - -/// A bridge between `fmt::Write` and `io::Write`. -/// -/// This is used by the timestamp formatting implementation for the `time` -/// crate and by the JSON formatter. In both cases, this is needed because -/// `tracing-subscriber`'s `FormatEvent`/`FormatTime` traits expect a -/// `fmt::Write` implementation, while `serde_json::Serializer` and `time`'s -/// `format_into` methods expect an `io::Write`. -#[cfg(any(feature = "json", feature = "time"))] -pub(in crate::fmt) struct WriteAdaptor<'a> { - fmt_write: &'a mut dyn fmt::Write, -} - -impl<'a, F, W> MakeWriter<'a> for F -where - F: Fn() -> W, - W: io::Write, -{ - type Writer = W; - - fn make_writer(&'a self) -> Self::Writer { - (self)() - } -} - -impl<'a, W> MakeWriter<'a> for Arc<W> -where - &'a W: io::Write + 'a, -{ - type Writer = &'a W; - fn make_writer(&'a self) -> Self::Writer { - &*self - } -} - -impl<'a> MakeWriter<'a> for std::fs::File { - type Writer = &'a std::fs::File; - fn make_writer(&'a self) -> Self::Writer { - self - } -} - -// === impl TestWriter === - -impl TestWriter { - /// Returns a new `TestWriter` with the default configuration. - pub fn new() -> Self { - Self::default() - } -} - -impl io::Write for TestWriter { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - let out_str = String::from_utf8_lossy(buf); - print!("{}", out_str); - Ok(buf.len()) - } - - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } -} - -impl<'a> MakeWriter<'a> for TestWriter { - type Writer = Self; - - fn make_writer(&'a self) -> Self::Writer { - Self::default() - } -} - -// === impl BoxMakeWriter === - -impl BoxMakeWriter { - /// Constructs a `BoxMakeWriter` wrapping a type implementing [`MakeWriter`]. - /// - pub fn new<M>(make_writer: M) -> Self - where - M: for<'a> MakeWriter<'a> + Send + Sync + 'static, - { - Self { - inner: Box::new(Boxed(make_writer)), - name: std::any::type_name::<M>(), - } - } -} - -impl fmt::Debug for BoxMakeWriter { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple("BoxMakeWriter") - .field(&format_args!("<{}>", self.name)) - .finish() - } -} - -impl<'a> MakeWriter<'a> for BoxMakeWriter { - type Writer = Box<dyn Write + 'a>; - - #[inline] - fn make_writer(&'a self) -> Self::Writer { - self.inner.make_writer() - } - - #[inline] - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - self.inner.make_writer_for(meta) - } -} - -struct Boxed<M>(M); - -impl<'a, M> MakeWriter<'a> for Boxed<M> -where - M: MakeWriter<'a>, -{ - type Writer = Box<dyn Write + 'a>; - - fn make_writer(&'a self) -> Self::Writer { - let w = self.0.make_writer(); - Box::new(w) - } - - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - let w = self.0.make_writer_for(meta); - Box::new(w) - } -} - -// === impl Mutex/MutexGuardWriter === - -impl<'a, W> MakeWriter<'a> for Mutex<W> -where - W: io::Write + 'a, -{ - type Writer = MutexGuardWriter<'a, W>; - - fn make_writer(&'a self) -> Self::Writer { - MutexGuardWriter(self.lock().expect("lock poisoned")) - } -} - -impl<'a, W> io::Write for MutexGuardWriter<'a, W> -where - W: io::Write, -{ - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - self.0.write(buf) - } - - #[inline] - fn flush(&mut self) -> io::Result<()> { - self.0.flush() - } - - #[inline] - fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { - self.0.write_vectored(bufs) - } - - #[inline] - fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - self.0.write_all(buf) - } - - #[inline] - fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { - self.0.write_fmt(fmt) - } -} - -// === impl EitherWriter === - -impl<A, B> io::Write for EitherWriter<A, B> -where - A: io::Write, - B: io::Write, -{ - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - match self { - EitherWriter::A(a) => a.write(buf), - EitherWriter::B(b) => b.write(buf), - } - } - - #[inline] - fn flush(&mut self) -> io::Result<()> { - match self { - EitherWriter::A(a) => a.flush(), - EitherWriter::B(b) => b.flush(), - } - } - - #[inline] - fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { - match self { - EitherWriter::A(a) => a.write_vectored(bufs), - EitherWriter::B(b) => b.write_vectored(bufs), - } - } - - #[inline] - fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - match self { - EitherWriter::A(a) => a.write_all(buf), - EitherWriter::B(b) => b.write_all(buf), - } - } - - #[inline] - fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { - match self { - EitherWriter::A(a) => a.write_fmt(fmt), - EitherWriter::B(b) => b.write_fmt(fmt), - } - } -} - -impl<T> OptionalWriter<T> { - /// Returns a [disabled writer]. - /// - /// Any bytes written to the returned writer are discarded. - /// - /// This is equivalent to returning [`Option::None`]. - /// - /// [disabled writer]: std::io::sink - #[inline] - pub fn none() -> Self { - EitherWriter::B(std::io::sink()) - } - - /// Returns an enabled writer of type `T`. - /// - /// This is equivalent to returning [`Option::Some`]. - #[inline] - pub fn some(t: T) -> Self { - EitherWriter::A(t) - } -} - -impl<T> From<Option<T>> for OptionalWriter<T> { - #[inline] - fn from(opt: Option<T>) -> Self { - match opt { - Some(writer) => Self::some(writer), - None => Self::none(), - } - } -} - -// === impl WithMaxLevel === - -impl<M> WithMaxLevel<M> { - /// Wraps the provided [`MakeWriter`] with a maximum [`Level`], so that it - /// returns [`OptionalWriter::none`] for spans and events whose level is - /// more verbose than the maximum level. - /// - /// See [`MakeWriterExt::with_max_level`] for details. - /// - /// [`Level`]: tracing_core::Level - pub fn new(make: M, level: tracing_core::Level) -> Self { - Self { make, level } - } -} - -impl<'a, M: MakeWriter<'a>> MakeWriter<'a> for WithMaxLevel<M> { - type Writer = OptionalWriter<M::Writer>; - - #[inline] - fn make_writer(&'a self) -> Self::Writer { - // If we don't know the level, assume it's disabled. - OptionalWriter::none() - } - - #[inline] - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - if meta.level() <= &self.level { - return OptionalWriter::some(self.make.make_writer_for(meta)); - } - OptionalWriter::none() - } -} - -// === impl WithMinLevel === - -impl<M> WithMinLevel<M> { - /// Wraps the provided [`MakeWriter`] with a minimum [`Level`], so that it - /// returns [`OptionalWriter::none`] for spans and events whose level is - /// less verbose than the maximum level. - /// - /// See [`MakeWriterExt::with_min_level`] for details. - /// - /// [`Level`]: tracing_core::Level - pub fn new(make: M, level: tracing_core::Level) -> Self { - Self { make, level } - } -} - -impl<'a, M: MakeWriter<'a>> MakeWriter<'a> for WithMinLevel<M> { - type Writer = OptionalWriter<M::Writer>; - - #[inline] - fn make_writer(&'a self) -> Self::Writer { - // If we don't know the level, assume it's disabled. - OptionalWriter::none() - } - - #[inline] - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - if meta.level() >= &self.level { - return OptionalWriter::some(self.make.make_writer_for(meta)); - } - OptionalWriter::none() - } -} - -// ==== impl WithFilter === - -impl<M, F> WithFilter<M, F> { - /// Wraps `make` with the provided `filter`, returning a [`MakeWriter`] that - /// will call `make.make_writer_for()` when `filter` returns `true` for a - /// span or event's [`Metadata`], and returns a [`sink`] otherwise. - /// - /// See [`MakeWriterExt::with_filter`] for details. - /// - /// [`Metadata`]: tracing_core::Metadata - /// [`sink`]: std::io::sink - pub fn new(make: M, filter: F) -> Self - where - F: Fn(&Metadata<'_>) -> bool, - { - Self { make, filter } - } -} - -impl<'a, M, F> MakeWriter<'a> for WithFilter<M, F> -where - M: MakeWriter<'a>, - F: Fn(&Metadata<'_>) -> bool, -{ - type Writer = OptionalWriter<M::Writer>; - - #[inline] - fn make_writer(&'a self) -> Self::Writer { - OptionalWriter::some(self.make.make_writer()) - } - - #[inline] - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - if (self.filter)(meta) { - OptionalWriter::some(self.make.make_writer_for(meta)) - } else { - OptionalWriter::none() - } - } -} - -// === impl Tee === - -impl<A, B> Tee<A, B> { - /// Combines two types implementing [`MakeWriter`], returning - /// a new [`MakeWriter`] that produces [writers] that write to *both* - /// outputs. - /// - /// See the documentation for [`MakeWriterExt::and`] for details. - /// - /// [writers]: std::io::Write - pub fn new(a: A, b: B) -> Self { - Self { a, b } - } -} - -impl<'a, A, B> MakeWriter<'a> for Tee<A, B> -where - A: MakeWriter<'a>, - B: MakeWriter<'a>, -{ - type Writer = Tee<A::Writer, B::Writer>; - - #[inline] - fn make_writer(&'a self) -> Self::Writer { - Tee::new(self.a.make_writer(), self.b.make_writer()) - } - - #[inline] - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - Tee::new(self.a.make_writer_for(meta), self.b.make_writer_for(meta)) - } -} - -macro_rules! impl_tee { - ($self_:ident.$f:ident($($arg:ident),*)) => { - { - let res_a = $self_.a.$f($($arg),*); - let res_b = $self_.b.$f($($arg),*); - (res_a?, res_b?) - } - } -} - -impl<A, B> io::Write for Tee<A, B> -where - A: io::Write, - B: io::Write, -{ - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - let (a, b) = impl_tee!(self.write(buf)); - Ok(std::cmp::max(a, b)) - } - - #[inline] - fn flush(&mut self) -> io::Result<()> { - impl_tee!(self.flush()); - Ok(()) - } - - #[inline] - fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { - let (a, b) = impl_tee!(self.write_vectored(bufs)); - Ok(std::cmp::max(a, b)) - } - - #[inline] - fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - impl_tee!(self.write_all(buf)); - Ok(()) - } - - #[inline] - fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { - impl_tee!(self.write_fmt(fmt)); - Ok(()) - } -} - -// === impl OrElse === - -impl<A, B> OrElse<A, B> { - /// Combines - pub fn new<'a, W>(inner: A, or_else: B) -> Self - where - A: MakeWriter<'a, Writer = OptionalWriter<W>>, - B: MakeWriter<'a>, - W: Write, - { - Self { inner, or_else } - } -} - -impl<'a, A, B, W> MakeWriter<'a> for OrElse<A, B> -where - A: MakeWriter<'a, Writer = OptionalWriter<W>>, - B: MakeWriter<'a>, - W: io::Write, -{ - type Writer = EitherWriter<W, B::Writer>; - - #[inline] - fn make_writer(&'a self) -> Self::Writer { - match self.inner.make_writer() { - EitherWriter::A(writer) => EitherWriter::A(writer), - EitherWriter::B(_) => EitherWriter::B(self.or_else.make_writer()), - } - } - - #[inline] - fn make_writer_for(&'a self, meta: &Metadata<'_>) -> Self::Writer { - match self.inner.make_writer_for(meta) { - EitherWriter::A(writer) => EitherWriter::A(writer), - EitherWriter::B(_) => EitherWriter::B(self.or_else.make_writer_for(meta)), - } - } -} - -// === impl ArcWriter === - -impl<W> io::Write for ArcWriter<W> -where - for<'a> &'a W: io::Write, -{ - #[inline] - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - (&*self.0).write(buf) - } - - #[inline] - fn flush(&mut self) -> io::Result<()> { - (&*self.0).flush() - } - - #[inline] - fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { - (&*self.0).write_vectored(bufs) - } - - #[inline] - fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - (&*self.0).write_all(buf) - } - - #[inline] - fn write_fmt(&mut self, fmt: std::fmt::Arguments<'_>) -> io::Result<()> { - (&*self.0).write_fmt(fmt) - } -} - -// === impl WriteAdaptor === - -#[cfg(any(feature = "json", feature = "time"))] -impl<'a> WriteAdaptor<'a> { - pub(in crate::fmt) fn new(fmt_write: &'a mut dyn fmt::Write) -> Self { - Self { fmt_write } - } -} -#[cfg(any(feature = "json", feature = "time"))] -impl<'a> io::Write for WriteAdaptor<'a> { - fn write(&mut self, buf: &[u8]) -> io::Result<usize> { - let s = - std::str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; - - self.fmt_write - .write_str(s) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; - - Ok(s.as_bytes().len()) - } - - fn flush(&mut self) -> io::Result<()> { - Ok(()) - } -} - -#[cfg(any(feature = "json", feature = "time"))] -impl<'a> fmt::Debug for WriteAdaptor<'a> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.pad("WriteAdaptor { .. }") - } -} -// === blanket impls === - -impl<'a, M> MakeWriterExt<'a> for M where M: MakeWriter<'a> {} -#[cfg(test)] -mod test { - use super::*; - use crate::fmt::format::Format; - use crate::fmt::test::{MockMakeWriter, MockWriter}; - use crate::fmt::Subscriber; - use std::sync::atomic::{AtomicBool, Ordering}; - use std::sync::{Arc, Mutex}; - use tracing::{debug, error, info, trace, warn, Level}; - use tracing_core::dispatcher::{self, Dispatch}; - - fn test_writer<T>(make_writer: T, msg: &str, buf: &Mutex<Vec<u8>>) - where - T: for<'writer> MakeWriter<'writer> + Send + Sync + 'static, - { - let subscriber = { - #[cfg(feature = "ansi")] - let f = Format::default().without_time().with_ansi(false); - #[cfg(not(feature = "ansi"))] - let f = Format::default().without_time(); - Subscriber::builder() - .event_format(f) - .with_writer(make_writer) - .finish() - }; - let dispatch = Dispatch::from(subscriber); - - dispatcher::with_default(&dispatch, || { - error!("{}", msg); - }); - - let expected = format!("ERROR {}: {}\n", module_path!(), msg); - let actual = String::from_utf8(buf.try_lock().unwrap().to_vec()).unwrap(); - assert!(actual.contains(expected.as_str())); - } - - fn has_lines(buf: &Mutex<Vec<u8>>, msgs: &[(tracing::Level, &str)]) { - let actual = String::from_utf8(buf.try_lock().unwrap().to_vec()).unwrap(); - let mut expected_lines = msgs.iter(); - for line in actual.lines() { - let line = dbg!(line).trim(); - let (level, msg) = expected_lines - .next() - .unwrap_or_else(|| panic!("expected no more lines, but got: {:?}", line)); - let expected = format!("{} {}: {}", level, module_path!(), msg); - assert_eq!(line, expected.as_str()); - } - } - - #[test] - fn custom_writer_closure() { - let buf = Arc::new(Mutex::new(Vec::new())); - let buf2 = buf.clone(); - let make_writer = move || MockWriter::new(buf2.clone()); - let msg = "my custom writer closure error"; - test_writer(make_writer, msg, &buf); - } - - #[test] - fn custom_writer_struct() { - let buf = Arc::new(Mutex::new(Vec::new())); - let make_writer = MockMakeWriter::new(buf.clone()); - let msg = "my custom writer struct error"; - test_writer(make_writer, msg, &buf); - } - - #[test] - fn custom_writer_mutex() { - let buf = Arc::new(Mutex::new(Vec::new())); - let writer = MockWriter::new(buf.clone()); - let make_writer = Mutex::new(writer); - let msg = "my mutex writer error"; - test_writer(make_writer, msg, &buf); - } - - #[test] - fn combinators_level_filters() { - let info_buf = Arc::new(Mutex::new(Vec::new())); - let info = MockMakeWriter::new(info_buf.clone()); - - let debug_buf = Arc::new(Mutex::new(Vec::new())); - let debug = MockMakeWriter::new(debug_buf.clone()); - - let warn_buf = Arc::new(Mutex::new(Vec::new())); - let warn = MockMakeWriter::new(warn_buf.clone()); - - let err_buf = Arc::new(Mutex::new(Vec::new())); - let err = MockMakeWriter::new(err_buf.clone()); - - let make_writer = info - .with_max_level(Level::INFO) - .and(debug.with_max_level(Level::DEBUG)) - .and(warn.with_max_level(Level::WARN)) - .and(err.with_max_level(Level::ERROR)); - - let c = { - #[cfg(feature = "ansi")] - let f = Format::default().without_time().with_ansi(false); - #[cfg(not(feature = "ansi"))] - let f = Format::default().without_time(); - Subscriber::builder() - .event_format(f) - .with_writer(make_writer) - .with_max_level(Level::TRACE) - .finish() - }; - - let _s = tracing::subscriber::set_default(c); - - trace!("trace"); - debug!("debug"); - info!("info"); - warn!("warn"); - error!("error"); - - let all_lines = [ - (Level::TRACE, "trace"), - (Level::DEBUG, "debug"), - (Level::INFO, "info"), - (Level::WARN, "warn"), - (Level::ERROR, "error"), - ]; - - println!("max level debug"); - has_lines(&debug_buf, &all_lines[1..]); - - println!("max level info"); - has_lines(&info_buf, &all_lines[2..]); - - println!("max level warn"); - has_lines(&warn_buf, &all_lines[3..]); - - println!("max level error"); - has_lines(&err_buf, &all_lines[4..]); - } - - #[test] - fn combinators_or_else() { - let some_buf = Arc::new(Mutex::new(Vec::new())); - let some = MockMakeWriter::new(some_buf.clone()); - - let or_else_buf = Arc::new(Mutex::new(Vec::new())); - let or_else = MockMakeWriter::new(or_else_buf.clone()); - - let return_some = AtomicBool::new(true); - let make_writer = move || { - if return_some.swap(false, Ordering::Relaxed) { - OptionalWriter::some(some.make_writer()) - } else { - OptionalWriter::none() - } - }; - let make_writer = make_writer.or_else(or_else); - let c = { - #[cfg(feature = "ansi")] - let f = Format::default().without_time().with_ansi(false); - #[cfg(not(feature = "ansi"))] - let f = Format::default().without_time(); - Subscriber::builder() - .event_format(f) - .with_writer(make_writer) - .with_max_level(Level::TRACE) - .finish() - }; - - let _s = tracing::subscriber::set_default(c); - info!("hello"); - info!("world"); - info!("goodbye"); - - has_lines(&some_buf, &[(Level::INFO, "hello")]); - has_lines( - &or_else_buf, - &[(Level::INFO, "world"), (Level::INFO, "goodbye")], - ); - } - - #[test] - fn combinators_or_else_chain() { - let info_buf = Arc::new(Mutex::new(Vec::new())); - let info = MockMakeWriter::new(info_buf.clone()); - - let debug_buf = Arc::new(Mutex::new(Vec::new())); - let debug = MockMakeWriter::new(debug_buf.clone()); - - let warn_buf = Arc::new(Mutex::new(Vec::new())); - let warn = MockMakeWriter::new(warn_buf.clone()); - - let err_buf = Arc::new(Mutex::new(Vec::new())); - let err = MockMakeWriter::new(err_buf.clone()); - - let make_writer = err.with_max_level(Level::ERROR).or_else( - warn.with_max_level(Level::WARN).or_else( - info.with_max_level(Level::INFO) - .or_else(debug.with_max_level(Level::DEBUG)), - ), - ); - - let c = { - #[cfg(feature = "ansi")] - let f = Format::default().without_time().with_ansi(false); - #[cfg(not(feature = "ansi"))] - let f = Format::default().without_time(); - Subscriber::builder() - .event_format(f) - .with_writer(make_writer) - .with_max_level(Level::TRACE) - .finish() - }; - - let _s = tracing::subscriber::set_default(c); - - trace!("trace"); - debug!("debug"); - info!("info"); - warn!("warn"); - error!("error"); - - println!("max level debug"); - has_lines(&debug_buf, &[(Level::DEBUG, "debug")]); - - println!("max level info"); - has_lines(&info_buf, &[(Level::INFO, "info")]); - - println!("max level warn"); - has_lines(&warn_buf, &[(Level::WARN, "warn")]); - - println!("max level error"); - has_lines(&err_buf, &[(Level::ERROR, "error")]); - } - - #[test] - fn combinators_and() { - let a_buf = Arc::new(Mutex::new(Vec::new())); - let a = MockMakeWriter::new(a_buf.clone()); - - let b_buf = Arc::new(Mutex::new(Vec::new())); - let b = MockMakeWriter::new(b_buf.clone()); - - let lines = &[(Level::INFO, "hello"), (Level::INFO, "world")]; - - let make_writer = a.and(b); - let c = { - #[cfg(feature = "ansi")] - let f = Format::default().without_time().with_ansi(false); - #[cfg(not(feature = "ansi"))] - let f = Format::default().without_time(); - Subscriber::builder() - .event_format(f) - .with_writer(make_writer) - .with_max_level(Level::TRACE) - .finish() - }; - - let _s = tracing::subscriber::set_default(c); - info!("hello"); - info!("world"); - - has_lines(&a_buf, &lines[..]); - has_lines(&b_buf, &lines[..]); - } -} |