2019-03-01 08:34:29 +00:00
|
|
|
use std::io::Write;
|
|
|
|
|
2021-09-21 05:58:40 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
use nix::fcntl::OFlag;
|
|
|
|
|
2022-04-06 14:55:39 +00:00
|
|
|
use proxmox_sys::fs::{atomic_open_or_create_file, CreateOptions};
|
2021-09-21 05:58:40 +00:00
|
|
|
|
2021-09-30 06:51:23 +00:00
|
|
|
/// Options to control the behavior of a [FileLogger] instance
|
2021-09-21 05:58:40 +00:00
|
|
|
#[derive(Default)]
|
2020-10-15 15:49:18 +00:00
|
|
|
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,
|
2021-09-21 05:58:40 +00:00
|
|
|
/// File owner/group and mode
|
|
|
|
pub file_opts: CreateOptions,
|
2020-10-15 15:49:18 +00:00
|
|
|
}
|
2019-03-01 08:34:29 +00:00
|
|
|
|
2021-09-30 06:51:23 +00:00
|
|
|
/// Log messages with optional automatically added timestamps into files
|
|
|
|
///
|
|
|
|
/// #### Example:
|
|
|
|
/// ```
|
|
|
|
/// # use anyhow::{bail, format_err, Error};
|
|
|
|
/// use proxmox_rest_server::{flog, FileLogger, FileLogOptions};
|
|
|
|
///
|
|
|
|
/// # std::fs::remove_file("test.log");
|
|
|
|
/// let options = FileLogOptions {
|
|
|
|
/// to_stdout: true,
|
|
|
|
/// exclusive: true,
|
|
|
|
/// ..Default::default()
|
|
|
|
/// };
|
|
|
|
/// let mut log = FileLogger::new("test.log", options).unwrap();
|
|
|
|
/// flog!(log, "A simple log: {}", "Hello!");
|
|
|
|
/// # std::fs::remove_file("test.log");
|
|
|
|
/// ```
|
2019-03-01 08:34:29 +00:00
|
|
|
pub struct FileLogger {
|
|
|
|
file: std::fs::File,
|
2020-11-02 07:51:24 +00:00
|
|
|
file_name: std::path::PathBuf,
|
2020-10-15 15:49:18 +00:00
|
|
|
options: FileLogOptions,
|
2019-03-01 08:34:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 06:51:23 +00:00
|
|
|
/// Log messages to [FileLogger] - ``println`` like macro
|
2019-03-01 08:34:29 +00:00
|
|
|
#[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> {
|
2020-11-02 07:51:24 +00:00
|
|
|
let file = Self::open(&file_name, &options)?;
|
|
|
|
|
|
|
|
let file_name: std::path::PathBuf = file_name.as_ref().to_path_buf();
|
|
|
|
|
2022-04-06 14:55:39 +00:00
|
|
|
Ok(Self {
|
|
|
|
file,
|
|
|
|
file_name,
|
|
|
|
options,
|
|
|
|
})
|
2020-11-02 07:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reopen(&mut self) -> Result<&Self, Error> {
|
|
|
|
let file = Self::open(&self.file_name, &self.options)?;
|
|
|
|
self.file = file;
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn open<P: AsRef<std::path::Path>>(
|
|
|
|
file_name: P,
|
|
|
|
options: &FileLogOptions,
|
|
|
|
) -> Result<std::fs::File, Error> {
|
2021-09-21 05:58:40 +00:00
|
|
|
let mut flags = OFlag::O_CLOEXEC;
|
|
|
|
|
2022-04-06 14:55:39 +00:00
|
|
|
if options.read {
|
|
|
|
flags |= OFlag::O_RDWR;
|
2021-09-21 05:58:40 +00:00
|
|
|
} else {
|
2022-04-06 14:55:39 +00:00
|
|
|
flags |= OFlag::O_WRONLY;
|
2021-09-21 05:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.append {
|
2022-04-06 14:55:39 +00:00
|
|
|
flags |= OFlag::O_APPEND;
|
2020-10-19 08:35:54 +00:00
|
|
|
}
|
2021-09-21 05:58:40 +00:00
|
|
|
if options.exclusive {
|
2022-04-06 14:55:39 +00:00
|
|
|
flags |= OFlag::O_EXCL;
|
2021-09-21 05:58:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 14:55:39 +00:00
|
|
|
let file =
|
|
|
|
atomic_open_or_create_file(&file_name, flags, &[], options.file_opts.clone(), false)?;
|
2019-03-01 08:34:29 +00:00
|
|
|
|
2020-11-02 07:51:24 +00:00
|
|
|
Ok(file)
|
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-10-15 15:49:18 +00:00
|
|
|
let line = if self.options.prefix_time {
|
2021-10-08 09:19:37 +00:00
|
|
|
let now = proxmox_time::epoch_i64();
|
|
|
|
let rfc3339 = match proxmox_time::epoch_to_rfc3339(now) {
|
2020-11-02 18:26:59 +00:00
|
|
|
Ok(rfc3339) => rfc3339,
|
|
|
|
Err(_) => "1970-01-01T00:00:00Z".into(), // for safety, should really not happen!
|
|
|
|
};
|
2020-10-15 15:49:18 +00:00
|
|
|
format!("{}: {}\n", rfc3339, msg)
|
|
|
|
} else {
|
|
|
|
format!("{}\n", msg)
|
|
|
|
};
|
2020-11-02 18:26:59 +00:00
|
|
|
if let Err(err) = self.file.write_all(line.as_bytes()) {
|
|
|
|
// avoid panicking, log methods should not do that
|
|
|
|
// FIXME: or, return result???
|
|
|
|
eprintln!("error writing to log file - {}", err);
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}
|