Skip to content

Commit

Permalink
better logging for the handled XCB error in gnome
Browse files Browse the repository at this point in the history
- add single warn message as a summary
- remove hundreds of redundant trace messages
  • Loading branch information
dnut committed Apr 6, 2023
1 parent 59ff995 commit 3b889d9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ pub enum MyError {
/// High level error to represent the idea that the application is crashing,
/// indicating the cause(s) of that crash.
#[error("Application crash '{msg}': {cause:#?}")]
Crash {
msg: String,
cause: Vec<MyError>,
},
Crash { msg: String, cause: Vec<MyError> },

#[error("failed to get wlr clipboard: {0}")]
WlcrsPaste(#[from] wl_clipboard_rs::paste::Error),
Expand Down
63 changes: 63 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,66 @@ pub fn truncate(s: &str, max_chars: usize) -> &str {
Some((idx, _)) => &s[..idx],
}
}

pub fn concise_numbers(ns: &[u8]) -> String {
if ns.len() == 0 {
return "[]".to_string();
}
if ns.len() == 1 {
return format!("[{}]", ns[0]);
}
let mut range_size = 1;
let mut strings = vec![];
for nn in ns.windows(2) {
let [n1, n2]: [u8] = *nn else {unreachable!()};
if n1 + 1 == n2 {
range_size += 1;
} else {
range_size = 1;
}
if range_size < 3 {
strings.push(format!("{n1}"));
}
if range_size == 3 {
strings.push("..".to_string());
}
}
strings.push(format!("{}", ns[ns.len() - 1]));
strings.push("..".to_owned());
let mut full_strings = vec![];
for ss in strings.windows(2) {
let [s1, s2] = ss else {unreachable!()};
full_strings.push(s1.to_owned());
if s1 != ".." && s2 != ".." {
full_strings.push(", ".to_owned());
}
}
format!("[{}]", full_strings.join(""))
}

#[test]
fn test() {
assert_eq!("[]", concise_numbers(&[]));
assert_eq!("[123]", concise_numbers(&[123]));
assert_eq!("[1..5]", concise_numbers(&[1, 2, 3, 4, 5]));
assert_eq!(
"[0, 2..4, 6..8, 10, 11]",
concise_numbers(&[0, 2, 3, 4, 6, 7, 8, 10, 11])
);
assert_eq!(
"[0, 2..4, 6..8, 10..12]",
concise_numbers(&[0, 2, 3, 4, 6, 7, 8, 10, 11, 12])
);
assert_eq!(
"[0, 2..4, 6..8, 10]",
concise_numbers(&[0, 2, 3, 4, 6, 7, 8, 10])
);
assert_eq!(
"[0..4, 6..8, 10]",
concise_numbers(&[0, 1, 2, 3, 4, 6, 7, 8, 10])
);
assert_eq!(
"[0, 1, 3, 4, 6..8, 10]",
concise_numbers(&[0, 1, 3, 4, 6, 7, 8, 10])
);
}
19 changes: 15 additions & 4 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wl_clipboard_rs::paste::Error as PasteError;

use crate::clipboard::*;
use crate::error::{MyError, MyResult, StandardizedError};
use crate::log;
use crate::log::{self, concise_numbers};

pub fn get_clipboards() -> MyResult<Vec<Box<dyn Clipboard>>> {
log::debug!("identifying unique clipboards...");
Expand Down Expand Up @@ -154,6 +154,8 @@ fn get_clipboards_spec<F: Fn(u8) -> MyResult<Option<Box<dyn Clipboard>>>>(
getter: F,
) -> Vec<Box<dyn Clipboard>> {
let mut clipboards: Vec<Box<dyn Clipboard>> = Vec::new();
let mut xcb_conn_err = None;
let mut xcb_conn_failed_clipboards = vec![];
for i in 0..u8::MAX {
let result = getter(i);
match result {
Expand All @@ -166,16 +168,25 @@ fn get_clipboards_spec<F: Fn(u8) -> MyResult<Option<Box<dyn Clipboard>>>>(
Err(MyError::TerminalClipboard(StandardizedError {
inner,
stdio: None,
})) if format!("{inner}") == "clipboard error: X11 clipboard error : XCB connection error: Connection" => log::trace!(
"known issue connecting to x11 clipboard :{i}. typically this happens when you try to attach to a gnome clipboard using too many x11 clipboards: {inner}",
),
})) if format!("{inner}") == "clipboard error: X11 clipboard error : XCB connection error: Connection" => {
xcb_conn_failed_clipboards.push(i);
xcb_conn_err = Some(inner);
},
Err(err) => log::error!(
"unexpected error while attempting to setup clipboard {}: {}",
i,
err
),
}
}
if let Some(err) = xcb_conn_err {
let displays = concise_numbers(&xcb_conn_failed_clipboards);
log::warning!(
"Issue connecting to some x11 clipboards. \
This is expected when hooking up to gnome wayland, and not a problem in that context. \
Details: '{err}' for x11 displays: {displays}",
);
}

clipboards
}
Expand Down

0 comments on commit 3b889d9

Please sign in to comment.