diff --git a/CHANGELOG.md b/CHANGELOG.md index 43e2caa..28ab2c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,12 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [0.24.0] - unpublished -Revert back to using `chrono`, since `chrono` is now fortunately maintained again and the timezone -handling is fixed (largely reverting the changes done for [0.19.6]). +Revert back to using `chrono`, since `chrono` is now fortunately maintained again and its timezone +handling is fixed meanwhile (largely reverting the changes done for [0.19.6]; +version bump since this affects the API, e.g. in `DeferredNow`). + +Improve the logic that handles the issue described again in +[issue-122](https://github.com/emabee/flexi_logger/issues/122). ## [0.23.3] - 2022-09-11 diff --git a/src/parameters.rs b/src/parameters.rs index 520575c..33b88c5 100644 --- a/src/parameters.rs +++ b/src/parameters.rs @@ -23,8 +23,9 @@ pub enum Criterion { /// for different reasons. /// /// To minimize the impact on age-based file-rotation, - /// `flexi_logger` uses on Windows and unix its initialization time - /// rather than the real file property + /// `flexi_logger` uses on Windows, and on all other platforms where the creation date + /// of a file is not available (like on Unix), the last modification date + /// (or, if this is also not available, the current time stamp) /// as the created_at-info of an rCURRENT file that already exists, and the /// current timestamp when file rotation happens during further execution. /// Consequently, a left-over rCURRENT file from a previous program run will look newer @@ -42,11 +43,6 @@ pub enum Criterion { /// /// \[1\] [https://superuser.com/questions/966490/windows-7-what-is-date-created-file-property-referring-to](https://superuser.com/questions/966490/windows-7-what-is-date-created-file-property-referring-to). /// - /// #### Issue on unix - /// - /// `std::fs::metadata.created()` returns `Err`, because unix does not maintain a - /// created-at-timestamp. - /// Age(Age), /// Rotate the file when it has either become older than the specified age, or when it has /// exceeded the specified size in bytes. diff --git a/src/writers/file_log_writer/state.rs b/src/writers/file_log_writer/state.rs index 2e44de9..9b91025 100644 --- a/src/writers/file_log_writer/state.rs +++ b/src/writers/file_log_writer/state.rs @@ -736,26 +736,28 @@ fn rotate_output_file_to_idx( // See documentation of Criterion::Age. fn get_creation_date(path: &Path) -> DateTime { // On windows, we know that try_get_creation_date() returns a result, but it is wrong. - // On unix, we know that try_get_creation_date() returns an error. - if cfg!(any(target_os = "windows", target_family = "unix")) { - get_fake_creation_date() + if cfg!(target_os = "windows") { + try_get_modification_date(path).unwrap_or_else(|_| get_current_date()) } else { // On all others of the many platforms, we give the real creation date a try, - // and fall back to the fake if it is not available. - match try_get_creation_date(path) { - Ok(d) => d, - Err(_e) => get_fake_creation_date(), - } + // and fall back if it is not available. + try_get_creation_date(path) + .or_else(|_| try_get_modification_date(path)) + .unwrap_or_else(|_| get_current_date()) } } -fn get_fake_creation_date() -> DateTime { - Local::now() -} - fn try_get_creation_date(path: &Path) -> Result, FlexiLoggerError> { Ok(std::fs::metadata(path)?.created()?.into()) } +fn try_get_modification_date(path: &Path) -> Result, FlexiLoggerError> { + let md = std::fs::metadata(path)?; + let d = md.created().or_else(|_| md.modified())?; + Ok(d.into()) +} +fn get_current_date() -> DateTime { + Local::now() +} mod platform { #[cfg(target_family = "unix")]