Skip to content

Commit

Permalink
Relax rustfmt line length and reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
borntyping committed Oct 19, 2022
1 parent 1ed54e3 commit febfcef
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 46 deletions.
5 changes: 1 addition & 4 deletions examples/init_with_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use log::LevelFilter;
use simple_logger::SimpleLogger;

fn main() {
SimpleLogger::new()
.with_level(LevelFilter::Warn)
.init()
.unwrap();
SimpleLogger::new().with_level(LevelFilter::Warn).init().unwrap();

log::warn!("This will be logged.");
log::info!("This will NOT be logged.");
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
max_width = 120
77 changes: 35 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ const TIMESTAMP_FORMAT_OFFSET: &[FormatItem] = time::macros::format_description!
);

#[cfg(feature = "timestamps")]
const TIMESTAMP_FORMAT_UTC: &[FormatItem] = time::macros::format_description!(
"[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z"
);
const TIMESTAMP_FORMAT_UTC: &[FormatItem] =
time::macros::format_description!("[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z");

#[cfg(feature = "timestamps")]
#[derive(PartialEq)]
Expand Down Expand Up @@ -138,9 +137,7 @@ impl SimpleLogger {
note = "Use [`env`](#method.env) instead. Will be removed in version 2.0.0."
)]
pub fn from_env() -> SimpleLogger {
SimpleLogger::new()
.with_level(log::LevelFilter::Error)
.env()
SimpleLogger::new().with_level(log::LevelFilter::Error).env()
}

/// Simulates env_logger behavior, which enables the user to choose log
Expand Down Expand Up @@ -226,10 +223,7 @@ impl SimpleLogger {
since = "1.11.0",
note = "Use [`with_module_level`](#method.with_module_level) instead. Will be removed in version 2.0.0."
)]
pub fn with_target_levels(
mut self,
target_levels: HashMap<String, LevelFilter>,
) -> SimpleLogger {
pub fn with_target_levels(mut self, target_levels: HashMap<String, LevelFilter>) -> SimpleLogger {
self.module_levels = target_levels.into_iter().collect();

/* Normally this is only called in `init` to avoid redundancy, but we can't initialize the logger in tests */
Expand Down Expand Up @@ -332,12 +326,7 @@ impl SimpleLogger {
*/
self.module_levels
.sort_by_key(|(name, _level)| name.len().wrapping_neg());
let max_level = self
.module_levels
.iter()
.map(|(_name, level)| level)
.copied()
.max();
let max_level = self.module_levels.iter().map(|(_name, level)| level).copied().max();
let max_level = max_level
.map(|lvl| lvl.max(self.default_level))
.unwrap_or(self.default_level);
Expand Down Expand Up @@ -375,11 +364,12 @@ impl Log for SimpleLogger {
{
if self.colors {
match record.level() {
Level::Error => format!("{:<5}",record.level().to_string()).red().to_string(),
Level::Warn => format!("{:<5}",record.level().to_string()).yellow().to_string(),
Level::Info => format!("{:<5}",record.level().to_string()).cyan().to_string(),
Level::Debug => format!("{:<5}",record.level().to_string()).purple().to_string(),
Level::Trace => format!("{:<5}",record.level().to_string()).normal().to_string(), }
Level::Error => format!("{:<5}", record.level().to_string()).red().to_string(),
Level::Warn => format!("{:<5}", record.level().to_string()).yellow().to_string(),
Level::Info => format!("{:<5}", record.level().to_string()).cyan().to_string(),
Level::Debug => format!("{:<5}", record.level().to_string()).purple().to_string(),
Level::Trace => format!("{:<5}", record.level().to_string()).normal().to_string(),
}
} else {
format!("{:<5}", record.level().to_string())
}
Expand Down Expand Up @@ -424,31 +414,36 @@ impl Log for SimpleLogger {
#[cfg(feature = "timestamps")]
match self.timestamps {
Timestamps::None => "".to_string(),
Timestamps::Local => format!("{} ", OffsetDateTime::now_local().expect(concat!(
"Could not determine the UTC offset on this system. ",
"Consider displaying UTC time instead. ",
"Possible causes are that the time crate does not implement \"local_offset_at\" ",
"on your system, or that you are running in a multi-threaded environment and ",
"the time crate is returning \"None\" from \"local_offset_at\" to avoid unsafe ",
"behaviour. See the time crate's documentation for more information. ",
"(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
)).format(&TIMESTAMP_FORMAT_OFFSET).unwrap()),
Timestamps::Local => format!(
"{} ",
OffsetDateTime::now_local()
.expect(concat!(
"Could not determine the UTC offset on this system. ",
"Consider displaying UTC time instead. ",
"Possible causes are that the time crate does not implement \"local_offset_at\" ",
"on your system, or that you are running in a multi-threaded environment and ",
"the time crate is returning \"None\" from \"local_offset_at\" to avoid unsafe ",
"behaviour. See the time crate's documentation for more information. ",
"(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
))
.format(&TIMESTAMP_FORMAT_OFFSET)
.unwrap()
),
Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&TIMESTAMP_FORMAT_UTC).unwrap()),
Timestamps::UtcOffset(offset) => format!("{} ", OffsetDateTime::now_utc().to_offset(offset).format(&TIMESTAMP_FORMAT_OFFSET).unwrap()),
Timestamps::UtcOffset(offset) => format!(
"{} ",
OffsetDateTime::now_utc()
.to_offset(offset)
.format(&TIMESTAMP_FORMAT_OFFSET)
.unwrap()
),
}

#[cfg(not(feature = "timestamps"))]
""
};

let message = format!(
"{}{} [{}{}] {}",
timestamp,
level_string,
target,
thread,
record.args()
);
let message = format!("{}{} [{}{}] {}", timestamp, level_string, target, thread, record.args());

#[cfg(not(feature = "stderr"))]
println!("{}", message);
Expand Down Expand Up @@ -521,9 +516,7 @@ pub fn init_with_env() -> Result<(), SetLoggerError> {
/// Log messages below the given [`Level`] will be filtered.
/// The `RUST_LOG` environment variable is not used.
pub fn init_with_level(level: Level) -> Result<(), SetLoggerError> {
SimpleLogger::new()
.with_level(level.to_level_filter())
.init()
SimpleLogger::new().with_level(level.to_level_filter()).init()
}

/// Use [`init_with_env`] instead.
Expand Down

0 comments on commit febfcef

Please sign in to comment.