2020-10-14 17:02:03 +00:00
|
|
|
use anyhow::Error;
|
2019-03-01 08:34:29 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2020-10-16 09:16:29 +00:00
|
|
|
/// Log messages with optional automatically added timestamps into files
|
2019-03-01 08:34:29 +00:00
|
|
|
///
|
2020-05-30 14:37:33 +00:00
|
|
|
/// Logs messages to file, and optionally to standard output.
|
2019-03-01 08:34:29 +00:00
|
|
|
///
|
|
|
|
///
|
|
|
|
/// #### Example:
|
|
|
|
/// ```
|
|
|
|
/// #[macro_use] extern crate proxmox_backup;
|
2020-04-17 12:11:25 +00:00
|
|
|
/// # use anyhow::{bail, format_err, Error};
|
2020-10-16 09:16:29 +00:00
|
|
|
/// use proxmox_backup::tools::{FileLogger, FileLogOptions};
|
2019-03-01 08:34:29 +00:00
|
|
|
///
|
2019-04-06 09:24:37 +00:00
|
|
|
/// # std::fs::remove_file("test.log");
|
2020-10-16 09:16:29 +00:00
|
|
|
/// let options = FileLogOptions {
|
|
|
|
/// to_stdout: true,
|
|
|
|
/// exclusive: true,
|
|
|
|
/// ..Default::default()
|
|
|
|
/// };
|
|
|
|
/// let mut log = FileLogger::new("test.log", options).unwrap();
|
2019-03-01 08:34:29 +00:00
|
|
|
/// flog!(log, "A simple log: {}", "Hello!");
|
|
|
|
/// ```
|
|
|
|
|
2020-10-15 15:49:18 +00:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
/// Options to control the behavior of a ['FileLogger'] instance
|
|
|
|
pub struct FileLogOptions {
|
2020-10-16 09:16:29 +00:00
|
|
|
/// Open underlying log file in append mode, useful when multiple concurrent processes
|
|
|
|
/// want to log to the same file (e.g., HTTP access log). Note that it is only atomic
|
|
|
|
/// for writes smaller than the PIPE_BUF (4k on Linux).
|
|
|
|
/// Inside the same process you may need to still use an mutex, for shared access.
|
2020-10-15 15:49:18 +00:00
|
|
|
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,
|
|
|
|
}
|
2019-03-01 08:34:29 +00:00
|
|
|
|
2019-04-03 06:58:43 +00:00
|
|
|
#[derive(Debug)]
|
2019-03-01 08:34:29 +00:00
|
|
|
pub struct FileLogger {
|
|
|
|
file: std::fs::File,
|
2020-10-15 15:49:18 +00:00
|
|
|
options: FileLogOptions,
|
2019-03-01 08:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Log messages to [FileLogger](tools/struct.FileLogger.html)
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! flog {
|
|
|
|
($log:expr, $($arg:tt)*) => ({
|
|
|
|
$log.log(format!($($arg)*));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileLogger {
|
2020-10-15 15:49:18 +00:00
|
|
|
pub fn new<P: AsRef<std::path::Path>>(
|
|
|
|
file_name: P,
|
|
|
|
options: FileLogOptions,
|
|
|
|
) -> Result<Self, Error> {
|
2019-03-01 08:34:29 +00:00
|
|
|
let file = std::fs::OpenOptions::new()
|
2020-10-15 15:49:18 +00:00
|
|
|
.read(options.read)
|
2019-03-01 08:34:29 +00:00
|
|
|
.write(true)
|
2020-10-15 15:49:18 +00:00
|
|
|
.append(options.append)
|
|
|
|
.create_new(options.exclusive)
|
|
|
|
.create(!options.exclusive)
|
2019-03-01 08:34:29 +00:00
|
|
|
.open(file_name)?;
|
|
|
|
|
2020-10-15 15:49:18 +00:00
|
|
|
Ok(Self { file, options })
|
2019-03-01 08:34:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log<S: AsRef<str>>(&mut self, msg: S) {
|
|
|
|
let msg = msg.as_ref();
|
|
|
|
|
2020-10-15 15:49:18 +00:00
|
|
|
if self.options.to_stdout {
|
|
|
|
let mut stdout = std::io::stdout();
|
2019-09-11 11:56:09 +00:00
|
|
|
stdout.write_all(msg.as_bytes()).unwrap();
|
|
|
|
stdout.write_all(b"\n").unwrap();
|
2019-03-01 08:34:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
let now = proxmox::tools::time::epoch_i64();
|
|
|
|
let rfc3339 = proxmox::tools::time::epoch_to_rfc3339(now).unwrap();
|
|
|
|
|
2020-10-15 15:49:18 +00:00
|
|
|
let line = if self.options.prefix_time {
|
|
|
|
format!("{}: {}\n", rfc3339, msg)
|
|
|
|
} else {
|
|
|
|
format!("{}\n", msg)
|
|
|
|
};
|
2019-09-11 11:56:09 +00:00
|
|
|
self.file.write_all(line.as_bytes()).unwrap();
|
2019-03-01 08:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::io::Write for FileLogger {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
|
2020-10-15 15:49:18 +00:00
|
|
|
if self.options.to_stdout {
|
|
|
|
let _ = std::io::stdout().write(buf);
|
|
|
|
}
|
2019-03-01 08:34:29 +00:00
|
|
|
self.file.write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> Result<(), std::io::Error> {
|
2020-10-15 15:49:18 +00:00
|
|
|
if self.options.to_stdout {
|
|
|
|
let _ = std::io::stdout().flush();
|
|
|
|
}
|
2019-03-01 08:34:29 +00:00
|
|
|
self.file.flush()
|
|
|
|
}
|
|
|
|
}
|