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 posibility to print module #129

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub struct Config {
pub(crate) target: LevelFilter,
pub(crate) target_padding: TargetPadding,
pub(crate) location: LevelFilter,
pub(crate) module: LevelFilter,
pub(crate) time_format: TimeFormat,
pub(crate) time_offset: UtcOffset,
pub(crate) filter_allow: Cow<'static, [Cow<'static, str>]>,
Expand Down Expand Up @@ -149,6 +150,12 @@ impl ConfigBuilder {
self
}

/// Set at which level and above (more verbose) a module shall be logged (default is Off)
pub fn set_module_level(&mut self, module: LevelFilter) -> &mut ConfigBuilder {
self.0.module = module;
self
}

/// Set how the levels should be padded, when logging (default is Off)
pub fn set_level_padding(&mut self, padding: LevelPadding) -> &mut ConfigBuilder {
self.0.level_padding = padding;
Expand Down Expand Up @@ -335,6 +342,7 @@ impl Default for Config {
target: LevelFilter::Debug,
target_padding: TargetPadding::Off,
location: LevelFilter::Trace,
module: LevelFilter::Off,
time_format: TimeFormat::Custom(format_description!("[hour]:[minute]:[second]")),
time_offset: UtcOffset::UTC,
filter_allow: Cow::Borrowed(&[]),
Expand Down
14 changes: 14 additions & 0 deletions src/loggers/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ where
write_location(record, write)?;
}

if config.module <= record.level() && config.module != LevelFilter::Off {
write_module(record, write)?;
}

#[cfg(feature = "paris")]
return write_args(record, write, config.enable_paris_formatting);
#[cfg(not(feature = "paris"))]
Expand Down Expand Up @@ -167,6 +171,16 @@ where
Ok(())
}

#[inline(always)]
pub fn write_module<W>(record: &Record<'_>, write: &mut W) -> Result<(), Error>
where
W: Write + Sized,
{
let module = record.module_path().unwrap_or("<unknown>");
write!(write, "[{}] ", module)?;
Ok(())
}

pub fn write_thread_name<W>(write: &mut W, config: &Config) -> Result<(), Error>
where
W: Write + Sized,
Expand Down
4 changes: 4 additions & 0 deletions src/loggers/termlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ impl TermLogger {
write_location(record, term_lock)?;
}

if self.config.module <= record.level() && self.config.module != LevelFilter::Off {
write_module(record, term_lock)?;
}

#[cfg(feature = "paris")]
write_args(record, term_lock, self.config.enable_paris_formatting)?;
#[cfg(not(feature = "paris"))]
Expand Down
10 changes: 10 additions & 0 deletions src/loggers/testlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ pub fn log(config: &Config, record: &Record<'_>) {
write_location(record);
}

if config.module <= record.level() && config.module != LevelFilter::Off {
write_module(record);
}

write_args(record);
}

Expand Down Expand Up @@ -175,6 +179,12 @@ pub fn write_location(record: &Record<'_>) {
}
}

#[inline(always)]
pub fn write_module(record: &Record<'_>) {
let module = record.module_path().unwrap_or("<unknown>");
print!("[{}] ", module);
}

#[inline(always)]
pub fn write_args(record: &Record<'_>) {
println!("{}", record.args());
Expand Down