Skip to content
Open
Changes from all 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
53 changes: 46 additions & 7 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ pub trait CologStyle {
default_prefix_token(self, level)
}

fn first_line_separator(&self) -> String {
String::new()
}

fn first_line_separator_len(&self) -> usize {
self.first_line_separator().chars().count()
}

/// Returns the default line separator string, used when formatting
/// multi-line log messages.
///
Expand All @@ -135,6 +143,10 @@ pub trait CologStyle {
format!("\n{} ", " | ".white().bold())
}

fn final_line_separator(&self) -> String {
format!("\n{} ", " | ".white().bold())
}

/// Top-level formatting function for log messages.
///
/// This method is not typically overriden (rather [`Self::level_color`] or
Expand Down Expand Up @@ -271,12 +283,39 @@ pub fn default_format(
buf: &mut Formatter,
record: &Record<'_>,
) -> Result<(), Error> {
let sep = style.line_separator();
let first_sep = style.first_line_separator();
let prefix = style.prefix_token(&record.level());
writeln!(
buf,
"{} {}",
prefix,
record.args().to_string().replace('\n', &sep),
)

let args = record.args().to_string();
let lines: Vec<_> = args.lines().collect();

if lines.len() == 1 {
// have to count chars because of color codes and unicode things
// counting actual graphemes would be better but this is good enough
// and style.first_line_separator_len() can be overridden if really necessary
let padding = style.first_line_separator_len() + prefix.chars().count();

writeln!(buf, "{:>width$} {}", prefix, args, width = padding)?;
} else {
let sep = style.line_separator();
let final_sep = style.final_line_separator();

let mut lines = lines.iter();

if let Some(first_line) = lines.next() {
write!(buf, "{}{} {}", first_sep, prefix, first_line)?;
}

let last_line = lines.next_back();

for line in lines {
write!(buf, "{}{}", sep, line)?;
}

if let Some(last_line) = last_line {
writeln!(buf, "{}{}", final_sep, last_line)?;
}
}

Ok(())
}