Coder Social home page Coder Social logo

Comments (9)

kbknapp avatar kbknapp commented on August 15, 2024

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.

azerupi avatar azerupi commented on August 15, 2024

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.

kbknapp avatar kbknapp commented on August 15, 2024

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.

azerupi avatar azerupi commented on August 15, 2024

Nice! I will take a look into that. It looks a lot cleaner than passing an argument :)

from mdbook.

azerupi avatar azerupi commented on August 15, 2024

@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.

kbknapp avatar kbknapp commented on August 15, 2024

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.

azerupi avatar azerupi commented on August 15, 2024

I will push to master since it does not prevent compiling without the feature. Thanks for looking :)

from mdbook.

azerupi avatar azerupi commented on August 15, 2024

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.

kbknapp avatar kbknapp commented on August 15, 2024

No worries :)

from mdbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.