From 6785d154460921cc3b774376a676a226402d8270 Mon Sep 17 00:00:00 2001 From: tcmal Date: Wed, 26 Jun 2024 21:03:36 +0100 Subject: add logging macros for debug mode only printing --- src/conn_info/mod.rs | 19 +++++-------------- src/log.rs | 24 ++++++++++++++++++++++++ src/main.rs | 6 ++---- 3 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 src/log.rs diff --git a/src/conn_info/mod.rs b/src/conn_info/mod.rs index 7ef6b5a..7bf09ba 100644 --- a/src/conn_info/mod.rs +++ b/src/conn_info/mod.rs @@ -6,7 +6,7 @@ use xcb::{ self, ChangeProperty, ChangeWindowAttributes, CreateWindow, DeleteProperty, DestroyWindow, Window, WindowClass, }, - Connection as RawConnection, Cookie, VoidCookieChecked, + Connection as RawConnection, VoidCookieChecked, }; #[doc(hidden)] @@ -24,7 +24,10 @@ pub use self::{ cursors::Cursors, keys::KeyboardInfo, }; -use crate::error::{Error, Result}; +use crate::{ + debug_req, + error::{Error, Result}, +}; /// The connection, along with some cached resources required for WM operations. pub struct Connection<'a> { @@ -54,18 +57,6 @@ pub struct Connection<'a> { pub keyboard_state: KeyboardInfo, } -/// Helper macro to log a request / cookie when `debug_assertions` are on -macro_rules! debug_req { - ($req:ident) => { - #[cfg(debug_assertions)] - eprintln!("seq ?: {:?}", $req) - }; - ($req:ident, $cookie:ident) => { - #[cfg(debug_assertions)] - eprintln!("seq {}: {:?}", $cookie.sequence(), $req) - }; -} - impl<'a> Connection<'a> { /// Prepare the window manager to run on the given connection and screen number. /// This will fail if another WM is running. diff --git a/src/log.rs b/src/log.rs new file mode 100644 index 0000000..5be30d3 --- /dev/null +++ b/src/log.rs @@ -0,0 +1,24 @@ +//! Helper macros for logging + +/// Log to standard error, only compiled when in debug mode. +#[macro_export] +macro_rules! debug { + ($($e:expr),*) => { + #[cfg(debug_assertions)] + { + eprintln!($($e),*); + } + }; +} + +/// Log a request / cookie when `debug_assertions` are on +#[macro_export] +macro_rules! debug_req { + ($req:ident) => { + crate::debug!("seq ?: {:?}", $req) + }; + ($req:ident, $cookie:ident) => { + use xcb::Cookie; + crate::debug!("seq {}: {:?}", $cookie.sequence(), $req) + }; +} diff --git a/src/main.rs b/src/main.rs index 83cabd4..3de3df9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,6 +41,7 @@ mod error; #[doc(hidden)] mod focus; pub mod keys; +pub mod log; fn main() -> Result<()> { cleanup_process_children(); @@ -79,10 +80,7 @@ impl<'a> WM<'a> { loop { match self.conn.wait_for_event() { Ok(x) => { - #[cfg(debug_assertions)] - { - eprintln!("received event: {x:?}"); - }; + debug!("received event: {x:?}"); match x { // See keys.rs -- cgit v1.2.3