rust: Add support to forward rust logs to the kotlin side

This commit is contained in:
Damir Jelić 2021-02-16 11:18:31 +01:00
parent 3ddbe7e69b
commit 4589b882c0
3 changed files with 63 additions and 7 deletions

View File

@ -11,10 +11,13 @@ name = "matrix_crypto"
[dependencies] [dependencies]
matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk/"} matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk/"}
futures = { version = "0.3.12", default_features = false, features = ["executor"] } futures = { version = "0.3.12", default_features = false, features = ["executor"] }
tokio = { version = "1.1.1", default_features = false, features = ["rt-multi-thread", "time"] } tokio = { version = "1.2.0", default_features = false, features = ["rt-multi-thread", "time"] }
serde_json = "1.0.61" serde_json = "1.0.62"
thiserror = "1.0.23" thiserror = "1.0.23"
http = "0.2.3" http = "0.2.3"
tracing = "0.1.23"
tracing-subscriber = "0.2.15"
once_cell = "1.5.2"
[dependencies.matrix-sdk-crypto] [dependencies.matrix-sdk-crypto]
git = "https://github.com/matrix-org/matrix-rust-sdk/" git = "https://github.com/matrix-org/matrix-rust-sdk/"

View File

@ -1,6 +1,7 @@
use std::{ use std::{
collections::{BTreeMap, HashMap}, collections::{BTreeMap, HashMap},
convert::TryFrom, convert::TryFrom,
sync::{Arc, Mutex},
time::Duration, time::Duration,
}; };
@ -25,6 +26,55 @@ use matrix_sdk_crypto::{
store::CryptoStoreError as InnerStoreError, IncomingResponse, OlmError, store::CryptoStoreError as InnerStoreError, IncomingResponse, OlmError,
OlmMachine as InnerMachine, OutgoingRequest, ToDeviceRequest, OlmMachine as InnerMachine, OutgoingRequest, ToDeviceRequest,
}; };
use tracing_subscriber::{fmt::MakeWriter, EnvFilter};
pub trait Logger: Send {
fn log(&self, log_line: String);
}
impl std::io::Write for LoggerWrapper {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let data = String::from_utf8_lossy(buf).to_string();
self.inner.lock().unwrap().log(data);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl MakeWriter for LoggerWrapper {
type Writer = LoggerWrapper;
fn make_writer(&self) -> Self::Writer {
self.clone()
}
}
#[derive(Clone)]
pub struct LoggerWrapper {
inner: Arc<Mutex<Box<dyn Logger>>>,
}
pub fn set_logger(logger: Box<dyn Logger>) {
let logger = LoggerWrapper {
inner: Arc::new(Mutex::new(logger)),
};
let filter = EnvFilter::from_default_env().add_directive(
"matrix_sdk_crypto=trace"
.parse()
.expect("Can't parse logging filter directive"),
);
let _ = tracing_subscriber::fmt()
.with_writer(logger)
.with_env_filter(filter)
.without_time()
.try_init();
}
pub struct OlmMachine { pub struct OlmMachine {
inner: InnerMachine, inner: InnerMachine,
@ -205,10 +255,7 @@ impl OlmMachine {
Ok(OlmMachine { Ok(OlmMachine {
inner: block_on(InnerMachine::new_with_default_store( inner: block_on(InnerMachine::new_with_default_store(
&user_id, &user_id, device_id, path, None,
device_id,
path,
Some("DEFAULT_PASSPHRASE"),
))?, ))?,
runtime: Runtime::new().unwrap(), runtime: Runtime::new().unwrap(),
}) })

View File

@ -1,4 +1,10 @@
namespace olm {}; namespace olm {
void set_logger(Logger logger);
};
callback interface Logger {
void log(string log_line);
};
[Error] [Error]
enum MachineCreationError { enum MachineCreationError {