Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriber: add with_binary_name and with_process_id options. #2655

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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