diff --git a/rslib-bridge/Cargo.lock b/rslib-bridge/Cargo.lock index 6558a0244..c418c857c 100644 --- a/rslib-bridge/Cargo.lock +++ b/rslib-bridge/Cargo.lock @@ -41,6 +41,24 @@ dependencies = [ "url", ] +[[package]] +name = "android_log-sys" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85965b6739a430150bdd138e2374a98af0c3ee0d030b3bb7fc3bddff58d0102e" + +[[package]] +name = "android_logger" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b7ddf197de32e415d197aa21c1c0cb36e01e4794fd801302280ac7847ee02" +dependencies = [ + "android_log-sys", + "env_logger", + "log", + "once_cell", +] + [[package]] name = "anki" version = "0.0.0" @@ -403,6 +421,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" +dependencies = [ + "log", + "regex", +] + [[package]] name = "error-chain" version = "0.12.4" @@ -1574,11 +1602,13 @@ dependencies = [ name = "rsdroid" version = "0.1.0" dependencies = [ + "android_logger", "anki", "itertools", "jni", "lazy_static", "lexical-core", + "log", "num_enum", "prost", "prost-build", @@ -1586,6 +1616,7 @@ dependencies = [ "serde", "serde_derive", "serde_json", + "slog", ] [[package]] diff --git a/rslib-bridge/Cargo.toml b/rslib-bridge/Cargo.toml index db28ba8bd..a8a7c9745 100644 --- a/rslib-bridge/Cargo.toml +++ b/rslib-bridge/Cargo.toml @@ -23,9 +23,12 @@ lexical-core = "0.7.5" # picked bundled - TODO: Is this correct? rusqlite = { version = "0.26.0", features = ["trace", "functions", "collation", "bundled"] } +android_logger = "0.11.0" +log = "0.4.17" +slog = "2.7.0" [features] no-android = [] [build-dependencies] -prost-build = "0.9" \ No newline at end of file +prost-build = "0.9" diff --git a/rslib-bridge/src/lib.rs b/rslib-bridge/src/lib.rs index 63ec16f2d..9dc568583 100644 --- a/rslib-bridge/src/lib.rs +++ b/rslib-bridge/src/lib.rs @@ -12,14 +12,18 @@ use prost::Message; use std::any::Any; use std::panic::{catch_unwind, AssertUnwindSafe}; +mod logging; + #[no_mangle] pub unsafe extern "C" fn Java_net_ankiweb_rsdroid_NativeMethods_openBackend( env: JNIEnv, _: JClass, args: jbyteArray, ) -> jarray { + let logger = logging::setup_logging(); + let input = env.convert_byte_array(args).unwrap(); - let result = init_backend(&input, None) + let result = init_backend(&input, Some(logger)) .map(|backend| { let backend_ptr = Box::into_raw(Box::new(backend)) as i64; Int64 { val: backend_ptr }.encode_to_vec() diff --git a/rslib-bridge/src/logging.rs b/rslib-bridge/src/logging.rs new file mode 100644 index 000000000..bcf2de6cf --- /dev/null +++ b/rslib-bridge/src/logging.rs @@ -0,0 +1,44 @@ +//! A simple adaptor that takes log messages from the backend and sends them to +//! the Android logs. + +use android_logger::Config; +use log::Level; +use slog::*; +use std::{fmt, result}; + +pub struct AndroidSerializer; + +impl Serializer for AndroidSerializer { + fn emit_arguments(&mut self, key: Key, val: &fmt::Arguments<'_>) -> Result { + log::debug!("{}={}", key, val); + Ok(()) + } +} + +pub struct AndroidDrain; + +impl Drain for AndroidDrain { + type Ok = (); + type Err = (); + + fn log( + &self, + record: &Record<'_>, + values: &OwnedKVList, + ) -> result::Result { + log::debug!("{}", record.msg()); + + record + .kv() + .serialize(record, &mut AndroidSerializer) + .unwrap(); + values.serialize(record, &mut AndroidSerializer).unwrap(); + + Ok(()) + } +} + +pub(crate) fn setup_logging() -> Logger { + android_logger::init_once(Config::default().with_min_level(Level::Debug)); + Logger::root(AndroidDrain {}.fuse(), slog_o!()) +}