Skip to content

Commit

Permalink
Revamp how logging is done
Browse files Browse the repository at this point in the history
  • Loading branch information
dcvz committed Dec 1, 2023
1 parent d5e10a9 commit 16cd081
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 50 deletions.
2 changes: 2 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ license = "MIT OR Apache-2.0"
[dependencies]
anyhow = "1.0.75"
clap = { version = "4.4.8", features = ["derive"] }
env_logger = "0.10.0"
log = "0.4.17"
serde_json = { version = "1" }
65 changes: 65 additions & 0 deletions xtask/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::io::Write;

/// Initialise and create a `env_logger::Builder` which follows the
/// GitHub Actions logging syntax when running on CI.
pub fn init_logger() -> env_logger::Builder {
let mut builder = env_logger::Builder::from_default_env();
builder.target(env_logger::Target::Stdout);

// Find and setup the correct log level
builder.filter(None, get_log_level());
builder.write_style(env_logger::WriteStyle::Always);

// Custom Formatter for Github Actions
if std::env::var("CI").is_ok() {
builder.format(|buf, record| match record.level().as_str() {
"DEBUG" => writeln!(buf, "::debug:: {}", record.args()),
"WARN" => writeln!(buf, "::warning:: {}", record.args()),
"ERROR" => {
writeln!(buf, "::error:: {}", record.args())
}
_ => writeln!(buf, "{}", record.args()),
});
}

builder
}

/// Determine the LogLevel for the logger
fn get_log_level() -> log::LevelFilter {
// DEBUG
match std::env::var("DEBUG") {
Ok(_value) => return log::LevelFilter::Debug,
Err(_err) => (),
}
// ACTIONS_RUNNER_DEBUG
match std::env::var("ACTIONS_RUNNER_DEBUG") {
Ok(_value) => return log::LevelFilter::Debug,
Err(_err) => (),
};

log::LevelFilter::Info
}

/// Group Macro
#[macro_export]
macro_rules! group {
// group!()
($($arg:tt)*) => {
let title = format!($($arg)*);
if std::env::var("CI").is_ok() {
log!(log::Level::Info, "::group::{}", title)
} else {
log!(log::Level::Info, "{}", title)
}
};
}

/// End Group Macro
#[macro_export]
macro_rules! endgroup {
// endgroup!()
() => {
log!(log::Level::Info, "::endgroup::")
};
}
6 changes: 5 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use clap::{Parser, Subcommand};

mod logging;
mod publish;
mod runchecks;
pub(crate) mod utils;
mod utils;

#[macro_use]
extern crate log;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand Down
59 changes: 31 additions & 28 deletions xtask/src/runchecks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//!
//! It is also used to check that the code is formatted correctly and passes clippy.

