Skip to content

Commit

Permalink
feat(aot): log rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Mar 21, 2024
1 parent 904b8ca commit 1072681
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 25 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tarpc = { version = "0.34", features = ["tokio1", "serde1"] }
thiserror = "1.0"
tokio = { version = "1", features = ["full", "macros"] }
tracing = "0.1"
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
nix = { version = "0.28", features = ["process"] }

Expand Down
1 change: 1 addition & 0 deletions crates/aot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ snarkvm.workspace = true
snot-common.workspace = true
tokio.workspace = true
tracing-flame = "0.2.0"
tracing-appender.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
crossterm = { version = "0.27", optional = true }
Expand Down
41 changes: 24 additions & 17 deletions crates/aot/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use anyhow::Result;
use clap::Parser;
use crossterm::tty::IsTty;
use std::{
fs::File,
io::{self, BufWriter},
path::PathBuf,
};

use anyhow::Result;
use clap::Parser;
use crossterm::tty::IsTty;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_flame::FlushGuard;
use tracing_subscriber::{layer::SubscriberExt, Layer};

Expand Down Expand Up @@ -48,7 +50,7 @@ impl Cli {
/// 5 => info, debug, trace, snarkos_node_router=trace
/// 6 => info, debug, trace, snarkos_node_tcp=trace
/// ```
pub fn init_logger(&self) -> Option<FlushGuard<BufWriter<File>>> {
pub fn init_logger(&self) -> (Option<FlushGuard<BufWriter<File>>>, Vec<WorkerGuard>) {
let verbosity = self.verbosity;

match verbosity {
Expand Down Expand Up @@ -101,6 +103,7 @@ impl Cli {
});

let mut layers = vec![];
let mut guards = vec![];

let guard = if self.enable_profiling {
let (flame_layer, guard) =
Expand All @@ -111,7 +114,7 @@ impl Cli {
None
};

if let Some(logfile) = &self.log {
if let Some(logfile) = self.log.as_ref() {
// Create the directories tree for a logfile if it doesn't exist.
let logfile_dir = logfile
.parent()
Expand All @@ -120,19 +123,16 @@ impl Cli {
std::fs::create_dir_all(logfile_dir)
.expect("Failed to create a directories: '{logfile_dir}', please check if user has permissions");
}
// Create a file to write logs to.
// TODO: log rotation
let logfile = File::options()
.append(true)
.create(true)
.open(logfile)
.expect("Failed to open the file for writing logs");

let file_appender = tracing_appender::rolling::daily(&logfile_dir, logfile);
let (non_blocking, file_guard) = tracing_appender::non_blocking(file_appender);
guards.push(file_guard);

// Add layer redirecting logs to the file
layers.push(
tracing_subscriber::fmt::Layer::default()
.with_ansi(false)
.with_writer(logfile)
.with_writer(non_blocking)
.with_filter(filter2)
.boxed(),
);
Expand All @@ -141,27 +141,34 @@ impl Cli {
// Initialize tracing.
// Add layer using LogWriter for stdout / terminal
if matches!(self.command, Command::Run(_)) {
let (stdout, g) = tracing_appender::non_blocking(io::stdout());
guards.push(g);

layers.push(
tracing_subscriber::fmt::Layer::default()
.with_ansi(io::stdout().is_tty())
.with_writer(stdout)
.with_filter(filter)
.boxed(),
);
} else {
let (stderr, g) = tracing_appender::non_blocking(io::stderr());
guards.push(g);
layers.push(
tracing_subscriber::fmt::Layer::default()
.with_writer(io::stderr)
.with_writer(stderr)
.boxed(),
);
}
};

let subscriber = tracing_subscriber::registry::Registry::default().with(layers);

tracing::subscriber::set_global_default(subscriber).unwrap();
guard
(guard, guards)
}

pub fn run(self) -> Result<()> {
self.init_logger();
let _guards = self.init_logger();

match self.command {
Command::Genesis(command) => command.parse(),
Expand Down
5 changes: 0 additions & 5 deletions crates/aot/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ pub struct Runner {
#[clap(long = "private-key")]
pub private_key: PrivateKey,

/// A path to the log file
#[clap(long = "log")]
#[serde_clap_default(PathBuf::from("snarkos.log"))]
pub log: PathBuf,

/// Specify the IP address and port for the node server
#[clap(long = "node")]
#[serde_clap_default(4130)]
Expand Down
6 changes: 3 additions & 3 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ STORAGE="${LEDGER}_${MODE}_${INDEX}"
rm -rf $STORAGE
cp -r $LEDGER $STORAGE


$BINARY run --type $MODE \
echo "Starting $MODE $INDEX" "${TEST_PATH}/${MODE}_${INDEX}.log"
$BINARY --log "${TEST_PATH}/${MODE}_${INDEX}.log" \
run --type $MODE \
--bft "500$INDEX" \
--rest "303$INDEX" \
--node "413$INDEX" \
--genesis $GENESIS \
--ledger $STORAGE \
--log "${TEST_PATH}/${MODE}_${INDEX}.log" \
--private-key $(pk 0) \
--peers "127.0.0.1:4130,127.0.0.1:4131,127.0.0.1:4132,127.0.0.1:4133,127.0.0.1:4134" \
--validators "127.0.0.1:5000,127.0.0.1:5001,127.0.0.1:5002,127.0.0.1:5003,127.0.0.1:5004,127.0.0.1:5005"

0 comments on commit 1072681

Please sign in to comment.