diff --git a/README.md b/README.md index 9773cd13..4a7bf501 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ The `sys` publisher is not a real node ID, but it's also not a special case valu - CTRL+J to toggle debug mode - CTRL+S to step through events in debug mode -- CTRL+L to toggle logging mode, which writes all terminal output to the `.terminal_log` file. Off by default, this will write all events and verbose prints with timestamps. +- CTRL+L to toggle logging mode, which writes all terminal output to the `.terminal_log` file. On by default, this will write all events and verbose prints with timestamps. - CTRL+A to jump to beginning of input - CTRL+E to jump to end of input diff --git a/kinode/src/main.rs b/kinode/src/main.rs index cade9f5d..61619479 100644 --- a/kinode/src/main.rs +++ b/kinode/src/main.rs @@ -79,6 +79,8 @@ async fn main() { // logging mode is toggled at runtime by CTRL+L let is_logging = !*matches.get_one::("logging-off").unwrap(); + let max_log_size = matches.get_one::("max-log-size"); + let number_log_files = matches.get_one::("number-log-files"); // detached determines whether terminal is interactive let detached = *matches.get_one::("detached").unwrap(); @@ -427,6 +429,8 @@ async fn main() { detached, verbose_mode, is_logging, + max_log_size.copied(), + number_log_files.copied(), ) => { match quit { Ok(()) => { @@ -666,7 +670,15 @@ fn build_command() -> Command { .action(clap::ArgAction::SetTrue), ) .arg(arg!(--rpc "Add a WebSockets RPC URL at boot")) - .arg(arg!(--password "Node password (in double quotes)")); + .arg(arg!(--password "Node password (in double quotes)")) + .arg( + arg!(--"max-log-size" "Max size of all logs in bytes; setting to 0 -> no size limit (default 16MB)") + .value_parser(value_parser!(u64)), + ) + .arg( + arg!(--"number-log-files" "Number of logs to rotate (default 4)") + .value_parser(value_parser!(u64)), + ); #[cfg(feature = "simulation-mode")] let app = app diff --git a/kinode/src/terminal/mod.rs b/kinode/src/terminal/mod.rs index ce943452..382888be 100644 --- a/kinode/src/terminal/mod.rs +++ b/kinode/src/terminal/mod.rs @@ -182,6 +182,8 @@ pub async fn terminal( is_detached: bool, verbose_mode: u8, is_logging: bool, + max_log_size: Option, + number_log_files: Option, ) -> anyhow::Result<()> { let (stdout, _maybe_raw_mode) = utils::splash(&our, version, is_detached)?; @@ -214,11 +216,11 @@ pub async fn terminal( // if CTRL+L is used to turn on logging, all prints to terminal // will also be written with their full timestamp to the .terminal_log file. - // logging mode is always off by default. TODO add a boot flag to change this. + // logging mode is always on by default let log_dir_path = std::fs::canonicalize(&home_directory_path) .expect("terminal: could not get path for .terminal_logs dir") .join(".terminal_logs"); - let logger = utils::Logger::new(log_dir_path); + let logger = utils::Logger::new(log_dir_path, max_log_size, number_log_files); let mut state = State { stdout, diff --git a/kinode/src/terminal/utils.rs b/kinode/src/terminal/utils.rs index d2a2d0ce..f234d517 100644 --- a/kinode/src/terminal/utils.rs +++ b/kinode/src/terminal/utils.rs @@ -351,25 +351,37 @@ pub enum LoggerStrategy { } impl LoggerStrategy { - fn default() -> Self { - LoggerStrategy::Rotating { - max_log_dir_bytes: DEFAULT_MAX_LOGS_BYTES, - number_log_files: DEFAULT_NUMBER_LOG_FILES, + fn new(max_log_size: Option, number_log_files: Option) -> Self { + let max_log_size = max_log_size.unwrap_or_else(|| DEFAULT_MAX_LOGS_BYTES); + let number_log_files = number_log_files.unwrap_or_else(|| DEFAULT_NUMBER_LOG_FILES); + if max_log_size == 0 { + LoggerStrategy::Infinite + } else { + LoggerStrategy::Rotating { + max_log_dir_bytes: max_log_size, + number_log_files, + } } } } impl Logger { - pub fn new(log_dir_path: PathBuf) -> Self { + pub fn new( + log_dir_path: PathBuf, + max_log_size: Option, + number_log_files: Option, + ) -> Self { let log_writer = make_log_writer(&log_dir_path).unwrap(); Self { log_dir_path, log_writer, - strategy: LoggerStrategy::default(), + strategy: LoggerStrategy::new(max_log_size, number_log_files), } } pub fn write(&mut self, line: &str) -> anyhow::Result<()> { + let now = chrono::Local::now(); + let line = &format!("[{}] {}", now.to_rfc2822(), line); match self.strategy { LoggerStrategy::Infinite => {} LoggerStrategy::Rotating { @@ -389,8 +401,7 @@ impl Logger { } } - let now = chrono::Local::now(); - writeln!(self.log_writer, "[{}] {}", now.to_rfc2822(), line)?; + writeln!(self.log_writer, "{}", line)?; Ok(()) }