Skip to content

Commit

Permalink
Send logs from the backend to the Android log
Browse files Browse the repository at this point in the history
  • Loading branch information
dae committed Jun 19, 2022
1 parent 658ab8c commit 1bd4227
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
31 changes: 31 additions & 0 deletions rslib-bridge/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion rslib-bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
prost-build = "0.9"
6 changes: 5 additions & 1 deletion rslib-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
44 changes: 44 additions & 0 deletions rslib-bridge/src/logging.rs
Original file line number Diff line number Diff line change
@@ -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<Self::Ok, Self::Err> {
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!())
}

0 comments on commit 1bd4227

Please sign in to comment.