Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option for line endings. #151

Merged
merged 6 commits into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/config.rs
Expand Up @@ -58,6 +58,26 @@
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:
Expand Down Expand Up @@ -86,9 +106,10 @@
pub(crate) filter_ignore: Cow<'static, [Cow<'static, str>]>,
#[cfg(feature = "termcolor")]
pub(crate) level_color: [Option<Color>; 6],
pub(crate) write_log_enable_colors: bool,

Check warning on line 109 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (beta, --no-default-features --features "test")

field `write_log_enable_colors` is never read

Check warning on line 109 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (nightly, --no-default-features)

field `write_log_enable_colors` is never read

Check warning on line 109 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (stable, --no-default-features --features "test")

field `write_log_enable_colors` is never read

Check warning on line 109 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (nightly, --no-default-features --features "test")

field `write_log_enable_colors` is never read

Check warning on line 109 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (stable, --no-default-features)

field `write_log_enable_colors` is never read

Check warning on line 109 in src/config.rs

View workflow job for this annotation

GitHub Actions / test (beta, --no-default-features)

field `write_log_enable_colors` is never read
#[cfg(feature = "paris")]
pub(crate) enable_paris_formatting: bool,
pub(crate) line_ending: String,
}

impl Config {
Expand Down Expand Up @@ -121,6 +142,21 @@
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::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;
Expand Down Expand Up @@ -368,6 +404,7 @@

#[cfg(feature = "paris")]
enable_paris_formatting: true,
line_ending: String::from("\u{000A}"),
}
}
}
27 changes: 19 additions & 8 deletions src/loggers/logging.rs
Expand Up @@ -62,9 +62,14 @@ 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)]
Expand Down Expand Up @@ -227,28 +232,34 @@ where

#[inline(always)]
#[cfg(feature = "paris")]
pub fn write_args<W>(record: &Record<'_>, write: &mut W, with_colors: bool) -> Result<(), Error>
pub fn write_args<W>(
record: &Record<'_>,
write: &mut W,
with_colors: bool,
line_ending: &str,
) -> Result<(), Error>
where
W: Write + Sized,
{
writeln!(
write!(
write,
"{}",
"{}{}",
crate::__private::paris::formatter::format_string(
format!("{}", record.args()),
with_colors
)
),
line_ending
)?;
Ok(())
}

#[inline(always)]
#[cfg(not(feature = "paris"))]
pub fn write_args<W>(record: &Record<'_>, write: &mut W) -> Result<(), Error>
pub fn write_args<W>(record: &Record<'_>, write: &mut W, line_ending: &str) -> Result<(), Error>
where
W: Write + Sized,
{
writeln!(write, "{}", record.args())?;
write!(write, "{}{}", record.args(), line_ending)?;
Ok(())
}

Expand Down
9 changes: 7 additions & 2 deletions src/loggers/termlog.rs
Expand Up @@ -176,9 +176,14 @@ 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)?;
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
Expand Down