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

Avoid unwrap log initialization #4934

Closed
allevo opened this issue Jun 5, 2022 · 10 comments
Closed

Avoid unwrap log initialization #4934

allevo opened this issue Jun 5, 2022 · 10 comments
Labels
A-Diagnostics Logging, crash handling, error reporting and performance analysis C-Usability A targeted quality-of-life change that makes Bevy easier to use

Comments

@allevo
Copy link

allevo commented Jun 5, 2022

What problem does this solve or what need does it fill?

I would like to test heavily my application. Obviously my test suite is composed by multiple test and every test create a new through App::new() and register the plugin I need to the new app instance.
Because logs are important I really would like keep them active during the tests runs.

What solution would you like?

Remove the unwrap from here

LogTracer::init().unwrap();
substituting it with a different error handling.

What alternative(s) have you considered?

Remove LogPlugin from my tests

Additional context

// test 1
let mut app = App::new();
app.add_plugin(LogPlugin::default());
// test 2
let mut app = App::new();
app.add_plugin(LogPlugin::default()); // <----- this 

Error log:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SetLoggerError(())', .......\.cargo\registry\src\github.com-1ecc6299db9ec823\bevy_log-0.7.0\src\lib.rs:127:27
@allevo allevo added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jun 5, 2022
@allevo allevo changed the title Avoid unwrap on log initialization Avoid unwrap on initialization Jun 5, 2022
@allevo allevo changed the title Avoid unwrap on initialization Avoid unwrap log initialization Jun 5, 2022
@alice-i-cecile alice-i-cecile added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Diagnostics Logging, crash handling, error reporting and performance analysis D-Trivial Nice and easy! A great choice to get started with Bevy and removed C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Jun 5, 2022
@mockersf
Copy link
Member

mockersf commented Jun 5, 2022

when you are using multiple app (or in test), you should setup login yourself, and not add the LogPlugin several time. That way you would have unified login and no panic 🙂

@alice-i-cecile alice-i-cecile removed the D-Trivial Nice and easy! A great choice to get started with Bevy label Jun 5, 2022
@allevo
Copy link
Author

allevo commented Jun 6, 2022

Hi, thanks for your comment.

I wouldn't like to configure by myself the log part because:

  • this is not how other plugins work. In fact I'm able to adding them in different tests and all works fine
  • tests can not behave like the debug/release build doesp
  • copy and paste the code from bevy code to my one just for removing an unwrap is not so ok
  • could be a messy managing a own version for future changes

If my solution (adds a flag force) is not the best one, we can leverage on #[config(test)] to avoid the unwrap.

@bjorn3
Copy link
Contributor

bjorn3 commented Jun 6, 2022

we can leverage on #[config(test)] to avoid the unwrap.

#[cfg(test)] is only enabled for the crate actually being tested, and not for it's dependencies like bevy_log.

@allevo
Copy link
Author

allevo commented Jun 6, 2022

unwrap in a library still seems to me an ugly stuff...
Which approach we can take?
Just for knowing if you are interested in this enhancement or no....

@bjorn3
Copy link
Contributor

bjorn3 commented Jun 6, 2022

Honestly to me it feels like LogPlugin shouldn't be a plugin in the first place as it mutates global state and doesn't affect the App at all. Instead I feel like it should be a bevy::log::setup() call at the start of main. This would have a negative effect on ergonomics in case only a single App is used though.

@allevo
Copy link
Author

allevo commented Jun 6, 2022

Fair enough.
If you want, I can submit a PR for that

@Ryder17z
Copy link

Ryder17z commented Aug 3, 2022

Fair enough. If you want, I can submit a PR for that

please do. having to change it locally is annoying.

@anchpop
Copy link
Contributor

anchpop commented Sep 10, 2022

I tried changing LogTracer::init().unwrap(); to if let Ok(_) = LogTracer::init() { here: anchpop@ec8713b#diff-ce0ef3820ac31e1eaf71e97972a68d832239ca8affc9441f6dd419f434d15781R125

It does work at preventing it from panicking, but the problem is that it seems to be circumventing whatever magic cargo test does to suppress output to stdout when --nocapture isn't passed to the test binary. So I get noise like this in my tests:

2022-09-10T23:10:16.374437Z  INFO my_game::file: test log
2022-09-10T23:10:16.375310Z  INFO my_game::file: test log    // ← noise from logs
test tests::change_tests::test::my_test ... ok               // ← test output
2022-09-10T23:10:16.375938Z  INFO my_game::file: test log    // ← noise from logs
2022-09-10T23:10:16.376509Z  INFO my_game::file: test log
2022-09-10T23:10:16.376962Z  INFO my_game::file: test log

@bjorn3 do you know of a solution to this? I would be happy to make a PR if so, since would go well with #5931.

@bjorn3
Copy link
Contributor

bjorn3 commented Sep 11, 2022

Maybe the same thread pool is reused by multiple tests and once the test spawning the thread pool tests initially libtest doesn't know to which test to attribute the output anymore?

bors bot pushed a commit that referenced this issue Nov 28, 2022
# Objective

When a global tracing subscriber has already been set, `LogPlugin` panics with an error message explaining this. However, if a global logger has already been set, it simply panics on an unwrap.

#6426 mentiones the panic and has been fixed by unique plugins, but the panic can still occur if a logger has been set through different means or multiple apps are created, as in  #4934. The solution to that specific case isn't clear; this PR only fixes the missing error message.

## Solution

- ~add error message to panic~
- turn into warning
taiyoungjang pushed a commit to taiyoungjang/bevy that referenced this issue Dec 15, 2022
# Objective

When a global tracing subscriber has already been set, `LogPlugin` panics with an error message explaining this. However, if a global logger has already been set, it simply panics on an unwrap.

bevyengine#6426 mentiones the panic and has been fixed by unique plugins, but the panic can still occur if a logger has been set through different means or multiple apps are created, as in  bevyengine#4934. The solution to that specific case isn't clear; this PR only fixes the missing error message.

## Solution

- ~add error message to panic~
- turn into warning
alradish pushed a commit to alradish/bevy that referenced this issue Jan 22, 2023
# Objective

When a global tracing subscriber has already been set, `LogPlugin` panics with an error message explaining this. However, if a global logger has already been set, it simply panics on an unwrap.

bevyengine#6426 mentiones the panic and has been fixed by unique plugins, but the panic can still occur if a logger has been set through different means or multiple apps are created, as in  bevyengine#4934. The solution to that specific case isn't clear; this PR only fixes the missing error message.

## Solution

- ~add error message to panic~
- turn into warning
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
# Objective

When a global tracing subscriber has already been set, `LogPlugin` panics with an error message explaining this. However, if a global logger has already been set, it simply panics on an unwrap.

bevyengine#6426 mentiones the panic and has been fixed by unique plugins, but the panic can still occur if a logger has been set through different means or multiple apps are created, as in  bevyengine#4934. The solution to that specific case isn't clear; this PR only fixes the missing error message.

## Solution

- ~add error message to panic~
- turn into warning
@james7132
Copy link
Member

This was changed to a non-panicking warning in #6757.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Diagnostics Logging, crash handling, error reporting and performance analysis C-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants