Skip to content

Commit 779872b

Browse files
committed
Add macros for logging.
1 parent e5f77ef commit 779872b

21 files changed

Lines changed: 246 additions & 79 deletions

File tree

.github/workflows/check.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ jobs:
2626
- name: Build libclide
2727
run: |
2828
cargo b -p libclide --release
29+
- name: Build libclide-macros
30+
run: |
31+
cargo b -p libclide-macros --release
2932
- name: Build clide
3033
run: |
3134
cargo b --release
@@ -44,6 +47,9 @@ jobs:
4447
- name: Test libclide
4548
run: |
4649
cargo test -p libclide
50+
- name: Test libclide-macros
51+
run: |
52+
cargo test -p libclide-macros
4753
- name: Test clide
4854
run: |
4955
cargo test
@@ -62,6 +68,9 @@ jobs:
6268
- name: Lint libclide
6369
run: |
6470
cargo clippy -p libclide -- -D warnings
71+
- name: Lint libclide-macros
72+
run: |
73+
cargo clippy -p libclide-macros -- -D warnings
6574
- name: Lint clide
6675
run: |
6776
cargo clippy -- -D warnings
@@ -80,6 +89,9 @@ jobs:
8089
- name: Format libclide
8190
run: |
8291
cargo fmt -p libclide --verbose -- --check
92+
- name: Format libclide-macros
93+
run: |
94+
cargo fmt -p libclide-macros -- --check
8395
- name: Format clide
8496
run: |
8597
cargo fmt --verbose -- --check

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ edition = "2024"
55

66
[workspace]
77
resolver = "3"
8-
members = [
9-
".",
10-
"libclide",
11-
]
8+
members = [".", "libclide", "libclide-macros", ]
129

1310
[workspace.dependencies]
1411
anyhow = "1.0.100"
1512
strum = "0.27.2"
13+
log = { version = "0.4.27", features = [] }
1614

1715
[dependencies]
1816
cxx = "1.0.95"
1917
cxx-qt = "0.8.0"
2018
cxx-qt-lib = { version = "0.8.0", features = ["qt_full", "qt_gui", "qt_qml"] }
21-
log = { version = "0.4.27", features = [] }
2219
dirs = "6.0.0"
2320
syntect = "5.2.0"
2421
clap = { version = "4.5.54", features = ["derive"] }
@@ -28,9 +25,11 @@ tui-logger = "0.18.1"
2825
edtui = "0.11.1"
2926
devicons = "0.6.12"
3027
libclide = { path = "./libclide" }
28+
libclide-macros = { path = "./libclide-macros" }
3129
anyhow = { workspace = true }
3230
strum = { workspace = true }
31+
log = { workspace = true }
3332

3433
[build-dependencies]
3534
# The link_qt_object_files feature is required for statically linking Qt 6.
36-
cxx-qt-build = { version = "0.8.0", features = ["link_qt_object_files"] }
35+
cxx-qt-build = { version = "0.8.0", features = ["link_qt_object_files"] }

libclide-macros/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "libclide-macros"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
syn = { version = "2", features = ["full"] }
11+
quote = "1"
12+
proc-macro2 = "1"

libclide-macros/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use proc_macro::TokenStream;
2+
use quote::quote;
3+
use syn::{ItemStruct, parse_macro_input};
4+
5+
#[proc_macro_attribute]
6+
pub fn log_id(_attr: TokenStream, item: TokenStream) -> TokenStream {
7+
let input = parse_macro_input!(item as ItemStruct);
8+
9+
let struct_name = &input.ident;
10+
let generics = &input.generics;
11+
12+
// This is the important part
13+
let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
14+
15+
let struct_name_str = struct_name.to_string();
16+
17+
let expanded = quote! {
18+
#input
19+
20+
impl #impl_generics #struct_name #type_generics #where_clause {
21+
#[allow(unused)]
22+
pub const ID: &'static str = #struct_name_str;
23+
}
24+
};
25+
26+
TokenStream::from(expanded)
27+
}

libclide/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ edition = "2024"
66
[dependencies]
77
anyhow = { workspace = true }
88
strum = { workspace = true }
9+
log = { workspace = true }

libclide/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
// SPDX-License-Identifier: GNU General Public License v3.0 or later
44

55
pub mod fs;
6+
pub mod log;
67
pub mod theme;

libclide/src/log.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// SPDX-FileCopyrightText: 2026, Shaun Reed <shaunrd0@gmail.com>
2+
//
3+
// SPDX-License-Identifier: GNU General Public License v3.0 or later
4+
5+
pub mod macros;

