1//===- CLog.h - Logging Interface -------------------------------*- C++ -*-===//
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#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H
10#define LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H
11
12#include "clang-c/Index.h"
13#include "clang/Basic/FileEntry.h"
14#include "clang/Basic/LLVM.h"
15#include "llvm/ADT/IntrusiveRefCntPtr.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/Support/Format.h"
20#include "llvm/Support/raw_ostream.h"
21#include <string>
22
23namespace llvm {
24class format_object_base;
25}
26
27namespace clang {
28namespace cxindex {
29
30class Logger;
31typedef IntrusiveRefCntPtr<Logger> LogRef;
32
33/// Collects logging output and writes it to stderr when it's destructed.
34/// Common use case:
35/// \code
36/// if (LogRef Log = Logger::make(__func__)) {
37/// *Log << "stuff";
38/// }
39/// \endcode
40class Logger : public RefCountedBase<Logger> {
41 std::string Name;
42 bool Trace;
43 SmallString<64> Msg;
44 llvm::raw_svector_ostream LogOS;
45public:
46 static const char *getEnvVar() {
47 static const char *sCachedVar = ::getenv(name: "LIBCLANG_LOGGING");
48 return sCachedVar;
49 }
50 static bool isLoggingEnabled() { return getEnvVar() != nullptr; }
51 static bool isStackTracingEnabled() {
52 if (const char *EnvOpt = Logger::getEnvVar())
53 return llvm::StringRef(EnvOpt) == "2";
54 return false;
55 }
56 static LogRef make(llvm::StringRef name,
57 bool trace = isStackTracingEnabled()) {
58 if (isLoggingEnabled())
59 return new Logger(name, trace);
60 return nullptr;
61 }
62
63 explicit Logger(llvm::StringRef name, bool trace)
64 : Name(std::string(name)), Trace(trace), LogOS(Msg) {}
65 ~Logger();
66
67 Logger &operator<<(CXTranslationUnit);
68 Logger &operator<<(FileEntryRef FE);
69 Logger &operator<<(CXCursor cursor);
70 Logger &operator<<(CXSourceLocation);
71 Logger &operator<<(CXSourceRange);
72 Logger &operator<<(CXString);
73 Logger &operator<<(llvm::StringRef Str) { LogOS << Str; return *this; }
74 Logger &operator<<(const char *Str) {
75 if (Str)
76 LogOS << Str;
77 return *this;
78 }
79 Logger &operator<<(unsigned long N) { LogOS << N; return *this; }
80 Logger &operator<<(long N) { LogOS << N ; return *this; }
81 Logger &operator<<(unsigned int N) { LogOS << N; return *this; }
82 Logger &operator<<(int N) { LogOS << N; return *this; }
83 Logger &operator<<(char C) { LogOS << C; return *this; }
84 Logger &operator<<(unsigned char C) { LogOS << C; return *this; }
85 Logger &operator<<(signed char C) { LogOS << C; return *this; }
86
87 template <typename... Ts>
88 Logger &operator<<(const llvm::format_object<Ts...> &Fmt) {
89 LogOS << Fmt;
90 return *this;
91 }
92};
93
94}
95}
96
97/// Macros to automate common uses of Logger. Like this:
98/// \code
99/// LOG_FUNC_SECTION {
100/// *Log << "blah";
101/// }
102/// \endcode
103#define LOG_SECTION(NAME) \
104 if (clang::cxindex::LogRef Log = clang::cxindex::Logger::make(NAME))
105#define LOG_FUNC_SECTION LOG_SECTION(__func__)
106
107#endif
108