7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | use std::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | io::{Result, Write}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | sync::{Arc, Mutex}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | use tracing_subscriber::{EnvFilter, fmt::MakeWriter}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | /// Trait that can be used to forward Rust logs over FFI to a language specific |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | /// logger. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | #[matrix_sdk_ffi_macros::export(callback_interface)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | pub trait Logger: Send { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | /// Called every time the Rust side wants to post a log line. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | fn log(&self, log_line: String); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | // TODO add support for different log levels, do this by adding more methods |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | // to the trait. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | impl Write for LoggerWrapper { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | fn write(&mut self, buf: &[u8]) -> Result<usize> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | let data = String::from_utf8_lossy(buf).to_string(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | self.inner.lock().unwrap().log(data); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | Ok(buf.len()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | fn flush(&mut self) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | impl MakeWriter<'_> for LoggerWrapper { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | type Writer = LoggerWrapper; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | fn make_writer(&self) -> Self::Writer { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | self.clone() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | #[derive(Clone)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | pub struct LoggerWrapper { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | inner: Arc<Mutex<Box<dyn Logger>>>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | /// Set the logger that should be used to forward Rust logs over FFI. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | #[matrix_sdk_ffi_macros::export] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | pub fn set_logger(logger: Box<dyn Logger>) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | let logger = LoggerWrapper { inner: Arc::new(Mutex::new(logger)) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | let filter = EnvFilter::from_default_env() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | .add_directive( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | "matrix_sdk_crypto=trace".parse().expect("Can't parse logging filter directive"), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | .add_directive( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | "matrix_sdk_sqlite=debug".parse().expect("Can't parse logging filter directive"), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | let _ = tracing_subscriber::fmt() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | .with_writer(logger) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | .with_env_filter(filter) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | .with_ansi(false) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | .without_time() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | .try_init(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | } |