Created
December 2, 2012 02:13
-
-
Save ariofrio/4186597 to your computer and use it in GitHub Desktop.
C++ output stream proxy that adds a prefix to every line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "debug.h" | |
debug_stream debug; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef DEBUG_H | |
#define DEBUG_H | |
#include <iostream> | |
class debug_stream { | |
public: | |
debug_stream() : new_line(true), on(false) {} | |
template<class T> | |
debug_stream &operator<<(T val) { | |
if(new_line) { | |
std::cerr << "-- "; | |
new_line = false; | |
} | |
std::cerr << val; | |
return *this; | |
} | |
debug_stream &operator<<(std::ostream& (*fn)(std::ostream&)) { | |
std::cerr << fn; | |
if(fn == (std::ostream& (*)(std::ostream&)) std::endl) { | |
new_line = true; | |
} | |
return *this; | |
} | |
bool on; | |
private: | |
bool new_line; | |
}; | |
extern debug_stream debug; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code comes from my work for CS 130A (PA2: Binary Search Trees, AVL Trees, and Splay Trees). Once I realized I didn't need it, I didn't want to throw it away, so I packaged it into a Gist. :)