-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create the
ruff_server
crate and the ruff server
command.
- Loading branch information
1 parent
a284c71
commit d1c23eb
Showing
43 changed files
with
5,375 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::ExitStatus; | ||
use anyhow::Result; | ||
use ruff_linter::logging::LogLevel; | ||
use ruff_server::Server; | ||
use tracing::{level_filters::LevelFilter, metadata::Level, subscriber::Interest, Metadata}; | ||
use tracing_subscriber::{ | ||
layer::{Context, Filter, SubscriberExt}, | ||
Layer, Registry, | ||
}; | ||
use tracing_tree::time::Uptime; | ||
|
||
pub(crate) fn run_server(log_level: LogLevel) -> Result<ExitStatus> { | ||
let trace_level = if log_level == LogLevel::Verbose { | ||
Level::TRACE | ||
} else { | ||
Level::DEBUG | ||
}; | ||
|
||
let subscriber = Registry::default().with( | ||
tracing_tree::HierarchicalLayer::default() | ||
.with_indent_lines(true) | ||
.with_indent_amount(2) | ||
.with_bracketed_fields(true) | ||
.with_targets(true) | ||
.with_writer(|| Box::new(std::io::stderr())) | ||
.with_timer(Uptime::default()) | ||
.with_filter(LoggingFilter { trace_level }), | ||
); | ||
|
||
tracing::subscriber::set_global_default(subscriber)?; | ||
|
||
let server = Server::new()?; | ||
|
||
server.run().map(|()| ExitStatus::Success) | ||
} | ||
|
||
struct LoggingFilter { | ||
trace_level: Level, | ||
} | ||
|
||
impl LoggingFilter { | ||
fn is_enabled(&self, meta: &Metadata<'_>) -> bool { | ||
let filter = if meta.target().starts_with("ruff") { | ||
self.trace_level | ||
} else { | ||
Level::INFO | ||
}; | ||
|
||
meta.level() <= &filter | ||
} | ||
} | ||
|
||
impl<S> Filter<S> for LoggingFilter { | ||
fn enabled(&self, meta: &Metadata<'_>, _cx: &Context<'_, S>) -> bool { | ||
self.is_enabled(meta) | ||
} | ||
|
||
fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest { | ||
if self.is_enabled(meta) { | ||
Interest::always() | ||
} else { | ||
Interest::never() | ||
} | ||
} | ||
|
||
fn max_level_hint(&self) -> Option<LevelFilter> { | ||
Some(LevelFilter::from_level(self.trace_level)) | ||
} | ||
} |
Oops, something went wrong.