From 8d03a8a400a71541b91bdccb8a135b52e2199a85 Mon Sep 17 00:00:00 2001 From: Alfred Date: Sun, 24 Mar 2024 04:51:49 -0400 Subject: [PATCH 1/6] Update logging.rs Added LINE_END string constant for windows and non-windows platforms. For older versions of windows systems were it matters, this could be helpful. --- src/loggers/logging.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/loggers/logging.rs b/src/loggers/logging.rs index 47bc99ff..e0dfe101 100644 --- a/src/loggers/logging.rs +++ b/src/loggers/logging.rs @@ -21,6 +21,11 @@ pub fn termcolor_to_ansiterm(color: &Color) -> Option { } } +#[cfg(target_os = "windows")] +const LINE_END: &str = "\r\n"; +#[cfg(not(target_os = "windows"))] +const LINE_END: &str = "\n"; + #[inline(always)] pub fn try_log(config: &Config, record: &Record<'_>, write: &mut W) -> Result<(), Error> where @@ -231,13 +236,14 @@ pub fn write_args(record: &Record<'_>, write: &mut W, with_colors: bool) -> R where W: Write + Sized, { - writeln!( + write!( write, - "{}", + "{}{}", crate::__private::paris::formatter::format_string( format!("{}", record.args()), with_colors - ) + ), + LINE_END )?; Ok(()) } @@ -248,7 +254,7 @@ pub fn write_args(record: &Record<'_>, write: &mut W) -> Result<(), Error> where W: Write + Sized, { - writeln!(write, "{}", record.args())?; + write!(write, "{}{}", record.args(), LINE_END)?; Ok(()) } From 5d44659da04e6bb3ebd7cf025915427496b8055a Mon Sep 17 00:00:00 2001 From: Alfred Date: Mon, 25 Mar 2024 03:45:35 -0400 Subject: [PATCH 2/6] Added a feature flag --- Cargo.toml | 1 + src/loggers/logging.rs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 47a7b25d..c69d89c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ include = [ [features] test = [] +crlf-line-endings = [] default = ["termcolor", "local-offset"] local-offset = ["time/local-offset"] diff --git a/src/loggers/logging.rs b/src/loggers/logging.rs index e0dfe101..653b2f29 100644 --- a/src/loggers/logging.rs +++ b/src/loggers/logging.rs @@ -21,9 +21,9 @@ pub fn termcolor_to_ansiterm(color: &Color) -> Option { } } -#[cfg(target_os = "windows")] +#[cfg(feature = "crlf-line-endings")] const LINE_END: &str = "\r\n"; -#[cfg(not(target_os = "windows"))] +#[cfg(not(feature = "crlf-line-endings"))] const LINE_END: &str = "\n"; #[inline(always)] From a3433d069c405abe45e2380d8b4fdc8260a4c191 Mon Sep 17 00:00:00 2001 From: Alfred Date: Mon, 25 Mar 2024 05:28:06 -0400 Subject: [PATCH 3/6] Added line ending as a config option. --- Cargo.toml | 2 +- src/config.rs | 36 ++++++++++++++++++++++++++++++++++++ src/loggers/logging.rs | 17 ++++++----------- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c69d89c5..24b497b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ include = [ [features] test = [] -crlf-line-endings = [] + default = ["termcolor", "local-offset"] local-offset = ["time/local-offset"] diff --git a/src/config.rs b/src/config.rs index 5f5196ef..d0c2dab1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -58,6 +58,26 @@ pub(crate) enum TimeFormat { Custom(&'static [time::format_description::FormatItem<'static>]), } +/// UTF-8 end of line character sequences +pub enum LineEnding { + /// Line feed + LF, + /// Carriage return + CR, + /// Carriage return + Line feed + CRLF, + /// Vertical tab + VT, + /// Form feed + FF, + /// Next line + NEL, + /// Line separator + LS, + /// Paragraph separator + PS, +} + /// Configuration for the Loggers /// /// All loggers print the message in the following form: @@ -89,6 +109,7 @@ pub struct Config { pub(crate) write_log_enable_colors: bool, #[cfg(feature = "paris")] pub(crate) enable_paris_formatting: bool, + pub(crate) line_ending: String, } impl Config { @@ -121,6 +142,20 @@ impl ConfigBuilder { ConfigBuilder(Config::default()) } + pub fn set_line_ending(&mut self, line_ending: LineEnding) -> &mut ConfigBuilder { + match line_ending { + LineEnding::LF => self.0.line_ending = String::from("\u{000A}"), + LineEnding::CR => self.0.line_ending = String::from("\u{000D}"), + LineEnding::CRLF => self.0.line_ending = String::from("\u{000D}\u{000A}"), + LineEnding::VT => self.0.line_ending = String::from("\u{000B}"), + LineEnding::FF => self.0.line_ending = String::from("\u{000C}"), + LineEnding::NEL => self.0.line_ending = String::from("\u{0085}"), + LineEnding::LS => self.0.line_ending = String::from("\u{2028}"), + LineEnding::PS => self.0.line_ending = String::from("\u{2029}"), + } + self + } + /// Set at which level and above (more verbose) the level itself shall be logged (default is Error) pub fn set_max_level(&mut self, level: LevelFilter) -> &mut ConfigBuilder { self.0.level = level; @@ -368,6 +403,7 @@ impl Default for Config { #[cfg(feature = "paris")] enable_paris_formatting: true, + line_ending: '\n' } } } diff --git a/src/loggers/logging.rs b/src/loggers/logging.rs index 653b2f29..37c2f5f7 100644 --- a/src/loggers/logging.rs +++ b/src/loggers/logging.rs @@ -21,11 +21,6 @@ pub fn termcolor_to_ansiterm(color: &Color) -> Option { } } -#[cfg(feature = "crlf-line-endings")] -const LINE_END: &str = "\r\n"; -#[cfg(not(feature = "crlf-line-endings"))] -const LINE_END: &str = "\n"; - #[inline(always)] pub fn try_log(config: &Config, record: &Record<'_>, write: &mut W) -> Result<(), Error> where @@ -67,9 +62,9 @@ where } #[cfg(feature = "paris")] - return write_args(record, write, config.enable_paris_formatting); + return write_args(record, write, config.enable_paris_formatting, &config.line_ending); #[cfg(not(feature = "paris"))] - return write_args(record, write); + return write_args(record, write, &config.line_ending); } #[inline(always)] @@ -232,7 +227,7 @@ where #[inline(always)] #[cfg(feature = "paris")] -pub fn write_args(record: &Record<'_>, write: &mut W, with_colors: bool) -> Result<(), Error> +pub fn write_args(record: &Record<'_>, write: &mut W, with_colors: bool, line_ending: &str) -> Result<(), Error> where W: Write + Sized, { @@ -243,18 +238,18 @@ where format!("{}", record.args()), with_colors ), - LINE_END + line_ending )?; Ok(()) } #[inline(always)] #[cfg(not(feature = "paris"))] -pub fn write_args(record: &Record<'_>, write: &mut W) -> Result<(), Error> +pub fn write_args(record: &Record<'_>, write: &mut W, line_ending: &str) -> Result<(), Error> where W: Write + Sized, { - write!(write, "{}{}", record.args(), LINE_END)?; + write!(write, "{}{}", record.args(), line_ending)?; Ok(()) } From c73458947f4b735fb0f1fa0f5c521024d6b2a8c3 Mon Sep 17 00:00:00 2001 From: Alfred Date: Mon, 25 Mar 2024 05:47:48 -0400 Subject: [PATCH 4/6] Fixed issue with previous commit --- src/config.rs | 11 ++++++----- src/loggers/termlog.rs | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index d0c2dab1..d01c3ebb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -65,13 +65,13 @@ pub enum LineEnding { /// Carriage return CR, /// Carriage return + Line feed - CRLF, + Crlf, /// Vertical tab VT, /// Form feed FF, /// Next line - NEL, + Nel, /// Line separator LS, /// Paragraph separator @@ -142,14 +142,15 @@ impl ConfigBuilder { ConfigBuilder(Config::default()) } + /// Set a custom line ending pub fn set_line_ending(&mut self, line_ending: LineEnding) -> &mut ConfigBuilder { match line_ending { LineEnding::LF => self.0.line_ending = String::from("\u{000A}"), LineEnding::CR => self.0.line_ending = String::from("\u{000D}"), - LineEnding::CRLF => self.0.line_ending = String::from("\u{000D}\u{000A}"), + LineEnding::Crlf => self.0.line_ending = String::from("\u{000D}\u{000A}"), LineEnding::VT => self.0.line_ending = String::from("\u{000B}"), LineEnding::FF => self.0.line_ending = String::from("\u{000C}"), - LineEnding::NEL => self.0.line_ending = String::from("\u{0085}"), + LineEnding::Nel => self.0.line_ending = String::from("\u{0085}"), LineEnding::LS => self.0.line_ending = String::from("\u{2028}"), LineEnding::PS => self.0.line_ending = String::from("\u{2029}"), } @@ -403,7 +404,7 @@ impl Default for Config { #[cfg(feature = "paris")] enable_paris_formatting: true, - line_ending: '\n' + line_ending: String::from("\u{000A}"), } } } diff --git a/src/loggers/termlog.rs b/src/loggers/termlog.rs index 1858d235..b73b2c06 100644 --- a/src/loggers/termlog.rs +++ b/src/loggers/termlog.rs @@ -178,7 +178,7 @@ impl TermLogger { #[cfg(feature = "paris")] write_args(record, term_lock, self.config.enable_paris_formatting)?; #[cfg(not(feature = "paris"))] - write_args(record, term_lock)?; + write_args(record, term_lock, &self.config.line_ending)?; // The log crate holds the logger as a `static mut`, which isn't dropped // at program exit: https://doc.rust-lang.org/reference/items/static-items.html From 6177ef7569663ca0de81db99fb2143f8b0a7b2d1 Mon Sep 17 00:00:00 2001 From: Alfred Date: Mon, 25 Mar 2024 05:50:35 -0400 Subject: [PATCH 5/6] Removed extra newline --- Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 24b497b9..47a7b25d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ include = [ [features] test = [] - default = ["termcolor", "local-offset"] local-offset = ["time/local-offset"] From ff7f4e76c8f5be0725400429493c80b6fa622140 Mon Sep 17 00:00:00 2001 From: Alfred Date: Mon, 25 Mar 2024 15:09:11 -0400 Subject: [PATCH 6/6] Fixed formatting --- src/loggers/logging.rs | 14 ++++++++++++-- src/loggers/termlog.rs | 7 ++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/loggers/logging.rs b/src/loggers/logging.rs index 37c2f5f7..4366ba87 100644 --- a/src/loggers/logging.rs +++ b/src/loggers/logging.rs @@ -62,7 +62,12 @@ where } #[cfg(feature = "paris")] - return write_args(record, write, config.enable_paris_formatting, &config.line_ending); + return write_args( + record, + write, + config.enable_paris_formatting, + &config.line_ending, + ); #[cfg(not(feature = "paris"))] return write_args(record, write, &config.line_ending); } @@ -227,7 +232,12 @@ where #[inline(always)] #[cfg(feature = "paris")] -pub fn write_args(record: &Record<'_>, write: &mut W, with_colors: bool, line_ending: &str) -> Result<(), Error> +pub fn write_args( + record: &Record<'_>, + write: &mut W, + with_colors: bool, + line_ending: &str, +) -> Result<(), Error> where W: Write + Sized, { diff --git a/src/loggers/termlog.rs b/src/loggers/termlog.rs index b73b2c06..460c36e6 100644 --- a/src/loggers/termlog.rs +++ b/src/loggers/termlog.rs @@ -176,7 +176,12 @@ impl TermLogger { } #[cfg(feature = "paris")] - write_args(record, term_lock, self.config.enable_paris_formatting)?; + write_args( + record, + term_lock, + self.config.enable_paris_formatting, + &self.config.line_ending, + )?; #[cfg(not(feature = "paris"))] write_args(record, term_lock, &self.config.line_ending)?;