Skip to content

Commit

Permalink
Merge #129
Browse files Browse the repository at this point in the history
129: Add tracing for extra structured debug logging r=foresterre a=foresterre



Co-authored-by: Martijn Gribnau <[email protected]>
  • Loading branch information
bors[bot] and foresterre authored Oct 15, 2021
2 parents 79b95d5 + aa84e6e commit a2bb835
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 10 deletions.
171 changes: 166 additions & 5 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ json = "0.12.4"
# read Cargo.toml
decent-toml-rs-alternative = "0.3.0"

tracing = "0.1"
tracing-subscriber = "0.2"
tracing-appender = "0.1"
dirs = "4.0.0"

# Get the available rust versions
[dependencies.rust-releases]
version = "0.16.1"
Expand Down
54 changes: 50 additions & 4 deletions src/bin/cargo-msrv.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,70 @@
use cargo_msrv::config::Config;
use cargo_msrv::errors::TResult;
use cargo_msrv::errors::{CargoMSRVError, TResult};
use cargo_msrv::reporter::ReporterBuilder;
use cargo_msrv::{cli, run_app};
use std::convert::TryFrom;
use tracing_appender::rolling::{RollingFileAppender, Rotation};
use tracing_subscriber::filter::LevelFilter;

fn main() {
if let Err(err) = init_and_run() {
if let Err(err) = _main() {
eprintln!("{}", err);
}
}

fn init_and_run() -> TResult<()> {
fn _main() -> TResult<Option<tracing_appender::non_blocking::WorkerGuard>> {
let matches = cli::cli().get_matches();
let config = Config::try_from(&matches)?;

// NB: We must collect the guard of the non-blocking tracing appender, since it will only live as
// long as the lifetime of the worker guard. If we don't do this, the guard would be dropped after
// the scope of `if !config.no_tracing() { ... }` ended, and as a result, anything logged in
// `init_and_run` would not be logged.
let mut guard = Option::None;

if !config.no_tracing() {
guard = Some(init_tracing()?);
}

init_and_run(&config)?;

Ok(guard)
}

fn init_and_run(config: &Config) -> TResult<()> {
let target = config.target().as_str();
let cmd = config.check_command_string();

tracing::info!("Initializing reporter");
let reporter = ReporterBuilder::new(target, cmd.as_str())
.output_format(config.output_format())
.build();

run_app(&config, &reporter)
tracing::info!("Running app");

let _ = run_app(&config, &reporter)?;

tracing::info!("Finished app");

Ok(())
}

fn init_tracing() -> TResult<tracing_appender::non_blocking::WorkerGuard> {
let log_folder = dirs::data_local_dir()
.map(|path| path.join("cargo-msrv"))
.ok_or_else(|| CargoMSRVError::UnableToAccessLogFolder)?;

let file_appender = RollingFileAppender::new(Rotation::DAILY, log_folder, "cargo-msrv-log");
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);

let subscriber = tracing_subscriber::fmt()
.json()
.with_max_level(LevelFilter::INFO)
.with_writer(non_blocking)
.finish();

tracing::subscriber::set_global_default(subscriber)
.map_err(|_| CargoMSRVError::UnableToInitTracing)?;

Ok(guard)
}
3 changes: 3 additions & 0 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn examine_toolchain(
)
}

#[tracing::instrument]
fn download_if_required(
version: &semver::Version,
toolchain_specifier: &str,
Expand All @@ -98,6 +99,8 @@ fn download_if_required(
let toolchain = toolchain_specifier.to_owned();
output.progress(ProgressAction::Installing(version));

tracing::info!("Installing toolchain {}", toolchain);

let status = command(&["install", "--profile", "minimal", &toolchain], None)
.and_then(|mut c| c.wait().map_err(CargoMSRVError::Io))?;

Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod id {
pub const ARG_OUTPUT_FORMAT: &str = "output_format";
pub const ARG_VERIFY: &str = "verify_msrv";
pub const ARG_RELEASE_SOURCE: &str = "release_source";
pub const ARG_NO_LOG: &str = "no_log";
}

pub fn cli() -> App<'static, 'static> {
Expand Down Expand Up @@ -133,6 +134,11 @@ rustup like so: `rustup run <toolchain> <COMMAND...>`. You'll only need to provi
.possible_values(&["rust-changelog", "rust-dist"])
.default_value("rust-changelog")
)
.arg(Arg::with_name(id::ARG_NO_LOG)
.long("no-log")
.help("Disable logging")
.takes_value(false)
)
.arg(
Arg::with_name(id::ARG_CUSTOM_CHECK)
.value_name("COMMAND")
Expand Down
Loading

0 comments on commit a2bb835

Please sign in to comment.