use crate::utils::{
end_log_group, get_workspaces, pretty_print_duration, start_log_group, WorkspaceMemberType,
};
use crate::logging::init_logger;
use crate::utils::{format_duration, get_workspaces, WorkspaceMemberType};
use crate::{endgroup, group};
use std::env;
use std::path::Path;
use std::process::{Child, Command, Stdio};
Expand All @@ -33,7 +33,7 @@ fn handle_child_process(mut child: Child, error: &str) {
// Run a command
fn run_command(command: &str, args: &[&str], command_error: &str, child_error: &str) {
// Format command
println!("{command} {}\n\n", args.join(" "));
info!("{command} {}\n\n", args.join(" "));

// Run command as child process
let command = Command::new(command)
Expand All @@ -49,14 +49,14 @@ fn run_command(command: &str, args: &[&str], command_error: &str, child_error: &

// Define and run rustup command
fn rustup(command: &str, target: &str) {
start_log_group(format!("Rustup: {} add {}", command, target));
group!("Rustup: {} add {}", command, target);
run_command(
"rustup",
&[command, "add", target],
"Failed to run rustup",
"Failed to wait for rustup child process",
);
end_log_group();
endgroup!();
}

// Define and run a cargo command
Expand All @@ -72,7 +72,7 @@ fn run_cargo_with_path<P: AsRef<Path>>(
error: &str,
) {
// Print cargo command
println!("\ncargo {} {}\n", command, params);
info!("cargo {} {}\n", command, params);

// Run cargo
let mut cargo = Command::new("cargo");
Expand Down Expand Up @@ -125,13 +125,13 @@ fn cargo_test(params: Params) {

// Run cargo fmt command
fn cargo_fmt() {
start_log_group("Cargo: fmt".to_string());
group!("Cargo: fmt");
run_cargo(
"fmt",
["--check", "--all", "--", "--color=always"].into(),
"Failed to run cargo fmt",
);
end_log_group();
endgroup!();
}

// Run cargo clippy command
Expand All @@ -155,7 +155,7 @@ fn cargo_doc(params: Params) {

// Build and test a crate in a no_std environment
fn build_and_test_no_std<const N: usize>(crate_name: &str, extra_args: [&str; N]) {
start_log_group(format!("Checks: {} (no_std)", crate_name));
group!("Checks: {} (no-std)", crate_name);

// Run cargo build --no-default-features
cargo_build(Params::from(["-p", crate_name, "--no-default-features"]) + extra_args);
Expand Down Expand Up @@ -185,7 +185,7 @@ fn build_and_test_no_std<const N: usize>(crate_name: &str, extra_args: [&str; N]
]) + extra_args,
);

end_log_group();
endgroup!();
}

// Setup code coverage
Expand Down Expand Up @@ -247,21 +247,21 @@ fn no_std_checks() {
// Test burn-core with tch and wgpu backend
fn burn_core_std() {
// Run cargo test --features test-tch
start_log_group("Test: burn-core (tch)".to_string());
group!("Test: burn-core (tch)");
cargo_test(["-p", "burn-core", "--features", "test-tch"].into());
end_log_group();
endgroup!();

// Run cargo test --features test-wgpu
if std::env::var("DISABLE_WGPU").is_err() {
start_log_group("Test: burn-core (wgpu)".to_string());
group!("Test: burn-core (wgpu)");
cargo_test(["-p", "burn-core", "--features", "test-wgpu"].into());
end_log_group();
endgroup!();
}
}

// Test burn-dataset features
fn burn_dataset_features_std() {
start_log_group("Checks: burn-dataset (all-features)".to_string());
group!("Checks: burn-dataset (all-features)");

// Run cargo build --all-features
cargo_build(["-p", "burn-dataset", "--all-features"].into());
Expand All @@ -272,16 +272,16 @@ fn burn_dataset_features_std() {
// Run cargo doc --all-features
cargo_doc(["-p", "burn-dataset", "--all-features"].into());

end_log_group();
endgroup!();
}

// Test burn-candle with accelerate (macOS only)
// Leverages the macOS Accelerate framework: https://developer.apple.com/documentation/accelerate
#[cfg(target_os = "macos")]
fn burn_candle_accelerate() {
start_log_group("Checks: burn-candle (accelerate)".to_string());
group!("Checks: burn-candle (accelerate)");
cargo_test(["-p", "burn-candle", "--features", "accelerate"].into());
end_log_group();
endgroup!();
}

fn std_checks() {
Expand All @@ -300,9 +300,9 @@ fn std_checks() {
cargo_clippy();

// Produce documentation for each workspace
start_log_group("Docs: workspaces".to_string());
group!("Docs: workspaces");
cargo_doc(["--workspace"].into());
end_log_group();
endgroup!();

// Setup code coverage
if is_coverage {
Expand All @@ -320,10 +320,10 @@ fn std_checks() {
continue;
}

start_log_group(format!("Checks: {}", workspace.name));
group!("Checks: {}", workspace.name);
cargo_build(Params::from(["-p", &workspace.name]));
cargo_test(Params::from(["-p", &workspace.name]));
end_log_group();
endgroup!();
}

// Test burn-candle with accelerate (macOS only)
Expand Down Expand Up @@ -356,7 +356,7 @@ fn check_typos() {
cargo_install(["typos-cli", "--version", "1.16.5"].into());
}

println!("Running typos check \n\n");
info!("Running typos check \n\n");

// Run typos command as child process
let typos = Command::new("typos")
Expand All @@ -376,14 +376,14 @@ fn check_examples() {
continue;
}

start_log_group(format!("Checks: Example - {}", workspace.name));
group!("Checks: Example - {}", workspace.name);
run_cargo_with_path(
"check",
["--examples"].into(),
Some(workspace.path),
"Failed to check example",
);
end_log_group();
endgroup!();
}
}

Expand All @@ -403,6 +403,9 @@ pub enum CheckType {
}

pub fn run(env: CheckType) -> anyhow::Result<()> {
// Setup logger
init_logger().init();

// Start time measurement
let start = Instant::now();

Expand Down Expand Up @@ -433,9 +436,9 @@ pub fn run(env: CheckType) -> anyhow::Result<()> {
let duration = start.elapsed();

// Print duration
println!(
info!(
"Time elapsed for the current execution: {}",
pretty_print_duration(&duration)
format_duration(&duration)
);

Ok(())
Expand Down
24 changes: 3 additions & 21 deletions xtask/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub(crate) enum WorkspaceMemberType {

#[derive(Debug)]
pub(crate) struct WorkspaceMember {
pub name: String,
pub path: String,
pub(crate) name: String,
pub(crate) path: String,
}

impl WorkspaceMember {
Expand Down Expand Up @@ -66,26 +66,8 @@ pub(crate) fn get_workspaces(w_type: WorkspaceMemberType) -> Vec<WorkspaceMember
workspaces
}

/// Start log group
pub(crate) fn start_log_group(title: String) {
// When running on CI, uses special grouping log
if std::env::var("CI").is_ok() {
println!("::group::{}", title);
} else {
println!("\n\n{}", title);
}
}

/// End log group
pub(crate) fn end_log_group() {
// When running on CI, uses special grouping log
if std::env::var("CI").is_ok() {
println!("::endgroup::");
}
}

/// Print duration as HH:MM:SS format
pub(crate) fn pretty_print_duration(duration: &Duration) -> String {
pub(crate) fn format_duration(duration: &Duration) -> String {
let seconds = duration.as_secs();
let minutes = seconds / 60;
let hours = minutes / 60;
Expand Down

0 comments on commit 16cd081

Please sign in to comment.