From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id C6B6F60917 for ; Thu, 15 Oct 2020 17:49:28 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B3A6614412 for ; Thu, 15 Oct 2020 17:49:28 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 96194143C0 for ; Thu, 15 Oct 2020 17:49:25 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5AC2245DA1 for ; Thu, 15 Oct 2020 17:49:25 +0200 (CEST) From: Thomas Lamprecht To: pbs-devel@lists.proxmox.com Date: Thu, 15 Oct 2020 17:49:19 +0200 Message-Id: <20201015154919.19776-5-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20201015154919.19776-1-t.lamprecht@proxmox.com> References: <20201015154919.19776-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.134 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH backup 4/4] tools: file logger: allow more control over file creation and log format X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Oct 2020 15:49:28 -0000 Signed-off-by: Thomas Lamprecht --- 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 { 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