Add macros for logging.

This commit is contained in:
2026-02-22 10:09:53 -05:00
parent c21ede292c
commit be969ef335
21 changed files with 246 additions and 79 deletions

View File

@@ -6,3 +6,4 @@ edition = "2024"
[dependencies]
anyhow = { workspace = true }
strum = { workspace = true }
log = { workspace = true }

View File

@@ -3,4 +3,5 @@
// SPDX-License-Identifier: GNU General Public License v3.0 or later
pub mod fs;
pub mod log;
pub mod theme;

5
libclide/src/log.rs Normal file
View File

@@ -0,0 +1,5 @@
// SPDX-FileCopyrightText: 2026, Shaun Reed <shaunrd0@gmail.com>
//
// SPDX-License-Identifier: GNU General Public License v3.0 or later
pub mod macros;

106
libclide/src/log/macros.rs Normal file
View File

@@ -0,0 +1,106 @@
//! Logging targets allow filtering of log messages by their source. By default, the log crate sets
//! the target to the module path where the log macro was invoked if no target is provided.
//!
//! These macros essentially disable using the default target and instead require the target to be
//! explicitly set. This is to avoid implicit pooling of log messages under the same default target,
//! which can make it difficult to filter log messages by their source.
#[macro_export]
macro_rules! info {
// The target argument can be overridden using one of the following macros.
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
log::info!(logger: $logger, target: $target, $($arg)+)
});
(target: $target:expr, $($arg:tt)+) => ({
log::info!(target: $target, $($arg)+)
});
// The target argument will default to Self::ID if not provided.
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
(logger: $logger:expr, $($arg:tt)+) => ({
log::info!(logger: $logger, target: Self::ID, $($arg)+)
});
($($arg:tt)+) => (log::info!(target: Self::ID, $($arg)+))
}
#[macro_export]
macro_rules! debug {
// The target argument can be overridden using one of the following macros.
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
log::debug!(logger: $logger, target: $target, $($arg)+)
});
(target: $target:expr, $($arg:tt)+) => ({
log::debug!(target: $target, $($arg)+)
});
// The target argument will default to Self::ID if not provided.
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
(logger: $logger:expr, $($arg:tt)+) => ({
log::debug!(logger: $logger, target: Self::ID, $($arg)+)
});
($($arg:tt)+) => (log::debug!(target: Self::ID, $($arg)+))
}
#[macro_export]
macro_rules! warn {
// The target argument can be overridden using one of the following macros.
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
log::warn!(logger: $logger, target: $target, $($arg)+)
});
(target: $target:expr, $($arg:tt)+) => ({
log::warn!(target: $target, $($arg)+)
});
// The target argument will default to Self::ID if not provided.
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
(logger: $logger:expr, $($arg:tt)+) => ({
log::warn!(logger: $logger, target: Self::ID, $($arg)+)
});
($($arg:tt)+) => (log::warn!(target: Self::ID, $($arg)+))
}
#[macro_export]
macro_rules! error {
// The target argument can be overridden using one of the following macros.
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
log::error!(logger: $logger, target: $target, $($arg)+)
});
(target: $target:expr, $($arg:tt)+) => ({
log::error!(target: $target, $($arg)+)
});
// The target argument will default to Self::ID if not provided.
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
(logger: $logger:expr, $($arg:tt)+) => ({
log::error!(logger: $logger, target: Self::ID, $($arg)+)
});
($($arg:tt)+) => (log::error!(target: Self::ID, $($arg)+))
}
#[macro_export]
macro_rules! trace {
// The target argument can be overridden using one of the following macros.
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
log::trace!(logger: $logger, target: $target, $($arg)+)
});
(target: $target:expr, $($arg:tt)+) => ({
log::trace!(target: $target, $($arg)+)
});
// The target argument will default to Self::ID if not provided.
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
(logger: $logger:expr, $($arg:tt)+) => ({
log::trace!(logger: $logger, target: Self::ID, $($arg)+)
});
($($arg:tt)+) => (log::trace!(target: Self::ID, $($arg)+))
}

View File

@@ -41,7 +41,7 @@ impl Colors {
let hex_full = match hex.len() {
3 => hex
.chars()
.map(|c| std::iter::repeat(c).take(2).collect::<String>())
.map(|c| std::iter::repeat_n(c, 2).collect::<String>())
.collect::<String>(),
6 => hex.to_string(),
_ => panic!("Invalid hex color length: {hex:?}"),