[clang][NFC] Generalize getSpecificAttr for const attributes (#116606)
[llvm-project.git] / clang / tools / libclang / CLog.h
blob6ce43a01ee8f2d742bf2b033225517521effd06d
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H
10 #define LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H
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/raw_ostream.h"
20 #include <string>
22 namespace llvm {
23 class format_object_base;
26 namespace clang {
27 namespace cxindex {
29 class Logger;
30 typedef IntrusiveRefCntPtr<Logger> LogRef;
32 /// Collects logging output and writes it to stderr when it's destructed.
33 /// Common use case:
34 /// \code
35 /// if (LogRef Log = Logger::make(__func__)) {
36 /// *Log << "stuff";
37 /// }
38 /// \endcode
39 class Logger : public RefCountedBase<Logger> {
40 std::string Name;
41 bool Trace;
42 SmallString<64> Msg;
43 llvm::raw_svector_ostream LogOS;
44 public:
45 static const char *getEnvVar() {
46 static const char *sCachedVar = ::getenv("LIBCLANG_LOGGING");
47 return sCachedVar;
49 static bool isLoggingEnabled() { return getEnvVar() != nullptr; }
50 static bool isStackTracingEnabled() {
51 if (const char *EnvOpt = Logger::getEnvVar())
52 return llvm::StringRef(EnvOpt) == "2";
53 return false;
55 static LogRef make(llvm::StringRef name,
56 bool trace = isStackTracingEnabled()) {
57 if (isLoggingEnabled())
58 return new Logger(name, trace);
59 return nullptr;
62 explicit Logger(llvm::StringRef name, bool trace)
63 : Name(std::string(name)), Trace(trace), LogOS(Msg) {}
64 ~Logger();
66 Logger &operator<<(CXTranslationUnit);
67 Logger &operator<<(FileEntryRef FE);
68 Logger &operator<<(CXCursor cursor);
69 Logger &operator<<(CXSourceLocation);
70 Logger &operator<<(CXSourceRange);
71 Logger &operator<<(CXString);
72 Logger &operator<<(llvm::StringRef Str) { LogOS << Str; return *this; }
73 Logger &operator<<(const char *Str) {
74 if (Str)
75 LogOS << Str;
76 return *this;
78 Logger &operator<<(unsigned long N) { LogOS << N; return *this; }
79 Logger &operator<<(long N) { LogOS << N ; return *this; }
80 Logger &operator<<(unsigned int N) { LogOS << N; return *this; }
81 Logger &operator<<(int N) { LogOS << N; return *this; }
82 Logger &operator<<(char C) { LogOS << C; return *this; }
83 Logger &operator<<(unsigned char C) { LogOS << C; return *this; }
84 Logger &operator<<(signed char C) { LogOS << C; return *this; }
85 Logger &operator<<(const llvm::format_object_base &Fmt);
91 /// Macros to automate common uses of Logger. Like this:
92 /// \code
93 /// LOG_FUNC_SECTION {
94 /// *Log << "blah";
95 /// }
96 /// \endcode
97 #define LOG_SECTION(NAME) \
98 if (clang::cxindex::LogRef Log = clang::cxindex::Logger::make(NAME))
99 #define LOG_FUNC_SECTION LOG_SECTION(__func__)
101 #endif