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 10, 2024
1 parent 44861ca commit 12f69b5
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tracing-subscriber/src/fmt/fmt_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,38 @@ where
}
}

/// Sets whether or not the binary name of the process is displayed when
/// formatting events. If executable_name is None, argv\[0\] will be used,
/// otherwise the spcified string will be used.
///
/// [argv\[0\]]: std::env::args
pub fn with_executable_name(
self,
display_executable_name: bool,
executable_name: Option<impl Into<String>>,
) -> Subscriber<C, N, format::Format<L, T>, W> {
Subscriber {
fmt_event: self
.fmt_event
.with_executable_name(display_executable_name, executable_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
109 changes: 109 additions & 0 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,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_executable_name: bool,
pub(crate) executable_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 @@ -599,6 +602,9 @@ impl Default for Format<Full, SystemTime> {
display_timestamp: true,
display_target: true,
display_level: true,
display_executable_name: false,
executable_name: None,
display_process_id: false,
display_thread_id: false,
display_thread_name: false,
display_filename: false,
Expand All @@ -619,6 +625,9 @@ impl<F, T> Format<F, T> {
display_target: false,
display_timestamp: self.display_timestamp,
display_level: self.display_level,
display_executable_name: self.display_executable_name,
executable_name: self.executable_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 @@ -658,6 +667,9 @@ impl<F, T> Format<F, T> {
display_target: self.display_target,
display_timestamp: self.display_timestamp,
display_level: self.display_level,
display_executable_name: self.display_executable_name,
executable_name: self.executable_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 @@ -690,6 +702,9 @@ impl<F, T> Format<F, T> {
display_target: self.display_target,
display_timestamp: self.display_timestamp,
display_level: self.display_level,
display_executable_name: self.display_executable_name,
executable_name: self.executable_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 @@ -719,6 +734,9 @@ impl<F, T> Format<F, T> {
display_target: self.display_target,
display_timestamp: self.display_timestamp,
display_level: self.display_level,
display_executable_name: self.display_executable_name,
executable_name: self.executable_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 @@ -735,6 +753,9 @@ impl<F, T> Format<F, T> {
display_timestamp: false,
display_target: self.display_target,
display_level: self.display_level,
display_executable_name: self.display_executable_name,
executable_name: self.executable_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 @@ -766,6 +787,34 @@ impl<F, T> Format<F, T> {
}
}

/// Sets whether or not the binary name of the process is displayed when
/// formatting events. If executable_name is None, argv\[0\] will be used,
/// otherwise the spcified string will be used.
///
/// [argv\[0\]]: std::env::args
pub fn with_executable_name(
self,
display_executable_name: bool,
executable_name: Option<impl Into<String>>,
) -> Format<F, T> {
Format {
display_executable_name,
executable_name: executable_name.map(Into::into),
..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 @@ -951,6 +1000,20 @@ where
self.format_timestamp(&mut writer)?;
self.format_level(*meta.level(), &mut writer)?;

if self.display_executable_name {
if let Some(executable_name) = &self.executable_name {
write!(writer, "{} ", executable_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 @@ -1678,6 +1741,8 @@ pub(super) mod test {
.without_time()
.with_level(false)
.with_target(false)
.with_executable_name(false, None::<&str>)
.with_process_id(false)
.with_thread_ids(false)
.with_thread_names(false);
#[cfg(feature = "ansi")]
Expand Down Expand Up @@ -1793,6 +1858,50 @@ pub(super) mod test {
assert_info_hello(subscriber, make_writer, expected);
}

#[test]
fn with_automatic_executable_name() {
let make_writer = MockMakeWriter::default();
let subscriber = crate::fmt::Collector::builder()
.with_writer(make_writer.clone())
.with_executable_name(true, None::<&str>)
.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_executable_name() {
let make_writer = MockMakeWriter::default();
let subscriber = crate::fmt::Collector::builder()
.with_writer(make_writer.clone())
.with_executable_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
32 changes: 32 additions & 0 deletions tracing-subscriber/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,38 @@ where
}
}

/// Sets whether or not the binary name of the process is displayed when
/// formatting events. If executable_name is None, argv\[0\] will be used,
/// otherwise the spcified string will be used.
///
/// [argv\[0\]]: std::env::args
pub fn with_executable_name(
self,
display_executable_name: bool,
executable_name: Option<impl Into<String>>,
) -> CollectorBuilder<N, format::Format<L, T>, F, W> {
CollectorBuilder {
inner: self
.inner
.with_executable_name(display_executable_name, executable_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 12f69b5

Please sign in to comment.