[pbs-devel] [PATCH backup 4/4] tools: file logger: allow more control over file creation and log format

Thomas Lamprecht t.lamprecht at proxmox.com
Thu Oct 15 17:49:19 CEST 2020


Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/server/worker_task.rs |  3 +++
 src/tools/file_logger.rs  | 15 +++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs
index fc41b52a..f3846596 100644
--- a/src/server/worker_task.rs
+++ b/src/server/worker_task.rs
@@ -674,6 +674,9 @@ impl WorkerTask {
 
         let logger_options = FileLogOptions {
             to_stdout: to_stdout,
+            exclusive: true,
+            read: true,
+            ..Default::default()
         };
         let logger = FileLogger::new(&path, logger_options)?;
         nix::unistd::chown(&path, Some(backup_user.uid), Some(backup_user.gid))?;
diff --git a/src/tools/file_logger.rs b/src/tools/file_logger.rs
index 69dd0bd4..2f406c0d 100644
--- a/src/tools/file_logger.rs
+++ b/src/tools/file_logger.rs
@@ -20,8 +20,17 @@ use std::io::Write;
 #[derive(Debug, Default)]
 /// Options to control the behavior of a ['FileLogger'] instance
 pub struct FileLogOptions {
+    /// Open underlying log file in append mode, useful when multiple concurrent
+    /// writers log to the same file. For example, an HTTP access log.
+    pub append: bool,
+    /// Open underlying log file as readable
+    pub read: bool,
+    /// If set, ensure that the file is newly created or error out if already existing.
+    pub exclusive: bool,
     /// Duplicate logged messages to STDOUT, like tee
     pub to_stdout: bool,
+    /// Prefix messages logged to the file with the current local time as RFC 3339
+    pub prefix_time: bool,
 }
 
 #[derive(Debug)]
@@ -44,9 +53,11 @@ impl FileLogger {
         options: FileLogOptions,
     ) -> Result<Self, Error> {
         let file = std::fs::OpenOptions::new()
-            .read(true)
+            .read(options.read)
             .write(true)
-            .create_new(true)
+            .append(options.append)
+            .create_new(options.exclusive)
+            .create(!options.exclusive)
             .open(file_name)?;
 
         Ok(Self { file, options })
-- 
2.27.0






More information about the pbs-devel mailing list