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 5 commits
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
37 changes: 37 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -121,6 +142,21 @@ 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::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 @@ impl Default for Config {

#[cfg(feature = "paris")]
enable_paris_formatting: true,
line_ending: String::from("\u{000A}"),
}
}
}
17 changes: 9 additions & 8 deletions src/loggers/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,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)]
Expand Down Expand Up @@ -227,28 +227,29 @@ 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
2 changes: 1 addition & 1 deletion src/loggers/termlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@
}

#[cfg(feature = "paris")]
write_args(record, term_lock, self.config.enable_paris_formatting)?;

Check failure on line 179 in src/loggers/termlog.rs

View workflow job for this annotation

GitHub Actions / check-minimal

this function takes 4 arguments but 3 arguments were supplied
#[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
Loading