From 71bba29c9a164e23c102dda5cc19834e79fa840a Mon Sep 17 00:00:00 2001 From: cloudhead Date: Wed, 31 Jan 2024 14:23:43 +0100 Subject: [PATCH] cli: Add log rotation to `node start` Very simple log rotation with two files. --- radicle-cli/src/commands/node/control.rs | 29 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/radicle-cli/src/commands/node/control.rs b/radicle-cli/src/commands/node/control.rs index ef6c9941..b24addd0 100644 --- a/radicle-cli/src/commands/node/control.rs +++ b/radicle-cli/src/commands/node/control.rs @@ -1,7 +1,7 @@ use std::ffi::OsString; use std::fs::{File, OpenOptions}; use std::io::{BufRead, BufReader, Read, Seek, SeekFrom}; -use std::{path::Path, process, thread, time}; +use std::{fs, io, path::Path, process, thread, time}; use localtime::LocalTime; @@ -15,6 +15,10 @@ use crate::terminal::Element as _; /// How long to wait for the node to start before returning an error. pub const NODE_START_TIMEOUT: time::Duration = time::Duration::from_secs(6); +/// Node log file name. +pub const NODE_LOG: &str = "node.log"; +/// Node log old file name, after rotation. +pub const NODE_LOG_OLD: &str = "node.log.old"; pub fn start( node: Node, @@ -50,11 +54,7 @@ pub fn start( options.push(OsString::from("--force")); } if daemon { - let log = OpenOptions::new() - .append(true) - .create(true) - .open(profile.home.node().join("node.log"))?; - + let log = log_rotate(profile)?; let child = process::Command::new(cmd) .args(options) .envs(envs) @@ -267,3 +267,20 @@ pub fn config(node: &Node) -> anyhow::Result<()> { Ok(()) } + +fn log_rotate(profile: &Profile) -> io::Result { + let base = profile.home.node(); + if base.join(NODE_LOG).exists() { + // Let this fail, eg. if the file doesn't exist. + fs::remove_file(base.join(NODE_LOG_OLD)).ok(); + fs::rename(base.join(NODE_LOG), base.join(NODE_LOG_OLD))?; + } + + let log = OpenOptions::new() + .append(false) + .truncate(true) + .create(true) + .open(base.join(NODE_LOG))?; + + Ok(log) +}