| 1 | //===- Logging.h - LSP Server Logging ----------------------*- 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_SUPPORT_LSP_LOGGING_H |
| 10 | #define LLVM_SUPPORT_LSP_LOGGING_H |
| 11 | |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "llvm/Support/FormatVariadic.h" |
| 14 | #include <mutex> |
| 15 | |
| 16 | namespace llvm { |
| 17 | namespace lsp { |
| 18 | |
| 19 | /// This class represents the main interface for logging, and allows for |
| 20 | /// filtering logging based on different levels of severity or significance. |
| 21 | class Logger { |
| 22 | public: |
| 23 | /// The level of significance for a log message. |
| 24 | enum class Level { Debug, Info, Error }; |
| 25 | |
| 26 | /// Set the severity level of the logger. |
| 27 | LLVM_ABI_FOR_TEST static void setLogLevel(Level LogLevel); |
| 28 | |
| 29 | /// Initiate a log message at various severity levels. These should be called |
| 30 | /// after a call to `initialize`. |
| 31 | template <typename... Ts> static void debug(const char *Fmt, Ts &&...Vals) { |
| 32 | log(LogLevel: Level::Debug, Fmt, Message: llvm::formatv(Fmt, std::forward<Ts>(Vals)...)); |
| 33 | } |
| 34 | template <typename... Ts> static void info(const char *Fmt, Ts &&...Vals) { |
| 35 | log(LogLevel: Level::Info, Fmt, Message: llvm::formatv(Fmt, std::forward<Ts>(Vals)...)); |
| 36 | } |
| 37 | template <typename... Ts> static void error(const char *Fmt, Ts &&...Vals) { |
| 38 | log(LogLevel: Level::Error, Fmt, Message: llvm::formatv(Fmt, std::forward<Ts>(Vals)...)); |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | Logger() = default; |
| 43 | |
| 44 | /// Return the main logger instance. |
| 45 | static Logger &get(); |
| 46 | |
| 47 | /// Start a log message with the given severity level. |
| 48 | LLVM_ABI_FOR_TEST static void log(Level LogLevel, const char *Fmt, |
| 49 | const llvm::formatv_object_base &Message); |
| 50 | |
| 51 | /// The minimum logging level. Messages with lower level are ignored. |
| 52 | Level LogLevel = Level::Error; |
| 53 | |
| 54 | /// A mutex used to guard logging. |
| 55 | std::mutex Mutex; |
| 56 | }; |
| 57 | } // namespace lsp |
| 58 | } // namespace llvm |
| 59 | |
| 60 | #endif // LLVM_SUPPORT_LSP_LOGGING_H |
| 61 | |