-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send logs from the backend to the Android log
Closes #24
- Loading branch information
Showing
4 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!()) | ||
} |