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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/config.rs
Expand Up @@ -79,13 +79,14 @@
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>]>,
pub(crate) filter_ignore: Cow<'static, [Cow<'static, str>]>,
#[cfg(feature = "termcolor")]
pub(crate) level_color: [Option<Color>; 6],
pub(crate) write_log_enable_colors: bool,

Check warning on line 89 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 89 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 89 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 89 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

Check warning on line 89 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 89 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
#[cfg(feature = "paris")]
pub(crate) enable_paris_formatting: bool,
}
Expand Down Expand Up @@ -149,6 +150,12 @@
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 @@ -180,7 +187,7 @@
/// The easiest way to satisfy the static lifetime of the argument is to directly use the
/// re-exported [`time::macros::format_description`] macro.
///
/// *Note*: The default time format is "[hour]:[minute]:[second]".

Check warning on line 190 in src/config.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `hour`

Check warning on line 190 in src/config.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `minute`

Check warning on line 190 in src/config.rs

View workflow job for this annotation

GitHub Actions / doc

unresolved link to `second`
///
/// The syntax for the format_description macro can be found in the
/// [`time` crate book](https://time-rs.github.io/book/api/format-description.html).
Expand Down Expand Up @@ -335,6 +342,7 @@
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
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
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
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