Skip to content

Commit

Permalink
Add suffix customization to default formatter.
Browse files Browse the repository at this point in the history
This defaults to the same suffix as before, \n.
Supports several use cases:
* When writing to a raw TTY, \r\n is required
* Custom formatting of messages may be desirable, e.g. \n\n
  • Loading branch information
jthacker committed Dec 31, 2020
1 parent 035cda8 commit 894bfcd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
63 changes: 60 additions & 3 deletions src/fmt/mod.rs
Expand Up @@ -144,6 +144,7 @@ pub(crate) struct Builder {
pub format_level: bool,
pub format_indent: Option<usize>,
pub custom_format: Option<FormatFn>,
pub format_suffix: &'static str,
built: bool,
}

Expand All @@ -155,6 +156,7 @@ impl Default for Builder {
format_level: true,
format_indent: Some(4),
custom_format: None,
format_suffix: "\n",
built: false,
}
}
Expand Down Expand Up @@ -187,6 +189,7 @@ impl Builder {
level: built.format_level,
written_header_value: false,
indent: built.format_indent,
suffix: built.format_suffix,
buf,
};

Expand All @@ -211,6 +214,7 @@ struct DefaultFormat<'a> {
written_header_value: bool,
indent: Option<usize>,
buf: &'a mut Formatter,
suffix: &'a str,
}

impl<'a> DefaultFormat<'a> {
Expand Down Expand Up @@ -319,7 +323,7 @@ impl<'a> DefaultFormat<'a> {
fn write_args(&mut self, record: &Record) -> io::Result<()> {
match self.indent {
// Fast path for no indentation
None => writeln!(self.buf, "{}", record.args()),
None => write!(self.buf, "{}{}", record.args(), self.suffix),

Some(indent_count) => {
// Create a wrapper around the buffer only if we have to actually indent the message
Expand All @@ -334,7 +338,13 @@ impl<'a> DefaultFormat<'a> {
let mut first = true;
for chunk in buf.split(|&x| x == b'\n') {
if !first {
write!(self.fmt.buf, "\n{:width$}", "", width = self.indent_count)?;
write!(
self.fmt.buf,
"{}{:width$}",
self.fmt.suffix,
"",
width = self.indent_count
)?;
}
self.fmt.buf.write_all(chunk)?;
first = false;
Expand All @@ -357,7 +367,7 @@ impl<'a> DefaultFormat<'a> {
write!(wrapper, "{}", record.args())?;
}

writeln!(self.buf)?;
write!(self.buf, "{}", self.suffix)?;

Ok(())
}
Expand Down Expand Up @@ -402,6 +412,7 @@ mod tests {
level: true,
written_header_value: false,
indent: None,
suffix: "\n",
buf: &mut f,
});

Expand All @@ -422,6 +433,7 @@ mod tests {
level: false,
written_header_value: false,
indent: None,
suffix: "\n",
buf: &mut f,
});

Expand All @@ -442,6 +454,7 @@ mod tests {
level: true,
written_header_value: false,
indent: Some(4),
suffix: "\n",
buf: &mut f,
});

Expand All @@ -462,6 +475,7 @@ mod tests {
level: true,
written_header_value: false,
indent: Some(0),
suffix: "\n",
buf: &mut f,
});

Expand All @@ -482,9 +496,52 @@ mod tests {
level: false,
written_header_value: false,
indent: Some(4),
suffix: "\n",
buf: &mut f,
});

assert_eq!("log\n message\n", written);
}

#[test]
fn format_suffix() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();

let mut f = Formatter::new(&writer);

let written = write(DefaultFormat {
timestamp: None,
module_path: false,
level: false,
written_header_value: false,
indent: None,
suffix: "\n\n",
buf: &mut f,
});

assert_eq!("log\nmessage\n\n", written);
}

#[test]
fn format_suffix_with_indent() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();

let mut f = Formatter::new(&writer);

let written = write(DefaultFormat {
timestamp: None,
module_path: false,
level: false,
written_header_value: false,
indent: Some(4),
suffix: "\n\n",
buf: &mut f,
});

assert_eq!("log\n\n message\n\n", written);
}
}
6 changes: 6 additions & 0 deletions src/lib.rs
Expand Up @@ -632,6 +632,12 @@ impl Builder {
self.format_timestamp(Some(fmt::TimestampPrecision::Nanos))
}

/// Configures the end of line suffix.
pub fn format_suffix(&mut self, suffix: &'static str) -> &mut Self {
self.format.format_suffix = suffix;
self
}

/// Adds a directive to the filter for a specific module.
///
/// # Examples
Expand Down

0 comments on commit 894bfcd

Please sign in to comment.