Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $/logTrace for server trace logs in Zed and VS Code #12564

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/ruff_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ impl Server {
} = all_settings;

crate::trace::init_tracing(
connection.make_sender(),
&connection.make_sender(),
global_settings
.tracing
.log_level
.unwrap_or(crate::trace::LogLevel::Info),
global_settings.tracing.log_file.as_deref(),
&init_params.client_info,
);

let mut workspace_for_url = |url: lsp_types::Url| {
Expand Down
65 changes: 57 additions & 8 deletions crates/ruff_server/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@
//!
//! Tracing will write to `stderr` by default, which should appear in the logs for most LSP clients.
//! A `logFile` path can also be specified in the settings, and output will be directed there instead.
use lsp_types::TraceValue;
use core::str;
use lsp_server::{Message, Notification};
use lsp_types::{
notification::{LogTrace, Notification as _},
ClientInfo, TraceValue,
};
use serde::Deserialize;
use std::{
io::{Error as IoError, ErrorKind, Write},
path::PathBuf,
str::FromStr,
sync::{Arc, Mutex, OnceLock},
};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{
fmt::{time::Uptime, writer::BoxMakeWriter},
fmt::{time::Uptime, writer::BoxMakeWriter, MakeWriter},
layer::SubscriberExt,
Layer,
};
Expand All @@ -43,13 +49,46 @@ pub(crate) fn set_trace_value(trace_value: TraceValue) {
*global_trace_value = trace_value;
}

// A tracing writer that uses LSPs logTrace method.
struct TraceLogWriter;

impl Write for TraceLogWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let message = str::from_utf8(buf).map_err(|e| IoError::new(ErrorKind::InvalidData, e))?;
LOGGING_SENDER
.get()
.expect("logging sender should be initialized at this point")
.send(Message::Notification(Notification {
method: LogTrace::METHOD.to_owned(),
params: serde_json::json!({
"message": message
}),
}))
.map_err(|e| IoError::new(ErrorKind::Other, e))?;
Ok(buf.len())
}

fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}

impl<'a> MakeWriter<'a> for TraceLogWriter {
type Writer = Self;

fn make_writer(&'a self) -> Self::Writer {
Self
}
}

pub(crate) fn init_tracing(
sender: ClientSender,
sender: &ClientSender,
log_level: LogLevel,
log_file: Option<&std::path::Path>,
client: &Option<ClientInfo>,
) {
LOGGING_SENDER
.set(sender)
.set(sender.clone())
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
.expect("logging sender should only be initialized once");

let log_file = log_file
Expand Down Expand Up @@ -82,15 +121,25 @@ pub(crate) fn init_tracing(
.ok()
});

let logger = match log_file {
Some(file) => BoxMakeWriter::new(Arc::new(file)),
None => {
if client
.as_ref()
.is_some_and(|client| client.name.starts_with("Zed ") || client.name == "Zed")
dhruvmanila marked this conversation as resolved.
Show resolved Hide resolved
{
BoxMakeWriter::new(TraceLogWriter)
} else {
BoxMakeWriter::new(std::io::stderr)
}
}
};
let subscriber = tracing_subscriber::Registry::default().with(
tracing_subscriber::fmt::layer()
.with_timer(Uptime::default())
.with_thread_names(true)
.with_ansi(false)
.with_writer(match log_file {
Some(file) => BoxMakeWriter::new(Arc::new(file)),
None => BoxMakeWriter::new(std::io::stderr),
})
.with_writer(logger)
.with_filter(TraceLevelFilter)
.with_filter(LogLevelFilter { filter: log_level }),
);
Expand Down
Loading