wayland_commons/
debug.rs

1//! Debugging helpers to handle `WAYLAND_DEBUG` env variable.
2
3use std::time::{SystemTime, UNIX_EPOCH};
4
5use crate::wire::Argument;
6
7/// Print the dispatched message to stderr in a following format:
8///
9/// [timestamp] <- interface@id.msg_name(args)
10pub fn print_dispatched_message(interface: &str, id: u32, msg_name: &str, args: &[Argument]) {
11    // Add timestamp to output.
12    print_timestamp();
13
14    eprint!(" <- {}@{}.{}", interface, id, msg_name);
15
16    print_args(args);
17
18    // Add a new line.
19    eprintln!();
20}
21
22/// Print the send message to stderr in a following format:
23///
24/// [timestamp] -> interface@id.msg_name(args)
25///
26/// If `is_alive` is `false` the `[ZOMBIE]` is added after `id`.
27pub fn print_send_message(
28    interface: &str,
29    id: u32,
30    is_alive: bool,
31    msg_name: &str,
32    args: &[Argument],
33) {
34    // Add timestamp to output.
35    print_timestamp();
36
37    eprint!(" -> {}@{}{}.{}", interface, id, if is_alive { "" } else { "[ZOMBIE]" }, msg_name);
38
39    print_args(args);
40
41    // Add a new line.
42    eprintln!();
43}
44
45/// Print arguments with opening/closing bracket.
46fn print_args(args: &[Argument]) {
47    let num_args = args.len();
48
49    eprint!("(");
50
51    if num_args > 0 {
52        // Explicitly handle first argument to handle one arg functions nicely.
53        eprint!("{}", args[0]);
54
55        // Handle the rest.
56        for arg in args.iter().take(num_args).skip(1) {
57            eprint!(", {}", arg);
58        }
59    }
60
61    eprint!(")")
62}
63
64/// Print timestamp in seconds.microseconds format.
65fn print_timestamp() {
66    if let Ok(timestamp) = SystemTime::now().duration_since(UNIX_EPOCH) {
67        let sc = timestamp.as_secs();
68        let ms = timestamp.subsec_micros();
69        eprint!("[{}.{:06}]", sc, ms);
70    }
71}