From 0fce274f57c1e4666e8fc53860795f800bbf4bc6 Mon Sep 17 00:00:00 2001 From: Peter Mutsaers Date: Wed, 8 Mar 2023 15:27:20 +0100 Subject: [PATCH 1/4] Add option with_trimmed_directory. This option strips the directory from the file path, like some other glog libraries do by default. --- src/format.rs | 19 +++++++++++++++++-- src/lib.rs | 11 +++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/format.rs b/src/format.rs index 94caa1c..ef0240b 100644 --- a/src/format.rs +++ b/src/format.rs @@ -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}; @@ -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() + } else { + f + } + }) + .unwrap_or(""); let line = match self.metadata.line() { Some(line) => format!("{}", line), None => String::new(), diff --git a/src/lib.rs b/src/lib.rs index 540b965..cbdfc98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -150,6 +150,7 @@ pub struct Glog { with_span_context: bool, with_thread_names: bool, with_target: bool, + with_trimmed_directory: bool, } impl Glog { @@ -168,6 +169,7 @@ impl Glog { 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, } } @@ -185,6 +187,13 @@ impl Glog { } } + pub fn with_trimmed_directory(self, with_trimmed_directory: bool) -> Glog { + 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`] @@ -220,6 +229,7 @@ impl Default for Glog { with_thread_names: false, with_target: false, with_span_context: true, + with_trimmed_directory: false, } } } @@ -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)?; From d1a764cf57f922f60e6cd96e0cfd8a51eac9d4b6 Mon Sep 17 00:00:00 2001 From: plm Date: Thu, 9 Mar 2023 22:28:55 +0100 Subject: [PATCH 2/4] fixup! Add option with_trimmed_directory. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cbdfc98..589a4c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -189,7 +189,7 @@ impl Glog { pub fn with_trimmed_directory(self, with_trimmed_directory: bool) -> Glog { Glog { - with_trimmed_directory: with_trimmed_directory, + with_trimmed_directory, ..self } } From 4c0b6040615e633b32e7c69ea6d81fdabd9b9a7d Mon Sep 17 00:00:00 2001 From: plm Date: Thu, 9 Mar 2023 22:29:04 +0100 Subject: [PATCH 3/4] fixup! Add option with_trimmed_directory. --- src/format.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/format.rs b/src/format.rs index ef0240b..3ce7126 100644 --- a/src/format.rs +++ b/src/format.rs @@ -223,8 +223,8 @@ impl<'a> fmt::Display for FormatProcessData<'a> { let path = Path::new(f); path.file_name() .map(OsStr::to_str) - .unwrap_or_default() - .unwrap_or_default() + .unwrap_or(Some(f)) + .unwrap_or(f) } else { f } From f0d00b4e5d5aedd102b1701001bece39bce85b61 Mon Sep 17 00:00:00 2001 From: Peter Mutsaers Date: Fri, 10 Mar 2023 14:18:31 +0100 Subject: [PATCH 4/4] Add option with_strip_prefix. This option strips a prefix from the target file path. If the prefix doesn't match, the full path is logged. --- src/format.rs | 6 ++++++ src/lib.rs | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/format.rs b/src/format.rs index 3ce7126..8f9f76d 100644 --- a/src/format.rs +++ b/src/format.rs @@ -209,6 +209,7 @@ pub(crate) struct FormatProcessData<'a> { #[cfg(feature = "ansi")] pub(crate) ansi: bool, pub(crate) with_trimmed_directory: bool, + pub(crate) with_strip_prefix: &'a Option, } impl<'a> fmt::Display for FormatProcessData<'a> { @@ -225,6 +226,11 @@ impl<'a> fmt::Display for FormatProcessData<'a> { .map(OsStr::to_str) .unwrap_or(Some(f)) .unwrap_or(f) + } else if let Some(prefix) = &self.with_strip_prefix { + Path::new(f) + .strip_prefix(prefix) + .map(|path| path.to_str().unwrap()) + .unwrap_or(f) } else { f } diff --git a/src/lib.rs b/src/lib.rs index 589a4c9..aed3f20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -151,6 +151,7 @@ pub struct Glog { with_thread_names: bool, with_target: bool, with_trimmed_directory: bool, + with_strip_prefix: Option, } impl Glog { @@ -170,6 +171,7 @@ impl Glog { with_target: self.with_target, with_span_context: self.with_span_context, with_trimmed_directory: self.with_trimmed_directory, + with_strip_prefix: self.with_strip_prefix, } } @@ -194,6 +196,13 @@ impl Glog { } } + pub fn with_strip_prefix(self, with_strip_prefix: Option) -> Glog { + Glog { + with_strip_prefix: with_strip_prefix.map(|s|s.to_string()), + ..self + } + } + /// Sets whether or not the span context is included. Defaults to true. /// /// By default, formatters building atop of [`mod@tracing_subscriber::fmt`] @@ -230,6 +239,7 @@ impl Default for Glog { with_target: false, with_span_context: true, with_trimmed_directory: false, + with_strip_prefix: None, } } } @@ -276,6 +286,7 @@ where #[cfg(feature = "ansi")] ansi: writer.has_ansi_escapes(), with_trimmed_directory: self.with_trimmed_directory, + with_strip_prefix: &self.with_strip_prefix, }; write!(writer, "{}] ", data)?;