Debug.cpp revision 193323
1193323Sed//===-- Debug.cpp - An easy way to add debug output to your code ----------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements a handle way of adding debugging information to your 11193323Sed// code, without it being enabled all of the time, and without having to add 12193323Sed// command line options to enable it. 13193323Sed// 14193323Sed// In particular, just wrap your code with the DEBUG() macro, and it will be 15193323Sed// enabled automatically if you specify '-debug' on the command-line. 16193323Sed// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify 17193323Sed// that your debug code belongs to class "foo". Then, on the command line, you 18193323Sed// can specify '-debug-only=foo' to enable JUST the debug information for the 19193323Sed// foo class. 20193323Sed// 21193323Sed// When compiling in release mode, the -debug-* options and all code in DEBUG() 22193323Sed// statements disappears, so it does not effect the runtime of the code. 23193323Sed// 24193323Sed//===----------------------------------------------------------------------===// 25193323Sed 26193323Sed#include "llvm/Support/CommandLine.h" 27193323Sed#include "llvm/Support/Debug.h" 28193323Sedusing namespace llvm; 29193323Sed 30193323Sedbool llvm::DebugFlag; // DebugFlag - Exported boolean set by the -debug option 31193323Sed 32193323Sednamespace { 33193323Sed#ifndef NDEBUG 34193323Sed // -debug - Command line option to enable the DEBUG statements in the passes. 35193323Sed // This flag may only be enabled in debug builds. 36193323Sed static cl::opt<bool, true> 37193323Sed Debug("debug", cl::desc("Enable debug output"), cl::Hidden, 38193323Sed cl::location(DebugFlag)); 39193323Sed 40193323Sed static std::string CurrentDebugType; 41193323Sed static struct DebugOnlyOpt { 42193323Sed void operator=(const std::string &Val) const { 43193323Sed DebugFlag |= !Val.empty(); 44193323Sed CurrentDebugType = Val; 45193323Sed } 46193323Sed } DebugOnlyOptLoc; 47193323Sed 48193323Sed static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> > 49193323Sed DebugOnly("debug-only", cl::desc("Enable a specific type of debug output"), 50193323Sed cl::Hidden, cl::value_desc("debug string"), 51193323Sed cl::location(DebugOnlyOptLoc), cl::ValueRequired); 52193323Sed#endif 53193323Sed} 54193323Sed 55193323Sed// isCurrentDebugType - Return true if the specified string is the debug type 56193323Sed// specified on the command line, or if none was specified on the command line 57193323Sed// with the -debug-only=X option. 58193323Sed// 59193323Sedbool llvm::isCurrentDebugType(const char *DebugType) { 60193323Sed#ifndef NDEBUG 61193323Sed return CurrentDebugType.empty() || DebugType == CurrentDebugType; 62193323Sed#else 63193323Sed return false; 64193323Sed#endif 65193323Sed} 66193323Sed 67193323Sed// getErrorOutputStream - Returns the error output stream (std::cerr). This 68193323Sed// places the std::c* I/O streams into one .cpp file and relieves the whole 69193323Sed// program from having to have hundreds of static c'tor/d'tors for them. 70193323Sed// 71193323SedOStream &llvm::getErrorOutputStream(const char *DebugType) { 72193323Sed static OStream cnoout(0); 73193323Sed if (DebugFlag && isCurrentDebugType(DebugType)) 74193323Sed return cerr; 75193323Sed else 76193323Sed return cnoout; 77193323Sed} 78