Skip to content

Commit

Permalink
[larry-robotics#60] Add log and tracing logger
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Eltzschig <me@elchris.org>
  • Loading branch information
elfenpiff authored and elBoberido committed Nov 27, 2023
1 parent b4dd0e0 commit a749a3d
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ clap = { version = "3.2.0", features = ["derive"] }
enum-iterator = { version = "1.2.0" }
generic-tests = { version = "0.1.2" }
lazy_static = { version = "1.4.0" }
log = { version = "0.4.20" }
once_cell = { version = "1.16.0" }
ouroboros = { version = "0.17.2" }
pin-init = { version = "0.2.0" }
Expand All @@ -65,6 +66,7 @@ sha1_smol = { version = "1.0.0" }
termsize = { version = "0.1" }
tiny-fn = { version = "0.1.5" }
toml = { version = "0.5.9" }
tracing = { version = "0.1.40" }
windows-sys = { version = "0.48.0", features = ["Win32_Security", "Win32_Security_Authorization", "Win32_System_Memory", "Win32_System_Threading", "Win32_Foundation", "Win32_System_WindowsProgramming", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_Diagnostics_Debug", "Win32_System_SystemInformation", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Console", "Win32_Networking_WinSock"] }

[profile.release]
Expand Down
8 changes: 8 additions & 0 deletions elkodon_bb/log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ version = { workspace = true }
repository = { workspace = true }
edition = { workspace = true }

[features]
# Enables https://crates.io/crates/log as default logger
logger_log = ["dep:log"]
# Enables https://crates.io/crates/tracing as default logger
logger_tracing = ["dep:tracing"]

[dependencies]
termsize = { workspace = true }
log = { workspace = true, optional = true }
tracing = { workspace = true, optional = true }
9 changes: 8 additions & 1 deletion elkodon_bb/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ use std::{

use logger::Logger;

#[cfg(feature = "logger_tracing")]
static DEFAULT_LOGGER: logger::tracing::Logger = logger::tracing::Logger::new();
#[cfg(feature = "logger_log")]
static DEFAULT_LOGGER: logger::log::Logger = logger::log::Logger::new();
#[cfg(not(any(feature = "logger_tracing", feature = "logger_log")))]
static DEFAULT_LOGGER: logger::console::Logger = logger::console::Logger::new();

static mut LOGGER: Option<&'static dyn logger::Logger> = None;
static LOG_LEVEL: AtomicU8 = AtomicU8::new(LogLevel::Info as u8);
static INIT: Once = Once::new();
Expand All @@ -159,7 +165,8 @@ pub enum LogLevel {
Fatal = 5,
}

/// Sets the current log level
/// Sets the current log level. This is ignored for external frameworks like `log` or `tracing`.
/// Here you have to use the log-level settings of that framework.
pub fn set_log_level(v: LogLevel) {
LOG_LEVEL.store(v as u8, Ordering::Relaxed);
}
Expand Down
30 changes: 30 additions & 0 deletions elkodon_bb/log/src/logger/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::LogLevel;

pub struct Logger {
_priv: (),
}

impl Logger {
pub const fn new() -> Self {
Self { _priv: () }
}
}

impl crate::logger::Logger for Logger {
fn log(
&self,
log_level: crate::LogLevel,
origin: std::fmt::Arguments,
formatted_message: std::fmt::Arguments,
) {
let origin = format!("{}", origin);
match log_level {
LogLevel::Trace => log::trace!(target: &origin, "{}", formatted_message),
LogLevel::Debug => log::debug!(target: &origin, "{}", formatted_message),
LogLevel::Info => log::info!(target: &origin, "{}", formatted_message),
LogLevel::Warn => log::warn!(target: &origin, "{}", formatted_message),
LogLevel::Error => log::error!(target: &origin, "{}", formatted_message),
LogLevel::Fatal => log::error!(target: &origin, "{}", formatted_message),
}
}
}
4 changes: 4 additions & 0 deletions elkodon_bb/log/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
pub mod buffer;
pub mod console;
#[cfg(feature = "logger_log")]
pub mod log;
#[cfg(feature = "logger_tracing")]
pub mod tracing;

use std::fmt::Arguments;

Expand Down
29 changes: 29 additions & 0 deletions elkodon_bb/log/src/logger/tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::LogLevel;

pub struct Logger {
_priv: (),
}

impl Logger {
pub const fn new() -> Self {
Self { _priv: () }
}
}

impl crate::logger::Logger for Logger {
fn log(
&self,
log_level: crate::LogLevel,
origin: std::fmt::Arguments,
formatted_message: std::fmt::Arguments,
) {
match log_level {
LogLevel::Trace => tracing::trace!(origin, "{}", formatted_message),
LogLevel::Debug => tracing::debug!(origin, "{}", formatted_message),
LogLevel::Info => tracing::info!(origin, "{}", formatted_message),
LogLevel::Warn => tracing::warn!(origin, "{}", formatted_message),
LogLevel::Error => tracing::error!(origin, "{}", formatted_message),
LogLevel::Fatal => tracing::error!(origin, "{}", formatted_message),
}
}
}

0 comments on commit a749a3d

Please sign in to comment.