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 D053A60941 for ; Thu, 15 Oct 2020 17:49:58 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B64B414414 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 A287E143CF 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 6870D45DB8 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:18 +0200 Message-Id: <20201015154919.19776-4-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.133 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 3/4] tools: file logger: use option struct to control behavior 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:58 -0000 will be extended in the next patch Signed-off-by: Thomas Lamprecht --- src/server/worker_task.rs | 7 +++++-- src/tools/file_logger.rs | 35 +++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs index bd19782c..fc41b52a 100644 --- a/src/server/worker_task.rs +++ b/src/server/worker_task.rs @@ -21,7 +21,7 @@ use proxmox::tools::fs::{create_path, open_file_locked, replace_file, CreateOpti use super::UPID; use crate::tools::logrotate::{LogRotate, LogRotateFiles}; -use crate::tools::FileLogger; +use crate::tools::{FileLogger, FileLogOptions}; use crate::api2::types::Userid; macro_rules! PROXMOX_BACKUP_VAR_RUN_DIR_M { () => ("/run/proxmox-backup") } @@ -672,7 +672,10 @@ impl WorkerTask { println!("FILE: {:?}", path); - let logger = FileLogger::new(&path, to_stdout)?; + let logger_options = FileLogOptions { + to_stdout: to_stdout, + }; + let logger = FileLogger::new(&path, logger_options)?; nix::unistd::chown(&path, Some(backup_user.uid), Some(backup_user.gid))?; let worker = Arc::new(Self { diff --git a/src/tools/file_logger.rs b/src/tools/file_logger.rs index 48a0bae2..69dd0bd4 100644 --- a/src/tools/file_logger.rs +++ b/src/tools/file_logger.rs @@ -17,11 +17,17 @@ use std::io::Write; /// flog!(log, "A simple log: {}", "Hello!"); /// ``` +#[derive(Debug, Default)] +/// Options to control the behavior of a ['FileLogger'] instance +pub struct FileLogOptions { + /// Duplicate logged messages to STDOUT, like tee + pub to_stdout: bool, +} #[derive(Debug)] pub struct FileLogger { file: std::fs::File, - to_stdout: bool, + options: FileLogOptions, } /// Log messages to [FileLogger](tools/struct.FileLogger.html) @@ -33,23 +39,24 @@ macro_rules! flog { } impl FileLogger { - - pub fn new>(file_name: P, to_stdout: bool) -> Result { - + pub fn new>( + file_name: P, + options: FileLogOptions, + ) -> Result { let file = std::fs::OpenOptions::new() .read(true) .write(true) .create_new(true) .open(file_name)?; - Ok(Self { file , to_stdout }) + Ok(Self { file, options }) } pub fn log>(&mut self, msg: S) { let msg = msg.as_ref(); - let mut stdout = std::io::stdout(); - if self.to_stdout { + if self.options.to_stdout { + let mut stdout = std::io::stdout(); stdout.write_all(msg.as_bytes()).unwrap(); stdout.write_all(b"\n").unwrap(); } @@ -57,19 +64,27 @@ impl FileLogger { let now = proxmox::tools::time::epoch_i64(); let rfc3339 = proxmox::tools::time::epoch_to_rfc3339(now).unwrap(); - let line = format!("{}: {}\n", rfc3339, msg); + let line = if self.options.prefix_time { + format!("{}: {}\n", rfc3339, msg) + } else { + format!("{}\n", msg) + }; self.file.write_all(line.as_bytes()).unwrap(); } } impl std::io::Write for FileLogger { fn write(&mut self, buf: &[u8]) -> Result { - if self.to_stdout { let _ = std::io::stdout().write(buf); } + if self.options.to_stdout { + let _ = std::io::stdout().write(buf); + } self.file.write(buf) } fn flush(&mut self) -> Result<(), std::io::Error> { - if self.to_stdout { let _ = std::io::stdout().flush(); } + if self.options.to_stdout { + let _ = std::io::stdout().flush(); + } self.file.flush() } } -- 2.27.0