Skip to content

Commit 7a4df08

Browse files
committed
Make Simplelogger.timeformat an Option
1 parent 168c403 commit 7a4df08

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

src/lib.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub struct SimpleLogger {
8282
#[cfg(feature = "timestamps")]
8383
timestamps: Timestamps,
8484
#[cfg(feature = "timestamps")]
85-
timeformat: &'static [FormatItem<'static>],
85+
timeformat: Option<&'static [FormatItem<'static>]>,
8686

8787
/// Whether to use color output or not.
8888
///
@@ -113,8 +113,9 @@ impl SimpleLogger {
113113

114114
#[cfg(feature = "timestamps")]
115115
timestamps: Timestamps::Utc,
116+
116117
#[cfg(feature = "timestamps")]
117-
timeformat: time::macros::format_description!(""),
118+
timeformat: None,
118119

119120
#[cfg(feature = "colored")]
120121
colors: true,
@@ -285,7 +286,7 @@ impl SimpleLogger {
285286
#[must_use = "You must call init() to begin logging"]
286287
#[cfg(feature = "timestamps")]
287288
pub fn with_custom_timestamps(mut self, timeformat: &'static [FormatItem<'static>]) -> SimpleLogger {
288-
self.timeformat = timeformat;
289+
self.timeformat = Some(timeformat);
289290
self
290291
}
291292

@@ -345,17 +346,6 @@ impl SimpleLogger {
345346
#[cfg(all(windows, feature = "colored"))]
346347
set_up_color_terminal();
347348

348-
// Set default timestamp format
349-
#[cfg(feature = "timestamps")]
350-
if self.timeformat.len() <= 0 {
351-
self.timeformat = match self.timestamps {
352-
Timestamps::Local => TIMESTAMP_FORMAT_OFFSET,
353-
Timestamps::Utc => TIMESTAMP_FORMAT_UTC,
354-
Timestamps::UtcOffset(_) => TIMESTAMP_FORMAT_OFFSET,
355-
_ => self.timeformat,
356-
};
357-
}
358-
359349
/* Sort all module levels from most specific to least specific. The length of the module
360350
* name is used instead of its actual depth to avoid module name parsing.
361351
*/
@@ -461,15 +451,20 @@ impl Log for SimpleLogger {
461451
"behaviour. See the time crate's documentation for more information. ",
462452
"(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
463453
))
464-
.format(&self.timeformat)
454+
.format(&self.timeformat.unwrap_or(TIMESTAMP_FORMAT_OFFSET))
455+
.unwrap()
456+
),
457+
Timestamps::Utc => format!(
458+
"{} ",
459+
OffsetDateTime::now_utc()
460+
.format(&self.timeformat.unwrap_or(TIMESTAMP_FORMAT_UTC))
465461
.unwrap()
466462
),
467-
Timestamps::Utc => format!("{} ", OffsetDateTime::now_utc().format(&self.timeformat).unwrap()),
468463
Timestamps::UtcOffset(offset) => format!(
469464
"{} ",
470465
OffsetDateTime::now_utc()
471466
.to_offset(offset)
472-
.format(&self.timeformat)
467+
.format(&self.timeformat.unwrap_or(TIMESTAMP_FORMAT_OFFSET))
473468
.unwrap()
474469
),
475470
}

0 commit comments

Comments
 (0)