Re: std::iostream help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Brian,

> I need a wrapper to use around std::iostream...
> 
> Right now, I have this:
> 
> std::iostream debug_out;
> 
> And I use it the same way you¹d use std::cout...
> 
> Debug_out << ³Debug Message² << std::endl;
> 
> And it works fine.
> 
> But, debug_out gets used in millions of places in my code, so, somehow I
> need to come up with a wrapper around the std::iostream to check the debug
> level...  I realize the below isn¹t valid code (I tried) but, will give and
> idea of what I want to do:
> 
> U_short TH_ALGO_DEBUG = 0;
> 
> If (TH_ALGO_DEBUG > 0) {
>     std::iostream debug_out;
> }

Could you do this?

#if DEBUG_LEVEL > 0
std::iostream debug_out;
#else
boost::iostreams::basic_null_sink<char> debug_out;
#endif

That would affect the build, rather than being able to alter TH_ALGO_DEBUG
on the fly.

And I assume you have access to Boost:

http://www.boost.org/doc/libs/1_38_0/libs/iostreams/doc/classes/null.html

If you need to be able to alter TH_ALGO_DEBUG on the fly, you could make
your own ostream derived class which redirects slewing characters to either
std::iostream or just ignore the characters much in the same way that
boost:iostreams::basic_null_sink<char> does.

If you want something trickier, such as...

debug_out << ExpensiveFunctionForDebugOnly();

And you don't want ExpensiveFunctionForDebugOnly() to be called in the
TH_ALGO_DEBUG < 1 situation... alas, you need macro magic.

#define DEBUG_OUT(x) do{if(TH_ALGO_DEBUG > 0) debug_out << x;}while(false)

Which would be used like:
DEBUG_OUT(ExpensiveFunctionForDebugOnly());

> So I want debug_out to only with if debug is greater than zero; without
> having to change the debug_out call in millions of places in my code?!?!?!

Hmmm, the combination of debug_out as being compiled std::iostream or
boost::iostreams::basic_null_sink<char> ... or perhaps having your own
ostream derived class that ignores the << stream based on TH_ALGO_DEBUG, and
the magic macro DEBUG_OUT (which would hopefully only require thousands of
select changes instead of millions of changes) may accomplish your goal.

Sincerely,
--Eljay



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux