Comments (9)
A way I've gone about this is to add a cargo
feature (I usually name it "debug") and create a macro which prints debugging output, but when compiled without the "debug" feature it does nothing and gets optimized away.
i.e.
# in Cargo.toml
[features]
debug = []
// In lib.rs
#[cfg(feature = "debug")]
macro_rules! debug {
($fmt:expr) => (println!(concat!("**DEBUG** ", $fmt)));
($fmt:expr, $($arg:tt)*) => (println!(concat!("**DEBUG** ",$fmt), $($arg)*));
}
#[cfg(not(feature = "debug"))]
macro_rules! debug {
($fmt:expr) => ();
($fmt:expr, $($arg:tt)*) => ();
}
Then throughout your code:
// In some source file...
fn my_function() {
debug!("Just entered my_function");
let a = 20;
debug!("A = {}", a);
}
Now when compiled with cargo build --features "debug"
you will see these debug messages printed to stdout
, but when compiled normally, nothing.
I like how it works, because it's easy to tell a user, "Recompile with --features "debug"
and send me the output"
from mdbook.
Seems like a pretty good idea.
I have never used macros, but they seem pretty powerful. Maybe I should learn how to use them properly.
Also since I want to make mdBook available as a crate for others to use, is there a way for them to enable / disable output without having to pass an argument to the called functions? Something similar to fn function(..., print: bool)
in functionality. Maybe there is some rust / cargo magic that can be used for this? 😉
from mdbook.
Yep! If you use the method described above users of mdbook
as a library
simply change their Cargo.toml
to enable/disable debugging output.
For example, no debugging info:
[dependencies.mdbook]
version = "*"
Or if they want debugging info:
[dependencies.mdbook]
version = "*"
features = ["debug"]
They could also do the same thing inline with the regular dependencies
[dependencies]
mdbook = { version = "*", features = ["debug"] }
from mdbook.
Nice! I will take a look into that. It looks a lot cleaner than passing an argument :)
from mdbook.
@kbknapp I am trying to implement this but I get an error when compiling with the debug feature.
I have made a macros.rs
file containing
#[cfg(feature = "debug")]
#[macro_export]
macro_rules! debug {
($fmt:expr) => (println!($fmt));
($fmt:expr, $($arg:tt)*) => (println!($fmt, $($arg)*));
}
#[cfg(not(feature = "debug"))]
#[macro_export]
macro_rules! debug {
($fmt:expr) => ();
($fmt:expr, $($arg:tt)*) => ();
}
But when compiling I get this error:
<std macros>:1:33: 1:58 error: invalid reference to argument `0` (no arguments given)
<std macros>:1 ( $ fmt : expr ) => ( print ! ( concat ! ( $ fmt , "\n" ) ) ) ; (
^~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of format_args!
<std macros>:2:25: 2:56 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:1:23: 1:60 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
src/macros.rs:4:21: 4:35 note: expansion site
src/macros.rs:3:1: 6:2 note: in expansion of debug!
src/parse/summary.rs:73:9: 73:100 note: expansion site
error: aborting due to previous error
Could not compile `mdbook`.
Have you any idea why?
from mdbook.
Could you commit what you've got to a separate branch and push it? I'd need to see the whole macros.rs
and src/parse/summary.rs
to see what the issue is.
Also, you don't need the #[macro_export]
unless you want consumers of the mdbook
library to use debug!()
which isn't required even if they compile with features = ["debug"]
from mdbook.
I will push to master since it does not prevent compiling without the feature. Thanks for looking :)
from mdbook.
I'd need to see the whole macros.rs and src/parse/summary.rs to see what the issue is.
Ow I didn't even notice that it was telling me to look at src/parse/summary.rs
I found the issue now. I feel a little bit ashamed for not catching that 😊 ..
Thanks for the help! :)
from mdbook.
No worries :)
from mdbook.
Related Issues (20)
- Replace icon fonts with SVG HOT 1
- Use capital letters to indicate the default value in (y/n) prompts
- serve only html backend
- Support for automated tests in languages other than Rust
- Exclude page from printing
- If `!search_config.copy_js`, then perform useless search indexing, and search is disabled
- feat(ux): add CMD+K support HOT 3
- Cannot load custom font files with 'mdbook serve' on macOS
- warn on orphan files
- Ordering of footnote HOT 1
- Ensure Consistent Table Widths in mdBook
- Keep summary links untouched (including Markdown formatting) instead of extracting just plain text
- Is there an option to disable automatic file creation from `SUMMARY.md`? HOT 2
- Small options for creating interactive fictions HOT 2
- Use Content-Secure-Policy compatible scripts
- Add predefined reference files for compilation
- Support different URL formats HOT 1
- mdBook x PyOdide: How to make Python code snippets run using pyodide? HOT 1
- Is there a way to force horizontal rules under headings while generating the book? HOT 2
- Is there a way to divide SUMMARY.md into sections, each with their own preliminary, main, and concluding chapters? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mdbook.