summaryrefslogtreecommitdiffstats
path: root/clangd/Logger.h
blob: 30dd6cc2ad06f90ee31254c1fee28b94727c5dfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===--- Logger.h - Logger interface for clangd ------------------*- C++-*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H

#include "llvm/ADT/Twine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatVariadic.h"

namespace clang {
namespace clangd {

/// Interface to allow custom logging in clangd.
class Logger {
public:
  virtual ~Logger() = default;

  enum Level { Debug, Verbose, Info, Error };
  static char indicator(Level L) { return "DVIE"[L]; }

  /// Implementations of this method must be thread-safe.
  virtual void log(Level, const llvm::formatv_object_base &Message) = 0;
};

namespace detail {
const char *debugType(const char *Filename);
void log(Logger::Level, const llvm::formatv_object_base &);

// We often want to consume llvm::Errors by value when passing them to log().
// This is tricky because the logging infrastructure must mark them as handled.
// When forwarding argument to formatv, we wrap Errors-by-value in this type
// whose destructor handles the cleanup.
// FIXME: simplify after D49170 lands.
struct WrappedError {
  llvm::Error E;
  WrappedError(WrappedError &&) = default;
  ~WrappedError() { consumeError(std::move(E)); }
};
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
                                     const WrappedError &Err) {
  return OS << Err.E;
}
template <typename T> T &&wrap(T &&V) { return std::forward<T>(V); }
inline WrappedError wrap(llvm::Error &&V) { return WrappedError{std::move(V)}; }
template <typename... Ts>
void log(Logger::Level L, const char *Fmt, Ts &&... Vals) {
  detail::log(L, llvm::formatv(Fmt, detail::wrap(std::forward<Ts>(Vals))...));
}
} // namespace detail

// Clangd logging functions write to a global logger set by LoggingSession.
// If no logger is registered, writes to llvm::errs().
// All accept llvm::formatv()-style arguments, e.g. log("Text={0}", Text).

// elog() is used for "loud" errors and warnings.
// This level is often visible to users.
template <typename... Ts> void elog(const char *Fmt, Ts &&... Vals) {
  detail::log(Logger::Error, Fmt, std::forward<Ts>(Vals)...);
}
// log() is used for information important to understanding a clangd session.
// e.g. the names of LSP messages sent are logged at this level.
// This level could be enabled in production builds to allow later inspection.
template <typename... Ts> void log(const char *Fmt, Ts &&... Vals) {
  detail::log(Logger::Info, Fmt, std::forward<Ts>(Vals)...);
}
// vlog() is used for details often needed for debugging clangd sessions.
// This level would typically be enabled for clangd developers.
template <typename... Ts> void vlog(const char *Fmt, Ts &&... Vals) {
  detail::log(Logger::Verbose, Fmt, std::forward<Ts>(Vals)...);
}
// dlog only logs if --debug was passed, or --debug_only=Basename.
// This level would be enabled in a targeted way when debugging.
#define dlog(...)                                                              \
  DEBUG_WITH_TYPE(::clang::clangd::detail::debugType(__FILE__),                \
                  ::clang::clangd::detail::log(Logger::Debug, __VA_ARGS__))

/// Only one LoggingSession can be active at a time.
class LoggingSession {
public:
  LoggingSession(clangd::Logger &Instance);
  ~LoggingSession();

  LoggingSession(LoggingSession &&) = delete;
  LoggingSession &operator=(LoggingSession &&) = delete;

  LoggingSession(LoggingSession const &) = delete;
  LoggingSession &operator=(LoggingSession const &) = delete;
};

} // namespace clangd
} // namespace clang

#endif