clang-tools  11.0.0
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/FormatVariadic.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include <mutex>
15 
16 namespace clang {
17 namespace clangd {
18 
19 namespace {
20 Logger *L = nullptr;
21 } // namespace
22 
24  assert(!L);
25  L = &Instance;
26 }
27 
29 
31  const llvm::formatv_object_base &Message) {
32  if (L)
33  L->log(Level, Message);
34  else {
35  static std::mutex Mu;
36  std::lock_guard<std::mutex> Guard(Mu);
37  llvm::errs() << Message << "\n";
38  }
39 }
40 
41 const char *detail::debugType(const char *Filename) {
42  if (const char *Slash = strrchr(Filename, '/'))
43  return Slash + 1;
44  if (const char *Backslash = strrchr(Filename, '\\'))
45  return Backslash + 1;
46  return Filename;
47 }
48 
50  const llvm::formatv_object_base &Message) {
51  if (Level < MinLevel)
52  return;
53  llvm::sys::TimePoint<> Timestamp = std::chrono::system_clock::now();
55  std::lock_guard<std::mutex> Guard(StreamMutex);
56  Logs << llvm::formatv("{0}[{1:%H:%M:%S.%L}] {2}\n", indicator(Level),
57  Timestamp, Message);
58  Logs.flush();
59 }
60 
61 } // namespace clangd
62 } // namespace clang
clang::clangd::Logger
Interface to allow custom logging in clangd.
Definition: Logger.h:23
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:39
clang::clangd::Logger::Level
Level
Definition: Logger.h:27
Trace.h
clang::clangd::LoggingSession::~LoggingSession
~LoggingSession()
Definition: Logger.cpp:28
clang::clangd::trace::log
void log(const llvm::Twine &Message)
Records a single instant event, associated with the current thread.
Definition: Trace.cpp:274
clang::clangd::detail::debugType
const char * debugType(const char *Filename)
Definition: Logger.cpp:41
clang::clangd::StreamLogger::log
void log(Level, const llvm::formatv_object_base &Message) override
Write a line to the logging stream.
Definition: Logger.cpp:49
Logger.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Logger::indicator
static char indicator(Level L)
Definition: Logger.h:28
clang::clangd::LoggingSession::LoggingSession
LoggingSession(clangd::Logger &Instance)
Definition: Logger.cpp:23
clang::clangd::detail::log
void log(Logger::Level, const llvm::formatv_object_base &)
Definition: Logger.cpp:30