Skip to content

Commit

Permalink
Merge branch 'logging-improvements' into draft
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelMarks committed Jul 26, 2024
2 parents 8ec041b + 9923b46 commit 809280d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
13 changes: 9 additions & 4 deletions src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ pub(crate) static DEFAULT_LOG_LEVEL: &str = "info";
pub(crate) static DEFAULT_TASK_NAME: &str = "default";
pub(crate) static DEFAULT_OUTPUT_FORMAT: &str = "default";

pub fn run(cli_args: &CliArgs, global_config: &GlobalConfig) -> Result<(), CargoMakeError> {
pub fn run(
cli_args: &CliArgs,
global_config: &GlobalConfig,
logger_options: Option<LoggerOptions>,
) -> Result<(), CargoMakeError> {
let start_time = SystemTime::now();

recursion_level::increment();

logger::init(&LoggerOptions {
logger::init(&logger_options.unwrap_or(LoggerOptions {
name: String::from(env!("CARGO_PKG_NAME")),
level: cli_args.log_level.clone(),
color: !cli_args.disable_color,
});
}));

if recursion_level::is_top() {
info!("{} {}", &cli_args.command, &VERSION);
Expand Down Expand Up @@ -173,6 +178,6 @@ pub fn run_cli(command_name: String, sub_command: bool) -> Result<CliArgs, Cargo

let cli_args = cli_parser::parse(&global_config, &command_name, sub_command)?;

run(&cli_args, &global_config)?;
run(&cli_args, &global_config, None)?;
Ok(cli_args)
}
36 changes: 23 additions & 13 deletions src/lib/cli_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn run_makefile_not_found() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -77,6 +78,7 @@ fn run_empty_task() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -115,6 +117,7 @@ fn print_empty_task() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -153,6 +156,7 @@ fn list_empty_task() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -191,6 +195,7 @@ fn run_file_and_task() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -232,6 +237,7 @@ fn run_cwd_with_file() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -271,6 +277,7 @@ fn run_file_not_go_to_project_root() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -310,6 +317,7 @@ fn run_cwd_go_to_project_root_current_dir() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -352,6 +360,7 @@ fn run_cwd_go_to_project_root_child_dir() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -394,6 +403,7 @@ fn run_cwd_task_not_found() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand All @@ -414,7 +424,7 @@ fn run_bad_subcommand() {
"ParserError { error: InvalidCommandLine(\"Command does not match spec, command line: [\\\"bad\\\"]\") }"
);

// run(&cli_args.unwrap(), &global_config).unwrap();
// run(&cli_args.unwrap(), &global_config, None).unwrap();
}

#[test]
Expand All @@ -441,7 +451,7 @@ fn run_valid() {
)
.unwrap();

assert_eq!(run(&cli_args, &global_config).unwrap(), ());
assert_eq!(run(&cli_args, &global_config, None).unwrap(), ());
}

#[test]
Expand All @@ -460,7 +470,7 @@ fn run_with_global_config() {
)
.unwrap();

run(&cli_args, &global_config).unwrap();
run(&cli_args, &global_config, None).unwrap();
}

#[test]
Expand All @@ -486,7 +496,7 @@ fn run_log_level_override() {
)
.unwrap();

run(&cli_args, &global_config).unwrap();
run(&cli_args, &global_config, None).unwrap();
}

#[test]
Expand Down Expand Up @@ -519,7 +529,7 @@ fn run_set_env_values() {
envmnt::set("ENV2_TEST", "EMPTY");
envmnt::set("ENV3_TEST", "EMPTY");

run(&cli_args, &global_config).unwrap();
run(&cli_args, &global_config, None).unwrap();

assert_eq!(envmnt::get_or_panic("ENV1_TEST"), "TEST1");
assert_eq!(envmnt::get_or_panic("ENV2_TEST"), "TEST2a=TEST2b");
Expand Down Expand Up @@ -550,7 +560,7 @@ fn run_set_env_via_file() {
envmnt::set("ENV2_TEST", "EMPTY");
envmnt::set("ENV3_TEST", "EMPTY");

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();

assert_eq!(envmnt::get_or_panic("ENV1_TEST"), "TEST1");
assert_eq!(envmnt::get_or_panic("ENV2_TEST"), "TEST2");
Expand Down Expand Up @@ -590,7 +600,7 @@ fn run_set_env_both() {
envmnt::set("ENV5_TEST", "EMPTY");
envmnt::set("ENV6_TEST", "EMPTY");

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();

assert_eq!(envmnt::get_or_panic("ENV1_TEST"), "TEST1");
assert_eq!(envmnt::get_or_panic("ENV2_TEST"), "TEST2");
Expand Down Expand Up @@ -627,7 +637,7 @@ fn run_print_only() {
create_cli(&global_config, CliSpec::new(), true),
);

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();
}

#[test]
Expand All @@ -653,7 +663,7 @@ fn run_diff_steps() {
create_cli(&global_config, CliSpec::new(), true),
);

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();
}

#[test]
Expand All @@ -674,7 +684,7 @@ fn run_protected_flow_example() {
create_cli(&global_config, CliSpec::new(), true),
);

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();
}

