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

implements format_file_location #232

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
55 changes: 54 additions & 1 deletion src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub(crate) type FormatFn = Box<dyn Fn(&mut Formatter, &Record) -> io::Result<()>
pub(crate) struct Builder {
pub format_timestamp: Option<TimestampPrecision>,
pub format_module_path: bool,
pub format_file_location: bool,
pub format_target: bool,
pub format_level: bool,
pub format_indent: Option<usize>,
Expand All @@ -154,6 +155,7 @@ impl Default for Builder {
Builder {
format_timestamp: Some(Default::default()),
format_module_path: false,
format_file_location: false,
format_target: true,
format_level: true,
format_indent: Some(4),
Expand Down Expand Up @@ -188,6 +190,7 @@ impl Builder {
let fmt = DefaultFormat {
timestamp: built.format_timestamp,
module_path: built.format_module_path,
location: built.format_file_location,
target: built.format_target,
level: built.format_level,
written_header_value: false,
Expand All @@ -213,6 +216,7 @@ type SubtleStyle = &'static str;
struct DefaultFormat<'a> {
timestamp: Option<TimestampPrecision>,
module_path: bool,
location: bool,
target: bool,
level: bool,
written_header_value: bool,
Expand All @@ -226,6 +230,7 @@ impl<'a> DefaultFormat<'a> {
self.write_timestamp()?;
self.write_level(record)?;
self.write_module_path(record)?;
self.write_location(record)?;
self.write_target(record)?;
self.finish_header()?;

Expand Down Expand Up @@ -316,6 +321,21 @@ impl<'a> DefaultFormat<'a> {
}
}

fn write_location(&mut self, record: &Record) -> io::Result<()> {
if !self.location {
return Ok(());
}

if let Some((file, line)) = record
.file()
.and_then(|file| record.line().map(|line| (file, line)))
{
self.write_header_value(format!("{file}:{line}"))
} else {
Ok(())
}
}

fn write_target(&mut self, record: &Record) -> io::Result<()> {
if !self.target {
return Ok(());
Expand Down Expand Up @@ -425,7 +445,7 @@ mod tests {
}

#[test]
fn format_with_header() {
fn format_with_module_header() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();
Expand All @@ -435,6 +455,7 @@ mod tests {
let written = write(DefaultFormat {
timestamp: None,
module_path: true,
location: false,
target: false,
level: true,
written_header_value: false,
Expand All @@ -446,6 +467,29 @@ mod tests {
assert_eq!("[INFO test::path] log\nmessage\n", written);
}

#[test]
fn format_with_location_header() {
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,
location: true,
target: false,
level: true,
written_header_value: false,
indent: None,
suffix: "\n",
buf: &mut f,
});

assert_eq!("[INFO test.rs:144] log\nmessage\n", written);
}

#[test]
fn format_no_header() {
let writer = writer::Builder::new()
Expand All @@ -457,6 +501,7 @@ mod tests {
let written = write(DefaultFormat {
timestamp: None,
module_path: false,
location: false,
target: false,
level: false,
written_header_value: false,
Expand All @@ -479,6 +524,7 @@ mod tests {
let written = write(DefaultFormat {
timestamp: None,
module_path: true,
location: false,
target: false,
level: true,
written_header_value: false,
Expand All @@ -501,6 +547,7 @@ mod tests {
let written = write(DefaultFormat {
timestamp: None,
module_path: true,
location: false,
target: false,
level: true,
written_header_value: false,
Expand All @@ -523,6 +570,7 @@ mod tests {
let written = write(DefaultFormat {
timestamp: None,
module_path: false,
location: false,
target: false,
level: false,
written_header_value: false,
Expand All @@ -545,6 +593,7 @@ mod tests {
let written = write(DefaultFormat {
timestamp: None,
module_path: false,
location: false,
target: false,
level: false,
written_header_value: false,
Expand All @@ -567,6 +616,7 @@ mod tests {
let written = write(DefaultFormat {
timestamp: None,
module_path: false,
location: false,
target: false,
level: false,
written_header_value: false,
Expand All @@ -591,6 +641,7 @@ mod tests {
DefaultFormat {
timestamp: None,
module_path: true,
location: false,
target: true,
level: true,
written_header_value: false,
Expand All @@ -614,6 +665,7 @@ mod tests {
let written = write(DefaultFormat {
timestamp: None,
module_path: true,
location: false,
target: true,
level: true,
written_header_value: false,
Expand All @@ -638,6 +690,7 @@ mod tests {
DefaultFormat {
timestamp: None,
module_path: true,
location: false,
target: false,
level: true,
written_header_value: false,
Expand Down
21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,27 @@ impl Builder {
}

/// Whether or not to write the module path in the default format.
///
/// Cannot be used in combination with [format_file_location](Self::format_file_location).
pub fn format_module_path(&mut self, write: bool) -> &mut Self {
self.format.format_module_path = write;
if write && self.format.format_file_location {
println!("Cannot use both `format_module_path` and `format_log_location`!")
} else {
self.format.format_module_path = write;
}
self
}

/// Whether or not to write the location of the log (file and line number)
/// in the default format.
///
/// Cannot be used in combination with [format_module_path](Self::format_file_location).
pub fn format_file_location(&mut self, write: bool) -> &mut Self {
if write && self.format.format_module_path {
println!("Cannot use both `format_module_path` and `format_log_location`!")
} else {
self.format.format_file_location = write;
}
self
}

Expand Down