As someone else said, there are some good frameworks. However, if you want to collapse your own, the first thing to note is that cout is not a function, it is a stream. Function operator<<
. What you can do is something like the following:
extern ostream debug; void trace_init(); void trace_done(); #include "trace.h" ostream debug(cout.rdbuf()); static ofstream null; void trace_init() { null.open("/dev/null"); if(output_is_disabled) {
You may need to adjust a bit if you are on a platform without /dev/null
. It allows you to write
debug << "here some output << endl;
and if you turned on the output, it will write cout. If not, it will write to /dev/null
where you will not see anything.
In this case, you can just set cout rdbuf somewhere where you will not see this output, but I would consider this to be a really bad idea. Creating new threads gives you great flexibility in managing your output.
deong source share