From a9fcbec9dc9b2cfa4b267f56a313e914220be2a2 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Mon, 2 Nov 2020 08:51:24 +0100 Subject: [PATCH] file logger: allow reopening file Signed-off-by: Thomas Lamprecht --- src/tools/file_logger.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/tools/file_logger.rs b/src/tools/file_logger.rs index cfd43401..ac6c83a6 100644 --- a/src/tools/file_logger.rs +++ b/src/tools/file_logger.rs @@ -47,6 +47,7 @@ pub struct FileLogOptions { #[derive(Debug)] pub struct FileLogger { file: std::fs::File, + file_name: std::path::PathBuf, options: FileLogOptions, } @@ -63,6 +64,23 @@ impl FileLogger { file_name: P, options: FileLogOptions, ) -> Result { + let file = Self::open(&file_name, &options)?; + + let file_name: std::path::PathBuf = file_name.as_ref().to_path_buf(); + + Ok(Self { file, file_name, options }) + } + + pub fn reopen(&mut self) -> Result<&Self, Error> { + let file = Self::open(&self.file_name, &self.options)?; + self.file = file; + Ok(self) + } + + fn open>( + file_name: P, + options: &FileLogOptions, + ) -> Result { let file = std::fs::OpenOptions::new() .read(options.read) .write(true) @@ -76,7 +94,7 @@ impl FileLogger { nix::unistd::chown(file_name.as_ref(), Some(backup_user.uid), Some(backup_user.gid))?; } - Ok(Self { file, options }) + Ok(file) } pub fn log>(&mut self, msg: S) {