[clang-tools-extra] r322827 - [clangd] Output log messages to stderr when not configured (e.g. in tests). NFC

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 18 02:24:01 PST 2018


Author: sammccall
Date: Thu Jan 18 02:24:01 2018
New Revision: 322827

URL: http://llvm.org/viewvc/llvm-project?rev=322827&view=rev
Log:
[clangd] Output log messages to stderr when not configured (e.g. in tests). NFC

Modified:
    clang-tools-extra/trunk/clangd/Logger.cpp
    clang-tools-extra/trunk/clangd/Logger.h

Modified: clang-tools-extra/trunk/clangd/Logger.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Logger.cpp?rev=322827&r1=322826&r2=322827&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Logger.cpp (original)
+++ clang-tools-extra/trunk/clangd/Logger.cpp Thu Jan 18 02:24:01 2018
@@ -8,6 +8,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "Logger.h"
+#include "llvm/Support/raw_ostream.h"
+#include <mutex>
 
 namespace clang {
 namespace clangd {
@@ -24,9 +26,13 @@ LoggingSession::LoggingSession(clangd::L
 LoggingSession::~LoggingSession() { L = nullptr; }
 
 void log(const Context &Ctx, const llvm::Twine &Message) {
-  if (!L)
-    return;
-  L->log(Ctx, Message);
+  if (L)
+    L->log(Ctx, Message);
+  else {
+    static std::mutex Mu;
+    std::lock_guard<std::mutex> Guard(Mu);
+    llvm::errs() << Message << "\n";
+  }
 }
 
 } // namespace clangd

Modified: clang-tools-extra/trunk/clangd/Logger.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Logger.h?rev=322827&r1=322826&r2=322827&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/Logger.h (original)
+++ clang-tools-extra/trunk/clangd/Logger.h Thu Jan 18 02:24:01 2018
@@ -16,8 +16,9 @@
 namespace clang {
 namespace clangd {
 
-/// Main logging function. Logs messages to a global logger, which can be set up
-/// by LoggingSesssion.
+/// Main logging function.
+/// Logs messages to a global logger, which can be set up by LoggingSesssion.
+/// If no logger is registered, writes to llvm::errs().
 void log(const Context &Ctx, const llvm::Twine &Message);
 
 /// Interface to allow custom logging in clangd.




More information about the cfe-commits mailing list