From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH backup 3/4] tools: file logger: use option struct to control behavior
Date: Thu, 15 Oct 2020 17:49:18 +0200 [thread overview]
Message-ID: <20201015154919.19776-4-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20201015154919.19776-1-t.lamprecht@proxmox.com>
will be extended in the next patch
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
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<P: AsRef<std::path::Path>>(file_name: P, to_stdout: bool) -> Result<Self, Error> {
-
+ pub fn new<P: AsRef<std::path::Path>>(
+ file_name: P,
+ options: FileLogOptions,
+ ) -> Result<Self, Error> {
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<S: AsRef<str>>(&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<usize, std::io::Error> {
- 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
next prev 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 ` Thomas Lamprecht [this message]
2020-10-16 8:44 ` [pbs-devel] [PATCH backup 3/4] tools: file logger: use option struct to control behavior Dietmar Maurer
2020-10-16 8:46 ` Thomas Lamprecht
2020-10-15 15:49 ` [pbs-devel] [PATCH backup 4/4] tools: file logger: allow more control over file creation and log format Thomas Lamprecht
2020-10-16 8:51 ` [pbs-devel] applied: " 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-4-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.