nixd
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1#pragma once
2
3#include <llvm/Support/Debug.h>
4#include <llvm/Support/Error.h>
5#include <llvm/Support/FormatAdapters.h>
6#include <llvm/Support/FormatVariadic.h>
7
8#include <mutex>
9
10namespace lspserver {
11
12/// Interface to allow custom logging in clangd.
13class Logger {
14public:
15 virtual ~Logger() = default;
16
17 /// The significance or severity of this message.
18 /// Typically used to filter the output to an interesting level.
19 enum Level : unsigned char { Debug, Verbose, Info, Error };
20 static char indicator(Level L) { return "DVIE"[L]; }
21
22 /// Implementations of this method must be thread-safe.
23 virtual void log(Level, const char *Fmt,
24 const llvm::formatv_object_base &Message) = 0;
25};
26
27namespace detail {
28const char *debugType(const char *Filename);
29void logImpl(Logger::Level, const char *Fmt, const llvm::formatv_object_base &);
30
31// We often want to consume llvm::Errors by value when passing them to log().
32// We automatically wrap them in llvm::fmt_consume() as formatv requires.
33template <typename T> T &&wrap(T &&V) { return std::forward<T>(V); }
34inline decltype(fmt_consume(llvm::Error::success())) wrap(llvm::Error &&V) {
35 return fmt_consume(std::move(V));
36}
37template <typename... Ts>
38void log(Logger::Level L, const char *Fmt, Ts &&...Vals) {
40 llvm::formatv(Fmt, detail::wrap(std::forward<Ts>(Vals))...));
41}
42
43llvm::Error error(std::error_code, std::string &&);
44} // namespace detail
45
46// Clangd logging functions write to a global logger set by LoggingSession.
47// If no logger is registered, writes to llvm::errs().
48// All accept llvm::formatv()-style arguments, e.g. log("Text={0}", Text).
49
50// elog() is used for "loud" errors and warnings.
51// This level is often visible to users.
52template <typename... Ts> void elog(const char *Fmt, Ts &&...Vals) {
53 detail::log(Logger::Error, Fmt, std::forward<Ts>(Vals)...);
54}
55// log() is used for information important to understand a clangd session.
56// e.g. the names of LSP messages sent are logged at this level.
57// This level could be enabled in production builds to allow later inspection.
58template <typename... Ts> void log(const char *Fmt, Ts &&...Vals) {
59 detail::log(Logger::Info, Fmt, std::forward<Ts>(Vals)...);
60}
61// vlog() is used for details often needed for debugging clangd sessions.
62// This level would typically be enabled for clangd developers.
63template <typename... Ts> void vlog(const char *Fmt, Ts &&...Vals) {
64 detail::log(Logger::Verbose, Fmt, std::forward<Ts>(Vals)...);
65}
66// error() constructs an llvm::Error object, using formatv()-style arguments.
67// It is not automatically logged! (This function is a little out of place).
68// The error simply embeds the message string.
69template <typename... Ts>
70llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals) {
71 // We must render the formatv_object eagerly, while references are valid.
72 return detail::error(
73 EC, llvm::formatv(Fmt, detail::wrap(std::forward<Ts>(Vals))...).str());
74}
75// Overload with no error_code conversion, the error will be inconvertible.
76template <typename... Ts> llvm::Error error(const char *Fmt, Ts &&...Vals) {
77 return detail::error(
78 llvm::inconvertibleErrorCode(),
79 llvm::formatv(Fmt, detail::wrap(std::forward<Ts>(Vals))...).str());
80}
81// Overload to avoid formatv complexity for simple strings.
82inline llvm::Error error(std::error_code EC, std::string Msg) {
83 return detail::error(EC, std::move(Msg));
84}
85// Overload for simple strings with no error_code conversion.
86inline llvm::Error error(std::string Msg) {
87 return detail::error(llvm::inconvertibleErrorCode(), std::move(Msg));
88}
89
90/// Only one LoggingSession can be active at a time.
102
103// Logs to an output stream, such as stderr.
104class StreamLogger : public Logger {
105 std::mutex LogsLock;
106
107public:
108 StreamLogger(llvm::raw_ostream &Logs, Logger::Level MinLevel);
109
110 /// Write a line to the logging stream.
111 void log(Level, const char *Fmt,
112 const llvm::formatv_object_base &Message) override;
113
114private:
115 Logger::Level MinLevel;
116 llvm::raw_ostream &Logs;
117};
118
119} // namespace lspserver
Interface to allow custom logging in clangd.
Definition Logger.h:13
virtual ~Logger()=default
static char indicator(Level L)
Definition Logger.h:20
virtual void log(Level, const char *Fmt, const llvm::formatv_object_base &Message)=0
Implementations of this method must be thread-safe.
Only one LoggingSession can be active at a time.
Definition Logger.h:91
LoggingSession & operator=(LoggingSession &&)=delete
LoggingSession(LoggingSession &&)=delete
LoggingSession(LoggingSession const &)=delete
LoggingSession & operator=(LoggingSession const &)=delete
LoggingSession(Logger &Instance)
Definition Logger.cpp:30
void log(Level, const char *Fmt, const llvm::formatv_object_base &Message) override
Write a line to the logging stream.
Definition Logger.cpp:56
StreamLogger(llvm::raw_ostream &Logs, Logger::Level MinLevel)
Definition Logger.cpp:91
void logImpl(Logger::Level, const char *Fmt, const llvm::formatv_object_base &)
Definition Logger.cpp:37
T && wrap(T &&V)
Definition Logger.h:33
const char * debugType(const char *Filename)
Definition Logger.cpp:48
void log(Logger::Level L, const char *Fmt, Ts &&...Vals)
Definition Logger.h:38
llvm::Error error(std::error_code, std::string &&)
Definition Logger.cpp:87
Whether current platform treats paths case insensitively.
Definition Connection.h:11
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals)
Definition Logger.h:70
bool fromJSON(const llvm::json::Value &, URIForFile &, llvm::json::Path)
void elog(const char *Fmt, Ts &&...Vals)
Definition Logger.h:52
void vlog(const char *Fmt, Ts &&...Vals)
Definition Logger.h:63
void log(const char *Fmt, Ts &&...Vals)
Definition Logger.h:58