public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH backup 4/4] tools: file logger: allow more control over file creation and log format
Date: Thu, 15 Oct 2020 17:49:19 +0200	[thread overview]
Message-ID: <20201015154919.19776-5-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20201015154919.19776-1-t.lamprecht@proxmox.com>

Signed-off-by: Thomas Lamprecht <t.lamprecht@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





  parent reply	other threads:[~2020-10-15 15:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-15 15:49 [pbs-devel] Preperation patches for access/auth log Thomas Lamprecht
2020-10-15 15:49 ` [pbs-devel] [PATCH backup 1/4] server: rest: implement max URI path and query length request limits Thomas Lamprecht
2020-10-16  8:45   ` [pbs-devel] applied: " Dietmar Maurer
2020-10-15 15:49 ` [pbs-devel] [PATCH backup 2/4] server: rest: also log the query part of URL Thomas Lamprecht
2020-10-16  8:46   ` [pbs-devel] applied: " Dietmar Maurer
2020-10-15 15:49 ` [pbs-devel] [PATCH backup 3/4] tools: file logger: use option struct to control behavior Thomas Lamprecht
2020-10-16  8:44   ` Dietmar Maurer
2020-10-16  8:46     ` Thomas Lamprecht
2020-10-15 15:49 ` Thomas Lamprecht [this message]
2020-10-16  8:51   ` [pbs-devel] applied: [PATCH backup 4/4] tools: file logger: allow more control over file creation and log format Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201015154919.19776-5-t.lamprecht@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal