2017-09-20 15:24:15 +08:00
|
|
|
//===--- Logger.h - Logger interface for clangd ------------------*- C++-*-===//
|
|
|
|
//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/Twine.h"
|
[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
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
2018-07-12 16:00:21 +08:00
|
|
|
#include "llvm/Support/FormatAdapters.h"
|
[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
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-10-17 15:39:32 +08:00
|
|
|
#include <mutex>
|
2017-09-20 15:24:15 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
/// Interface to allow custom logging in clangd.
|
|
|
|
class Logger {
|
|
|
|
public:
|
|
|
|
virtual ~Logger() = default;
|
|
|
|
|
[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
|
|
|
enum Level { Debug, Verbose, Info, Error };
|
|
|
|
static char indicator(Level L) { return "DVIE"[L]; }
|
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
/// Implementations of this method must be thread-safe.
|
[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
|
|
|
virtual void log(Level, const llvm::formatv_object_base &Message) = 0;
|
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
|
|
|
namespace detail {
|
|
|
|
const char *debugType(const char *Filename);
|
|
|
|
void log(Logger::Level, const llvm::formatv_object_base &);
|
|
|
|
|
|
|
|
// We often want to consume llvm::Errors by value when passing them to log().
|
2018-07-12 16:00:21 +08:00
|
|
|
// We automatically wrap them in llvm::fmt_consume() as formatv requires.
|
[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
|
|
|
template <typename T> T &&wrap(T &&V) { return std::forward<T>(V); }
|
2018-07-12 16:00:21 +08:00
|
|
|
inline decltype(fmt_consume(llvm::Error::success())) wrap(llvm::Error &&V) {
|
|
|
|
return fmt_consume(std::move(V));
|
|
|
|
}
|
[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
|
|
|
template <typename... Ts>
|
|
|
|
void log(Logger::Level L, const char *Fmt, Ts &&... Vals) {
|
|
|
|
detail::log(L, llvm::formatv(Fmt, detail::wrap(std::forward<Ts>(Vals))...));
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// Clangd logging functions write to a global logger set by LoggingSession.
|
|
|
|
// If no logger is registered, writes to llvm::errs().
|
|
|
|
// All accept llvm::formatv()-style arguments, e.g. log("Text={0}", Text).
|
|
|
|
|
|
|
|
// elog() is used for "loud" errors and warnings.
|
|
|
|
// This level is often visible to users.
|
|
|
|
template <typename... Ts> void elog(const char *Fmt, Ts &&... Vals) {
|
|
|
|
detail::log(Logger::Error, Fmt, std::forward<Ts>(Vals)...);
|
|
|
|
}
|
2018-09-01 15:47:03 +08:00
|
|
|
// log() is used for information important to understand a clangd session.
|
[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
|
|
|
// e.g. the names of LSP messages sent are logged at this level.
|
|
|
|
// This level could be enabled in production builds to allow later inspection.
|
|
|
|
template <typename... Ts> void log(const char *Fmt, Ts &&... Vals) {
|
|
|
|
detail::log(Logger::Info, Fmt, std::forward<Ts>(Vals)...);
|
|
|
|
}
|
|
|
|
// vlog() is used for details often needed for debugging clangd sessions.
|
|
|
|
// This level would typically be enabled for clangd developers.
|
|
|
|
template <typename... Ts> void vlog(const char *Fmt, Ts &&... Vals) {
|
|
|
|
detail::log(Logger::Verbose, Fmt, std::forward<Ts>(Vals)...);
|
|
|
|
}
|
|
|
|
// dlog only logs if --debug was passed, or --debug_only=Basename.
|
|
|
|
// This level would be enabled in a targeted way when debugging.
|
|
|
|
#define dlog(...) \
|
|
|
|
DEBUG_WITH_TYPE(::clang::clangd::detail::debugType(__FILE__), \
|
|
|
|
::clang::clangd::detail::log(Logger::Debug, __VA_ARGS__))
|
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
/// Only one LoggingSession can be active at a time.
|
|
|
|
class LoggingSession {
|
2017-09-20 15:24:15 +08:00
|
|
|
public:
|
2017-12-13 20:51:22 +08:00
|
|
|
LoggingSession(clangd::Logger &Instance);
|
|
|
|
~LoggingSession();
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
LoggingSession(LoggingSession &&) = delete;
|
|
|
|
LoggingSession &operator=(LoggingSession &&) = delete;
|
2017-09-20 15:24:15 +08:00
|
|
|
|
2017-12-13 20:51:22 +08:00
|
|
|
LoggingSession(LoggingSession const &) = delete;
|
|
|
|
LoggingSession &operator=(LoggingSession const &) = delete;
|
2017-09-20 15:24:15 +08:00
|
|
|
};
|
|
|
|
|
2018-10-17 15:39:32 +08:00
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
2017-09-20 15:24:15 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|