Skip to content

Commit

Permalink
subscriber: add with_binary_name and with_process_id options.
Browse files Browse the repository at this point in the history
These options are useful when multiple processes are logging to the same
destination (stderr, in the common case). This can happen when a process
launches a child process or when a user simply launches multiple
processes in the same terminal.

Fixes tokio-rs#2447.

Implement these two options.
  • Loading branch information
nicolasavru committed Jul 16, 2023
1 parent 27f688e commit ebdcbcc
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,36 @@ where
}
}

/// Sets whether or not the binary name of the process is displayed when
/// formatting events. If binary_name is None, argv\[0\] will be used,
/// otherwise the spcified string will be used.
///
/// [argv\[0\]]: std::env::args
pub fn with_binary_name(
self,
display_binary_name: bool,
binary_name: Option<&str>,
) -> Subscriber<C, N, format::Format<L, T>, W> {
Subscriber {
fmt_event: self.fmt_event.with_binary_name(display_binary_name, binary_name),
..self
}
}

/// Sets whether or not the [process ID] of the current thread is displayed
/// when formatting events.
///
/// [process ID]: std::process::id
pub fn with_process_id(
self,
display_process_id: bool,
) -> Subscriber<C, N, format::Format<L, T>, W> {
Subscriber {
fmt_event: self.fmt_event.with_process_id(display_process_id),
..self
}
}

