Added "DOUT" macro. This is used as a replacement for the std::cerr
authorBill Wendling <isanbard@gmail.com>
Fri, 17 Nov 2006 00:49:12 +0000 (00:49 +0000)
committerBill Wendling <isanbard@gmail.com>
Fri, 17 Nov 2006 00:49:12 +0000 (00:49 +0000)
stream. It centralizes the use of std::cerr so that static c'tor/d'tors
aren't scattered around all over the place. The way to use it is like this:

       DOUT << "This is a status line: " << Var << "\n";

If "-debug" is specified, it will print. Otherwise, it'll not print. If
NDEBUG is defined, the DOUT does nothing.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31798 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/Debug.h
lib/Support/Debug.cpp

index 472364ba2fa92ed2ea49f1c2546913172fa08033..f14da843d76d5f8e0101a5097c6ffacd4d01f831 100644 (file)
@@ -26,6 +26,8 @@
 #ifndef LLVM_SUPPORT_DEBUG_H
 #define LLVM_SUPPORT_DEBUG_H
 
+#include <ostream>              // Doesn't have static d'tors!!
+
 namespace llvm {
 
 // DebugFlag - This boolean is set to true if the '-debug' command line option
@@ -59,6 +61,34 @@ bool isCurrentDebugType(const char *Type);
   do { if (DebugFlag && isCurrentDebugType(DEBUG_TYPE)) { X; } } while (0)
 #endif
 
+// llvm_ostream - Acts like an ostream. However, it doesn't print things out if
+// an ostream isn't specified.
+// 
+class llvm_ostream {
+  std::ostream* Stream;
+public:
+  llvm_ostream() : Stream(0) {}
+  llvm_ostream(std::ostream& OStream) : Stream(&OStream) {}
+
+  template <typename Ty>
+  llvm_ostream& operator << (const Ty& Thing) {
+    if (Stream) *Stream << Thing;
+    return *this;
+  }
+};
+
+// getErrorOutputStream - Returns the error output stream (std::cerr). This
+// places the std::c* I/O streams into one .cpp file and relieves the whole
+// program from having to have hundreds of static c'tor/d'tors for them.
+// 
+llvm_ostream getErrorOutputStream(const char *DebugType);
+
+#ifdef NDEBUG
+#define DOUT llvm_ostream()
+#else
+#define DOUT getErrorOutputStream(DEBUG_TYPE)
+#endif
+
 } // End llvm namespace
 
 #endif
index 394d9c110945f8f7e392dd9e5f6c610efd88b0d6..cbf12c7c0d72da10fae40a812cf366c847d199c2 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/CommandLine.h"
+#include <iostream>
 using namespace llvm;
 
 bool llvm::DebugFlag;  // DebugFlag - Exported boolean set by the -debug option
@@ -63,3 +64,14 @@ bool llvm::isCurrentDebugType(const char *DebugType) {
   return false;
 #endif
 }
+
+// getErrorOutputStream - Returns the error output stream (std::cerr). This
+// places the std::c* I/O streams into one .cpp file and relieves the whole
+// program from having to have hundreds of static c'tor/d'tors for them.
+// 
+llvm_ostream llvm::getErrorOutputStream(const char *DebugType) {
+  if (DebugFlag && isCurrentDebugType(DebugType))
+    return llvm_ostream(std::cerr);
+  else
+    return llvm_ostream();
+}