nixd
Loading...
Searching...
No Matches
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
24namespace lspserver {
25
26namespace {
27Logger *L = nullptr;
28} // namespace
29
34
36
37void 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
48const 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
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
69namespace {
70// Like llvm::StringError but with fewer options and no gratuitous copies.
71class SimpleStringError : public llvm::ErrorInfo<SimpleStringError> {
72 std::error_code EC;
73 std::string Message;
74
75public:
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};
83char SimpleStringError::ID;
84
85} // namespace
86
87llvm::Error detail::error(std::error_code EC, std::string &&Msg) {
88 return llvm::make_error<SimpleStringError>(EC, std::move(Msg));
89}
90
91StreamLogger::StreamLogger(llvm::raw_ostream &Logs, Logger::Level MinLevel)
92 : MinLevel(MinLevel), Logs(Logs) {}
93} // namespace lspserver
Interface to allow custom logging in clangd.
Definition Logger.h:13
static char indicator(Level L)
Definition Logger.h:20
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
const char * debugType(const char *Filename)
Definition Logger.cpp:48
llvm::Error error(std::error_code, std::string &&)
Definition Logger.cpp:87
Whether current platform treats paths case insensitively.
Definition Connection.h:11
bool fromJSON(const llvm::json::Value &, URIForFile &, llvm::json::Path)
void log(const char *Fmt, Ts &&...Vals)
Definition Logger.h:58