1 //===--- Logger.cpp - Logger interface for clangd -------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
24 LoggingSession::LoggingSession(clangd::Logger
&Instance
) {
29 LoggingSession::~LoggingSession() { L
= nullptr; }
31 void detail::logImpl(Logger::Level Level
, const char *Fmt
,
32 const llvm::formatv_object_base
&Message
) {
34 L
->log(Level
, Fmt
, Message
);
37 std::lock_guard
<std::mutex
> Guard(Mu
);
38 llvm::errs() << Message
<< "\n";
42 const char *detail::debugType(const char *Filename
) {
43 if (const char *Slash
= strrchr(Filename
, '/'))
45 if (const char *Backslash
= strrchr(Filename
, '\\'))
50 void StreamLogger::log(Logger::Level Level
, const char *Fmt
,
51 const llvm::formatv_object_base
&Message
) {
54 llvm::sys::TimePoint
<> Timestamp
= std::chrono::system_clock::now();
56 std::lock_guard
<std::mutex
> Guard(StreamMutex
);
57 Logs
<< llvm::formatv("{0}[{1:%H:%M:%S.%L}] {2}\n", indicator(Level
),
63 // Like llvm::StringError but with fewer options and no gratuitous copies.
64 class SimpleStringError
: public llvm::ErrorInfo
<SimpleStringError
> {
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
; }
76 char SimpleStringError::ID
;
80 llvm::Error
detail::error(std::error_code EC
, std::string
&&Msg
) {
81 return llvm::make_error
<SimpleStringError
>(EC
, std::move(Msg
));