clang-tools  8.0.0
Logger.cpp
Go to the documentation of this file.
1 //===--- Logger.cpp - Logger interface for clangd -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Logger.h"
11 #include "Trace.h"
12 #include "llvm/Support/Chrono.h"
13 #include "llvm/Support/FormatVariadic.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <mutex>
16 
17 namespace clang {
18 namespace clangd {
19 
20 namespace {
21 Logger *L = nullptr;
22 } // namespace
23 
25  assert(!L);
26  L = &Instance;
27 }
28 
30 
32  const llvm::formatv_object_base &Message) {
33  if (L)
34  L->log(Level, Message);
35  else {
36  static std::mutex Mu;
37  std::lock_guard<std::mutex> Guard(Mu);
38  llvm::errs() << Message << "\n";
39  }
40 }
41 
42 const 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 
62 } // namespace clangd
63 } // namespace clang
void log(Logger::Level, const llvm::formatv_object_base &)
Definition: Logger.cpp:31
const char * debugType(const char *Filename)
Definition: Logger.cpp:42
std::string Filename
Filename as a string.
Interface to allow custom logging in clangd.
Definition: Logger.h:24
void log(Level, const llvm::formatv_object_base &Message) override
Write a line to the logging stream.
Definition: Logger.cpp:50
LoggingSession(clangd::Logger &Instance)
Definition: Logger.cpp:24
===– 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