nixd
Toggle main menu visibility
Loading...
Searching...
No Matches
nixd
lspserver
src
Logger.cpp
Go to the documentation of this file.
1
//===--- Logger.cpp - Logger interface for clangd -------------------------===//
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
#include "
lspserver/Logger.h
"
10
11
#include <llvm/Support/Chrono.h>
12
#include <llvm/Support/Error.h>
13
#include <llvm/Support/FormatVariadic.h>
14
#include <llvm/Support/raw_ostream.h>
15
16
#include <boost/interprocess/sync/named_mutex.hpp>
17
#include <boost/interprocess/sync/scoped_lock.hpp>
18
19
#include <mutex>
20
21
#include <sys/mman.h>
22
#include <unistd.h>
23
24
namespace
lspserver
{
25
26
namespace
{
27
Logger
*L =
nullptr
;
28
}
// namespace
29
30
LoggingSession::LoggingSession
(
Logger
&Instance) {
31
assert(!L);
32
L = &Instance;
33
}
34
35
LoggingSession::~LoggingSession
() { L =
nullptr
; }
36
37
void
detail::logImpl
(
Logger::Level
Level,
const
char
*Fmt,
38
const
llvm::formatv_object_base &Message) {
39
if
(L)
40
L->
log
(Level, Fmt, Message);
41
else
{
42
static
std::mutex Mu;
43
std::lock_guard<std::mutex> Guard(Mu);
44
llvm::errs() << Message <<
"\n"
;
45
}
46
}
47
48
const
char
*
detail::debugType
(
const
char
*Filename) {
49
if
(
const
char
*Slash = strrchr(Filename,
'/'
))
50
return
Slash + 1;
51
if
(
const
char
*Backslash = strrchr(Filename,
'\\'
))
52
return
Backslash + 1;
53
return
Filename;
54
}
55
56
void
StreamLogger::log
(
Logger::Level
Level
,
const
char
*Fmt,
57
const
llvm::formatv_object_base &Message) {
58
using namespace
boost::interprocess;
59
if
(
Level
< MinLevel)
60
return
;
61
llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
62
std::lock_guard _(LogsLock);
63
64
Logs << llvm::formatv(
"{0}[{1:%H:%M:%S.%L}] {2}: {3}\n"
,
indicator
(
Level
),
65
Timestamp, getpid(), Message);
66
Logs.flush();
67
}
68
69
namespace
{
70
// Like llvm::StringError but with fewer options and no gratuitous copies.
71
class
SimpleStringError :
public
llvm::ErrorInfo<SimpleStringError> {
72
std::error_code EC;
73
std::string Message;
74
75
public
:
76
SimpleStringError(std::error_code EC, std::string &&Message)
77
: EC(EC), Message(std::move(Message)) {}
78
void
log
(llvm::raw_ostream &OS)
const override
{ OS << Message; }
79
std::string message()
const override
{
return
Message; }
80
std::error_code convertToErrorCode()
const override
{
return
EC; }
81
static
char
ID;
82
};
83
char
SimpleStringError::ID;
84
85
}
// namespace
86
87
llvm::Error
detail::error
(std::error_code EC, std::string &&Msg) {
88
return
llvm::make_error<SimpleStringError>(EC, std::move(Msg));
89
}
90
91
StreamLogger::StreamLogger
(llvm::raw_ostream &Logs,
Logger::Level
MinLevel)
92
: MinLevel(MinLevel), Logs(Logs) {}
93
}
// namespace lspserver
Logger.h
lspserver::Logger
Interface to allow custom logging in clangd.
Definition
Logger.h:13
lspserver::Logger::Level
Level
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::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::logImpl
void logImpl(Logger::Level, const char *Fmt, const llvm::formatv_object_base &)
Definition
Logger.cpp:37
lspserver::detail::debugType
const char * debugType(const char *Filename)
Definition
Logger.cpp:48
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::log
void log(const char *Fmt, Ts &&...Vals)
Definition
Logger.h:58
Generated by
1.17.0