Skip to content

Commit

Permalink
Fix tracing support, resolve #70
Browse files Browse the repository at this point in the history
  • Loading branch information
doesnotcompete committed Sep 25, 2024
1 parent c119110 commit 28fdc68
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tui-logger"
version = "0.13.1"
version = "0.13.2"
authors = ["Jochen Kiemes <jochen@kiemes.de>"]
edition = "2021"
license = "MIT"
Expand Down
40 changes: 26 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,13 @@ pub enum TuiLoggerError {
ThreadError(std::io::Error),
}
impl std::error::Error for TuiLoggerError {
fn description(&self) -> &str{
fn description(&self) -> &str {
match self {
TuiLoggerError::SetLoggerError(_) => "SetLoggerError",
TuiLoggerError::ThreadError(_) => "ThreadError",
}
}
fn cause(& self) -> Option<&dyn std::error::Error> {
fn cause(&self) -> Option<&dyn std::error::Error> {
match self {
TuiLoggerError::SetLoggerError(_) => None,
TuiLoggerError::ThreadError(err) => Some(err),
Expand All @@ -488,16 +488,24 @@ impl std::fmt::Display for TuiLoggerError {

/// Init the logger and record with `log` crate.
pub fn init_logger(max_level: LevelFilter) -> Result<(), TuiLoggerError> {
log::set_max_level(max_level);
let join_handle = thread::Builder::new().name("tui-logger::move_events".into()).spawn(|| {
let duration = std::time::Duration::from_millis(10);
loop {
thread::park_timeout(duration);
TUI_LOGGER.move_events();
}
}).map_err(|err| {TuiLoggerError::ThreadError(err)})?;
let join_handle = thread::Builder::new()
.name("tui-logger::move_events".into())
.spawn(|| {
let duration = std::time::Duration::from_millis(10);
loop {
thread::park_timeout(duration);
TUI_LOGGER.move_events();
}
})
.map_err(|err| TuiLoggerError::ThreadError(err))?;
TUI_LOGGER.hot_log.lock().mover_thread = Some(join_handle);
log::set_logger(&*TUI_LOGGER).map_err(|err| {TuiLoggerError::SetLoggerError(err)})
if cfg!(feature = "tracing-support") {
set_default_level(max_level);
Ok(())
} else {
log::set_max_level(max_level);
log::set_logger(&*TUI_LOGGER).map_err(|err| TuiLoggerError::SetLoggerError(err))
}
}

#[cfg(feature = "slog-support")]
Expand Down Expand Up @@ -559,11 +567,15 @@ impl TuiLogger {
line: record.line().unwrap_or(0),
msg: format!("{}", record.args()),
};
let mut events_lock =self.hot_log.lock();
let mut events_lock = self.hot_log.lock();
events_lock.events.push(log_entry);
let need_signal = (events_lock.events.total_elements() % (events_lock.events.capacity()/2)) == 0 ;
let need_signal =
(events_lock.events.total_elements() % (events_lock.events.capacity() / 2)) == 0;
if need_signal {
events_lock.mover_thread.as_ref().map(|jh| {thread::Thread::unpark(jh.thread())});
events_lock
.mover_thread
.as_ref()
.map(|jh| thread::Thread::unpark(jh.thread()));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tracing_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl<'a> tracing::field::Visit for ToStringVisitor<'a> {
/// tracing_subscriber::registry()
/// .with(tui_logger::tracing_subscriber_layer())
/// .init();
/// tui_logger::init_logger(LevelFilter::Trace).unwrap();
/// info!(log, "Logging via tracing works!");
/// }
pub struct TuiTracingSubscriberLayer;
Expand Down

0 comments on commit 28fdc68

Please sign in to comment.