diff --git a/src/cli_util.rs b/src/cli_util.rs index 57858564a..051f3fa2d 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -50,6 +50,7 @@ use jujutsu_lib::working_copy::{ use jujutsu_lib::workspace::{Workspace, WorkspaceInitError, WorkspaceLoadError}; use jujutsu_lib::{dag_walk, file_util, git, revset}; use thiserror::Error; +use tracing_subscriber::prelude::*; use crate::formatter::Formatter; use crate::merge_tools::{ConflictResolveError, DiffEditError}; @@ -218,6 +219,45 @@ impl From 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 { app: clap::Command, string_args: Vec, diff --git a/src/main.rs b/src/main.rs index a50395dae..ed9f178e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,33 +12,17 @@ // See the License for the specific language governing permissions and // 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::config::read_config; use jujutsu::ui::Ui; -use tracing::metadata::LevelFilter; -use tracing_subscriber::prelude::*; -use tracing_subscriber::reload::Handle; -use tracing_subscriber::EnvFilter; -fn run( - ui: &mut Ui, - reload_log_filter: Handle, -) -> Result<(), CommandError> { +fn run(ui: &mut Ui, tracing_subscription: &TracingSubscription) -> Result<(), CommandError> { ui.reset(read_config()?); let app = default_app(); let (command_helper, matches) = parse_args(ui, app, std::env::args_os())?; if command_helper.global_args().verbose { - reload_log_filter - .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"); + tracing_subscription.enable_verbose_logging()?; } run_command(ui, &command_helper, &matches) } @@ -48,18 +32,10 @@ fn main() { // having verbose logging set up as early as possible, and to support // custom commands. See discussion on: // https://github.com/martinvonz/jj/pull/771 - let filter = EnvFilter::builder() - .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(); - + let tracing_subscription = TracingSubscription::init(); jujutsu::cleanup_guard::init(); 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); ui.finalize_writes(); std::process::exit(exit_code); diff --git a/tests/test_global_opts.rs b/tests/test_global_opts.rs index 435948bb6..a483fcdfe 100644 --- a/tests/test_global_opts.rs +++ b/tests/test_global_opts.rs @@ -305,5 +305,5 @@ fn test_verbose_logging_enabled() { .split_at(36); // The log format is currently Pretty so we include the terminal markup. // 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"); }