Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "ansi")]
use crate::nu_ansi_term::{Color, Style};
use std::{fmt, io};
use std::{ffi::OsStr, fmt, io, path::Path};
use time::{format_description::FormatItem, formatting::Formattable, OffsetDateTime};
use tracing::{Level, Metadata};
use tracing_subscriber::fmt::{format::Writer, time::FormatTime};
Expand Down Expand Up @@ -208,13 +208,28 @@ pub(crate) struct FormatProcessData<'a> {
pub(crate) with_target: bool,
#[cfg(feature = "ansi")]
pub(crate) ansi: bool,
pub(crate) with_trimmed_directory: bool,
}

impl<'a> fmt::Display for FormatProcessData<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let thread_name = self.thread_name;
let target = self.metadata.target();
let file = self.metadata.file().unwrap_or("");
let file = self
.metadata
.file()
.map(|f| {
if self.with_trimmed_directory {
let path = Path::new(f);
path.file_name()
.map(OsStr::to_str)
.unwrap_or_default()
.unwrap_or_default()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use the original full filename if something goes wrong here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is better. I added a fixup that does this.

} else {
f
}
})
.unwrap_or("");
let line = match self.metadata.line() {
Some(line) => format!("{}", line),
None => String::new(),
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub struct Glog<T = UtcTime> {
with_span_context: bool,
with_thread_names: bool,
with_target: bool,
with_trimmed_directory: bool,
}

impl<T> Glog<T> {
Expand All @@ -168,6 +169,7 @@ impl<T> Glog<T> {
with_thread_names: self.with_thread_names,
with_target: self.with_target,
with_span_context: self.with_span_context,
with_trimmed_directory: self.with_trimmed_directory,
}
}

Expand All @@ -185,6 +187,13 @@ impl<T> Glog<T> {
}
}

pub fn with_trimmed_directory(self, with_trimmed_directory: bool) -> Glog<T> {
Glog {
with_trimmed_directory: with_trimmed_directory,
..self
}
}

/// Sets whether or not the span context is included. Defaults to true.
///
/// By default, formatters building atop of [`mod@tracing_subscriber::fmt`]
Expand Down Expand Up @@ -220,6 +229,7 @@ impl Default for Glog<UtcTime> {
with_thread_names: false,
with_target: false,
with_span_context: true,
with_trimmed_directory: false,
}
}
}
Expand Down Expand Up @@ -265,6 +275,7 @@ where
with_target: self.with_target,
#[cfg(feature = "ansi")]
ansi: writer.has_ansi_escapes(),
with_trimmed_directory: self.with_trimmed_directory,
};
write!(writer, "{}] ", data)?;

Expand Down