Skip to content

Instantly share code, notes, and snippets.

@ariofrio
Created December 2, 2012 02:13
Show Gist options
  • Save ariofrio/4186597 to your computer and use it in GitHub Desktop.
Save ariofrio/4186597 to your computer and use it in GitHub Desktop.
C++ output stream proxy that adds a prefix to every line
#include "debug.h"
debug_stream debug;
#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
@ariofrio
Copy link
Author

ariofrio commented Dec 2, 2012

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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment