Skip to content

Commit

Permalink
tracing-flame: add module_path and file_and_line configs (#1134)
Browse files Browse the repository at this point in the history
We should allow configuring whether or not to display module_path
or file/line in output.

Co-authored-by: 李小鹏 <[email protected]>
Co-authored-by: Jane Lusby <[email protected]>
Co-authored-by: Eliza Weisman <[email protected]>
  • Loading branch information
4 people authored Dec 14, 2020
1 parent 4f93a6e commit 27b2877
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions tracing-flame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,21 @@ struct Config {

/// Don't include thread_id
threads_collapsed: bool,

/// Don't display module_path
module_path: bool,

/// Don't display file and line
file_and_line: bool,
}

impl Default for Config {
fn default() -> Self {
Self {
empty_samples: true,
threads_collapsed: false,
module_path: true,
file_and_line: true,
}
}
}
Expand Down Expand Up @@ -307,6 +315,18 @@ where
self.config.threads_collapsed = enabled;
self
}

/// Configures whether or not module paths should be included in the output.
pub fn with_module_path(mut self, enabled: bool) -> Self {
self.config.module_path = enabled;
self
}

/// Configures whether or not file and line should be included in the output.
pub fn with_file_and_line(mut self, enabled: bool) -> Self {
self.config.file_and_line = enabled;
self
}
}

impl<W> FlushGuard<W>
Expand Down Expand Up @@ -390,7 +410,7 @@ where

for parent in parents {
stack += "; ";
write(&mut stack, parent).expect("expected: write to String never fails");
write(&mut stack, parent, &self.config).expect("expected: write to String never fails");
}

write!(&mut stack, " {}", samples.as_nanos())
Expand Down Expand Up @@ -432,14 +452,14 @@ where

for parent in parents {
expect!(
write(&mut stack, parent),
write(&mut stack, parent, &self.config),
"expected: write to String never fails"
);
stack += "; ";
}

expect!(
write(&mut stack, first),
write(&mut stack, first, &self.config),
"expected: write to String never fails"
);
expect!(
Expand Down Expand Up @@ -469,22 +489,26 @@ where
}
}

fn write<C>(dest: &mut String, span: SpanRef<'_, C>) -> fmt::Result
fn write<C>(dest: &mut String, span: SpanRef<'_, C>, config: &Config) -> fmt::Result
where
C: Collect + for<'span> LookupSpan<'span>,
{
if let Some(module_path) = span.metadata().module_path() {
write!(dest, "{}::", module_path)?;
if config.module_path {
if let Some(module_path) = span.metadata().module_path() {
write!(dest, "{}::", module_path)?;
}
}

write!(dest, "{}", span.name())?;

if let Some(file) = span.metadata().file() {
write!(dest, ":{}", file)?;
}
if config.file_and_line {
if let Some(file) = span.metadata().file() {
write!(dest, ":{}", file)?;
}

if let Some(line) = span.metadata().line() {
write!(dest, ":{}", line)?;
if let Some(line) = span.metadata().line() {
write!(dest, ":{}", line)?;
}
}

Ok(())
Expand Down

0 comments on commit 27b2877

Please sign in to comment.