servoshell/desktop/
tracing.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5/// Log an event from winit ([winit::event::Event]) at trace level.
6/// - To disable tracing: RUST_LOG='servoshell<winit@=off'
7/// - To enable tracing: RUST_LOG='servoshell<winit@'
8/// - Recommended filters when tracing is enabled:
9///   - servoshell<winit@DeviceEvent=off
10///   - servoshell<winit@MainEventsCleared=off
11///   - servoshell<winit@NewEvents(WaitCancelled)=off
12///   - servoshell<winit@RedrawEventsCleared=off
13///   - servoshell<winit@RedrawRequested=off
14///   - servoshell<winit@UserEvent(Waker)=off
15///   - servoshell<winit@WindowEvent(AxisMotion)=off
16///   - servoshell<winit@WindowEvent(CursorMoved)=off
17macro_rules! trace_winit_event {
18    // This macro only exists to put the docs in the same file as the target prefix,
19    // so the macro definition is always the same.
20    ($event:expr, $($rest:tt)+) => {
21        ::log::trace!(target: $crate::desktop::tracing::LogTarget::log_target(&$event), $($rest)+)
22    };
23}
24
25pub(crate) use trace_winit_event;
26
27/// Get the log target for an event, as a static string.
28pub(crate) trait LogTarget {
29    fn log_target(&self) -> &'static str;
30}
31
32mod from_winit {
33    use super::LogTarget;
34    use crate::desktop::events_loop::AppEvent;
35
36    macro_rules! target {
37        ($($name:literal)+) => {
38            concat!("servoshell<winit@", $($name),+)
39        };
40    }
41
42    impl LogTarget for winit::event::Event<AppEvent> {
43        fn log_target(&self) -> &'static str {
44            use winit::event::StartCause;
45            match self {
46                Self::NewEvents(start_cause) => match start_cause {
47                    StartCause::ResumeTimeReached { .. } => target!("NewEvents(ResumeTimeReached)"),
48                    StartCause::WaitCancelled { .. } => target!("NewEvents(WaitCancelled)"),
49                    StartCause::Poll => target!("NewEvents(Poll)"),
50                    StartCause::Init => target!("NewEvents(Init)"),
51                },
52                Self::WindowEvent { event, .. } => event.log_target(),
53                Self::DeviceEvent { .. } => target!("DeviceEvent"),
54                Self::UserEvent(AppEvent::Waker) => target!("UserEvent(Waker)"),
55                Self::UserEvent(AppEvent::Accessibility(..)) => target!("UserEvent(Accessibility)"),
56                Self::Suspended => target!("Suspended"),
57                Self::Resumed => target!("Resumed"),
58                Self::AboutToWait => target!("AboutToWait"),
59                Self::LoopExiting => target!("LoopExiting"),
60                Self::MemoryWarning => target!("MemoryWarning"),
61            }
62        }
63    }
64
65    impl LogTarget for winit::event::WindowEvent {
66        fn log_target(&self) -> &'static str {
67            macro_rules! target_variant {
68                ($name:literal) => {
69                    target!("WindowEvent(" $name ")")
70                };
71            }
72            match self {
73                Self::ActivationTokenDone { .. } => target!("ActivationTokenDone"),
74                Self::Resized(..) => target_variant!("Resized"),
75                Self::Moved(..) => target_variant!("Moved"),
76                Self::CloseRequested => target_variant!("CloseRequested"),
77                Self::Destroyed => target_variant!("Destroyed"),
78                Self::DroppedFile(..) => target_variant!("DroppedFile"),
79                Self::HoveredFile(..) => target_variant!("HoveredFile"),
80                Self::HoveredFileCancelled => target_variant!("HoveredFileCancelled"),
81                Self::Focused(..) => target_variant!("Focused"),
82                Self::KeyboardInput { .. } => target_variant!("KeyboardInput"),
83                Self::ModifiersChanged(..) => target_variant!("ModifiersChanged"),
84                Self::Ime(..) => target_variant!("Ime"),
85                Self::CursorMoved { .. } => target_variant!("CursorMoved"),
86                Self::CursorEntered { .. } => target_variant!("CursorEntered"),
87                Self::CursorLeft { .. } => target_variant!("CursorLeft"),
88                Self::MouseWheel { .. } => target_variant!("MouseWheel"),
89                Self::MouseInput { .. } => target_variant!("MouseInput"),
90                Self::PanGesture { .. } => target_variant!("PanGesture"),
91                Self::PinchGesture { .. } => target_variant!("PinchGesture"),
92                Self::DoubleTapGesture { .. } => target_variant!("DoubleTapGesture"),
93                Self::RotationGesture { .. } => target_variant!("RotationGesture"),
94                Self::TouchpadPressure { .. } => target_variant!("TouchpadPressure"),
95                Self::AxisMotion { .. } => target_variant!("AxisMotion"),
96                Self::Touch(..) => target_variant!("Touch"),
97                Self::ScaleFactorChanged { .. } => target_variant!("ScaleFactorChanged"),
98                Self::ThemeChanged(..) => target_variant!("ThemeChanged"),
99                Self::Occluded(..) => target_variant!("Occluded"),
100                Self::RedrawRequested => target!("RedrawRequested"),
101            }
102        }
103    }
104}