2013-01-11 02:58:44 +08:00
|
|
|
//===- CLog.h - Logging Interface -------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-01-11 02:58:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H
|
|
|
|
#define LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H
|
2013-01-11 02:58:44 +08:00
|
|
|
|
2014-03-04 18:05:20 +08:00
|
|
|
#include "clang-c/Index.h"
|
2013-01-19 16:09:44 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
2013-01-11 02:58:44 +08:00
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2013-01-11 03:43:44 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2013-01-11 09:15:02 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2013-01-11 02:58:44 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class format_object_base;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace clang {
|
2013-03-08 10:32:26 +08:00
|
|
|
class FileEntry;
|
2013-01-11 02:58:44 +08:00
|
|
|
|
|
|
|
namespace cxindex {
|
|
|
|
|
|
|
|
class Logger;
|
|
|
|
typedef IntrusiveRefCntPtr<Logger> LogRef;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Collects logging output and writes it to stderr when it's destructed.
|
2013-01-11 02:58:44 +08:00
|
|
|
/// Common use case:
|
|
|
|
/// \code
|
|
|
|
/// if (LogRef Log = Logger::make(__func__)) {
|
|
|
|
/// *Log << "stuff";
|
|
|
|
/// }
|
|
|
|
/// \endcode
|
|
|
|
class Logger : public RefCountedBase<Logger> {
|
|
|
|
std::string Name;
|
|
|
|
bool Trace;
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallString<64> Msg;
|
2013-01-11 02:58:44 +08:00
|
|
|
llvm::raw_svector_ostream LogOS;
|
|
|
|
public:
|
|
|
|
static const char *getEnvVar() {
|
|
|
|
static const char *sCachedVar = ::getenv("LIBCLANG_LOGGING");
|
|
|
|
return sCachedVar;
|
|
|
|
}
|
2014-06-08 16:38:04 +08:00
|
|
|
static bool isLoggingEnabled() { return getEnvVar() != nullptr; }
|
2013-01-11 02:58:44 +08:00
|
|
|
static bool isStackTracingEnabled() {
|
|
|
|
if (const char *EnvOpt = Logger::getEnvVar())
|
2013-01-11 03:43:44 +08:00
|
|
|
return llvm::StringRef(EnvOpt) == "2";
|
2013-01-11 02:58:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
static LogRef make(llvm::StringRef name,
|
|
|
|
bool trace = isStackTracingEnabled()) {
|
|
|
|
if (isLoggingEnabled())
|
|
|
|
return new Logger(name, trace);
|
2014-06-08 16:38:04 +08:00
|
|
|
return nullptr;
|
2013-01-11 02:58:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
explicit Logger(llvm::StringRef name, bool trace)
|
2020-01-29 03:23:46 +08:00
|
|
|
: Name(std::string(name)), Trace(trace), LogOS(Msg) {}
|
2013-01-11 02:58:44 +08:00
|
|
|
~Logger();
|
|
|
|
|
|
|
|
Logger &operator<<(CXTranslationUnit);
|
2013-03-08 10:32:26 +08:00
|
|
|
Logger &operator<<(const FileEntry *FE);
|
|
|
|
Logger &operator<<(CXCursor cursor);
|
2013-01-11 02:58:44 +08:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-11 02:58:44 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Macros to automate common uses of Logger. Like this:
|
2013-01-11 02:58:44 +08:00
|
|
|
/// \code
|
|
|
|
/// LOG_FUNC_SECTION {
|
|
|
|
/// *Log << "blah";
|
|
|
|
/// }
|
2013-01-11 03:43:44 +08:00
|
|
|
/// \endcode
|
2014-02-11 20:25:31 +08:00
|
|
|
#define LOG_SECTION(NAME) \
|
|
|
|
if (clang::cxindex::LogRef Log = clang::cxindex::Logger::make(NAME))
|
2016-10-20 10:46:22 +08:00
|
|
|
#define LOG_FUNC_SECTION LOG_SECTION(__func__)
|
2013-01-11 02:58:44 +08:00
|
|
|
|
|
|
|
#endif
|