Skip to content

Commit

Permalink
cli: add log file (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse authored Apr 16, 2024
1 parent e2ace48 commit 3efbdb9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
67 changes: 37 additions & 30 deletions cli/Cargo.lock

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

9 changes: 7 additions & 2 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ cargo run
To specify a custom data directory, use

```bash
cargo run -- --data_dir temp-dir
cargo run -- --data-dir temp-dir
```

To set a custom log level, use

```bash
RUST_LOG=info cargo run
RUST_LOG=info|debug|warn cargo run
```

To specify a file to pipe logs to, use
```bash
RUST_LOG=info|debug|warn cargo run -- --log-file /tmp/log
```
20 changes: 16 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod commands;
mod persist;

use std::{fs, path::PathBuf};
use std::{
fs::{self, File},
path::PathBuf,
};

use anyhow::{anyhow, Result};
use clap::Parser;
Expand All @@ -13,8 +16,11 @@ use rustyline::{error::ReadlineError, hint::HistoryHinter, Editor};

#[derive(Parser, Debug)]
pub(crate) struct Args {
#[clap(name = "data_dir", short = 'd', long = "data_dir")]
#[clap(short, long)]
pub(crate) data_dir: Option<String>,

#[clap(short, long)]
pub(crate) log_file: Option<String>,
}

fn show_results(result: Result<String>) -> Result<()> {
Expand All @@ -30,9 +36,15 @@ fn show_results(result: Result<String>) -> Result<()> {
}

fn main() -> Result<()> {
env_logger::init();

let args = Args::parse();

env_logger::builder()
.target(match args.log_file {
Some(log_file) => env_logger::Target::Pipe(Box::new(File::create(log_file)?)),
None => env_logger::Target::Stdout,
})
.init();

let data_dir_str = args.data_dir.clone().unwrap_or(".data".to_string());
let data_dir = PathBuf::from(&data_dir_str);
fs::create_dir_all(&data_dir)?;
Expand Down

0 comments on commit 3efbdb9

Please sign in to comment.