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" |
Sam McCall | b34565e | 2018-07-12 08:00:21 +0000 | [diff] [blame] | 16 | #include "llvm/Support/FormatAdapters.h" |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FormatVariadic.h" |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 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 | |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 27 | enum Level { Debug, Verbose, Info, Error }; |
| 28 | static char indicator(Level L) { return "DVIE"[L]; } |
| 29 | |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 30 | /// Implementations of this method must be thread-safe. |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 31 | virtual void log(Level, const llvm::formatv_object_base &Message) = 0; |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 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(). |
Sam McCall | b34565e | 2018-07-12 08:00:21 +0000 | [diff] [blame] | 39 | // We automatically wrap them in llvm::fmt_consume() as formatv requires. |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 40 | template <typename T> T &&wrap(T &&V) { return std::forward<T>(V); } |
Sam McCall | b34565e | 2018-07-12 08:00:21 +0000 | [diff] [blame] | 41 | inline decltype(fmt_consume(llvm::Error::success())) wrap(llvm::Error &&V) { |
| 42 | return fmt_consume(std::move(V)); |
| 43 | } |
Sam McCall | bed5885 | 2018-07-11 10:35:11 +0000 | [diff] [blame] | 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 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. |
| 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 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 76 | /// Only one LoggingSession can be active at a time. |
| 77 | class LoggingSession { |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 78 | public: |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 79 | LoggingSession(clangd::Logger &Instance); |
| 80 | ~LoggingSession(); |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 81 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 82 | LoggingSession(LoggingSession &&) = delete; |
| 83 | LoggingSession &operator=(LoggingSession &&) = delete; |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 84 | |
Ilya Biryukov | 940901e | 2017-12-13 12:51:22 +0000 | [diff] [blame] | 85 | LoggingSession(LoggingSession const &) = delete; |
| 86 | LoggingSession &operator=(LoggingSession const &) = delete; |
Ilya Biryukov | e5128f7 | 2017-09-20 07:24:15 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace clangd |
| 90 | } // namespace clang |
| 91 | |
| 92 | #endif |