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