c++ operator << support for debug()()

From: Robert Collins <robertc@dont-contact.us>
Date: 06 Jul 2003 23:26:56 +1000

I've hacked up a new debug macro for C++ streams:

instead of
debug(x,y)("foo: %d\n", (int)some_size_t);
we can do
debugs(x,y)("foo: " << some_size_t);

Note the lack of cast operators - which should improve portability and
accuracy.

The code is in my arch archive, in the
robertc@squid-cache.org--squid/squid--cplusplusdebug--3.0 branch.

But, for quick reference: this is the update Debug.h guts below.
I'd like to merge this into 3.0, as it's both minor and very useful.
Full conversion can be done incrementally: this uses _db_print once the
stream is finalised.

======

#ifndef SQUID_DEBUG
#define SQUID_DEBUG

#include <iostream>
#include <sstream>
class Debug
{

public:
    static int Levels[MAX_DEBUG_SECTIONS];
    static int level;
    static std::ostream &getDebugOut();
    static void finishDebug();
private:
    static std::ostringstream *currentDebug;
};

/* Debug stream */
#define DoDebug(CONTENT) { \
        Debug::getDebugOut() << CONTENT; \
        Debug::finishDebug(); }
#define debugs(SECTION, LEVEL) \
        if ((Debug::level = (LEVEL)) > Debug::Levels[SECTION]) \
          (void) 0; \
        else \
          DoDebug

/* The goal is:
 * debugs(x,y)(a << b << c)
 * to evaluate to
 * if ((Debug::level = (LEVEL)) > Debug::Levels[SECTION])
 * (void) 0;
 * else
 * { Debug::getDebugOut() << a << b << c; Debug::finishDebug();}
 */

#endif /* SQUID_DEBUG */

-- 
GPG key available at: <http://members.aardvark.net.au/lifeless/keys.txt>.

Received on Sun Jul 06 2003 - 07:27:01 MDT

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:20:15 MST