Crate tracing_logcat

Source
Expand description

tracing writer for logging into Android’s logcat. Instead of linking liblog, which isn’t available as a static library in the NDK, this library directly connects to logd and sends messages via the documented protocol.

There are a few behavioral differences compared to liblog:

  • In the very unlikely event that Android’s logd crashes, logging will stop working because tracing-logcat does not attempt to reconnect to the logd socket.
  • Only Android 5 and newer are supported. Previous versions of Android did not use logd and implemented logcat without a userspace daemon.
  • Log messages longer than 4068 - <tag length> - 2 bytes are split into multiple messages instead of being truncated. If the original message is valid UTF-8, then the message is split at a code point boundary (not at a grapheme cluster boundary). Otherwise, the message is split exactly at the length limit.

§Examples

§Using a fixed tag

use tracing::Level;
use tracing_logcat::{LogcatMakeWriter, LogcatTag};
use tracing_subscriber::fmt::format::Format;

let tag = LogcatTag::Fixed(env!("CARGO_PKG_NAME").to_owned());
let writer = LogcatMakeWriter::new(tag)
   .expect("Failed to initialize logcat writer");

tracing_subscriber::fmt()
    .event_format(Format::default().with_level(false).without_time())
    .with_writer(writer)
    .with_ansi(false)
    .with_max_level(Level::TRACE)
    .init();

§Using the tracing target as the tag

use tracing::Level;
use tracing_logcat::{LogcatMakeWriter, LogcatTag};
use tracing_subscriber::fmt::format::Format;

let writer = LogcatMakeWriter::new(LogcatTag::Target)
   .expect("Failed to initialize logcat writer");

tracing_subscriber::fmt()
    .event_format(Format::default().with_level(false).with_target(false).without_time())
    .with_writer(writer)
    .with_ansi(false)
    .with_max_level(Level::TRACE)
    .init();

Structs§

LogcatMakeWriter
A MakeWriter type that creates LogcatWriter instances that output to Android’s logcat.
LogcatWriter
An io::Write instance that outputs to Android’s logcat.

Enums§

LogcatTag
Tag string to use for log messages.