paranoid_android/
lib.rs

1#![cfg(target_os = "android")]
2
3//! Integration layer between `tracing` and Android logs.
4//!
5//! This crate provides a [`MakeWriter`](tracing_subscriber::fmt::MakeWriter) suitable for writing Android logs.
6//!
7//! It is designed as an integration with the [`fmt`](tracing_subscriber::fmt) subscriber from `tracing-subscriber`
8//! and as such inherits all of its features and customization options.
9//!
10//! ## Usage
11//!
12//! ```rust
13//! paranoid_android::init(env!("CARGO_PKG_NAME"));
14//! ```
15//!
16//! or with custom options and combined with other layers
17//!
18//! ```rust
19//! # let other_layer = paranoid_android::layer("other");
20//! #
21//! use tracing_subscriber::filter::LevelFilter;
22//! use tracing_subscriber::fmt::FmtSpan;
23//! use tracing_subscriber::prelude::*;
24//!
25//! let android_layer = paranoid_android::layer(env!("CARGO_PKG_NAME"))
26//!     .with_span_events(FmtSpan::CLOSE)
27//!     .with_thread_names(true)
28//!     .with_filter(LevelFilter::DEBUG);
29//!
30//! tracing_subcriber::registry()
31//!     .with(android_layer)
32//!     .with(other_layer)
33//!     .init();
34//! ```
35//!
36//! ## Cargo features
37//!
38//! * `api-30`: Enables support for Android API level 30 and source location information
39
40#![warn(rust_2018_idioms, missing_debug_implementations, missing_docs)]
41
42mod layer;
43mod logging;
44mod writer;
45
46use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Registry};
47
48pub use self::{
49    layer::{layer, with_buffer, Layer},
50    logging::Buffer,
51    writer::{AndroidLogMakeWriter, AndroidLogWriter},
52};
53
54/// Creates a [`Subscriber`](tracing_core::Subscriber) with the given tag
55/// and attempts to set it as the [global default subscriber] in the current scope, panicking if this fails.
56///
57/// [global default subscriber]: https://docs.rs/tracing/0.1/tracing/dispatcher/index.html#setting-the-default-subscriber
58pub fn init(tag: impl ToString) {
59    Registry::default().with(layer(tag)).init();
60}