Skip to content

Commit 37c3fea

Browse files
authored
Merge pull request #47 from sile/fix-issue43
Ignore ERROR_SHARING_VIOLATION (Windows) error when file rotation
2 parents fd9aebc + a3ba19b commit 37c3fea

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/file.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,28 @@ impl FileAppender {
276276

277277
let _ = self.file.take();
278278

279+
#[cfg(windows)]
280+
{
281+
if let Err(err) = self.rotate_old_files() {
282+
const ERROR_SHARING_VIOLATION: i32 = 32;
283+
284+
// To avoid the problem reported by https://github.com/sile/sloggers/issues/43,
285+
// we ignore the error if its code is 32.
286+
if err.raw_os_error() != Some(ERROR_SHARING_VIOLATION) {
287+
return Err(err);
288+
}
289+
}
290+
}
291+
#[cfg(not(windows))]
292+
self.rotate_old_files()?;
293+
294+
self.written_size = 0;
295+
self.next_reopen_check = Instant::now();
296+
self.reopen_if_needed()?;
297+
298+
Ok(())
299+
}
300+
fn rotate_old_files(&mut self) -> io::Result<()> {
279301
for i in (1..=self.rotate_keep).rev() {
280302
let from = self.rotated_path(i)?;
281303
let to = self.rotated_path(i + 1)?;
@@ -311,10 +333,6 @@ impl FileAppender {
311333
fs::remove_file(delete_path)?;
312334
}
313335

314-
self.written_size = 0;
315-
self.next_reopen_check = Instant::now();
316-
self.reopen_if_needed()?;
317-
318336
Ok(())
319337
}
320338
fn rotated_path(&self, i: usize) -> io::Result<PathBuf> {
@@ -527,9 +545,9 @@ fn path_template_to_path(
527545
let timestamp_string = match timezone {
528546
TimeZone::Local => {
529547
let local_timestamp = Local.from_utc_datetime(&date_time.naive_utc());
530-
local_timestamp.format(&timestamp_template)
548+
local_timestamp.format(timestamp_template)
531549
}
532-
TimeZone::Utc => date_time.format(&timestamp_template),
550+
TimeZone::Utc => date_time.format(timestamp_template),
533551
}
534552
.to_string();
535553
let path_string = path_template.replace("{timestamp}", &timestamp_string);

src/syslog/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<W: fmt::Write> fmt::Write for Rfc5424LikeValueEscaper<W> {
160160
if s.len() >= index {
161161
s = &s[(index + 1)..];
162162
} else {
163-
s = &"";
163+
s = "";
164164
break;
165165
}
166166
}

0 commit comments

Comments
 (0)