/// Sets whether or not the [thread ID] of the current thread is displayed
/// when formatting events.
///
Expand Down
107 changes: 107 additions & 0 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,9 @@ pub struct Format<F = Full, T = SystemTime> {
pub(crate) display_timestamp: bool,
pub(crate) display_target: bool,
pub(crate) display_level: bool,
pub(crate) display_binary_name: bool,
pub(crate) binary_name: Option<String>,
pub(crate) display_process_id: bool,
pub(crate) display_thread_id: bool,
pub(crate) display_thread_name: bool,
pub(crate) display_filename: bool,
Expand Down Expand Up @@ -588,6 +591,9 @@ impl Default for Format<Full, SystemTime> {
display_timestamp: true,
display_target: true,
display_level: true,
display_binary_name: false,
binary_name: None,
display_process_id: false,
display_thread_id: false,
display_thread_name: false,
display_filename: false,
Expand All @@ -608,6 +614,9 @@ impl<F, T> Format<F, T> {
display_target: false,
display_timestamp: self.display_timestamp,
display_level: self.display_level,
display_binary_name: self.display_binary_name,
binary_name: self.binary_name,
display_process_id: self.display_process_id,
display_thread_id: self.display_thread_id,
display_thread_name: self.display_thread_name,
display_filename: self.display_filename,
Expand Down Expand Up @@ -647,6 +656,9 @@ impl<F, T> Format<F, T> {
display_target: self.display_target,
display_timestamp: self.display_timestamp,
display_level: self.display_level,
display_binary_name: self.display_binary_name,
binary_name: self.binary_name,
display_process_id: self.display_process_id,
display_thread_id: self.display_thread_id,
display_thread_name: self.display_thread_name,
display_filename: true,
Expand Down Expand Up @@ -679,6 +691,9 @@ impl<F, T> Format<F, T> {
display_target: self.display_target,
display_timestamp: self.display_timestamp,
display_level: self.display_level,
display_binary_name: self.display_binary_name,
binary_name: self.binary_name,
display_process_id: self.display_process_id,
display_thread_id: self.display_thread_id,
display_thread_name: self.display_thread_name,
display_filename: self.display_filename,
Expand Down Expand Up @@ -708,6 +723,9 @@ impl<F, T> Format<F, T> {
display_target: self.display_target,
display_timestamp: self.display_timestamp,
display_level: self.display_level,
display_binary_name: self.display_binary_name,
binary_name: self.binary_name,
display_process_id: self.display_process_id,
display_thread_id: self.display_thread_id,
display_thread_name: self.display_thread_name,
display_filename: self.display_filename,
Expand All @@ -724,6 +742,9 @@ impl<F, T> Format<F, T> {
display_timestamp: false,
display_target: self.display_target,
display_level: self.display_level,
display_binary_name: self.display_binary_name,
binary_name: self.binary_name,
display_process_id: self.display_process_id,
display_thread_id: self.display_thread_id,
display_thread_name: self.display_thread_name,
display_filename: self.display_filename,
Expand Down Expand Up @@ -755,6 +776,33 @@ impl<F, T> Format<F, T> {
}
}

/// Sets whether or not the binary name of the process is displayed when
/// formatting events. If binary_name is None, argv\[0\] will be used,
/// otherwise the spcified string will be used.
///
/// [argv\[0\]]: std::env::args
pub fn with_binary_name(self,
display_binary_name: bool,
binary_name: Option<&str>,
) -> Format<F, T> {
Format {
display_binary_name,
binary_name: binary_name.map(str::to_string),
..self
}
}

/// Sets whether or not the [process ID] of the current thread is displayed
/// when formatting events.
///
/// [process ID]: std::process::id
pub fn with_process_id(self, display_process_id: bool) -> Format<F, T> {
Format {
display_process_id,
..self
}
}

/// Sets whether or not the [thread ID] of the current thread is displayed
/// when formatting events.
///
Expand Down Expand Up @@ -940,6 +988,20 @@ where
self.format_timestamp(&mut writer)?;
self.format_level(*meta.level(), &mut writer)?;

if self.display_binary_name {
if let Some(binary_name) = &self.binary_name {
write!(writer, "{} ", binary_name)?;
} else if let Some(argv0) = std::env::args().next() {
write!(writer, "{} ", argv0)?;
} else {
write!(writer, "<missing_argv[0] ")?;
}
}

if self.display_process_id {
write!(writer, "PID({}) ", std::process::id())?;
}

if self.display_thread_name {
let current_thread = std::thread::current();
match current_thread.name() {
Expand Down Expand Up @@ -1662,6 +1724,8 @@ pub(super) mod test {
.without_time()
.with_level(false)
.with_target(false)
.with_binary_name(false, None)
.with_process_id(false)
.with_thread_ids(false)
.with_thread_names(false);
#[cfg(feature = "ansi")]
Expand Down Expand Up @@ -1777,6 +1841,49 @@ pub(super) mod test {
assert_info_hello(subscriber, make_writer, expected);
}

#[test]
fn with_automatic_binary_name() {
let make_writer = MockMakeWriter::default();
let subscriber = crate::fmt::Collector::builder()
.with_writer(make_writer.clone())
.with_binary_name(true, None)
.with_ansi(false)
.with_timer(MockTime);
let expected =
format!("fake time INFO {} tracing_subscriber::fmt::format::test: hello\n",
std::env::args().next().unwrap());

assert_info_hello(subscriber, make_writer, &expected);
}

#[test]
fn with_manual_binary_name() {
let make_writer = MockMakeWriter::default();
let subscriber = crate::fmt::Collector::builder()
.with_writer(make_writer.clone())
.with_binary_name(true, Some("a_nice_rust_binary"))
.with_ansi(false)
.with_timer(MockTime);
let expected =
"fake time INFO a_nice_rust_binary tracing_subscriber::fmt::format::test: hello\n";

assert_info_hello(subscriber, make_writer, expected);
}

#[test]
fn with_process_id() {
let make_writer = MockMakeWriter::default();
let subscriber = crate::fmt::Collector::builder()
.with_writer(make_writer.clone())
.with_process_id(true)
.with_ansi(false)
.with_timer(MockTime);
let expected =
"fake time INFO PID(NUMERIC) tracing_subscriber::fmt::format::test: hello\n";

assert_info_hello_ignore_numeric(subscriber, make_writer, expected);
}

#[test]
fn with_thread_ids() {
let make_writer = MockMakeWriter::default();
Expand Down
30 changes: 30 additions & 0 deletions tracing-subscriber/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,36 @@ where
}
}

/// Sets whether or not the binary name of the process is displayed when
/// formatting events. If binary_name is None, argv\[0\] will be used,
/// otherwise the spcified string will be used.
///
/// [argv\[0\]]: std::env::args
pub fn with_binary_name(
self,
display_binary_name: bool,
binary_name: Option<&str>,
) -> CollectorBuilder<N, format::Format<L, T>, F, W> {
CollectorBuilder {
inner: self.inner.with_binary_name(display_binary_name, binary_name),
..self
}
}

/// Sets whether or not the [process ID] of the current thread is displayed
/// when formatting events.
///
/// [process ID]: std::process::id
pub fn with_process_id(
self,
display_process_id: bool,
) -> CollectorBuilder<N, format::Format<L, T>, F, W> {
CollectorBuilder {
inner: self.inner.with_process_id(display_process_id),
..self
}
}

/// Sets whether or not the [name] of the current thread is displayed
/// when formatting events.
///
Expand Down

0 comments on commit ebdcbcc

Please sign in to comment.