From f9bb86c52f62e9eadf0ddaecb4a029fff068300e Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Wed, 20 Jul 2022 12:56:42 +0300 Subject: [PATCH] feat(bindings/sdk-ffi): Allow ffi users to configure tracing and log levels --- bindings/matrix-sdk-ffi/Cargo.toml | 1 + bindings/matrix-sdk-ffi/src/api.udl | 10 ++++++++++ bindings/matrix-sdk-ffi/src/lib.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/bindings/matrix-sdk-ffi/Cargo.toml b/bindings/matrix-sdk-ffi/Cargo.toml index bc2f81bb1..04824f389 100644 --- a/bindings/matrix-sdk-ffi/Cargo.toml +++ b/bindings/matrix-sdk-ffi/Cargo.toml @@ -31,6 +31,7 @@ thiserror = "1.0.30" tokio = { version = "1", features = ["rt-multi-thread", "macros"] } tokio-stream = "0.1.8" tracing = "0.1.32" +tracing-subscriber = { version = "0.3", features = [ "env-filter" ]} # keep in sync with uniffi dependency in matrix-sdk-crypto-ffi, and uniffi_bindgen in ffi CI job uniffi = "0.18.0" uniffi_macros = "0.18.0" diff --git a/bindings/matrix-sdk-ffi/src/api.udl b/bindings/matrix-sdk-ffi/src/api.udl index 2c705695f..1db732a0f 100644 --- a/bindings/matrix-sdk-ffi/src/api.udl +++ b/bindings/matrix-sdk-ffi/src/api.udl @@ -1,9 +1,19 @@ namespace sdk { + void setup_tracing(LogLevel log_level); + MediaSource media_source_from_url(string url); MessageEventContent message_event_content_from_markdown(string md); string gen_transaction_id(); }; +enum LogLevel { + "Trace", + "Debug", + "Info", + "Warn", + "Error", +}; + [Error] interface ClientError { Generic(string msg); diff --git a/bindings/matrix-sdk-ffi/src/lib.rs b/bindings/matrix-sdk-ffi/src/lib.rs index 7fb23d268..f47405d7e 100644 --- a/bindings/matrix-sdk-ffi/src/lib.rs +++ b/bindings/matrix-sdk-ffi/src/lib.rs @@ -17,6 +17,7 @@ use matrix_sdk::Session; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; use tokio::runtime::Runtime; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; pub use uniffi_api::*; pub static RUNTIME: Lazy = @@ -55,3 +56,30 @@ impl From for ClientError { ClientError::Generic { msg: e.to_string() } } } + +pub enum LogLevel { + Trace, + Debug, + Info, + Warn, + Error, +} + +impl LogLevel { + fn as_str(&self) -> &'static str { + match self { + LogLevel::Trace => "trace", + LogLevel::Debug => "debug", + LogLevel::Info => "info", + LogLevel::Warn => "warn", + LogLevel::Error => "error", + } + } +} + +fn setup_tracing(log_level: LogLevel) { + tracing_subscriber::registry() + .with(EnvFilter::new(log_level.as_str())) + .with(fmt::layer().with_ansi(false)) + .init(); +}