Skip to content

Commit

Permalink
Merge pull request #83 from jabibamman/chore/logger-cli-enhanced
Browse files Browse the repository at this point in the history
Chore: Update logging levels based on flags
  • Loading branch information
jabibamman authored Feb 24, 2024
2 parents 8119e8d + b10e906 commit 90fefb1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
7 changes: 5 additions & 2 deletions cli/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ mod operation_tests {
CliServerArgs {
hostname: "127.0.0.1".to_string(),
port: 8787,
verbose: false,
debug: false,
verbose: 0,
debug: 0,
trace: 0,
width: 1,
height: 1,
}
Expand All @@ -22,6 +23,7 @@ mod operation_tests {
worker_name: "worker".to_string(),
verbose: args.verbose.clone(),
debug: args.debug.clone(),
trace: args.trace.clone(),
open: false,
save: false,
};
Expand All @@ -38,6 +40,7 @@ mod operation_tests {
port: args.port.clone(),
verbose: args.verbose.clone(),
debug: args.debug.clone(),
trace: args.trace.clone(),
width: 1,
height: 1,
};
Expand Down
34 changes: 22 additions & 12 deletions cli/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ pub struct CliClientArgs {
pub worker_name: String,

/// Optional: Add a flag to enable/disable logging.
/// Default: false
#[clap(short = 'v', long = "verbose", default_value = "false")]
pub verbose: bool,
/// Default: 0
#[clap(short = 'v', long = "verbose", action = clap::ArgAction::Count)]
pub verbose: u8,

/// Optional: Add a flag to enable/disable debug mode.
/// Default: false
#[clap(short = 'd', long = "debug", default_value = "false")]
pub debug: bool,
/// Default: 0
#[clap(short = 'd', long = "debug", action = clap::ArgAction::Count)]
pub debug: u8,

/// Optional: Add a flag to enable/disable trace mode.
/// Default: 0
#[clap(short = 't', long = "trace", action = clap::ArgAction::Count)]
pub trace: u8,

/// Optional: Add a flag to enable/disable opening the browser.
/// Default: false
Expand Down Expand Up @@ -84,14 +89,19 @@ pub struct CliServerArgs {
pub port: u16,

/// Optional: Add a flag to enable/disable logging.
/// Default: false
#[clap(short = 'v', long = "verbose", default_value = "false")]
pub verbose: bool,
/// Default: 0
#[clap(short = 'v', long = "verbose", action = clap::ArgAction::Count)]
pub verbose: u8,

/// Optional: Add a flag to enable/disable debug mode.
/// Default: false
#[clap(short = 'd', long = "debug", default_value = "false")]
pub debug: bool,
/// Default: 0
#[clap(short = 'd', long = "debug", action = clap::ArgAction::Count)]
pub debug: u8,

/// Optional: Add a flag to enable/disable trace mode.
/// Default: 0
#[clap(short = 't', long = "trace", action = clap::ArgAction::Count)]
pub trace: u8,

/// Optional: Add a flag to edit the width and height of the window.
/// Default: 1200
Expand Down
6 changes: 4 additions & 2 deletions client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::{error, info};
use log::{error, info, trace};
use networking::{connect_to_server, process_fragment_task, receive_fragment_task, send_request};
use shared::{types::error::FractalError, utils::fragment_request_impl::FragmentRequestOperation};

Expand All @@ -11,17 +11,19 @@ use shared::types::messages::FragmentRequest;

fn main() -> Result<(), FractalError> {
let cli_args: CliClientArgs = CliClientArgs::parse();
shared::logger::init_logger(cli_args.verbose, cli_args.debug)?;
shared::logger::init_logger(cli_args.verbose, cli_args.debug, cli_args.trace)?;

let serialized_request =
FragmentRequest::new(cli_args.worker_name.clone(), 1000).serialize()?;

let mut stream = connect_to_server(&cli_args)?;
send_request(&mut stream, &serialized_request)?;
log::trace!("Sent request to server");

loop {
match receive_fragment_task(&mut stream) {
Ok(Some((fragment_task, data))) => {
trace!("Received fragment task: {:?}", fragment_task);
stream = process_fragment_task(fragment_task, data, &cli_args)?;
}
Ok(None) => {
Expand Down
6 changes: 3 additions & 3 deletions client/src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use cli::parser::CliClientArgs;
use image::{ImageBuffer, Rgb};
use log::{debug, error, info};
use log::{debug, error, info, trace};
use server::services::{
reader::get_response,
write::{write, write_img},
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn process_fragment_task(
get_file_path("julia", dir_path_buf, get_extension_str(FileExtension::PNG))?;
let (img, pixel_data_bytes, pixel_intensity_matrice) = generate_fractal_set(task.clone())?;

debug!("Pixel data bytes: {:?}", pixel_data_bytes);
trace!("Pixel data bytes: {:?}", pixel_data_bytes);

let pixel_data = convert_to_pixel_data(pixel_data_bytes.clone(), task.clone());

Expand All @@ -148,7 +148,7 @@ pub fn process_fragment_task(
vec_data.extend(pixel_intensity_matrice[i].count.to_be_bytes());
}

debug!("Vec data: {:?}", vec_data);
trace!("Vec data: {:?}", vec_data);

if cli_args.save {
save_fractal_image(img.clone(), &img_path)?;
Expand Down
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use shared::types::error::FractalError;

fn main() -> Result<(), FractalError> {
let cli_args: CliServerArgs = CliServerArgs::parse();
shared::logger::init_logger(cli_args.verbose, cli_args.debug)?;
shared::logger::init_logger(cli_args.verbose, cli_args.debug, cli_args.trace)?;
env::set_var("RESOLUTION_WIDTH", cli_args.width.to_string());
env::set_var("RESOLUTION_HEIGHT", cli_args.height.to_string());

Expand Down
19 changes: 9 additions & 10 deletions shared/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ use crate::types::error::FractalError;
use env_logger::{Builder, Env};
use log::LevelFilter;

pub fn init_logger(verbose: bool, debug: bool) -> Result<(), FractalError> {
pub fn init_logger(verbose: u8, debug: u8, trace: u8) -> Result<(), FractalError> {
let log_level = match (verbose, debug, trace) {
(0, 0, 0) => LevelFilter::Error, // default
(_, 1, 0) => LevelFilter::Debug, // -d
(1, _, _) => LevelFilter::Info, // -v
(_, _, 1) => LevelFilter::Trace, // -t
_ => LevelFilter::Warn, // multiple flags
};

let mut builder = Builder::from_env(Env::default());

builder.format_timestamp(Some(env_logger::TimestampPrecision::Millis));

let log_level = if debug {
LevelFilter::Debug
} else if verbose {
LevelFilter::Info
} else {
LevelFilter::Error
};

builder.filter_level(log_level);
builder.init();

Expand Down

0 comments on commit 90fefb1

Please sign in to comment.