In a cmdline app I want to know if an error happened

For a command line app I use env_logger. At the end of the program I want to know
if there did an error!() happen so that I can exit with an error code <>l to 0.

I tried to use a global variable for this like follows:

In main.rs I defined

static ERROR_OCCURED: AtomicBool = AtomicBool::new(false);

In logger.rs where I format the output message I want to use ERROR_OCCURED so that in case the error level is log::Level::Error I can do ERROR_OCCURRED.store(true, Ordering::Relaxed);.

But I have no idea how I can declare ERROR_OCCURED in logger.rs so that I can use it there.

You shouldn't need to declare it again. Unless I misunderstand what you're asking, you should simply be able to say

fn some_function() {
    if /* level is error */ {
        crate::ERROR_OCCURED.store(true, Ordering::Relaxed);
    }
}
1 Like

Thanks, that's it.

Actually, I tried with crate:: but I had a typo ERROR_OCCURED instead of ERROR_OCCURRED and didn't understand the error message, telling me about similar name and such. :slight_smile: