Skip to content

Commit

Permalink
Improve combination of append with Criterion::Age. Fixes #122.
Browse files Browse the repository at this point in the history
  • Loading branch information
emabee committed Oct 6, 2022
1 parent 1d9f66e commit 3c0a779
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 3 additions & 7 deletions src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,11 +43,6 @@ pub enum Criterion {
///
/// <a name="ref-1">\[1\]</a> [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.
Expand Down
26 changes: 14 additions & 12 deletions src/writers/file_log_writer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,26 +736,28 @@ fn rotate_output_file_to_idx(
// See documentation of Criterion::Age.
fn get_creation_date(path: &Path) -> DateTime<Local> {
// 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> {
Local::now()
}

fn try_get_creation_date(path: &Path) -> Result<DateTime<Local>, FlexiLoggerError> {
Ok(std::fs::metadata(path)?.created()?.into())
}
fn try_get_modification_date(path: &Path) -> Result<DateTime<Local>, FlexiLoggerError> {
let md = std::fs::metadata(path)?;
let d = md.created().or_else(|_| md.modified())?;
Ok(d.into())
}
fn get_current_date() -> DateTime<Local> {
Local::now()
}

mod platform {
#[cfg(target_family = "unix")]
Expand Down

0 comments on commit 3c0a779

Please sign in to comment.