[clangd] Rename and move trivial logger to Logger.cpp. NFC

llvm-svn: 344675
This commit is contained in:
Sam McCall 2018-10-17 07:39:32 +00:00
parent 1d478900a0
commit 645203a8c5
6 changed files with 33 additions and 33 deletions

View File

@ -24,7 +24,6 @@
namespace clang {
namespace clangd {
class JSONOutput;
class SymbolIndex;
/// This class exposes ClangdServer's capabilities via Language Server Protocol.

View File

@ -15,9 +15,7 @@
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/Errno.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/SourceMgr.h"
@ -59,18 +57,6 @@ public:
Key<std::unique_ptr<RequestSpan>> RequestSpan::RSKey;
} // namespace
void JSONOutput::log(Logger::Level Level,
const llvm::formatv_object_base &Message) {
if (Level < MinLevel)
return;
llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
trace::log(Message);
std::lock_guard<std::mutex> Guard(StreamMutex);
Logs << llvm::formatv("{0}[{1:%H:%M:%S.%L}] {2}\n", indicator(Level),
Timestamp, Message);
Logs.flush();
}
void clangd::reply(json::Value &&Result) {
auto ID = getRequestId();
if (!ID) {

View File

@ -25,23 +25,6 @@
namespace clang {
namespace clangd {
// Logs to an output stream, such as stderr.
// FIXME: Rename to StreamLogger or such, and move to Logger.h.
class JSONOutput : public Logger {
public:
JSONOutput(llvm::raw_ostream &Logs, Logger::Level MinLevel)
: MinLevel(MinLevel), Logs(Logs) {}
/// Write a line to the logging stream.
void log(Level, const llvm::formatv_object_base &Message) override;
private:
Logger::Level MinLevel;
llvm::raw_ostream &Logs;
std::mutex StreamMutex;
};
/// Sends a successful reply.
/// Current context must derive from JSONRPCDispatcher::Handler.
void reply(llvm::json::Value &&Result);

View File

@ -8,6 +8,9 @@
//===----------------------------------------------------------------------===//
#include "Logger.h"
#include "Trace.h"
#include "llvm/Support/Chrono.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <mutex>
@ -44,5 +47,17 @@ const char *detail::debugType(const char *Filename) {
return Filename;
}
void StreamLogger::log(Logger::Level Level,
const llvm::formatv_object_base &Message) {
if (Level < MinLevel)
return;
llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
trace::log(Message);
std::lock_guard<std::mutex> Guard(StreamMutex);
Logs << llvm::formatv("{0}[{1:%H:%M:%S.%L}] {2}\n", indicator(Level),
Timestamp, Message);
Logs.flush();
}
} // namespace clangd
} // namespace clang

View File

@ -15,6 +15,7 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatAdapters.h"
#include "llvm/Support/FormatVariadic.h"
#include <mutex>
namespace clang {
namespace clangd {
@ -86,6 +87,22 @@ public:
LoggingSession &operator=(LoggingSession const &) = delete;
};
// Logs to an output stream, such as stderr.
class StreamLogger : public Logger {
public:
StreamLogger(llvm::raw_ostream &Logs, Logger::Level MinLevel)
: MinLevel(MinLevel), Logs(Logs) {}
/// Write a line to the logging stream.
void log(Level, const llvm::formatv_object_base &Message) override;
private:
Logger::Level MinLevel;
llvm::raw_ostream &Logs;
std::mutex StreamMutex;
};
} // namespace clangd
} // namespace clang

View File

@ -253,7 +253,7 @@ int main(int argc, char *argv[]) {
// Use buffered stream to stderr (we still flush each log message). Unbuffered
// stream can cause significant (non-deterministic) latency for the logger.
llvm::errs().SetBuffered();
JSONOutput Logger(llvm::errs(), LogLevel);
StreamLogger Logger(llvm::errs(), LogLevel);
clangd::LoggingSession LoggingSession(Logger);
// If --compile-commands-dir arg was invoked, check value and override default