diff --git a/fish-rust/src/flog.rs b/fish-rust/src/flog.rs index 54550f429..6f6a154c3 100644 --- a/fish-rust/src/flog.rs +++ b/fish-rust/src/flog.rs @@ -136,6 +136,28 @@ pub fn all_categories() -> Vec<&'static category_t> { ); } +/// FLOG formats values. By default we would like to use Display, and fall back to Debug. +/// However that would require specialization. So instead we make two "separate" traits, bring them both in scope, +/// and let Rust figure it out. +/// Clients can opt a Debug type into Floggable by implementing FloggableDebug: +/// impl FloggableDebug for MyType {} +pub trait FloggableDisplay { + /// Return a string representation of this thing. + fn to_flog_str(&self) -> String; +} + +impl FloggableDisplay for T { + fn to_flog_str(&self) -> String { + format!("{}", self) + } +} + +pub trait FloggableDebug: std::fmt::Debug { + fn to_flog_str(&self) -> String { + format!("{:?}", self) + } +} + /// Write to our FLOG file. pub fn flog_impl(s: &str) { let fd = get_flog_file_fd().0 as RawFd; @@ -151,9 +173,13 @@ pub fn flog_impl(s: &str) { macro_rules! FLOG { ($category:ident, $($elem:expr),+) => { if crate::flog::categories::$category.enabled.load(std::sync::atomic::Ordering::Relaxed) { + #[allow(unused_imports)] + use crate::flog::{FloggableDisplay, FloggableDebug}; let mut vs = Vec::new(); $( - vs.push(format!("{:?}", $elem)); + { + vs.push($elem.to_flog_str()) + } )+ // We don't use locking here so we have to append our own newline to avoid multiple writes. let mut v = vs.join(" "); diff --git a/fish-rust/src/threads.rs b/fish-rust/src/threads.rs index dc4f6419d..579eff682 100644 --- a/fish-rust/src/threads.rs +++ b/fish-rust/src/threads.rs @@ -1,9 +1,11 @@ //! The rusty version of iothreads from the cpp code, to be consumed by native rust code. This isn't //! ported directly from the cpp code so we can use rust threads instead of using pthreads. -use crate::flog::FLOG; +use crate::flog::{FloggableDebug, FLOG}; use std::thread::{self, ThreadId}; +impl FloggableDebug for ThreadId {} + // We don't want to use a full-blown Lazy for the cached main thread id, but we can't use // AtomicU64 since std::thread::ThreadId::as_u64() is a nightly-only feature (issue #67939, // thread_id_value). We also can't safely transmute `ThreadId` to `NonZeroU64` because there's no diff --git a/fish-rust/src/topic_monitor.rs b/fish-rust/src/topic_monitor.rs index 4ef936988..b4dbd291e 100644 --- a/fish-rust/src/topic_monitor.rs +++ b/fish-rust/src/topic_monitor.rs @@ -23,7 +23,7 @@ use crate::fd_readable_set::fd_readable_set_t; use crate::fds::{self, autoclose_pipes_t}; use crate::ffi::{self as ffi, c_int}; -use crate::flog::FLOG; +use crate::flog::{FloggableDebug, FLOG}; use crate::wchar::{widestrs, wstr, WString}; use crate::wchar_ffi::wcharz; use nix::errno::Errno; @@ -79,6 +79,8 @@ pub enum topic_t { pub use topic_monitor_ffi::{generation_list_t, topic_t}; pub type generation_t = u64; +impl FloggableDebug for topic_t {} + /// A generation value which indicates the topic is not of interest. pub const invalid_generation: generation_t = std::u64::MAX;