Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not call log::set_logger when tracing-support is enabled #71

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0"
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