Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli: Add log rotation to `node start`
Merged did:key:z6MksFqX...wzpT opened 2 years ago

Very simple log rotation with two files.

1 file changed +23 -6 1ece339e 71bba29c
modified 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<File> {
+
    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)
+
}