From 5265224bc51b8c7f3f11e8b2377e834911f6af37 Mon Sep 17 00:00:00 2001 From: Eyal Kalderon Date: Tue, 11 Aug 2020 19:33:56 +0800 Subject: [PATCH] Improve log message quality and reduce noise --- src/service.rs | 12 ++++-------- src/transport.rs | 34 +++++++++++++++++----------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/service.rs b/src/service.rs index 71c5c080..a21442a7 100644 --- a/src/service.rs +++ b/src/service.rs @@ -132,16 +132,12 @@ impl Service 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() } diff --git a/src/transport.rs b/src/transport.rs index 653ead7e..af398001 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -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}; @@ -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; } };