libclide/src/log/macros.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//! Logging targets allow filtering of log messages by their source. By default, the log crate sets
2+
//! the target to the module path where the log macro was invoked if no target is provided.
3+
//!
4+
//! These macros essentially disable using the default target and instead require the target to be
5+
//! explicitly set. This is to avoid implicit pooling of log messages under the same default target,
6+
//! which can make it difficult to filter log messages by their source.
7+
8+
#[macro_export]
9+
macro_rules! info {
10+
// The target argument can be overridden using one of the following macros.
11+
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
12+
log::info!(logger: $logger, target: $target, $($arg)+)
13+
});
14+
15+
(target: $target:expr, $($arg:tt)+) => ({
16+
log::info!(target: $target, $($arg)+)
17+
});
18+
19+
// The target argument will default to Self::ID if not provided.
20+
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
21+
(logger: $logger:expr, $($arg:tt)+) => ({
22+
log::info!(logger: $logger, target: Self::ID, $($arg)+)
23+
});
24+
25+
($($arg:tt)+) => (log::info!(target: Self::ID, $($arg)+))
26+
}
27+
28+
#[macro_export]
29+
macro_rules! debug {
30+
// The target argument can be overridden using one of the following macros.
31+
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
32+
log::debug!(logger: $logger, target: $target, $($arg)+)
33+
});
34+
35+
(target: $target:expr, $($arg:tt)+) => ({
36+
log::debug!(target: $target, $($arg)+)
37+
});
38+
39+
// The target argument will default to Self::ID if not provided.
40+
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
41+
(logger: $logger:expr, $($arg:tt)+) => ({
42+
log::debug!(logger: $logger, target: Self::ID, $($arg)+)
43+
});
44+
45+
($($arg:tt)+) => (log::debug!(target: Self::ID, $($arg)+))
46+
}
47+
48+
#[macro_export]
49+
macro_rules! warn {
50+
// The target argument can be overridden using one of the following macros.
51+
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
52+
log::warn!(logger: $logger, target: $target, $($arg)+)
53+
});
54+
55+
(target: $target:expr, $($arg:tt)+) => ({
56+
log::warn!(target: $target, $($arg)+)
57+
});
58+
59+
// The target argument will default to Self::ID if not provided.
60+
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
61+
(logger: $logger:expr, $($arg:tt)+) => ({
62+
log::warn!(logger: $logger, target: Self::ID, $($arg)+)
63+
});
64+
65+
($($arg:tt)+) => (log::warn!(target: Self::ID, $($arg)+))
66+
}
67+
68+
#[macro_export]
69+
macro_rules! error {
70+
// The target argument can be overridden using one of the following macros.
71+
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
72+
log::error!(logger: $logger, target: $target, $($arg)+)
73+
});
74+
75+
(target: $target:expr, $($arg:tt)+) => ({
76+
log::error!(target: $target, $($arg)+)
77+
});
78+
79+
// The target argument will default to Self::ID if not provided.
80+
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
81+
(logger: $logger:expr, $($arg:tt)+) => ({
82+
log::error!(logger: $logger, target: Self::ID, $($arg)+)
83+
});
84+
85+
($($arg:tt)+) => (log::error!(target: Self::ID, $($arg)+))
86+
}
87+
88+
#[macro_export]
89+
macro_rules! trace {
90+
// The target argument can be overridden using one of the following macros.
91+
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
92+
log::trace!(logger: $logger, target: $target, $($arg)+)
93+
});
94+
95+
(target: $target:expr, $($arg:tt)+) => ({
96+
log::trace!(target: $target, $($arg)+)
97+
});
98+
99+
// The target argument will default to Self::ID if not provided.
100+
// Obviously, this is an error if Self::ID is not defined, forcing you to use the explicit form.
101+
(logger: $logger:expr, $($arg:tt)+) => ({
102+
log::trace!(logger: $logger, target: Self::ID, $($arg)+)
103+
});
104+
105+
($($arg:tt)+) => (log::trace!(target: Self::ID, $($arg)+))
106+
}

libclide/src/theme/colors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Colors {
4141
let hex_full = match hex.len() {
4242
3 => hex
4343
.chars()
44-
.map(|c| std::iter::repeat(c).take(2).collect::<String>())
44+
.map(|c| std::iter::repeat_n(c, 2).collect::<String>())
4545
.collect::<String>(),
4646
6 => hex.to_string(),
4747
_ => panic!("Invalid hex color length: {hex:?}"),

0 commit comments

Comments
 (0)