Mercurial > hg > CbC > CbC_llvm
comparison clang-tools-extra/clangd/support/Logger.h @ 173:0572611fdcc8 llvm10 llvm12
reorgnization done
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2020 11:55:54 +0900 |
parents | |
children | 2e18cbf3894f |
comparison
equal
deleted
inserted
replaced
172:9fbae9c8bf63 | 173:0572611fdcc8 |
---|---|
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_SUPPORT_LOGGER_H | |
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_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 |