2017-09-20 15:24:15 +08:00
|
|
|
//===--- Logger.cpp - Logger interface for clangd -------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-09-20 15:24:15 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Logger.h"
|
2018-10-17 15:39:32 +08:00
|
|
|
#include "Trace.h"
|
|
|
|
#include "llvm/Support/Chrono.h"
|
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-01-18 18:24:01 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <mutex>
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
namespace {
|
|
|
|
Logger *L = nullptr;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
LoggingSession::LoggingSession(clangd::Logger &Instance) {
|
|
|
|
assert(!L);
|
|
|
|
L = &Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
LoggingSession::~LoggingSession() { L = nullptr; }
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void detail::log(Logger::Level Level,
|
|
|
|
const llvm::formatv_object_base &Message) {
|
2018-01-18 18:24:01 +08:00
|
|
|
if (L)
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
L->log(Level, Message);
|
2018-01-18 18:24:01 +08:00
|
|
|
else {
|
|
|
|
static std::mutex Mu;
|
|
|
|
std::lock_guard<std::mutex> Guard(Mu);
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::errs() << Message << "\n";
|
2018-01-18 18:24:01 +08:00
|
|
|
}
|
2017-09-20 15:24:15 +08:00
|
|
|
}
|
|
|
|
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
const char *detail::debugType(const char *Filename) {
|
|
|
|
if (const char *Slash = strrchr(Filename, '/'))
|
|
|
|
return Slash + 1;
|
|
|
|
if (const char *Backslash = strrchr(Filename, '\\'))
|
|
|
|
return Backslash + 1;
|
|
|
|
return Filename;
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:39:32 +08:00
|
|
|
void StreamLogger::log(Logger::Level Level,
|
2019-01-07 23:45:19 +08:00
|
|
|
const llvm::formatv_object_base &Message) {
|
2018-10-17 15:39:32 +08:00
|
|
|
if (Level < MinLevel)
|
|
|
|
return;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
|
2018-10-17 15:39:32 +08:00
|
|
|
trace::log(Message);
|
|
|
|
std::lock_guard<std::mutex> Guard(StreamMutex);
|
2019-01-07 23:45:19 +08:00
|
|
|
Logs << llvm::formatv("{0}[{1:%H:%M:%S.%L}] {2}\n", indicator(Level),
|
|
|
|
Timestamp, Message);
|
2018-10-17 15:39:32 +08:00
|
|
|
Logs.flush();
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|