1use bevy::{log::once, prelude::*};
4
5fn main() {
6 App::new()
7 .add_plugins(DefaultPlugins.set(bevy::log::LogPlugin {
8 ..default()
12 }))
13 .add_systems(Startup, setup)
14 .add_systems(Update, log_system)
15 .add_systems(Update, log_once_system)
16 .add_systems(Update, panic_on_p)
17 .run();
18}
19
20fn setup(mut commands: Commands) {
21 commands.spawn(Camera2d);
22 commands.spawn((
23 Text::new("Press P to panic"),
24 Node {
25 position_type: PositionType::Absolute,
26 top: px(12),
27 left: px(12),
28 ..default()
29 },
30 ));
31}
32
33fn panic_on_p(keys: Res<ButtonInput<KeyCode>>) {
34 if keys.just_pressed(KeyCode::KeyP) {
35 panic!("P pressed, panicking");
36 }
37}
38
39fn log_system() {
40 trace!("very noisy");
43 debug!("helpful for debugging");
44 info!("helpful information that is worth printing by default");
45 warn!("something bad happened that isn't a failure, but thats worth calling out");
46 error!("something failed");
47
48 }
55
56fn log_once_system() {
57 trace_once!("one time noisy message");
61 debug_once!("one time debug message");
62 info_once!("some info which is printed only once");
63 warn_once!("some warning we wish to call out only once");
64 error_once!("some error we wish to report only once");
65
66 for i in 0..10 {
67 info_once!("logs once per call site, so this works just fine: {}", i);
68 }
69
70 once!({
74 info!("doing expensive things");
75 let mut a: u64 = 0;
76 for i in 0..100000000 {
77 a += i;
78 }
79 info!("result of some expensive one time calculation: {}", a);
80 });
81}