logging: add args at runtime start to change logging params

This commit is contained in:
hosted-fornet 2024-09-25 10:58:40 -07:00
parent a2e26879ab
commit 52e90d599f
4 changed files with 37 additions and 12 deletions

View File

@ -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+J to toggle debug mode
- CTRL+S to step through events in 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+A to jump to beginning of input
- CTRL+E to jump to end of input - CTRL+E to jump to end of input

View File

@ -79,6 +79,8 @@ async fn main() {
// logging mode is toggled at runtime by CTRL+L // logging mode is toggled at runtime by CTRL+L
let is_logging = !*matches.get_one::<bool>("logging-off").unwrap(); let is_logging = !*matches.get_one::<bool>("logging-off").unwrap();
let max_log_size = matches.get_one::<u64>("max-log-size");
let number_log_files = matches.get_one::<u64>("number-log-files");
// detached determines whether terminal is interactive // detached determines whether terminal is interactive
let detached = *matches.get_one::<bool>("detached").unwrap(); let detached = *matches.get_one::<bool>("detached").unwrap();
@ -427,6 +429,8 @@ async fn main() {
detached, detached,
verbose_mode, verbose_mode,
is_logging, is_logging,
max_log_size.copied(),
number_log_files.copied(),
) => { ) => {
match quit { match quit {
Ok(()) => { Ok(()) => {
@ -666,7 +670,15 @@ fn build_command() -> Command {
.action(clap::ArgAction::SetTrue), .action(clap::ArgAction::SetTrue),
) )
.arg(arg!(--rpc <RPC> "Add a WebSockets RPC URL at boot")) .arg(arg!(--rpc <RPC> "Add a WebSockets RPC URL at boot"))
.arg(arg!(--password <PASSWORD> "Node password (in double quotes)")); .arg(arg!(--password <PASSWORD> "Node password (in double quotes)"))
.arg(
arg!(--"max-log-size" <MAX_LOG_SIZE_BYTES> "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_LOG_FILES> "Number of logs to rotate (default 4)")
.value_parser(value_parser!(u64)),
);
#[cfg(feature = "simulation-mode")] #[cfg(feature = "simulation-mode")]
let app = app let app = app

View File

@ -182,6 +182,8 @@ pub async fn terminal(
is_detached: bool, is_detached: bool,
verbose_mode: u8, verbose_mode: u8,
is_logging: bool, is_logging: bool,
max_log_size: Option<u64>,
number_log_files: Option<u64>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let (stdout, _maybe_raw_mode) = utils::splash(&our, version, is_detached)?; 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 // 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. // 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) let log_dir_path = std::fs::canonicalize(&home_directory_path)
.expect("terminal: could not get path for .terminal_logs dir") .expect("terminal: could not get path for .terminal_logs dir")
.join(".terminal_logs"); .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 { let mut state = State {
stdout, stdout,

View File

@ -351,25 +351,37 @@ pub enum LoggerStrategy {
} }
impl LoggerStrategy { impl LoggerStrategy {
fn default() -> Self { fn new(max_log_size: Option<u64>, number_log_files: Option<u64>) -> 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 { LoggerStrategy::Rotating {
max_log_dir_bytes: DEFAULT_MAX_LOGS_BYTES, max_log_dir_bytes: max_log_size,
number_log_files: DEFAULT_NUMBER_LOG_FILES, number_log_files,
}
} }
} }
} }
impl Logger { impl Logger {
pub fn new(log_dir_path: PathBuf) -> Self { pub fn new(
log_dir_path: PathBuf,
max_log_size: Option<u64>,
number_log_files: Option<u64>,
) -> Self {
let log_writer = make_log_writer(&log_dir_path).unwrap(); let log_writer = make_log_writer(&log_dir_path).unwrap();
Self { Self {
log_dir_path, log_dir_path,
log_writer, log_writer,
strategy: LoggerStrategy::default(), strategy: LoggerStrategy::new(max_log_size, number_log_files),
} }
} }
pub fn write(&mut self, line: &str) -> anyhow::Result<()> { pub fn write(&mut self, line: &str) -> anyhow::Result<()> {
let now = chrono::Local::now();
let line = &format!("[{}] {}", now.to_rfc2822(), line);
match self.strategy { match self.strategy {
LoggerStrategy::Infinite => {} LoggerStrategy::Infinite => {}
LoggerStrategy::Rotating { LoggerStrategy::Rotating {
@ -389,8 +401,7 @@ impl Logger {
} }
} }
let now = chrono::Local::now(); writeln!(self.log_writer, "{}", line)?;
writeln!(self.log_writer, "[{}] {}", now.to_rfc2822(), line)?;
Ok(()) Ok(())
} }