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