clang-tools 20.0.0git
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 "support/Logger.h"
10#include "support/Trace.h"
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#include <mutex>
16
17namespace clang {
18namespace clangd {
19
20namespace {
21Logger *L = nullptr;
22} // namespace
23
25 assert(!L);
26 L = &Instance;
27}
28
30
31void detail::logImpl(Logger::Level Level, const char *Fmt,
32 const llvm::formatv_object_base &Message) {
33 if (L)
34 L->log(Level, Fmt, Message);
35 else {
36 static std::mutex Mu;
37 std::lock_guard<std::mutex> Guard(Mu);
38 llvm::errs() << Message << "\n";
39 }
40}
41
42const char *detail::debugType(const char *Filename) {
43 if (const char *Slash = strrchr(Filename, '/'))
44 return Slash + 1;
45 if (const char *Backslash = strrchr(Filename, '\\'))
46 return Backslash + 1;
47 return Filename;
48}
49
51 const llvm::formatv_object_base &Message) {
52 if (Level < MinLevel)
53 return;
54 llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
55 trace::log(Message);
56 std::lock_guard<std::mutex> Guard(StreamMutex);
57 Logs << llvm::formatv("{0}[{1:%H:%M:%S.%L}] {2}\n", indicator(Level),
58 Timestamp, Message);
59 Logs.flush();
60}
61
62namespace {
63// Like llvm::StringError but with fewer options and no gratuitous copies.
64class SimpleStringError : public llvm::ErrorInfo<SimpleStringError> {
65 std::error_code EC;
66 std::string Message;
67
68public:
69 SimpleStringError(std::error_code EC, std::string &&Message)
70 : EC(EC), Message(std::move(Message)) {}
71 void log(llvm::raw_ostream &OS) const override { OS << Message; }
72 std::string message() const override { return Message; }
73 std::error_code convertToErrorCode() const override { return EC; }
74 static char ID;
75};
76char SimpleStringError::ID;
77
78} // namespace
79
80llvm::Error detail::error(std::error_code EC, std::string &&Msg) {
81 return llvm::make_error<SimpleStringError>(EC, std::move(Msg));
82}
83
84} // namespace clangd
85} // namespace clang
std::string Filename
Filename as a string.
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
Interface to allow custom logging in clangd.
Definition: Logger.h:22
virtual void log(Level, const char *Fmt, const llvm::formatv_object_base &Message)=0
Implementations of this method must be thread-safe.
Level
The significance or severity of this message.
Definition: Logger.h:28
static char indicator(Level L)
Definition: Logger.h:29
LoggingSession(clangd::Logger &Instance)
Definition: Logger.cpp:24
void log(Level, const char *Fmt, const llvm::formatv_object_base &Message) override
Write a line to the logging stream.
Definition: Logger.cpp:50
const char * debugType(const char *Filename)
Definition: Logger.cpp:42
void logImpl(Logger::Level, const char *Fmt, const llvm::formatv_object_base &)
Definition: Logger.cpp:31
llvm::Error error(std::error_code, std::string &&)
Definition: Logger.cpp:80
void log(const llvm::Twine &Message)
Records a single instant event, associated with the current thread.
Definition: Trace.cpp:277
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:67
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//