#[test]
Expand All @@ -696,7 +706,7 @@ fn run_no_task_args() {

envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();

assert_eq!(envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"), "");
}
Expand All @@ -723,7 +733,7 @@ fn run_set_task_args() {

envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();

assert_eq!(
envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"),
Expand All @@ -748,7 +758,7 @@ fn run_set_task_var_args() {

envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");

run(&cli_args, &global_config).unwrap();
run(&cli_args, &global_config, None).unwrap();

assert_eq!(
envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"),
Expand Down
21 changes: 10 additions & 11 deletions src/lib/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ pub(crate) enum LogLevel {
}

/// The logger options used to initialize the logger
pub(crate) struct LoggerOptions {
pub struct LoggerOptions {
/// Name to prefix each log message: "[name] %s"
pub name: String,
/// The logger level name (verbose, info, error, off)
pub(crate) level: String,
pub level: String,
/// True to printout colorful output
pub(crate) color: bool,
pub color: bool,
}

pub(crate) fn get_level(level_name: &str) -> LogLevel {
Expand All @@ -42,17 +44,16 @@ pub(crate) fn get_level(level_name: &str) -> LogLevel {

/// Returns the current logger level name
pub(crate) fn get_log_level() -> String {
let level = if envmnt::is_equal("CARGO_MAKE_LOG_LEVEL", "off") {
if envmnt::is_equal("CARGO_MAKE_LOG_LEVEL", "off") {
"off"
} else if log_enabled!(Level::Debug) {
"verbose"
} else if log_enabled!(Level::Info) {
"info"
} else {
"error"
};

level.to_string()
}
.to_string()
}

fn get_name_for_filter(filter: &LevelFilter) -> String {
Expand Down Expand Up @@ -139,10 +140,10 @@ pub(crate) fn init(options: &LoggerOptions) {
format!("[{}]", recursion_lvl)
};

let name_fmt = get_formatted_name(&options.name, color);

let result = fern::Dispatch::new()
.format(move |out, message, record| {
let name = env!("CARGO_PKG_NAME");

let record_level = record.level();

if cfg!(test) {
Expand All @@ -151,8 +152,6 @@ pub(crate) fn init(options: &LoggerOptions) {
}
}

let name_fmt = get_formatted_name(&name, color);

let record_level_fmt = get_formatted_log_level(&record_level, color);

out.finish(format_args!(
Expand Down
2 changes: 2 additions & 0 deletions src/lib/logger_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn get_formatted_log_level_error_no_color() {
#[should_panic]
fn create_error() {
init(&LoggerOptions {
name: String::from(env!("CARGO_PKG_NAME")),
level: "error".to_string(),
color: false,
});
Expand All @@ -183,6 +184,7 @@ fn update_disable_color_env_var() {
envmnt::remove("CARGO_MAKE_DISABLE_COLOR");

init(&LoggerOptions {
name: String::from(env!("CARGO_PKG_NAME")),
level: "info".to_string(),
color: false,
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod functions;
mod installer;
mod io;
mod legacy;
mod logger;
pub mod logger;
mod plugin;
mod profile;
mod proxy_task;
Expand Down
1 change: 1 addition & 0 deletions src/lib/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::path::PathBuf;

pub(crate) fn on_test_startup() {
logger::init(&LoggerOptions {
name: String::from(env!("CARGO_PKG_NAME")),
level: "error".to_string(),
color: true,
});
Expand Down

0 comments on commit 809280d

Please sign in to comment.