paranoid_android/
layer.rs

1use tracing_core::Subscriber;
2use tracing_subscriber::{
3    fmt::{
4        self,
5        format::{self, Format},
6    },
7    registry::LookupSpan,
8};
9
10use crate::{AndroidLogMakeWriter, Buffer};
11
12/// A [`Layer`](tracing_subscriber::Layer) that writes formatted representations of `tracing` events as Android logs.
13pub type Layer<S, N = format::DefaultFields, E = format::Full> =
14    fmt::Layer<S, N, format::Format<E, ()>, AndroidLogMakeWriter>;
15
16/// Returns a new [formatting layer](Layer) with the given tag,
17/// which can be [composed](tracing_subscriber::Layer) with other layers to construct a [`Subscriber`].
18pub fn layer<S>(tag: impl ToString) -> Layer<S>
19where
20    S: Subscriber,
21    for<'a> S: LookupSpan<'a>,
22{
23    with_buffer(tag, Default::default())
24}
25
26/// Returns a new [formatting layer](Layer) with the given tag and using the given [Android log buffer](Buffer),
27/// which can be [composed](tracing_subscriber::Layer) with other layers to construct a [`Subscriber`].
28pub fn with_buffer<S>(tag: impl ToString, buffer: Buffer) -> Layer<S>
29where
30    S: Subscriber,
31    for<'a> S: LookupSpan<'a>,
32{
33    fmt::Layer::new()
34        .event_format(Format::default().with_level(false).without_time())
35        .with_writer(AndroidLogMakeWriter::with_buffer(tag.to_string(), buffer))
36}