Skip to content

Commit

Permalink
Merge pull request #59 from os-checker/RAP_LOG
Browse files Browse the repository at this point in the history
feat: support `RAP_LOG` environment variable; remove `-debug` option
  • Loading branch information
hxuhack authored Oct 16, 2024
2 parents 8fcfa83 + a4b9268 commit 90385e2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 58 deletions.
3 changes: 3 additions & 0 deletions rap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ colorful = "0.2.1"

[features]
backtraces = ["snafu/backtraces", "snafu/backtraces-impl-backtrace-crate"]

[package.metadata.rust-analyzer]
rustc_private = true
8 changes: 4 additions & 4 deletions rap/src/bin/cargo-rap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The file references the cargo file for Miri: https://github.com/rust-lang/miri/blob/master/cargo-miri/src/main.rs
*/
use cargo_metadata::{Metadata, MetadataCommand};
use rap::utils::log::{rap_error_and_exit, Verbosity};
use rap::utils::log::{init_log, rap_error_and_exit};
use rap::{rap_debug, rap_error, rap_info};
use rustc_version::VersionMeta;
use std::env;
Expand Down Expand Up @@ -35,7 +35,6 @@ General command:
-V or -version: show the version of RAP
Debugging options:
-debug show the debug-level logs
-mir print the MIR of each function
"#;

Expand Down Expand Up @@ -374,8 +373,9 @@ fn main() {
2. Cargo check actually triggers `path/cargo-rap path/rustc` according to RUSTC_WRAPPER.
Because RUSTC_WRAPPER is defined, Cargo calls the command: `$RUSTC_WRAPPER path/rustc ...`
*/
// Init the log_system; Use Verbosity::Debug for printing debugging messages.
Verbosity::init_log(Verbosity::Info).expect("Failed to init log.");

// Init the log_system
init_log().expect("Failed to init log.");
rap_debug!("Enter cargo-rap; Received args: {:?}", env::args());

let first_arg = env::args().nth(1);
Expand Down
12 changes: 2 additions & 10 deletions rap/src/bin/rap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
extern crate rustc_driver;
extern crate rustc_session;

use rap::rap_debug;
use rap::utils::log::Verbosity;
use rap::{compile_time_sysroot, RapCallback, RAP_DEFAULT_ARGS};
use rap::{compile_time_sysroot, rap_debug, utils::log::init_log, RapCallback, RAP_DEFAULT_ARGS};
use rustc_session::config::ErrorOutputType;
use rustc_session::EarlyDiagCtxt;
use std::env;
Expand Down Expand Up @@ -35,7 +33,6 @@ fn run_complier(args: &mut Vec<String>, callback: &mut RapCallback) -> i32 {

fn main() {
// Parse the arguments from env.
let mut debug = false;
let mut args = vec![];
let mut compiler = RapCallback::default();
for arg in env::args() {
Expand All @@ -52,18 +49,13 @@ fn main() {
"-senryx" => compiler.enable_senryx(),
"-callgraph" => compiler.enable_callgraph(),
"-mir" => compiler.enable_show_mir(),
"-debug" => debug = true,
"-adt" => {}
"-z3" => {}
"-meta" => {}
_ => args.push(arg),
}
}
if debug == true {
Verbosity::init_log(Verbosity::Debug).expect("Failed to init debugging log");
} else {
Verbosity::init_log(Verbosity::Info).expect("Failed to init info log");
}
_ = init_log().inspect_err(|err| eprintln!("Failed to init log: {err}"));
rap_debug!("rap received arguments{:#?}", env::args());
rap_debug!("arguments to rustc: {:?}", &args);

Expand Down
87 changes: 43 additions & 44 deletions rap/src/utils/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,54 @@ use fern::colors::{Color, ColoredLevelConfig};
use fern::{self, Dispatch};
use log::LevelFilter;

#[derive(Debug, Copy, Clone, Hash)]
pub enum Verbosity {
Info,
Debug,
Trace,
fn log_level() -> LevelFilter {
if let Ok(s) = std::env::var("RAP_LOG") {
match s.parse() {
Ok(level) => return level,
Err(err) => eprintln!("RAP_LOG is invalid: {err}"),
}
}
LevelFilter::Info
}

impl Verbosity {
pub fn init_log(verbose: Verbosity) -> Result<(), fern::InitError> {
let mut dispatch = Dispatch::new();
let color_line = ColoredLevelConfig::new()
.info(Color::White)
.error(Color::Red)
.warn(Color::Yellow)
.debug(Color::White)
.trace(Color::BrightBlack);
/// Detect `RAP_LOG` environment variable first; if it's not set,
/// default to INFO level.
pub fn init_log() -> Result<(), fern::InitError> {
let dispatch = Dispatch::new().level(log_level());

let color_level = color_line.info(Color::Green);
dispatch = match verbose {
Verbosity::Info => dispatch.level(LevelFilter::Info),
Verbosity::Debug => dispatch.level(LevelFilter::Debug),
Verbosity::Trace => dispatch.level(LevelFilter::Trace),
}; //.level_for("rap", LevelFilter::Debug);
let color_line = ColoredLevelConfig::new()
.info(Color::White)
.error(Color::Red)
.warn(Color::Yellow)
.debug(Color::White)
.trace(Color::BrightBlack);

let stderr_dispatch = Dispatch::new()
.format(move |callback, args, record| {
let time_now = Local::now();
callback.finish(format_args!(
"{}{}:{}|RAP-FRONT|{}{}|: {}\x1B[0m",
format_args!(
"\x1B[{}m",
color_line.get_color(&record.level()).to_fg_str()
),
time_now.hour(),
time_now.minute(),
color_level.color(record.level()),
format_args!(
"\x1B[{}m",
color_line.get_color(&record.level()).to_fg_str()
),
args
))
})
.chain(std::io::stderr());
let color_level = color_line.info(Color::Green);

/* Note that we cannot dispatch to stdout due to some bugs */
dispatch.chain(stderr_dispatch).apply()?;
Ok(())
}
let stderr_dispatch = Dispatch::new()
.format(move |callback, args, record| {
let time_now = Local::now();
callback.finish(format_args!(
"{}{}:{}|RAP-FRONT|{}{}|: {}\x1B[0m",
format_args!(
"\x1B[{}m",
color_line.get_color(&record.level()).to_fg_str()
),
time_now.hour(),
time_now.minute(),
color_level.color(record.level()),
format_args!(
"\x1B[{}m",
color_line.get_color(&record.level()).to_fg_str()
),
args
))
})
.chain(std::io::stderr());

/* Note that we cannot dispatch to stdout due to some bugs */
dispatch.chain(stderr_dispatch).apply()?;
Ok(())
}

#[macro_export]
Expand Down

0 comments on commit 90385e2

Please sign in to comment.