blob: cc6e3a0a7c954d2612e5fdf041af989ade3df665 [file] [log] [blame]
Ilya Biryukove5128f72017-09-20 07:24:15 +00001//===--- Logger.h - Logger interface for clangd ------------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
12
13#include "llvm/ADT/Twine.h"
Sam McCallbed58852018-07-11 10:35:11 +000014#include "llvm/Support/Debug.h"
15#include "llvm/Support/Error.h"
Sam McCallb34565e2018-07-12 08:00:21 +000016#include "llvm/Support/FormatAdapters.h"
Sam McCallbed58852018-07-11 10:35:11 +000017#include "llvm/Support/FormatVariadic.h"
Ilya Biryukove5128f72017-09-20 07:24:15 +000018
19namespace clang {
20namespace clangd {
21
22/// Interface to allow custom logging in clangd.
23class Logger {
24public:
25 virtual ~Logger() = default;
26
Sam McCallbed58852018-07-11 10:35:11 +000027 enum Level { Debug, Verbose, Info, Error };
28 static char indicator(Level L) { return "DVIE"[L]; }
29
Ilya Biryukove5128f72017-09-20 07:24:15 +000030 /// Implementations of this method must be thread-safe.
Sam McCallbed58852018-07-11 10:35:11 +000031 virtual void log(Level, const llvm::formatv_object_base &Message) = 0;
Ilya Biryukove5128f72017-09-20 07:24:15 +000032};
33
Sam McCallbed58852018-07-11 10:35:11 +000034namespace detail {
35const char *debugType(const char *Filename);
36void log(Logger::Level, const llvm::formatv_object_base &);
37
38// We often want to consume llvm::Errors by value when passing them to log().
Sam McCallb34565e2018-07-12 08:00:21 +000039// We automatically wrap them in llvm::fmt_consume() as formatv requires.
Sam McCallbed58852018-07-11 10:35:11 +000040template <typename T> T &&wrap(T &&V) { return std::forward<T>(V); }
Sam McCallb34565e2018-07-12 08:00:21 +000041inline decltype(fmt_consume(llvm::Error::success())) wrap(llvm::Error &&V) {
42 return fmt_consume(std::move(V));
43}
Sam McCallbed58852018-07-11 10:35:11 +000044template <typename... Ts>
45void log(Logger::Level L, const char *Fmt, Ts &&... Vals) {
46 detail::log(L, llvm::formatv(Fmt, detail::wrap(std::forward<Ts>(Vals))...));
47}
48} // namespace detail
49
50// Clangd logging functions write to a global logger set by LoggingSession.
51// If no logger is registered, writes to llvm::errs().
52// All accept llvm::formatv()-style arguments, e.g. log("Text={0}", Text).
53
54// elog() is used for "loud" errors and warnings.
55// This level is often visible to users.
56template <typename... Ts> void elog(const char *Fmt, Ts &&... Vals) {
57 detail::log(Logger::Error, Fmt, std::forward<Ts>(Vals)...);
58}
59// log() is used for information important to understanding a clangd session.
60// e.g. the names of LSP messages sent are logged at this level.
61// This level could be enabled in production builds to allow later inspection.
62template <typename... Ts> void log(const char *Fmt, Ts &&... Vals) {
63 detail::log(Logger::Info, Fmt, std::forward<Ts>(Vals)...);
64}
65// vlog() is used for details often needed for debugging clangd sessions.
66// This level would typically be enabled for clangd developers.
67template <typename... Ts> void vlog(const char *Fmt, Ts &&... Vals) {
68 detail::log(Logger::Verbose, Fmt, std::forward<Ts>(Vals)...);
69}
70// dlog only logs if --debug was passed, or --debug_only=Basename.
71// This level would be enabled in a targeted way when debugging.
72#define dlog(...) \
73 DEBUG_WITH_TYPE(::clang::clangd::detail::debugType(__FILE__), \
74 ::clang::clangd::detail::log(Logger::Debug, __VA_ARGS__))
75
Ilya Biryukov940901e2017-12-13 12:51:22 +000076/// Only one LoggingSession can be active at a time.
77class LoggingSession {
Ilya Biryukove5128f72017-09-20 07:24:15 +000078public:
Ilya Biryukov940901e2017-12-13 12:51:22 +000079 LoggingSession(clangd::Logger &Instance);
80 ~LoggingSession();
Ilya Biryukove5128f72017-09-20 07:24:15 +000081
Ilya Biryukov940901e2017-12-13 12:51:22 +000082 LoggingSession(LoggingSession &&) = delete;
83 LoggingSession &operator=(LoggingSession &&) = delete;
Ilya Biryukove5128f72017-09-20 07:24:15 +000084
Ilya Biryukov940901e2017-12-13 12:51:22 +000085 LoggingSession(LoggingSession const &) = delete;
86 LoggingSession &operator=(LoggingSession const &) = delete;
Ilya Biryukove5128f72017-09-20 07:24:15 +000087};
88
89} // namespace clangd
90} // namespace clang
91
92#endif