Index: include/llvm/Support/raw_ostream.h =================================================================== --- include/llvm/Support/raw_ostream.h (revision 126745) +++ include/llvm/Support/raw_ostream.h (working copy) @@ -416,6 +416,30 @@ raw_ostream &nulls(); //===----------------------------------------------------------------------===// +// Debug output streams +//===----------------------------------------------------------------------===// + +/// raw_debugstring_ostream - A raw_ostream that writes to debug output. +/// +class raw_debugstring_ostream : public raw_ostream { + /// write_impl - See raw_ostream::write_impl. + virtual void write_impl(const char *Ptr, size_t size); + + /// current_pos - Return the current position within the stream, not + /// counting the bytes currently in the buffer. + virtual uint64_t current_pos() const; + +public: + explicit raw_debugstring_ostream() {} + ~raw_debugstring_ostream(); +}; + + +/// debugouts() - This returns a reference to a raw_ostream for debug output. +/// Use it like: debugouts() << "foo" << "bar"; +raw_ostream &debugouts(); + +//===----------------------------------------------------------------------===// // Output Stream Adaptors //===----------------------------------------------------------------------===// @@ -486,6 +510,29 @@ ~raw_null_ostream(); }; + +//===----------------------------------------------------------------------===// +// Output/Error virtual stream bundle +//===----------------------------------------------------------------------===// + +/// raw_output_streams - A class to virtualize outs and errs access +class raw_output_streams { +public: + /// outs - Return stream for outputtting to std out or equivalent + virtual raw_ostream &outs() const =0; + /// errs - Return stream for outputtting to std err or equivalent + virtual raw_ostream &errs() const =0; +}; + +/// stdstreams() - This returns a reference to raw_output_streams for +/// output to std streams +raw_output_streams &stdstreams(); + +/// debugstreams() - This returns a reference to raw_output_streams for +/// output to debug streams +raw_output_streams &debugstreams(); + + } // end llvm namespace #endif Index: lib/Support/raw_ostream.cpp =================================================================== --- lib/Support/raw_ostream.cpp (revision 126745) +++ lib/Support/raw_ostream.cpp (working copy) @@ -715,3 +715,89 @@ uint64_t raw_null_ostream::current_pos() const { return 0; } + + +//===----------------------------------------------------------------------===// +// raw_debugstring_ostream +//===----------------------------------------------------------------------===// + +raw_debugstring_ostream::~raw_debugstring_ostream() { + flush(); +} + +namespace llvm { + void output_debug_string(const char *String); +} + +void raw_debugstring_ostream::write_impl(const char *Ptr, size_t Size) { + char Temp[2048]; + while (Size) + { + size_t ThisTime = std::min(Size, size_t(2047u)); + memcpy(Temp, Ptr, ThisTime); + Temp[ThisTime] = 0; + Size -= ThisTime; + Ptr += ThisTime; + llvm::output_debug_string(Temp); + } +} + +uint64_t raw_debugstring_ostream::current_pos() const { + return 0; +} + +raw_ostream &llvm::debugouts() { +#ifdef LLVM_ON_WIN32 + static raw_debugstring_ostream S; + return S; +#else + return llvm::errs(); // On non-windows arch just return stderr +#endif +} + +#ifdef LLVM_ON_WIN32 + +#include "windows.h" +void llvm::output_debug_string(const char *String) { + OutputDebugString(String); +} + +#else + +void llvm::output_debug_string(const char *String) { +} + +#endif + +//===----------------------------------------------------------------------===// +// raw_output_streams +//===----------------------------------------------------------------------===// + +raw_output_streams &llvm::stdstreams() { + class local_streams : public raw_output_streams { + raw_ostream &outs() const { + return llvm::outs(); + } + raw_ostream &errs() const { + return llvm::errs(); + } + }; + + static local_streams S; + return S; +} + +raw_output_streams &llvm::debugstreams() { + class local_streams : public raw_output_streams { + raw_ostream &outs() const { + return llvm::debugouts(); + } + raw_ostream &errs() const { + return llvm::debugouts(); + } + }; + + static local_streams S; + return S; +} +