aboutsummaryrefslogtreecommitdiff
path: root/tools/libclang/CLog.h
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2013-01-10 18:58:44 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2013-01-10 18:58:44 +0000
commitd8d9b847bd7120ebd5be83a55d31d9c26142962e (patch)
tree67f1375cc4dcae548cb0cca9cf36f94a26ddb1ca /tools/libclang/CLog.h
parentc6f5c6a58d34833d1fe458a518d5f59462926c7b (diff)
[libclang] Add missing header file
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172090 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/libclang/CLog.h')
-rw-r--r--tools/libclang/CLog.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/tools/libclang/CLog.h b/tools/libclang/CLog.h
new file mode 100644
index 0000000000..ed055b5968
--- /dev/null
+++ b/tools/libclang/CLog.h
@@ -0,0 +1,95 @@
+//===- CLog.h - Logging Interface -------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBCLANG_CLOG_H
+#define LLVM_LIBCLANG_CLOG_H
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/raw_ostream.h"
+#include <string>
+
+namespace llvm {
+class format_object_base;
+}
+
+namespace clang {
+
+namespace cxindex {
+
+class Logger;
+typedef IntrusiveRefCntPtr<Logger> LogRef;
+
+/// \brief Collects logging output and writes it to stderr when it's destructed.
+/// Common use case:
+/// \code
+/// if (LogRef Log = Logger::make(__func__)) {
+/// *Log << "stuff";
+/// }
+/// \endcode
+class Logger : public RefCountedBase<Logger> {
+ std::string Name;
+ bool Trace;
+ llvm::SmallString<64> Msg;
+ llvm::raw_svector_ostream LogOS;
+public:
+ static const char *getEnvVar() {
+ static const char *sCachedVar = ::getenv("LIBCLANG_LOGGING");
+ return sCachedVar;
+ }
+ static bool isLoggingEnabled() { return getEnvVar() != 0; }
+ static bool isStackTracingEnabled() {
+ if (const char *EnvOpt = Logger::getEnvVar())
+ return llvm::StringRef(EnvOpt) =="2";
+ return false;
+ }
+ static LogRef make(llvm::StringRef name,
+ bool trace = isStackTracingEnabled()) {
+ if (isLoggingEnabled())
+ return new Logger(name, trace);
+ return 0;
+ }
+
+ explicit Logger(llvm::StringRef name, bool trace)
+ : Name(name), Trace(trace), LogOS(Msg) { }
+ ~Logger();
+
+ Logger &operator<<(CXTranslationUnit);
+ Logger &operator<<(CXSourceLocation);
+ Logger &operator<<(CXSourceRange);
+ Logger &operator<<(CXString);
+ Logger &operator<<(llvm::StringRef Str) { LogOS << Str; return *this; }
+ Logger &operator<<(const char *Str) {
+ if (Str)
+ LogOS << Str;
+ return *this;
+ }
+ Logger &operator<<(unsigned long N) { LogOS << N; return *this; }
+ Logger &operator<<(long N) { LogOS << N ; return *this; }
+ Logger &operator<<(unsigned int N) { LogOS << N; return *this; }
+ Logger &operator<<(int N) { LogOS << N; return *this; }
+ Logger &operator<<(char C) { LogOS << C; return *this; }
+ Logger &operator<<(unsigned char C) { LogOS << C; return *this; }
+ Logger &operator<<(signed char C) { LogOS << C; return *this; }
+ Logger &operator<<(const llvm::format_object_base &Fmt);
+};
+
+}
+}
+
+/// \brief Macros to automate common uses of Logger. Like this:
+/// \code
+/// LOG_FUNC_SECTION {
+/// *Log << "blah";
+/// }
+#define LOG_SECTION(NAME) if (LogRef Log = clang::cxindex::Logger::make(NAME))
+#define LOG_FUNC_SECTION LOG_SECTION(__func__)
+
+#endif