Skip to content

Commit

Permalink
Added argument for piping log to file
Browse files Browse the repository at this point in the history
  • Loading branch information
JacoMalan1 committed May 12, 2024
1 parent ed484c4 commit 8222234
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
*.log
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "strain"
version = "0.0.5"
version = "0.0.6"
edition = "2021"
description = "A CPU stressing utility written in Rust"
license = "GPL-3.0-or-later"
Expand Down
6 changes: 5 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str::FromStr;
use std::{path::PathBuf, str::FromStr};

use clap::Parser;

Expand Down Expand Up @@ -38,6 +38,10 @@ pub struct StrainArgs {
/// Mandelbrot set maximum iterations
#[arg(short = 'I', long, default_value_t = 1_000_000)]
pub mandelbrot_iterations: usize,

/// Write logs to file
#[arg(short = 'L', long)]
pub log_file: Option<PathBuf>,
}

#[derive(Debug, Copy, Clone)]
Expand Down
32 changes: 22 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ use crate::{
};
use clap::Parser;
use log::LevelFilter;
use std::{io::Write, str::FromStr, time::SystemTime};
use std::{io::Write, path::PathBuf, str::FromStr, time::SystemTime};

pub mod args;
pub mod lucas_lehmer;
pub mod mandelbrot;
pub mod rsa;
pub mod stress;

fn setup_logger(log_level: log::LevelFilter) -> Result<(), fern::InitError> {
fern::Dispatch::new()
fn setup_logger(
log_level: log::LevelFilter,
log_file: Option<PathBuf>,
) -> Result<(), fern::InitError> {
let mut dispatch = fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"[{} {} {}] {}",
Expand All @@ -27,18 +30,27 @@ fn setup_logger(log_level: log::LevelFilter) -> Result<(), fern::InitError> {
))
})
.level(log_level)
.chain(std::io::stdout())
.apply()?;
.chain(std::io::stdout());

if let Some(path) = log_file {
let file = std::fs::File::create(path).expect("Failed to create log file");
dispatch = dispatch.chain(file);
}

dispatch.apply()?;
Ok(())
}

fn main() {
let args = StrainArgs::parse();
setup_logger(if args.debug {
LevelFilter::Debug
} else {
LevelFilter::Info
})
setup_logger(
if args.debug {
LevelFilter::Debug
} else {
LevelFilter::Info
},
args.log_file,
)
.expect("Failed to setup logger");

if args.list_strategies {
Expand Down

0 comments on commit 8222234

Please sign in to comment.