From 17d2e8d9101b547e6cc3b19d278b2f925e2d972f Mon Sep 17 00:00:00 2001 From: David Barsky Date: Tue, 30 Jan 2024 13:37:27 -0500 Subject: internal: remove `tracing/mod.rs` --- crates/rust-analyzer/src/lib.rs | 7 +- crates/rust-analyzer/src/tracing/config.rs | 108 +++++++++++++++++++++++++++++ crates/rust-analyzer/src/tracing/mod.rs | 108 ----------------------------- 3 files changed, 114 insertions(+), 109 deletions(-) create mode 100644 crates/rust-analyzer/src/tracing/config.rs delete mode 100644 crates/rust-analyzer/src/tracing/mod.rs diff --git a/crates/rust-analyzer/src/lib.rs b/crates/rust-analyzer/src/lib.rs index b77e1dca29f..c22834ee100 100644 --- a/crates/rust-analyzer/src/lib.rs +++ b/crates/rust-analyzer/src/lib.rs @@ -37,9 +37,14 @@ mod handlers { pub(crate) mod request; } +pub mod tracing { + pub mod config; + pub use config::Config; + pub mod hprof; +} + pub mod config; pub mod lsp; -pub mod tracing; use self::lsp::ext as lsp_ext; #[cfg(test)] diff --git a/crates/rust-analyzer/src/tracing/config.rs b/crates/rust-analyzer/src/tracing/config.rs new file mode 100644 index 00000000000..fcdbd1e6d9b --- /dev/null +++ b/crates/rust-analyzer/src/tracing/config.rs @@ -0,0 +1,108 @@ +//! Simple logger that logs either to stderr or to a file, using `tracing_subscriber` +//! filter syntax and `tracing_appender` for non blocking output. + +use std::io; + +use anyhow::Context; +use tracing::{level_filters::LevelFilter, Level}; +use tracing_subscriber::{ + filter::{self, Targets}, + fmt::{format::FmtSpan, MakeWriter}, + layer::SubscriberExt, + util::SubscriberInitExt, + Layer, Registry, +}; +use tracing_tree::HierarchicalLayer; + +use crate::tracing::hprof; + +pub struct Config { + pub writer: T, + pub filter: String, + /// The meaning of CHALK_DEBUG is to tell chalk crates + /// (i.e. chalk-solve, chalk-ir, chalk-recursive) how to filter tracing + /// logs. But now we can only have just one filter, which means we have to + /// merge chalk filter to our main filter (from RA_LOG env). + /// + /// The acceptable syntax of CHALK_DEBUG is `target[span{field=value}]=level`. + /// As the value should only affect chalk crates, we'd better manually + /// specify the target. And for simplicity, CHALK_DEBUG only accept the value + /// that specify level. + pub chalk_filter: Option, + /// Filtering syntax, set in a shell: + /// ``` + /// env RA_PROFILE=* // dump everything + /// env RA_PROFILE=foo|bar|baz // enabled only selected entries + /// env RA_PROFILE=*@3>10 // dump everything, up to depth 3, if it takes more than 10 + /// ``` + pub profile_filter: Option, +} + +impl Config +where + T: for<'writer> MakeWriter<'writer> + Send + Sync + 'static, +{ + pub fn init(self) -> anyhow::Result<()> { + let filter: Targets = self + .filter + .parse() + .with_context(|| format!("invalid log filter: `{}`", self.filter))?; + + let writer = self.writer; + + let ra_fmt_layer = tracing_subscriber::fmt::layer() + .with_span_events(FmtSpan::CLOSE) + .with_writer(writer) + .with_filter(filter); + + let mut chalk_layer = None; + if let Some(chalk_filter) = self.chalk_filter { + let level: LevelFilter = + chalk_filter.parse().with_context(|| "invalid chalk log filter")?; + + let chalk_filter = Targets::new() + .with_target("chalk_solve", level) + .with_target("chalk_ir", level) + .with_target("chalk_recursive", level); + chalk_layer = Some( + HierarchicalLayer::default() + .with_indent_lines(true) + .with_ansi(false) + .with_indent_amount(2) + .with_writer(io::stderr) + .with_filter(chalk_filter), + ); + }; + + let mut profiler_layer = None; + if let Some(spec) = self.profile_filter { + let (write_filter, allowed_names) = hprof::WriteFilter::from_spec(&spec); + + // this filter the first pass for `tracing`: these are all the "profiling" spans, but things like + // span depth or duration are not filtered here: that only occurs at write time. + let profile_filter = filter::filter_fn(move |metadata| { + let allowed = match &allowed_names { + Some(names) => names.contains(metadata.name()), + None => true, + }; + + metadata.is_span() + && allowed + && metadata.level() >= &Level::INFO + && !metadata.target().starts_with("salsa") + && !metadata.target().starts_with("chalk") + }); + + let layer = hprof::SpanTree::default() + .aggregate(true) + .spec_filter(write_filter) + .with_filter(profile_filter); + + profiler_layer = Some(layer); + } + + Registry::default().with(ra_fmt_layer).with(chalk_layer).with(profiler_layer).try_init()?; + + Ok(()) + } +} diff --git a/crates/rust-analyzer/src/tracing/mod.rs b/crates/rust-analyzer/src/tracing/mod.rs deleted file mode 100644 index 0e681c37094..00000000000 --- a/crates/rust-analyzer/src/tracing/mod.rs +++ /dev/null @@ -1,108 +0,0 @@ -//! Simple logger that logs either to stderr or to a file, using `tracing_subscriber` -//! filter syntax and `tracing_appender` for non blocking output. - -use std::io; - -use anyhow::Context; -use tracing::{level_filters::LevelFilter, Level}; -use tracing_subscriber::{ - filter::{self, Targets}, - fmt::{format::FmtSpan, MakeWriter}, - layer::SubscriberExt, - util::SubscriberInitExt, - Layer, Registry, -}; -use tracing_tree::HierarchicalLayer; - -pub mod hprof; - -pub struct Config { - pub writer: T, - pub filter: String, - /// The meaning of CHALK_DEBUG is to tell chalk crates - /// (i.e. chalk-solve, chalk-ir, chalk-recursive) how to filter tracing - /// logs. But now we can only have just one filter, which means we have to - /// merge chalk filter to our main filter (from RA_LOG env). - /// - /// The acceptable syntax of CHALK_DEBUG is `target[span{field=value}]=level`. - /// As the value should only affect chalk crates, we'd better manually - /// specify the target. And for simplicity, CHALK_DEBUG only accept the value - /// that specify level. - pub chalk_filter: Option, - /// Filtering syntax, set in a shell: - /// ``` - /// env RA_PROFILE=* // dump everything - /// env RA_PROFILE=foo|bar|baz // enabled only selected entries - /// env RA_PROFILE=*@3>10 // dump everything, up to depth 3, if it takes more than 10 - /// ``` - pub profile_filter: Option, -} - -impl Config -where - T: for<'writer> MakeWriter<'writer> + Send + Sync + 'static, -{ - pub fn init(self) -> anyhow::Result<()> { - let filter: Targets = self - .filter - .parse() - .with_context(|| format!("invalid log filter: `{}`", self.filter))?; - - let writer = self.writer; - - let ra_fmt_layer = tracing_subscriber::fmt::layer() - .with_span_events(FmtSpan::CLOSE) - .with_writer(writer) - .with_filter(filter); - - let mut chalk_layer = None; - if let Some(chalk_filter) = self.chalk_filter { - let level: LevelFilter = - chalk_filter.parse().with_context(|| "invalid chalk log filter")?; - - let chalk_filter = Targets::new() - .with_target("chalk_solve", level) - .with_target("chalk_ir", level) - .with_target("chalk_recursive", level); - chalk_layer = Some( - HierarchicalLayer::default() - .with_indent_lines(true) - .with_ansi(false) - .with_indent_amount(2) - .with_writer(io::stderr) - .with_filter(chalk_filter), - ); - }; - - let mut profiler_layer = None; - if let Some(spec) = self.profile_filter { - let (write_filter, allowed_names) = hprof::WriteFilter::from_spec(&spec); - - // this filter the first pass for `tracing`: these are all the "profiling" spans, but things like - // span depth or duration are not filtered here: that only occurs at write time. - let profile_filter = filter::filter_fn(move |metadata| { - let allowed = match &allowed_names { - Some(names) => names.contains(metadata.name()), - None => true, - }; - - metadata.is_span() - && allowed - && metadata.level() >= &Level::INFO - && !metadata.target().starts_with("salsa") - && !metadata.target().starts_with("chalk") - }); - - let layer = hprof::SpanTree::default() - .aggregate(true) - .spec_filter(write_filter) - .with_filter(profile_filter); - - profiler_layer = Some(layer); - } - - Registry::default().with(ra_fmt_layer).with(chalk_layer).with(profiler_layer).try_init()?; - - Ok(()) - } -} -- cgit 1.4.1-3-g733a5