Skip to content

Commit

Permalink
Merge pull request #426 from breez/exclude-file-logger
Browse files Browse the repository at this point in the history
Exclude file logger. Revert to the old behavior
  • Loading branch information
roeierez authored Sep 5, 2023
2 parents 0173d26 + 5038b48 commit 8637341
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
41 changes: 21 additions & 20 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use anyhow::{anyhow, Result};
use log::{Metadata, Record};
use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::{Lazy, OnceCell};

use breez_sdk_core::{
Expand All @@ -24,25 +24,31 @@ use breez_sdk_core::{
};

static RT: Lazy<tokio::runtime::Runtime> = Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
static LOG_STREAM: OnceCell<Box<dyn LogStream>> = OnceCell::new();
static LOG_INIT: OnceCell<bool> = OnceCell::new();

struct BindingLogger {}
struct BindingLogger {
log_stream: Box<dyn LogStream>,
}

impl BindingLogger {
fn init(log_stream: Box<dyn LogStream>) {
let binding_logger = BindingLogger { log_stream };
log::set_boxed_logger(Box::new(binding_logger)).unwrap();
log::set_max_level(LevelFilter::Trace);
}
}

impl log::Log for BindingLogger {
fn enabled(&self, m: &Metadata) -> bool {
// ignore the internal uniffi log to prevent infinite loop.
return *m.target() != *"breez_sdk_bindings::uniffi_binding";
return m.level() <= Level::Debug && *m.target() != *"breez_sdk_bindings::uniffi_binding";
}

fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
if let Some(s) = LOG_STREAM.get() {
s.log(LogEntry {
line: record.args().to_string(),
level: record.level().as_str().to_string(),
});
}
}
self.log_stream.log(LogEntry {
line: record.args().to_string(),
level: record.level().as_str().to_string(),
});
}
fn flush(&self) {}
}
Expand Down Expand Up @@ -74,12 +80,6 @@ pub fn connect(
event_listener: Box<dyn EventListener>,
) -> SdkResult<Arc<BlockingBreezServices>> {
rt().block_on(async move {
let uniffi_logger: Option<Box<dyn log::Log>> = match LOG_STREAM.get() {
None => None,
Some(_) => Some(Box::new(BindingLogger {})),
};
BreezServices::init_logging(&config.working_dir, uniffi_logger)?;

let breez_services = BreezServices::connect(config, seed, event_listener).await?;

Ok(Arc::new(BlockingBreezServices { breez_services }))
Expand All @@ -88,9 +88,10 @@ pub fn connect(

/// If used, this must be called before `connect`
pub fn set_log_stream(log_stream: Box<dyn LogStream>) -> Result<()> {
LOG_STREAM
.set(log_stream)
LOG_INIT
.set(true)
.map_err(|_| anyhow!("log stream already created"))?;
BindingLogger::init(log_stream);
Ok(())
}

Expand Down
44 changes: 24 additions & 20 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::Arc;

use anyhow::{anyhow, Result};
use flutter_rust_bridge::StreamSink;
use log::{Level, Metadata, Record};
use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::{Lazy, OnceCell};
use tokio::sync::mpsc;

Expand All @@ -40,20 +40,14 @@ use crate::{
static BREEZ_SERVICES_INSTANCE: OnceCell<Arc<BreezServices>> = OnceCell::new();
static BREEZ_SERVICES_SHUTDOWN: OnceCell<mpsc::Sender<()>> = OnceCell::new();
static NOTIFICATION_STREAM: OnceCell<StreamSink<BreezEvent>> = OnceCell::new();
static LOG_STREAM: OnceCell<StreamSink<LogEntry>> = OnceCell::new();
static RT: Lazy<tokio::runtime::Runtime> = Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
static LOG_INIT: OnceCell<bool> = OnceCell::new();

/* Breez Services API's */

/// Wrapper around [BreezServices::connect] which also initializes SDK logging
pub fn connect(config: Config, seed: Vec<u8>) -> Result<()> {
block_on(async move {
BreezServices::init_logging(
&config.working_dir,
Some(Box::new(BindingLogStreamEventListener {})),
)
.map_err(anyhow::Error::new)?;

let breez_services =
BreezServices::connect(config, seed, Box::new(BindingEventListener {})).await?;

Expand Down Expand Up @@ -136,9 +130,11 @@ pub fn breez_events_stream(s: StreamSink<BreezEvent>) -> Result<()> {

/// If used, this must be called before `connect`. It can only be called once.
pub fn breez_log_stream(s: StreamSink<LogEntry>) -> Result<()> {
LOG_STREAM
.set(s)
.map_err(|_| anyhow!("log stream already created"))
LOG_INIT
.set(true)
.map_err(|_| anyhow!("log stream already created"))?;
BindingLogger::init(s);
Ok(())
}

/* LSP API's */
Expand Down Expand Up @@ -391,22 +387,30 @@ impl EventListener for BindingEventListener {
}
}

struct BindingLogStreamEventListener;
struct BindingLogger {
log_stream: StreamSink<LogEntry>,
}

impl BindingLogger {
fn init(log_stream: StreamSink<LogEntry>) {
let binding_logger = BindingLogger { log_stream };
log::set_boxed_logger(Box::new(binding_logger)).unwrap();
log::set_max_level(LevelFilter::Trace);
}
}

impl log::Log for BindingLogStreamEventListener {
impl log::Log for BindingLogger {
fn enabled(&self, m: &Metadata) -> bool {
m.level() <= Level::Debug
}

fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
if let Some(s) = LOG_STREAM.get() {
s.add(LogEntry {
line: record.args().to_string(),
level: record.level().as_str().to_string(),
});
}
};
self.log_stream.add(LogEntry {
line: record.args().to_string(),
level: record.level().as_str().to_string(),
});
}
}
fn flush(&self) {}
}
Expand Down

0 comments on commit 8637341

Please sign in to comment.