tools: file logger: use option struct to control behavior
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
400c568f8e
commit
c0df91f8bd
|
@ -21,7 +21,7 @@ use proxmox::tools::fs::{create_path, open_file_locked, replace_file, CreateOpti
|
||||||
use super::UPID;
|
use super::UPID;
|
||||||
|
|
||||||
use crate::tools::logrotate::{LogRotate, LogRotateFiles};
|
use crate::tools::logrotate::{LogRotate, LogRotateFiles};
|
||||||
use crate::tools::FileLogger;
|
use crate::tools::{FileLogger, FileLogOptions};
|
||||||
use crate::api2::types::Userid;
|
use crate::api2::types::Userid;
|
||||||
|
|
||||||
macro_rules! PROXMOX_BACKUP_VAR_RUN_DIR_M { () => ("/run/proxmox-backup") }
|
macro_rules! PROXMOX_BACKUP_VAR_RUN_DIR_M { () => ("/run/proxmox-backup") }
|
||||||
|
@ -672,7 +672,13 @@ impl WorkerTask {
|
||||||
|
|
||||||
println!("FILE: {:?}", path);
|
println!("FILE: {:?}", path);
|
||||||
|
|
||||||
let logger = FileLogger::new(&path, to_stdout)?;
|
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))?;
|
nix::unistd::chown(&path, Some(backup_user.uid), Some(backup_user.gid))?;
|
||||||
|
|
||||||
let worker = Arc::new(Self {
|
let worker = Arc::new(Self {
|
||||||
|
|
|
@ -17,11 +17,26 @@ use std::io::Write;
|
||||||
/// flog!(log, "A simple log: {}", "Hello!");
|
/// flog!(log, "A simple log: {}", "Hello!");
|
||||||
/// ```
|
/// ```
|
||||||
|
|
||||||
|
#[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)]
|
#[derive(Debug)]
|
||||||
pub struct FileLogger {
|
pub struct FileLogger {
|
||||||
file: std::fs::File,
|
file: std::fs::File,
|
||||||
to_stdout: bool,
|
options: FileLogOptions,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Log messages to [FileLogger](tools/struct.FileLogger.html)
|
/// Log messages to [FileLogger](tools/struct.FileLogger.html)
|
||||||
|
@ -33,23 +48,26 @@ macro_rules! flog {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileLogger {
|
impl FileLogger {
|
||||||
|
pub fn new<P: AsRef<std::path::Path>>(
|
||||||
pub fn new<P: AsRef<std::path::Path>>(file_name: P, to_stdout: bool) -> Result<Self, Error> {
|
file_name: P,
|
||||||
|
options: FileLogOptions,
|
||||||
|
) -> Result<Self, Error> {
|
||||||
let file = std::fs::OpenOptions::new()
|
let file = std::fs::OpenOptions::new()
|
||||||
.read(true)
|
.read(options.read)
|
||||||
.write(true)
|
.write(true)
|
||||||
.create_new(true)
|
.append(options.append)
|
||||||
|
.create_new(options.exclusive)
|
||||||
|
.create(!options.exclusive)
|
||||||
.open(file_name)?;
|
.open(file_name)?;
|
||||||
|
|
||||||
Ok(Self { file , to_stdout })
|
Ok(Self { file, options })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn log<S: AsRef<str>>(&mut self, msg: S) {
|
pub fn log<S: AsRef<str>>(&mut self, msg: S) {
|
||||||
let msg = msg.as_ref();
|
let msg = msg.as_ref();
|
||||||
|
|
||||||
|
if self.options.to_stdout {
|
||||||
let mut stdout = std::io::stdout();
|
let mut stdout = std::io::stdout();
|
||||||
if self.to_stdout {
|
|
||||||
stdout.write_all(msg.as_bytes()).unwrap();
|
stdout.write_all(msg.as_bytes()).unwrap();
|
||||||
stdout.write_all(b"\n").unwrap();
|
stdout.write_all(b"\n").unwrap();
|
||||||
}
|
}
|
||||||
|
@ -57,19 +75,27 @@ impl FileLogger {
|
||||||
let now = proxmox::tools::time::epoch_i64();
|
let now = proxmox::tools::time::epoch_i64();
|
||||||
let rfc3339 = proxmox::tools::time::epoch_to_rfc3339(now).unwrap();
|
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();
|
self.file.write_all(line.as_bytes()).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::io::Write for FileLogger {
|
impl std::io::Write for FileLogger {
|
||||||
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
|
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)
|
self.file.write(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flush(&mut self) -> Result<(), std::io::Error> {
|
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()
|
self.file.flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue