Skip to content

Commit

Permalink
Merge pull request #151 from javachaos/javachaos-patch-1
Browse files Browse the repository at this point in the history
Add config option for line endings.
  • Loading branch information
Drakulix committed Mar 26, 2024
2 parents 4ef071d + ff7f4e7 commit 7639530
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
37 changes: 37 additions & 0 deletions src/config.rs
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,

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 (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 (beta, --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 --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 (nightly, --no-default-features --features "test")

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 @@ 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}"),
}
}
}
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

0 comments on commit 7639530

Please sign in to comment.