#android #tracing #logcat #log-messages #send #sockets #logd

tracing-logcat

A writer for the tracing library that outputs to Android's logcat

1 unstable release

0.1.0 Jul 7, 2024

#661 in Debugging

Download history 3/week @ 2024-07-28 1/week @ 2024-08-25 2/week @ 2024-09-01 12/week @ 2024-09-15 22/week @ 2024-09-22 13/week @ 2024-09-29 9/week @ 2024-10-06 5/week @ 2024-10-13 38/week @ 2024-10-27 100/week @ 2024-11-03 96/week @ 2024-11-10

235 downloads per month

Apache-2.0

13KB
208 lines

tracing-logcat

tracing-logcat is a library that provides an Android logcat output for the tracing library. It directly communicates with Android's logd process instead of using liblog.so, making it suitable for use with statically linked executables.

See examples/ for examples of how to use this library.

License

tracing-logcat is licensed under Apache 2.0. Please see LICENSE for the full license text.


lib.rs:

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();

Dependencies

~3–11MB
~133K SLoC