gobanos / cargo-aoc

438 stars 47 forks source link

Running solutions with `RUST_LOG=DEBUG` #24

Open arosspope opened 5 years ago

arosspope commented 5 years ago

I was wondering if there is any way to show the debug statements produced by our 'day code'? Currently, it seems like cargo aoc is suppressing debug statements. As it seems like some of the problems in this year's AOC require visual checking, I think it would be cool if logging worked.

jonenst commented 5 years ago

You need to call the init function of the logging implementation you chose (for example env_logger). But init can only be called once, so maybe the aoc_main macro from https://github.com/gobanos/aoc-runner-derive should allow to configure it (or run arbitrary setup code ?) or maybe cargo-aoc should hardcode the use of env_logger ?

tbarusseau commented 4 years ago

I am wondering if there is now an existing solution for this?

Thanks in advance.

Chris--B commented 11 months ago

Has there been any movement on this?

I have a work around. It's not great but it works on my Mac, even when I run cargo aoc.

Use ctor to run code when the 'module' is loaded. See crate here. tldr:

Module initialization/teardown functions for Rust (like __attribute__((constructor)) in C/C++) for Linux, OSX, FreeBSD, NetBSD, Illumos, OpenBSD, DragonFlyBSD, Android, iOS, and Windows

I put this in my lib.rs:

#[ctor::ctor]
fn init_logging() {
    use env_logger::{Builder, Env};

    Builder::from_env(Env::default().default_filter_or("warn")).init();

    // Easy check to see if logging is logging
    trace!("Hello");
    debug!("Hello");
    info!("Hello");
    warn!("Hello");
    error!("Hello");
}

It does not appear to work in main.rs. This will also run if anything else consume yours library, and might do Weird Things:tm:. So take this with a grain of salt.