wayland_backend/
debug.rs
1use std::{
4 fmt::Display,
5 os::unix::io::AsRawFd,
6 time::{SystemTime, UNIX_EPOCH},
7};
8
9use crate::protocol::Argument;
10
11pub fn has_debug_client_env() -> bool {
13 matches!(std::env::var_os("WAYLAND_DEBUG"), Some(str) if str == "1" || str == "client")
14}
15
16#[cfg_attr(coverage, coverage(off))]
20pub fn print_dispatched_message<Id: Display, Fd: AsRawFd>(
21 interface: &str,
22 id: u32,
23 msg_name: &str,
24 args: &[Argument<Id, Fd>],
25) {
26 print_timestamp();
28
29 eprint!(" <- {}@{}.{}, ({})", interface, id, msg_name, DisplaySlice(args));
30
31 eprintln!();
33}
34
35#[cfg_attr(coverage, coverage(off))]
39pub fn print_send_message<Id: Display, Fd: AsRawFd>(
40 interface: &str,
41 id: u32,
42 msg_name: &str,
43 args: &[Argument<Id, Fd>],
44 discarded: bool,
45) {
46 print_timestamp();
48
49 if discarded {
50 eprint!("[discarded]");
51 }
52
53 eprint!(" -> {}@{}.{}({})", interface, id, msg_name, DisplaySlice(args));
54
55 eprintln!();
57}
58
59pub(crate) struct DisplaySlice<'a, D>(pub &'a [D]);
60
61impl<D: Display> Display for DisplaySlice<'_, D> {
62 #[cfg_attr(coverage, coverage(off))]
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 let mut it = self.0.iter();
65 if let Some(val) = it.next() {
66 write!(f, "{}", val)?;
67 }
68 for val in it {
69 write!(f, ", {}", val)?;
70 }
71 Ok(())
72 }
73}
74
75#[cfg_attr(coverage, coverage(off))]
77fn print_timestamp() {
78 if let Ok(timestamp) = SystemTime::now().duration_since(UNIX_EPOCH) {
79 let time = (timestamp.as_secs() * 1000000 + timestamp.subsec_nanos() as u64 / 1000) as u32;
82 eprint!("[{:7}.{:03}][rs]", time / 1000, time % 1000);
84 }
85}