Skip to content

Commit bb405cc

Browse files
committed
Use tracing macros in examples instead of println!
This allows the examples to work a bit better in WASM and on iOS.
1 parent 26ec6f0 commit bb405cc

File tree

14 files changed

+46
-34
lines changed

14 files changed

+46
-34
lines changed

clippy.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Using allow-invalid because this is platform-specific code
2+
disallowed-macros = [
3+
{ path = "std::print", reason = "works badly on web", replacement = "tracing::info" },
4+
{ path = "std::println", reason = "works badly on web", replacement = "tracing::info" },
5+
{ path = "std::eprint", reason = "works badly on web", replacement = "tracing::error" },
6+
{ path = "std::eprintln", reason = "works badly on web", replacement = "tracing::error" },
7+
{ path = "std::dbg", reason = "leftover debugging aid, remove it or use tracing" },
8+
]
29
disallowed-methods = [
310
{ allow-invalid = true, path = "objc2_app_kit::NSView::visibleRect", reason = "We expose a render target to the user, and visibility is not really relevant to that (and can break if you don't use the rectangle position as well). Use `frame` instead." },
411
{ allow-invalid = true, path = "objc2_app_kit::NSWindow::setFrameTopLeftPoint", reason = "Not sufficient when working with Winit's coordinate system, use `flip_window_screen_coordinates` instead" },

winit-common/src/event_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ impl EventHandler {
7979
// Allowed, happens if the handler was cleared manually
8080
// elsewhere (such as in `applicationWillTerminate:`).
8181
},
82+
// We use `eprintln!` here over `tracing::error!`, since we're going to abort
83+
// immediately after this, and it'd be annoying for the user if they didn't get
84+
// any feedback on that if they don't have a tracing subscriber.
85+
#[allow(clippy::disallowed_macros)]
8286
Err(_) => {
8387
// Note: This is not expected to ever happen, this
8488
// module generally controls the `RefCell`, and

winit-win32/src/keyboard_layout.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,7 @@ impl LayoutCache {
388388
let key = match unicode {
389389
ToUnicodeResult::Str(str) => Key::Character(SmolStr::new(str)),
390390
ToUnicodeResult::Dead(dead_char) => {
391-
// println!("{:?} - {:?} produced dead {:?}", key_code, mod_state,
392-
// dead_char);
391+
// trace!("{:?} - {:?} produced dead {:?}", key_code, mod_state, dead_char);
393392
Key::Dead(dead_char)
394393
},
395394
ToUnicodeResult::None => {

winit-x11/src/util/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ impl XConnection {
6666
// All util functions that abstract an async function will return a `Flusher`.
6767
pub fn flush_requests(&self) -> Result<(), XError> {
6868
unsafe { (self.xlib.XFlush)(self.display) };
69-
// println!("XFlush");
69+
// tracing::trace!("XFlush");
7070
// This isn't necessarily a useful time to check for errors (since our request hasn't
7171
// necessarily been processed yet)
7272
self.check_errors()
7373
}
7474

7575
pub fn sync_with_server(&self) -> Result<(), XError> {
7676
unsafe { (self.xlib.XSync)(self.display, ffi::False) };
77-
// println!("XSync");
77+
// tracing::trace!("XSync");
7878
self.check_errors()
7979
}
8080
}

winit/build.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use cfg_aliases::cfg_aliases;
22

3+
// Only relevant for examples and Winit, our usage of println! is fine here.
4+
#[allow(clippy::disallowed_macros)]
35
fn main() {
4-
// The script doesn't depend on our code.
6+
// Dummy invocation to enable change-tracking in build scripts.
57
println!("cargo:rerun-if-changed=build.rs");
68

79
// Setup cfg aliases.

winit/examples/child_window.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
fn main() -> Result<(), impl std::error::Error> {
44
use std::collections::HashMap;
55

6+
use tracing::info;
67
use winit::application::ApplicationHandler;
78
use winit::dpi::{LogicalPosition, LogicalSize, Position};
89
use winit::event::{ElementState, KeyEvent, WindowEvent};
@@ -38,7 +39,7 @@ fn main() -> Result<(), impl std::error::Error> {
3839
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
3940
.with_surface_size(LogicalSize::new(640.0f32, 480.0f32));
4041
let window = event_loop.create_window(attributes).unwrap();
41-
println!("Parent window id: {:?})", window.id());
42+
info!("Parent window id: {:?})", window.id());
4243
self.parent_window_id = Some(window.id());
4344

4445
self.windows.insert(window.id(), WindowData::new(window, 0xffbbbbbb));
@@ -56,12 +57,12 @@ fn main() -> Result<(), impl std::error::Error> {
5657
event_loop.exit();
5758
},
5859
WindowEvent::PointerEntered { device_id: _, .. } => {
59-
// On x11, println when the cursor entered in a window even if the child window
60+
// On x11, log when the cursor entered in a window even if the child window
6061
// is created by some key inputs.
6162
// the child windows are always placed at (0, 0) with size (200, 200) in the
6263
// parent window, so we also can see this log when we move
6364
// the cursor around (200, 200) in parent window.
64-
println!("cursor entered in the window {window_id:?}");
65+
info!("cursor entered in the window {window_id:?}");
6566
},
6667
WindowEvent::KeyboardInput {
6768
event: KeyEvent { state: ElementState::Pressed, .. },
@@ -75,7 +76,7 @@ fn main() -> Result<(), impl std::error::Error> {
7576
let child_window =
7677
spawn_child_window(parent_window.window.as_ref(), event_loop, child_index);
7778
let child_id = child_window.id();
78-
println!("Child window created with id: {child_id:?}");
79+
info!("Child window created with id: {child_id:?}");
7980
self.windows.insert(child_id, WindowData::new(child_window, child_color));
8081
},
8182
WindowEvent::RedrawRequested => {

winit/examples/control_flow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::thread;
44
#[cfg(not(web_platform))]
55
use std::time;
66

7-
use ::tracing::{info, warn};
7+
use tracing::{info, warn};
88
#[cfg(web_platform)]
99
use web_time as time;
1010
use winit::application::ApplicationHandler;

winit/examples/dnd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::error::Error;
22

3+
use tracing::info;
34
use winit::application::ApplicationHandler;
45
use winit::event::WindowEvent;
56
use winit::event_loop::{ActiveEventLoop, EventLoop};
@@ -49,7 +50,7 @@ impl ApplicationHandler for Application {
4950
| WindowEvent::DragEntered { .. }
5051
| WindowEvent::DragMoved { .. }
5152
| WindowEvent::DragDropped { .. } => {
52-
println!("{event:?}");
53+
info!("{event:?}");
5354
},
5455
WindowEvent::RedrawRequested => {
5556
let window = self.window.as_ref().unwrap();

winit/examples/ime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl ApplicationHandler for App {
7676
self.window = match event_loop.create_window(window_attributes) {
7777
Ok(window) => Some(window),
7878
Err(err) => {
79-
eprintln!("error creating window: {err}");
79+
error!("error creating window: {err}");
8080
event_loop.exit();
8181
return;
8282
},
@@ -346,7 +346,7 @@ fn main() -> Result<(), Box<dyn Error>> {
346346

347347
let event_loop = EventLoop::new()?;
348348

349-
println!(
349+
info!(
350350
r#"This showcases the use of an input method engine (IME) by emulating a text edit field.
351351
Use CTRL+i to toggle IME support.
352352
Use CTRL+p to cycle content purpose values.

winit/examples/pump_events.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ fn main() -> std::process::ExitCode {
77
use std::thread::sleep;
88
use std::time::Duration;
99

10+
use tracing::info;
1011
use winit::application::ApplicationHandler;
1112
use winit::event::WindowEvent;
1213
use winit::event_loop::pump_events::{EventLoopExtPumpEvents, PumpStatus};
@@ -33,7 +34,7 @@ fn main() -> std::process::ExitCode {
3334
_window_id: WindowId,
3435
event: WindowEvent,
3536
) {
36-
println!("{event:?}");
37+
info!("{event:?}");
3738

3839
let window = match self.window.as_ref() {
3940
Some(window) => window,
@@ -69,12 +70,12 @@ fn main() -> std::process::ExitCode {
6970
//
7071
// Since `pump_events` doesn't block it will be important to
7172
// throttle the loop in the app somehow.
72-
println!("Update()");
73+
info!("Update()");
7374
sleep(Duration::from_millis(16));
7475
}
7576
}
7677

7778
#[cfg(any(ios_platform, web_platform, orbital_platform))]
7879
fn main() {
79-
println!("This platform doesn't support pump_events.");
80+
panic!("This platform doesn't support pump_events.")
8081
}

0 commit comments

Comments
 (0)