Skip to content

Commit 0fce274

Browse files
committed
Add option with_trimmed_directory.
This option strips the directory from the file path, like some other glog libraries do by default.
1 parent c6ace03 commit 0fce274

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/format.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[cfg(feature = "ansi")]
22
use crate::nu_ansi_term::{Color, Style};
3-
use std::{fmt, io};
3+
use std::{ffi::OsStr, fmt, io, path::Path};
44
use time::{format_description::FormatItem, formatting::Formattable, OffsetDateTime};
55
use tracing::{Level, Metadata};
66
use tracing_subscriber::fmt::{format::Writer, time::FormatTime};
@@ -208,13 +208,28 @@ pub(crate) struct FormatProcessData<'a> {
208208
pub(crate) with_target: bool,
209209
#[cfg(feature = "ansi")]
210210
pub(crate) ansi: bool,
211+
pub(crate) with_trimmed_directory: bool,
211212
}
212213

213214
impl<'a> fmt::Display for FormatProcessData<'a> {
214215
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215216
let thread_name = self.thread_name;
216217
let target = self.metadata.target();
217-
let file = self.metadata.file().unwrap_or("");
218+
let file = self
219+
.metadata
220+
.file()
221+
.map(|f| {
222+
if self.with_trimmed_directory {
223+
let path = Path::new(f);
224+
path.file_name()
225+
.map(OsStr::to_str)
226+
.unwrap_or_default()
227+
.unwrap_or_default()
228+
} else {
229+
f
230+
}
231+
})
232+
.unwrap_or("");
218233
let line = match self.metadata.line() {
219234
Some(line) => format!("{}", line),
220235
None => String::new(),

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub struct Glog<T = UtcTime> {
150150
with_span_context: bool,
151151
with_thread_names: bool,
152152
with_target: bool,
153+
with_trimmed_directory: bool,
153154
}
154155

155156
impl<T> Glog<T> {
@@ -168,6 +169,7 @@ impl<T> Glog<T> {
168169
with_thread_names: self.with_thread_names,
169170
with_target: self.with_target,
170171
with_span_context: self.with_span_context,
172+
with_trimmed_directory: self.with_trimmed_directory,
171173
}
172174
}
173175

@@ -185,6 +187,13 @@ impl<T> Glog<T> {
185187
}
186188
}
187189

190+
pub fn with_trimmed_directory(self, with_trimmed_directory: bool) -> Glog<T> {
191+
Glog {
192+
with_trimmed_directory: with_trimmed_directory,
193+
..self
194+
}
195+
}
196+
188197
/// Sets whether or not the span context is included. Defaults to true.
189198
///
190199
/// By default, formatters building atop of [`mod@tracing_subscriber::fmt`]
@@ -220,6 +229,7 @@ impl Default for Glog<UtcTime> {
220229
with_thread_names: false,
221230
with_target: false,
222231
with_span_context: true,
232+
with_trimmed_directory: false,
223233
}
224234
}
225235
}
@@ -265,6 +275,7 @@ where
265275
with_target: self.with_target,
266276
#[cfg(feature = "ansi")]
267277
ansi: writer.has_ansi_escapes(),
278+
with_trimmed_directory: self.with_trimmed_directory,
268279
};
269280
write!(writer, "{}] ", data)?;
270281

0 commit comments

Comments
 (0)