Joel Galenson | d19fd2d | 2020-06-30 12:38:21 -0700 | [diff] [blame] | 1 | ## Send Rust logs to Logcat |
| 2 | |
| 3 | [](https://crates.io/crates/android_logger) |
Jeff Vander Stoep | c30f4eb | 2021-04-19 14:39:30 +0200 | [diff] [blame] | 4 | [](https://github.com/Nercury/android_logger-rs/actions/workflows/ci.yml/) |
| 5 | |
Joel Galenson | d19fd2d | 2020-06-30 12:38:21 -0700 | [diff] [blame] | 6 | |
| 7 | This library is a drop-in replacement for `env_logger`. Instead, it outputs messages to |
| 8 | android's logcat. |
| 9 | |
| 10 | This only works on Android and requires linking to `log` which |
| 11 | is only available under android. With Cargo, it is possible to conditionally require |
| 12 | this library: |
| 13 | |
| 14 | ```toml |
| 15 | [target.'cfg(target_os = "android")'.dependencies] |
Joel Galenson | e2d36b5 | 2021-04-01 15:00:19 -0700 | [diff] [blame] | 16 | android_logger = "0.10" |
Joel Galenson | d19fd2d | 2020-06-30 12:38:21 -0700 | [diff] [blame] | 17 | ``` |
| 18 | |
| 19 | Example of initialization on activity creation, with log configuration: |
| 20 | |
| 21 | ```rust |
| 22 | #[macro_use] extern crate log; |
| 23 | extern crate android_logger; |
| 24 | |
| 25 | use log::Level; |
| 26 | use android_logger::{Config,FilterBuilder}; |
| 27 | |
| 28 | fn native_activity_create() { |
| 29 | android_logger::init_once( |
| 30 | Config::default() |
| 31 | .with_min_level(Level::Trace) // limit log level |
| 32 | .with_tag("mytag") // logs will show under mytag tag |
| 33 | .with_filter( // configure messages for specific crate |
| 34 | FilterBuilder::new() |
| 35 | .parse("debug,hello::crate=error") |
| 36 | .build()) |
| 37 | ); |
| 38 | |
| 39 | trace!("this is a verbose {}", "message"); |
| 40 | error!("this is printed by default"); |
| 41 | } |
| 42 | ``` |
| 43 | |
| 44 | To allow all logs, use the default configuration with min level Trace: |
| 45 | |
| 46 | ```rust |
| 47 | #[macro_use] extern crate log; |
| 48 | extern crate android_logger; |
| 49 | |
| 50 | use log::Level; |
| 51 | use android_logger::Config; |
| 52 | |
| 53 | fn native_activity_create() { |
| 54 | android_logger::init_once( |
| 55 | Config::default().with_min_level(Level::Trace)); |
| 56 | } |
| 57 | ``` |
| 58 | |
| 59 | There is a caveat that this library can only be initialized once |
| 60 | (hence the `init_once` function name). However, Android native activity can be |
| 61 | re-created every time the screen is rotated, resulting in multiple initialization calls. |
| 62 | Therefore this library will only log a warning for subsequent `init_once` calls. |
| 63 | |
| 64 | This library ensures that logged messages do not overflow Android log message limits |
| 65 | by efficiently splitting messages into chunks. |
| 66 | |
| 67 | ## License |
| 68 | |
| 69 | Licensed under either of |
| 70 | |
| 71 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 72 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 73 | |
| 74 | at your option. |
| 75 | |
| 76 | ### Contribution |
| 77 | |
| 78 | Unless you explicitly state otherwise, any contribution intentionally |
| 79 | submitted for inclusion in the work by you, as defined in the Apache-2.0 |
| 80 | license, shall be dual licensed as above, without any additional terms or |
| 81 | conditions. |