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)
- Hidelines does not work with multiple languages HOT 3
- Example with closure in chapter 13-01 (code listing without num but between 13-2 and 13-3 HOT 2
- The table of contents is unusable because the buttons to go back to the previous chapter and the table of contents overlap.
- Verify all Markdown files are linked from SUMMARY.md (or from somewhere else) HOT 1
- Syntax Highlighting for "JSON with Comments" a.k.a. jsonc HOT 1
- Request for Support of .NET 8.0 Features Including Primary Constructors and File-Scoped Namespaces HOT 1
- Bug: separator gets folded by subsection HOT 4
- how to .. localization ? HOT 1
- Summary can't deal with fragments, `#id` style deep links HOT 1
- Advanced features when including files
- markdown > syntax cannot wrap HOT 1
- Links with empty path cause error
- How to prevent the .git directory in the output directory from being deletedοΌ HOT 2
- Let all the output html files share the same one sidebar HOT 6
- Accessibility: New Markdown files must start with Heading Level 2 because Book Title is Heading level 1 HOT 4
- Support '/' shortcut to search
- Images with relative path not displayed on index.html HOT 1
- Figure out a strategy for updating public dependencies HOT 1
- 0.4.38 release has no assets HOT 2
- Is there a way to use own markdown short code templates? HOT 2
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.