blob: 30dd6cc2ad06f90ee31254c1fee28b94727c5dfd [file] [log] [blame]
Ilya Biryukove5128f72017-09-20 07:24:15 +00001//===--- Logger.h - Logger interface for clangd ------------------*- C++-*-===//
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#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_LOGGER_H
12
13#include "llvm/ADT/Twine.h"
Sam McCallbed58852018-07-11 10:35:11 +000014#include "llvm/Support/Debug.h"
15#include "llvm/Support/Error.h"
16#include "llvm/Support/FormatVariadic.h"
Ilya Biryukove5128f72017-09-20 07:24:15 +000017
18namespace clang {
19namespace clangd {
20
21/// Interface to allow custom logging in clangd.
22class Logger {
23public:
24 virtual ~Logger() = default;
25
Sam McCallbed58852018-07-11 10:35:11 +000026 enum Level { Debug, Verbose, Info, Error };
27 static char indicator(Level L) { return "DVIE"[L]; }
28
Ilya Biryukove5128f72017-09-20 07:24:15 +000029 /// Implementations of this method must be thread-safe.
Sam McCallbed58852018-07-11 10:35:11 +000030 virtual void log(Level, const llvm::formatv_object_base &Message) = 0;
Ilya Biryukove5128f72017-09-20 07:24:15 +000031};
32
Sam McCallbed58852018-07-11 10:35:11 +000033namespace detail {
34const char *debugType(const char *Filename);
35void log(Logger::Level, const llvm::formatv_object_base &);
36
37// We often want to consume llvm::Errors by value when passing them to log().
38// This is tricky because the logging infrastructure must mark them as handled.
39// When forwarding argument to formatv, we wrap Errors-by-value in this type
40// whose destructor handles the cleanup.
41// FIXME: simplify after D49170 lands.
42struct WrappedError {
43 llvm::Error E;
44 WrappedError(WrappedError &&) = default;
45 ~WrappedError() { consumeError(std::move(E)); }
46};
47inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
48 const WrappedError &Err) {
49 return OS << Err.E;
50}
51template <typename T> T &&wrap(T &&V) { return std::forward<T>(V); }
52inline WrappedError wrap(llvm::Error &&V) { return WrappedError{std::move(V)}; }
53template <typename... Ts>
54void log(Logger::Level L, const char *Fmt, Ts &&... Vals) {
55 detail::log(L, llvm::formatv(Fmt, detail::wrap(std::forward<Ts>(Vals))...));
56}
57} // namespace detail
58
59// Clangd logging functions write to a global logger set by LoggingSession.
60// If no logger is registered, writes to llvm::errs().
61// All accept llvm::formatv()-style arguments, e.g. log("Text={0}", Text).
62
63// elog() is used for "loud" errors and warnings.
64// This level is often visible to users.
65template <typename... Ts> void elog(const char *Fmt, Ts &&... Vals) {
66 detail::log(Logger::Error, Fmt, std::forward<Ts>(Vals)...);
67}
68// log() is used for information important to understanding a clangd session.
69// e.g. the names of LSP messages sent are logged at this level.
70// This level could be enabled in production builds to allow later inspection.
71template <typename... Ts> void log(const char *Fmt, Ts &&... Vals) {
72 detail::log(Logger::Info, Fmt, std::forward<Ts>(Vals)...);
73}
74// vlog() is used for details often needed for debugging clangd sessions.
75// This level would typically be enabled for clangd developers.
76template <typename... Ts> void vlog(const char *Fmt, Ts &&... Vals) {
77 detail::log(Logger::Verbose, Fmt, std::forward<Ts>(Vals)...);
78}
79// dlog only logs if --debug was passed, or --debug_only=Basename.
80// This level would be enabled in a targeted way when debugging.
81#define dlog(...) \
82 DEBUG_WITH_TYPE(::clang::clangd::detail::debugType(__FILE__), \
83 ::clang::clangd::detail::log(Logger::Debug, __VA_ARGS__))
84
Ilya Biryukov940901e2017-12-13 12:51:22 +000085/// Only one LoggingSession can be active at a time.
86class LoggingSession {
Ilya Biryukove5128f72017-09-20 07:24:15 +000087public:
Ilya Biryukov940901e2017-12-13 12:51:22 +000088 LoggingSession(clangd::Logger &Instance);
89 ~LoggingSession();
Ilya Biryukove5128f72017-09-20 07:24:15 +000090
Ilya Biryukov940901e2017-12-13 12:51:22 +000091 LoggingSession(LoggingSession &&) = delete;
92 LoggingSession &operator=(LoggingSession &&) = delete;
Ilya Biryukove5128f72017-09-20 07:24:15 +000093
Ilya Biryukov940901e2017-12-13 12:51:22 +000094 LoggingSession(LoggingSession const &) = delete;
95 LoggingSession &operator=(LoggingSession const &) = delete;
Ilya Biryukove5128f72017-09-20 07:24:15 +000096};
97
98} // namespace clangd
99} // namespace clang
100
101#endif