-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example showing a macro wrapping bunt macros
- Loading branch information
1 parent
9d58680
commit 2bc1aa7
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! Sometimes, you might want to wrap bunt's macros into your own | ||
//! project-specific macro. Often, you want to print a prefix or something like | ||
//! that. | ||
|
||
|
||
macro_rules! log { | ||
($fmt:literal $(, $arg:expr)* $(,)?) => { | ||
bunt::println!( | ||
// Bunt macros allow to pass an "array" of format strings which are | ||
// then concatenated by bunt. | ||
[ | ||
// Our prefix | ||
"[{[magenta] log_module_path}] ", | ||
// What the user passed | ||
$fmt, | ||
// Our postfix | ||
" {$cyan}({log_file}:{log_line}){/$}", | ||
], | ||
$($arg ,)* | ||
log_module_path = std::module_path!(), | ||
log_file = std::file!(), | ||
log_line = std::line!(), | ||
) | ||
|
||
// This solution is not optimal though. For one, it would be nice to | ||
// `stringify!(std::module_path!())` instead of passing it as runtime | ||
// string argument. That's not possible because macro expansions are | ||
// lazy. | ||
// | ||
// Futhermore, we pass the arguments with names like `log_module_path`. | ||
// In theory, the user could also use a named argument with that name. | ||
// This `log!` macro is very much an internal helper macro and not | ||
// something you would want to put into your public API. | ||
}; | ||
} | ||
|
||
|
||
fn main() { | ||
log!("Hello {}", "peter"); | ||
banana::do_something(); | ||
} | ||
|
||
mod banana { | ||
pub fn do_something() { | ||
log!("I'm doing something with {[yellow]:?}", vec![1, 2, 4]); | ||
} | ||
} |