Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger crate catches Clion stop signal #305

Closed
lioriz1 opened this issue Feb 1, 2024 · 1 comment
Closed

logger crate catches Clion stop signal #305

lioriz1 opened this issue Feb 1, 2024 · 1 comment

Comments

@lioriz1
Copy link

lioriz1 commented Feb 1, 2024

Passing this issue, here.

I'm using Clion developing in a remote environment.
When I'm stoping the process from the clion stop the logs stops but the process not.

fn init_logger() -> Result<(), String> {
    env_logger::Builder::new()
        .format(|buf, record| {
            writeln!(
                buf,
                "{}:{} {} [{}]: {}",
                record.file().unwrap_or("unknown"),
                record.line().unwrap_or(0),
                chrono::Local::now().format("%Y-%m-%d %H:%M:%S.%3f"),
                record.level(),
                record.args()
            )
        })
        .filter(None, LevelFilter::Debug)
        .init();
    Ok(())
}

fn main() -> Result<(), String>  {
    let _ = init_logger();
    info!("Hello, world!");
    for i in 0..10 {
        info!("{}", i);
        // spdlog::info!("{}", i);
        // println!("{}", i);
        thread::sleep(Duration::from_secs(1));
    }
    return Ok(());
}

In this example when I'm stopping the process in the middle the logs will stop but the loop will run to the end, the full 10 seconds and the process will exit successfully.

If I change from log::info! to spdlog::info! or from log::info! to println! or adding .is_test(true) to log init, it works as expected, exits imidietly with fail code.

If I run in regular Clion run on my machine compiler log works fine the same as the other loggers but I would expect it to work the same in my remote environment.

Is there a reason for this behavior?
What is the .is_test(true) flag?

@epage
Copy link
Contributor

epage commented Feb 1, 2024

By "exit immediately", you are referring to a panic from a broken pipe? That is not expected behavior and we try to avoid it (e.g. #221).

println panics on a broken pipe error. It is generally best to use writeln and handle the broken pipe error as works best in your application. For example, a tool like ripgrep might decide that it should exit early with a success exit code on a broken pipe.

.is_test(true) is a hack to take advantage of a hack in std / test. The default test harness, test, uses an unstable hook into std to capture the output from print/println so that it can hide it by default and then only print it along with the test failure. .is_test(true) forces env_logger to go through println so logged messages from a test can be captured with the test.

As this is working as expected, I'm going to go ahead and close this.

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants