Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 1 | //===--- 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 McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 14 | #include "llvm/Support/Debug.h" |
| 15 | #include "llvm/Support/Error.h" |
| 16 | #include "llvm/Support/FormatVariadic.h" |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 17 | |
| 18 | namespace clang { |
| 19 | namespace clangd { |
| 20 | |
| 21 | /// Interface to allow custom logging in clangd. |
| 22 | class Logger { |
| 23 | public: |
| 24 | virtual ~Logger() = default; |
| 25 | |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 26 | enum Level { Debug, Verbose, Info, Error }; |
| 27 | static char indicator(Level L) { return "DVIE"[L]; } |
| 28 | |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 29 | /// Implementations of this method must be thread-safe. |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 30 | virtual void log(Level, const llvm::formatv_object_base &Message) = 0; |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame^] | 33 | namespace detail { |
| 34 | const char *debugType(const char *Filename); |
| 35 | void log(Logger::Level, const llvm::formatv_object_base &); |
| 36 | |
| 37 | // We often want to consume llvm::Errors by value when passing them to log(). |
| 38 | // This is tricky because the logging infrastructure must mark them as handled. |
| 39 | // When forwarding argument to formatv, we wrap Errors-by-value in this type |
| 40 | // whose destructor handles the cleanup. |
| 41 | // FIXME: simplify after D49170 lands. |
| 42 | struct WrappedError { |
| 43 | llvm::Error E; |
| 44 | WrappedError(WrappedError &&) = default; |
| 45 | ~WrappedError() { consumeError(std::move(E)); } |
| 46 | }; |
| 47 | inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, |
| 48 | const WrappedError &Err) { |
| 49 | return OS << Err.E; |
| 50 | } |
| 51 | template <typename T> T &&wrap(T &&V) { return std::forward<T>(V); } |
| 52 | inline WrappedError wrap(llvm::Error &&V) { return WrappedError{std::move(V)}; } |
| 53 | template <typename... Ts> |
| 54 | void log(Logger::Level L, const char *Fmt, Ts &&... Vals) { |
| 55 | detail::log(L, llvm::formatv(Fmt, detail::wrap(std::forward<Ts>(Vals))...)); |
| 56 | } |
| 57 | } // namespace detail |
| 58 | |
| 59 | // Clangd logging functions write to a global logger set by LoggingSession. |
| 60 | // If no logger is registered, writes to llvm::errs(). |
| 61 | // All accept llvm::formatv()-style arguments, e.g. log("Text={0}", Text). |
| 62 | |
| 63 | // elog() is used for "loud" errors and warnings. |
| 64 | // This level is often visible to users. |
| 65 | template <typename... Ts> void elog(const char *Fmt, Ts &&... Vals) { |
| 66 | detail::log(Logger::Error, Fmt, std::forward<Ts>(Vals)...); |
| 67 | } |
| 68 | // log() is used for information important to understanding a clangd session. |
| 69 | // e.g. the names of LSP messages sent are logged at this level. |
| 70 | // This level could be enabled in production builds to allow later inspection. |
| 71 | template <typename... Ts> void log(const char *Fmt, Ts &&... Vals) { |
| 72 | detail::log(Logger::Info, Fmt, std::forward<Ts>(Vals)...); |
| 73 | } |
| 74 | // vlog() is used for details often needed for debugging clangd sessions. |
| 75 | // This level would typically be enabled for clangd developers. |
| 76 | template <typename... Ts> void vlog(const char *Fmt, Ts &&... Vals) { |
| 77 | detail::log(Logger::Verbose, Fmt, std::forward<Ts>(Vals)...); |
| 78 | } |
| 79 | // dlog only logs if --debug was passed, or --debug_only=Basename. |
| 80 | // This level would be enabled in a targeted way when debugging. |
| 81 | #define dlog(...) \ |
| 82 | DEBUG_WITH_TYPE(::clang::clangd::detail::debugType(__FILE__), \ |
| 83 | ::clang::clangd::detail::log(Logger::Debug, __VA_ARGS__)) |
| 84 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 85 | /// Only one LoggingSession can be active at a time. |
| 86 | class LoggingSession { |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 87 | public: |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 88 | LoggingSession(clangd::Logger &Instance); |
| 89 | ~LoggingSession(); |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 90 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 91 | LoggingSession(LoggingSession &&) = delete; |
| 92 | LoggingSession &operator=(LoggingSession &&) = delete; |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 93 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 94 | LoggingSession(LoggingSession const &) = delete; |
| 95 | LoggingSession &operator=(LoggingSession const &) = delete; |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace clangd |
| 99 | } // namespace clang |
| 100 | |
| 101 | #endif |