From 4589b882c051cdfd83a6c408b57d74e1ae2c0ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 16 Feb 2021 11:18:31 +0100 Subject: [PATCH] rust: Add support to forward rust logs to the kotlin side --- rust-sdk/Cargo.toml | 7 ++++-- rust-sdk/src/lib.rs | 55 ++++++++++++++++++++++++++++++++++++++++---- rust-sdk/src/olm.udl | 8 ++++++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/rust-sdk/Cargo.toml b/rust-sdk/Cargo.toml index 54709d0928..ce3afcd490 100644 --- a/rust-sdk/Cargo.toml +++ b/rust-sdk/Cargo.toml @@ -11,10 +11,13 @@ name = "matrix_crypto" [dependencies] matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk/"} futures = { version = "0.3.12", default_features = false, features = ["executor"] } -tokio = { version = "1.1.1", default_features = false, features = ["rt-multi-thread", "time"] } -serde_json = "1.0.61" +tokio = { version = "1.2.0", default_features = false, features = ["rt-multi-thread", "time"] } +serde_json = "1.0.62" thiserror = "1.0.23" http = "0.2.3" +tracing = "0.1.23" +tracing-subscriber = "0.2.15" +once_cell = "1.5.2" [dependencies.matrix-sdk-crypto] git = "https://github.com/matrix-org/matrix-rust-sdk/" diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index f5ed343a74..3b84ce53c9 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -1,6 +1,7 @@ use std::{ collections::{BTreeMap, HashMap}, convert::TryFrom, + sync::{Arc, Mutex}, time::Duration, }; @@ -25,6 +26,55 @@ use matrix_sdk_crypto::{ store::CryptoStoreError as InnerStoreError, IncomingResponse, OlmError, 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 { + 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>>, +} + +pub fn set_logger(logger: Box) { + 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 { inner: InnerMachine, @@ -205,10 +255,7 @@ impl OlmMachine { Ok(OlmMachine { inner: block_on(InnerMachine::new_with_default_store( - &user_id, - device_id, - path, - Some("DEFAULT_PASSPHRASE"), + &user_id, device_id, path, None, ))?, runtime: Runtime::new().unwrap(), }) diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 1a49fee8d9..8d90cbadd4 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -1,4 +1,10 @@ -namespace olm {}; +namespace olm { + void set_logger(Logger logger); +}; + +callback interface Logger { + void log(string log_line); +}; [Error] enum MachineCreationError {