feat(bindings/sdk-ffi): Allow ffi users to configure tracing and log levels

This commit is contained in:
Stefan Ceriu
2022-07-20 12:56:42 +03:00
parent 1cd18f49aa
commit f9bb86c52f
3 changed files with 39 additions and 0 deletions

View File

@@ -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"

View File

@@ -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);

View File

@@ -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<Runtime> =
@@ -55,3 +56,30 @@ impl From<anyhow::Error> 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();
}