nixd
Toggle main menu visibility
Loading...
Searching...
No Matches
nixd
lspserver
include
lspserver
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
10
namespace
lspserver
{
11
12
/// Interface to allow custom logging in clangd.
13
class
Logger
{
14
public
:
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
27
namespace
detail
{
28
const
char
*
debugType
(
const
char
*Filename);
29
void
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.
33
template
<
typename
T> T &&
wrap
(T &&V) {
return
std::forward<T>(V); }
34
inline
decltype
(fmt_consume(llvm::Error::success()))
wrap
(llvm::Error &&V) {
35
return
fmt_consume(std::move(V));
36
}
37
template
<
typename
... Ts>
38
void
log
(
Logger::Level
L,
const
char
*Fmt, Ts &&...Vals) {
39
detail::logImpl
(L, Fmt,
40
llvm::formatv(Fmt,
detail::wrap
(std::forward<Ts>(Vals))...));
41
}
42
43
llvm::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.
52
template
<
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.
58
template
<
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.
63
template
<
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.
69
template
<
typename
... Ts>
70
llvm::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.
76
template
<
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.
82
inline
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.
86
inline
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.
91
class
LoggingSession
{
92
public
:
93
LoggingSession
(
Logger
&Instance);
94
~LoggingSession
();
95
96
LoggingSession
(
LoggingSession
&&) =
delete
;
97
LoggingSession
&
operator=
(
LoggingSession
&&) =
delete
;
98
99
LoggingSession
(
LoggingSession
const
&) =
delete
;
100
LoggingSession
&
operator=
(
LoggingSession
const
&) =
delete
;
101
};
102
103
// Logs to an output stream, such as stderr.
104
class
StreamLogger
:
public
Logger
{
105
std::mutex LogsLock;
106
107
public
:
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
114
private
:
115
Logger::Level
MinLevel;
116
llvm::raw_ostream &Logs;
117
};
118
119
}
// namespace lspserver
lspserver::Logger
Interface to allow custom logging in clangd.
Definition
Logger.h:13
lspserver::Logger::~Logger
virtual ~Logger()=default
lspserver::Logger::Level
Level
Definition
Logger.h:19
lspserver::Logger::Error
@ Error
Definition
Logger.h:19
lspserver::Logger::Info
@ Info
Definition
Logger.h:19
lspserver::Logger::Verbose
@ Verbose
Definition
Logger.h:19
lspserver::Logger::Debug
@ Debug
Definition
Logger.h:19
lspserver::Logger::indicator
static char indicator(Level L)
Definition
Logger.h:20
lspserver::Logger::log
virtual void log(Level, const char *Fmt, const llvm::formatv_object_base &Message)=0
Implementations of this method must be thread-safe.
lspserver::LoggingSession::operator=
LoggingSession & operator=(LoggingSession &&)=delete
lspserver::LoggingSession::LoggingSession
LoggingSession(LoggingSession &&)=delete
lspserver::LoggingSession::LoggingSession
LoggingSession(LoggingSession const &)=delete
lspserver::LoggingSession::operator=
LoggingSession & operator=(LoggingSession const &)=delete
lspserver::LoggingSession::LoggingSession
LoggingSession(Logger &Instance)
Definition
Logger.cpp:30
lspserver::LoggingSession::~LoggingSession
~LoggingSession()
Definition
Logger.cpp:35
lspserver::StreamLogger::log
void log(Level, const char *Fmt, const llvm::formatv_object_base &Message) override
Write a line to the logging stream.
Definition
Logger.cpp:56
lspserver::StreamLogger::StreamLogger
StreamLogger(llvm::raw_ostream &Logs, Logger::Level MinLevel)
Definition
Logger.cpp:91
lspserver::detail
Definition
Logger.h:27
lspserver::detail::logImpl
void logImpl(Logger::Level, const char *Fmt, const llvm::formatv_object_base &)
Definition
Logger.cpp:37
lspserver::detail::wrap
T && wrap(T &&V)
Definition
Logger.h:33
lspserver::detail::debugType
const char * debugType(const char *Filename)
Definition
Logger.cpp:48
lspserver::detail::log
void log(Logger::Level L, const char *Fmt, Ts &&...Vals)
Definition
Logger.h:38
lspserver::detail::error
llvm::Error error(std::error_code, std::string &&)
Definition
Logger.cpp:87
lspserver
Whether current platform treats paths case insensitively.
Definition
Connection.h:11
lspserver::error
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals)
Definition
Logger.h:70
lspserver::elog
void elog(const char *Fmt, Ts &&...Vals)
Definition
Logger.h:52
lspserver::vlog
void vlog(const char *Fmt, Ts &&...Vals)
Definition
Logger.h:63
lspserver::log
void log(const char *Fmt, Ts &&...Vals)
Definition
Logger.h:58
Generated by
1.17.0