cli: extract thin wrapper for tracing initialization

I'm going to add a high-level abstraction to hide details of the CLI startup,
but a low-level wrapper like this would still be useful.
This commit is contained in:
Yuya Nishihara 2023-01-03 16:24:44 +09:00
parent dc26d4db17
commit dbcfb00d2c
3 changed files with 46 additions and 30 deletions

View file

@ -50,6 +50,7 @@ use jujutsu_lib::working_copy::{
use jujutsu_lib::workspace::{Workspace, WorkspaceInitError, WorkspaceLoadError}; use jujutsu_lib::workspace::{Workspace, WorkspaceInitError, WorkspaceLoadError};
use jujutsu_lib::{dag_walk, file_util, git, revset}; use jujutsu_lib::{dag_walk, file_util, git, revset};
use thiserror::Error; use thiserror::Error;
use tracing_subscriber::prelude::*;
use crate::formatter::Formatter; use crate::formatter::Formatter;
use crate::merge_tools::{ConflictResolveError, DiffEditError}; use crate::merge_tools::{ConflictResolveError, DiffEditError};
@ -218,6 +219,45 @@ impl From<clap::Error> for CommandError {
} }
} }
/// Handle to initialize or change tracing subscription.
#[derive(Clone, Debug)]
pub struct TracingSubscription {
reload_log_filter: tracing_subscriber::reload::Handle<
tracing_subscriber::EnvFilter,
tracing_subscriber::Registry,
>,
}
impl TracingSubscription {
/// Initializes tracing with the default configuration. This should be
/// called as early as possible.
pub fn init() -> Self {
let filter = tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::metadata::LevelFilter::INFO.into())
.from_env_lossy();
let (filter, reload_log_filter) = tracing_subscriber::reload::Layer::new(filter);
tracing_subscriber::registry()
.with(filter)
.with(tracing_subscriber::fmt::Layer::default().with_writer(std::io::stderr))
.init();
TracingSubscription { reload_log_filter }
}
pub fn enable_verbose_logging(&self) -> Result<(), CommandError> {
self.reload_log_filter
.modify(|filter| {
*filter = tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::metadata::LevelFilter::DEBUG.into())
.from_env_lossy()
})
.map_err(|err| {
CommandError::InternalError(format!("failed to enable verbose logging: {err:?}"))
})?;
tracing::debug!("verbose logging enabled");
Ok(())
}
}
pub struct CommandHelper { pub struct CommandHelper {
app: clap::Command, app: clap::Command,
string_args: Vec<String>, string_args: Vec<String>,

View file

@ -12,33 +12,17 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use jujutsu::cli_util::{handle_command_result, parse_args, CommandError}; use jujutsu::cli_util::{handle_command_result, parse_args, CommandError, TracingSubscription};
use jujutsu::commands::{default_app, run_command}; use jujutsu::commands::{default_app, run_command};
use jujutsu::config::read_config; use jujutsu::config::read_config;
use jujutsu::ui::Ui; use jujutsu::ui::Ui;
use tracing::metadata::LevelFilter;
use tracing_subscriber::prelude::*;
use tracing_subscriber::reload::Handle;
use tracing_subscriber::EnvFilter;
fn run( fn run(ui: &mut Ui, tracing_subscription: &TracingSubscription) -> Result<(), CommandError> {
ui: &mut Ui,
reload_log_filter: Handle<EnvFilter, impl tracing::Subscriber>,
) -> Result<(), CommandError> {
ui.reset(read_config()?); ui.reset(read_config()?);
let app = default_app(); let app = default_app();
let (command_helper, matches) = parse_args(ui, app, std::env::args_os())?; let (command_helper, matches) = parse_args(ui, app, std::env::args_os())?;
if command_helper.global_args().verbose { if command_helper.global_args().verbose {
reload_log_filter tracing_subscription.enable_verbose_logging()?;
.modify(|filter| {
*filter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env_lossy()
})
.map_err(|err| {
CommandError::InternalError(format!("failed to enable verbose logging: {err:?}"))
})?;
tracing::debug!("verbose logging enabled");
} }
run_command(ui, &command_helper, &matches) run_command(ui, &command_helper, &matches)
} }
@ -48,18 +32,10 @@ fn main() {
// having verbose logging set up as early as possible, and to support // having verbose logging set up as early as possible, and to support
// custom commands. See discussion on: // custom commands. See discussion on:
// https://github.com/martinvonz/jj/pull/771 // https://github.com/martinvonz/jj/pull/771
let filter = EnvFilter::builder() let tracing_subscription = TracingSubscription::init();
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();
let (filter, reload_log_filter) = tracing_subscriber::reload::Layer::new(filter);
tracing_subscriber::registry()
.with(filter)
.with(tracing_subscriber::fmt::Layer::default().with_writer(std::io::stderr))
.init();
jujutsu::cleanup_guard::init(); jujutsu::cleanup_guard::init();
let mut ui = Ui::new(); let mut ui = Ui::new();
let result = run(&mut ui, reload_log_filter); let result = run(&mut ui, &tracing_subscription);
let exit_code = handle_command_result(&mut ui, result); let exit_code = handle_command_result(&mut ui, result);
ui.finalize_writes(); ui.finalize_writes();
std::process::exit(exit_code); std::process::exit(exit_code);

View file

@ -305,5 +305,5 @@ fn test_verbose_logging_enabled() {
.split_at(36); .split_at(36);
// The log format is currently Pretty so we include the terminal markup. // The log format is currently Pretty so we include the terminal markup.
// Luckily, insta will print this in colour when reviewing. // Luckily, insta will print this in colour when reviewing.
insta::assert_snapshot!(log_line, @"\u{1b}[34mDEBUG\u{1b}[0m \u{1b}[2mjj\u{1b}[0m\u{1b}[2m:\u{1b}[0m verbose logging enabled"); insta::assert_snapshot!(log_line, @"DEBUG jujutsu::cli_util: verbose logging enabled");
} }