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

[WIP] Add target padding #85

Merged
merged 3 commits into from Nov 7, 2021
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
19 changes: 19 additions & 0 deletions src/config.rs
Expand Up @@ -29,6 +29,17 @@ pub enum ThreadPadding {
Off,
}

#[derive(Debug, Clone, Copy)]
/// Padding to be used for logging the thread id/name
pub enum TargetPadding {
/// Add spaces on the left side, up to usize many
Left(usize),
/// Add spaces on the right side, up to usize many
Right(usize),
/// Do not pad the thread id/name
Off,
}

#[derive(Debug, Clone, Copy, PartialEq)]
/// Mode for logging the thread name or id or both.
pub enum ThreadLogMode {
Expand Down Expand Up @@ -59,6 +70,7 @@ pub struct Config {
pub(crate) thread_log_mode: ThreadLogMode,
pub(crate) thread_padding: ThreadPadding,
pub(crate) target: LevelFilter,
pub(crate) target_padding: TargetPadding,
pub(crate) location: LevelFilter,
pub(crate) time_format: Cow<'static, str>,
pub(crate) time_offset: FixedOffset,
Expand Down Expand Up @@ -116,6 +128,12 @@ impl ConfigBuilder {
self
}

/// Set how the thread should be padded
pub fn set_target_padding(&mut self, padding: TargetPadding) -> &mut ConfigBuilder {
self.0.target_padding = padding;
self
}

/// Set at which level and above (more verbose) a source code reference shall be logged (default is Trace)
pub fn set_location_level(&mut self, location: LevelFilter) -> &mut ConfigBuilder {
self.0.location = location;
Expand Down Expand Up @@ -256,6 +274,7 @@ impl Default for Config {
thread_log_mode: ThreadLogMode::IDs,
thread_padding: ThreadPadding::Off,
target: LevelFilter::Debug,
target_padding: TargetPadding::Off,
location: LevelFilter::Trace,
time_format: Cow::Borrowed("%H:%M:%S"),
time_offset: FixedOffset::east(0),
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Expand Up @@ -24,7 +24,9 @@
mod config;
mod loggers;

pub use self::config::{Config, ConfigBuilder, LevelPadding, ThreadLogMode, ThreadPadding};
pub use self::config::{
Config, ConfigBuilder, LevelPadding, TargetPadding, ThreadLogMode, ThreadPadding,
};
#[cfg(feature = "test")]
pub use self::loggers::TestLogger;
pub use self::loggers::{CombinedLogger, SimpleLogger, WriteLogger};
Expand Down
29 changes: 26 additions & 3 deletions src/loggers/logging.rs
@@ -1,3 +1,4 @@
use crate::config::TargetPadding;
use crate::{Config, LevelPadding, ThreadLogMode, ThreadPadding};
use log::{LevelFilter, Record};
use std::io::{Error, Write};
Expand Down Expand Up @@ -32,7 +33,7 @@ where
}

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

if config.location <= record.level() && config.location != LevelFilter::Off {
Expand Down Expand Up @@ -71,11 +72,33 @@ where
}

#[inline(always)]
pub fn write_target<W>(record: &Record<'_>, write: &mut W) -> Result<(), Error>
pub fn write_target<W>(record: &Record<'_>, write: &mut W, config: &Config) -> Result<(), Error>
where
W: Write + Sized,
{
write!(write, "{}: ", record.target())?;
// dbg!(&config.target_padding);
match config.target_padding {
TargetPadding::Left(pad) => {
write!(
write,
"{target:>pad$}: ",
pad = pad,
target = record.target()
)?;
}
TargetPadding::Right(pad) => {
write!(
write,
"{target:<pad$}: ",
pad = pad,
target = record.target()
)?;
}
TargetPadding::Off => {
write!(write, "{}: ", record.target())?;
}
}

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/loggers/termlog.rs
Expand Up @@ -152,7 +152,7 @@ impl TermLogger {
}

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

if self.config.location <= record.level() && self.config.location != LevelFilter::Off {
Expand Down