clang-tools  10.0.0svn
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 "Logger.h"
10 #include "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();
54  trace::log(Message);
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
void log(Logger::Level, const llvm::formatv_object_base &)
Definition: Logger.cpp:30
constexpr llvm::StringLiteral Message
const char * debugType(const char *Filename)
Definition: Logger.cpp:41
std::string Filename
Filename as a string.
Interface to allow custom logging in clangd.
Definition: Logger.h:23
void log(Level, const llvm::formatv_object_base &Message) override
Write a line to the logging stream.
Definition: Logger.cpp:49
LoggingSession(clangd::Logger &Instance)
Definition: Logger.cpp:23
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void log(const llvm::Twine &Message)
Records a single instant event, associated with the current thread.
Definition: Trace.cpp:205