Skip to content

Commit

Permalink
Move from log -> tracing; enable dynamic selection (#7)
Browse files Browse the repository at this point in the history
* Switch from log to tracing

* Enable runtime tracing options

* Fix workflow definition
  • Loading branch information
reubeno authored May 13, 2024
1 parent 9a52352 commit cf53bf2
Show file tree
Hide file tree
Showing 18 changed files with 242 additions and 115 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: "CI"
on:
pull_request: {}
push:
paths-ignore:
- "docs/**"
Expand Down
133 changes: 104 additions & 29 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ brush-interactive-shell = { path = "../interactive-shell" }
brush-parser = { path = "../parser" }
brush-shell = { path = "../shell" }
clap = { version = "4.5.4", features = ["derive"] }
env_logger = "0.11.3"
log = "0.4.21"
tokio = { version = "1.37.0", features = ["rt", "rt-multi-thread"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

[dev-dependencies]
anyhow = "1.0.82"
Expand Down
80 changes: 66 additions & 14 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{io::IsTerminal, io::Write, path::Path};
use std::{collections::HashSet, io::IsTerminal, path::Path};

use clap::Parser;
use log::error;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};

#[derive(Parser)]
#[clap(version, about, disable_help_flag = true, disable_version_flag = true)]
Expand Down Expand Up @@ -62,13 +62,30 @@ struct CommandLineArgs {
#[clap(long = "disable-bracketed-paste", help = "Disable bracketed paste.")]
disable_bracketed_paste: bool,

#[clap(long = "log-enable")]
enabled_log_events: Vec<TraceEvent>,

#[clap(help = "Path to script to execute")]
script_path: Option<String>,

#[clap(help = "Arguments for script")]
script_args: Vec<String>,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, clap::ValueEnum)]
enum TraceEvent {
#[clap(name = "arithmetic")]
Arithmetic,
#[clap(name = "complete")]
Complete,
#[clap(name = "expand")]
Expand,
#[clap(name = "parse")]
Parse,
#[clap(name = "tokenize")]
Tokenize,
}

impl CommandLineArgs {
pub fn is_interactive(&self) -> bool {
if self.interactive {
Expand All @@ -88,25 +105,59 @@ impl CommandLineArgs {
}

fn main() {
// Initialize logging. Default log level to INFO if not explicitly specified by the env.
// Keep verbosity on rustyline no more than WARNING, since it otherwise gets quite noisy.
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
.filter_module("rustyline", log::LevelFilter::Warn)
.format(|buf, record| writeln!(buf, "{}", record.args()))
.init();

//
// Parse args.
//
let args: Vec<_> = std::env::args().collect();
let parsed_args = CommandLineArgs::parse_from(&args);

//
// Initializing tracing.
//
let mut filter = tracing_subscriber::filter::Targets::new()
.with_default(tracing_subscriber::filter::LevelFilter::INFO);

let enabled_trace_events: HashSet<TraceEvent> =
parsed_args.enabled_log_events.iter().cloned().collect();
for event in enabled_trace_events {
let targets = match event {
TraceEvent::Arithmetic => vec!["parser::arithmetic"],
TraceEvent::Complete => vec!["shell::completion", "shell::builtins::complete"],
TraceEvent::Expand => vec![],
TraceEvent::Parse => vec!["parse"],
TraceEvent::Tokenize => vec!["tokenize"],
};

filter = filter.with_targets(
targets
.into_iter()
.map(|target| (target, tracing::Level::DEBUG)),
);
}

let stderr_log_layer = tracing_subscriber::fmt::layer()
.with_writer(std::io::stderr)
.without_time()
.with_filter(filter);

tracing_subscriber::registry()
.with(stderr_log_layer)
.try_init()
.expect("Failed to initialize tracing.");

//
// Run.
//
let result = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(run(args));
.block_on(run(args, parsed_args));

let exit_code = match result {
Ok(code) => code,
Err(e) => {
error!("error: {:#}", e);
tracing::error!("error: {:#}", e);
1
}
};
Expand All @@ -115,9 +166,10 @@ fn main() {
std::process::exit(exit_code as i32);
}

async fn run(cli_args: Vec<String>) -> Result<u8, interactive_shell::InteractiveShellError> {
let args = CommandLineArgs::parse_from(&cli_args);

async fn run(
cli_args: Vec<String>,
args: CommandLineArgs,
) -> Result<u8, interactive_shell::InteractiveShellError> {
let argv0 = if args.sh_mode {
// Simulate having been run as "sh".
Some(String::from("sh"))
Expand Down
Loading

0 comments on commit cf53bf2

Please sign in to comment.