[PATCH] D49224: [clangd] log request/response messages with method/ID/error at INFO level

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 12 02:14:13 PDT 2018


sammccall created this revision.
sammccall added a reviewer: hokein.
Herald added subscribers: cfe-commits, jkorous, MaskRay, ioeric, ilya-biryukov.

Bodies are logged at VERBOSE level (since r336785), tweak the formatting.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D49224

Files:
  clangd/JSONRPCDispatcher.cpp


Index: clangd/JSONRPCDispatcher.cpp
===================================================================
--- clangd/JSONRPCDispatcher.cpp
+++ clangd/JSONRPCDispatcher.cpp
@@ -69,7 +69,7 @@
     Outs << "Content-Length: " << S.size() << "\r\n\r\n" << S;
     Outs.flush();
   }
-  vlog("--> {0}\n", S);
+  vlog(">>> {0}\n", S);
 }
 
 void JSONOutput::log(Logger::Level Level,
@@ -99,6 +99,7 @@
     return;
   }
   RequestSpan::attach([&](json::Object &Args) { Args["Reply"] = Result; });
+  log("--> reply({0})", *ID);
   Context::current()
       .getExisting(RequestOut)
       ->writeMessage(json::Object{
@@ -116,6 +117,7 @@
   });
 
   if (auto ID = Context::current().get(RequestID)) {
+    log("--> reply({0}) error: {1}", *ID, Message);
     Context::current()
         .getExisting(RequestOut)
         ->writeMessage(json::Object{
@@ -133,11 +135,13 @@
   RequestSpan::attach([&](json::Object &Args) {
     Args["Call"] = json::Object{{"method", Method.str()}, {"params", Params}};
   });
+  auto ID = 1;
+  log("--> {0}({1})", Method, ID);
   Context::current()
       .getExisting(RequestOut)
       ->writeMessage(json::Object{
           {"jsonrpc", "2.0"},
-          {"id", 1},
+          {"id", ID},
           {"method", Method},
           {"params", std::move(Params)},
       });
@@ -148,6 +152,23 @@
   Handlers[Method] = std::move(H);
 }
 
+static void logIncomingMessage(const llvm::Optional<json::Value> &ID,
+                               llvm::Optional<StringRef> Method,
+                               const json::Object *Error) {
+  if (Method) { // incoming request
+    if (ID)     // call
+      log("<-- {0}({1})", *Method, *ID);
+    else // notification
+      log("<-- {0}", *Method);
+  } else if (ID) { // response, ID must be provided
+    if (Error)
+      log("<-- reply({0}) error: {1}", *ID,
+          Error->getString("message").getValueOr("<no message>"));
+    else
+      log("<-- reply({0})", *ID);
+  }
+}
+
 bool JSONRPCDispatcher::call(const json::Value &Message,
                              JSONOutput &Out) const {
   // Message must be an object with "jsonrpc":"2.0".
@@ -158,9 +179,9 @@
   llvm::Optional<json::Value> ID;
   if (auto *I = Object->get("id"))
     ID = std::move(*I);
-  // Method must be given.
   auto Method = Object->getString("method");
-  if (!Method)
+  logIncomingMessage(ID, Method, Object->getObject("error"));
+  if (!Method) // We only handle incoming requests, and ignore responses.
     return false;
   // Params should be given, use null if not.
   json::Value Params = nullptr;
@@ -333,13 +354,13 @@
     if (auto JSON = ReadMessage(In, Out)) {
       if (auto Doc = json::parse(*JSON)) {
         // Log the formatted message.
-        vlog(Out.Pretty ? "<-- {0:2}\n" : "<-- {0}\n", *Doc);
+        vlog(Out.Pretty ? "<<< {0:2}\n" : "<<< {0}\n", *Doc);
         // Finally, execute the action for this JSON message.
         if (!Dispatcher.call(*Doc, Out))
           elog("JSON dispatch failed!");
       } else {
         // Parse error. Log the raw message.
-        vlog("<-- {0}\n", *JSON);
+        vlog("<<< {0}\n", *JSON);
         elog("JSON parse error: {0}", llvm::toString(Doc.takeError()));
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D49224.155132.patch
Type: text/x-patch
Size: 3213 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180712/abce1151/attachment.bin>


More information about the cfe-commits mailing list