clang-tools 10.0.0

Logger.h
Go to the documentation of this file.
1 //===--- Logger.h - Logger interface for clangd ------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
11
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/FormatAdapters.h"
16 #include "llvm/Support/FormatVariadic.h"
17 #include <mutex>
18
19 namespace clang {
20 namespace clangd {
21
22 /// Interface to allow custom logging in clangd.
23 class Logger {
24 public:
25 virtual ~Logger() = default;
26
27 enum Level { Debug, Verbose, Info, Error };
28 static char indicator(Level L) { return "DVIE"[L]; }
29
30 /// Implementations of this method must be thread-safe.
31 virtual void log(Level, const llvm::formatv_object_base &Message) = 0;
32 };
33
34 namespace detail {
35 const char *debugType(const char *Filename);
36 void log(Logger::Level, const llvm::formatv_object_base &);
37
38 // We often want to consume llvm::Errors by value when passing them to log().
39 // We automatically wrap them in llvm::fmt_consume() as formatv requires.
40 template <typename T> T &&wrap(T &&V) { return std::forward<T>(V); }
41 inline decltype(fmt_consume(llvm::Error::success())) wrap(llvm::Error &&V) {
42 return fmt_consume(std::move(V));
43 }
44 template <typename... Ts>
45 void 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.
56 template <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 understand 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.
62 template <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.
67 template <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
76 /// Only one LoggingSession can be active at a time.
77 class LoggingSession {
78 public:
79 LoggingSession(clangd::Logger &Instance);
80 ~LoggingSession();
81
82 LoggingSession(LoggingSession &&) = delete;
83 LoggingSession &operator=(LoggingSession &&) = delete;
84
85 LoggingSession(LoggingSession const &) = delete;
86 LoggingSession &operator=(LoggingSession const &) = delete;
87 };
88
89 // Logs to an output stream, such as stderr.
90 class StreamLogger : public Logger {
91 public:
92 StreamLogger(llvm::raw_ostream &Logs, Logger::Level MinLevel)
93 : MinLevel(MinLevel), Logs(Logs) {}
94
95 /// Write a line to the logging stream.
96 void log(Level, const llvm::formatv_object_base &Message) override;
97
98 private:
99 Logger::Level MinLevel;
100 llvm::raw_ostream &Logs;
101
102 std::mutex StreamMutex;
103 };
104
105 } // namespace clangd
106 } // namespace clang
107
108 #endif
clang::clangd::Logger::Debug
Definition: Logger.h:27
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::clangd::detail::log
void log(Logger::Level, const llvm::formatv_object_base &)
Definition: Logger.cpp:30
clang::tidy::modernize::Message
constexpr llvm::StringLiteral Message
Definition: UseTrailingReturnTypeCheck.cpp:101
clang::clangd::Logger::indicator
static char indicator(Level L)
Definition: Logger.h:28
clang::clangd::detail::wrap
decltype(fmt_consume(llvm::Error::success())) wrap(llvm::Error &&V)
Definition: Logger.h:41
clang::clangd::vlog
void vlog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:67
clang::clangd::elog
void elog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:56
clang::clangd::detail::debugType
const char * debugType(const char *Filename)
Definition: Logger.cpp:41
clang::clangd::StreamLogger
Definition: Logger.h:90
clang::clangd::Logger::log
virtual void log(Level, const llvm::formatv_object_base &Message)=0
Implementations of this method must be thread-safe.
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
clang::clangd::Logger
Interface to allow custom logging in clangd.
Definition: Logger.h:23
clang::clangd::LoggingSession
Only one LoggingSession can be active at a time.
Definition: Logger.h:77
clang::clangd::Logger::Level
Level
Definition: Logger.h:27
clang::clangd::Logger::Info
Definition: Logger.h:27
clang::clangd::Logger::~Logger
virtual ~Logger()=default
clang::clangd::detail::wrap
T && wrap(T &&V)
Definition: Logger.h:40
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::StreamLogger::StreamLogger
StreamLogger(llvm::raw_ostream &Logs, Logger::Level MinLevel)
Definition: Logger.h:92
clang::clangd::Logger::Verbose
Definition: Logger.h:27
clang::clangd::Logger::Error
Definition: Logger.h:27