Skip to content

Commit

Permalink
Improve log message quality and reduce noise
Browse files Browse the repository at this point in the history
  • Loading branch information
ebkalderon committed Aug 11, 2020
1 parent d0b5a6a commit 5265224
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
12 changes: 4 additions & 8 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,12 @@ impl Service<Incoming> for LspService {
}
Incoming::Invalid { id, method } => match (id, method) {
(None, Some(method)) if method.starts_with("$/") => future::ok(None).boxed(),
(Some(id), Some(method)) => {
error!("received invalid JSON message {:?} with ID: {}", method, id);
let res = Response::error(Some(id), jsonrpc::Error::method_not_found());
(id, Some(method)) => {
error!("method {:?} not found", method);
let res = Response::error(id, jsonrpc::Error::method_not_found());
future::ok(Some(Outgoing::Response(res))).boxed()
}
(id, _) => {
match &id {
Some(id) => error!("received invalid JSON message with ID: {}", id),
None => error!("received invalid JSON message"),
}
(id, None) => {
let res = Response::error(id, jsonrpc::Error::invalid_request());
future::ok(Some(Outgoing::Response(res))).boxed()
}
Expand Down
34 changes: 17 additions & 17 deletions src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::task::{Context, Poll};
use futures::channel::mpsc;
use futures::future::{self, Either, FutureExt, TryFutureExt};
use futures::sink::SinkExt;
use futures::stream::{self, Empty, Stream, StreamExt};
use futures::stream::{self, Empty, Stream, StreamExt, TryStreamExt};
use log::{error, trace};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::{FramedRead, FramedWrite};
Expand Down Expand Up @@ -93,32 +93,32 @@ where
{
let (mut sender, receiver) = mpsc::channel(1);

let mut framed_stdin = FramedRead::new(self.stdin, LanguageServerCodec::default());
let framed_stdin = FramedRead::new(self.stdin, LanguageServerCodec::default());
let framed_stdout = FramedWrite::new(self.stdout, LanguageServerCodec::default());
let responses = receiver.buffer_unordered(4).filter_map(future::ready);
let interleave = self.interleave.fuse();

let mut messages = framed_stdin
.inspect_ok(|msg| trace!("<- {}", msg))
.inspect_err(|err| error!("failed to decode message: {}", err))
.map(Result::ok)
.filter_map(future::ready);

let printer = stream::select(responses, interleave)
.inspect(|msg| trace!("-> {}", msg))
.map(|msg| Ok(msg.to_string()))
.forward(framed_stdout.sink_map_err(|e| error!("failed to encode response: {}", e)))
.forward(framed_stdout.sink_map_err(|e| error!("failed to encode message: {}", e)))
.map(|_| ());

let reader = async move {
while let Some(line) = framed_stdin.next().await {
let request = match line {
Ok(line) => match serde_json::from_str(&line) {
Ok(json) => json,
Err(err) => {
error!("failed to parse JSON payload: {}", err);
trace!("raw message payload: {:?}", line);
let res = Response::error(None, jsonrpc::Error::parse_error());
let response_fut = future::ready(Some(Outgoing::Response(res)));
sender.send(Either::Right(response_fut)).await.unwrap();
continue;
}
},
while let Some(msg) = messages.next().await {
let request = match serde_json::from_str(&msg) {
Ok(req) => req,
Err(err) => {
error!("failed to decode message: {}", err);
error!("failed to parse JSON payload: {}", err);
let response = Response::error(None, jsonrpc::Error::parse_error());
let response_fut = future::ready(Some(Outgoing::Response(response)));
sender.send(Either::Right(response_fut)).await.unwrap();
continue;
}
};
Expand Down

0 comments on commit 5265224

Please sign in to comment.