[clangd] Refactor stream output into a single thread-safe output object.

This abstracts away the passing of raw_ostreams everywhere, thread
safety will be used soon.

llvm-svn: 294747
diff --git a/clang-tools-extra/clangd/JSONRPCDispatcher.cpp b/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
index 5f82f0a..fd0b394 100644
--- a/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
+++ b/clang-tools-extra/clangd/JSONRPCDispatcher.cpp
@@ -15,10 +15,11 @@
 using namespace clang;
 using namespace clangd;
 
-void Handler::writeMessage(const Twine &Message) {
+void JSONOutput::writeMessage(const Twine &Message) {
   llvm::SmallString<128> Storage;
   StringRef M = Message.toStringRef(Storage);
 
+  std::lock_guard<std::mutex> Guard(StreamMutex);
   // Log without headers.
   Logs << "--> " << M << '\n';
   Logs.flush();
@@ -29,7 +30,7 @@
 }
 
 void Handler::handleMethod(llvm::yaml::MappingNode *Params, StringRef ID) {
-  Logs << "Method ignored.\n";
+  Output.logs() << "Method ignored.\n";
   // Return that this method is unsupported.
   writeMessage(
       R"({"jsonrpc":"2.0","id":)" + ID +
@@ -37,7 +38,7 @@
 }
 
 void Handler::handleNotification(llvm::yaml::MappingNode *Params) {
-  Logs << "Notification ignored.\n";
+  Output.logs() << "Notification ignored.\n";
 }
 
 void JSONRPCDispatcher::registerHandler(StringRef Method,