[webkit-dev] Easing printf based debugging in WebKit with an helper.

Shezan Baig shezbaig.wk at gmail.com
Fri Jul 20 07:53:54 PDT 2012


On Fri, Jul 20, 2012 at 7:46 AM, Konstantin Tokarev <annulen at yandex.ru> wrote:
>
> 19.07.2012, 22:20, "Filip Pizlo" <fpizlo at apple.com>:
>> Now consider the stream form:
>> thingy << "foo " << a << " bar " << someWeirdNonsenseToEnableHex << b << " baz " << c << endl;
>> This is 8 procedure calls and three string constants. This code will be somewhere around 8 times fatter. Hence, you will be less likely to want to enable such debug statements in release builds both due to fears concerning unnecessary increases in binary size, and unnecessary increases in compile times.
>
> Well, if all << operators are inline, it will be optimized. You also don't have to add endl, because "thingy" can add '\n' and flush buffer at the end by default (like qDebug does)
>


Also, if "START_THINGY" and "END_THINGY" are defined as:

#define START_THINGY  if (enableDebug) { thingy
#define END_THINGY    << endl; }

Then a statement like:

START_THINGY << "foo " << a << " bar " << someWeirdNonsenseToEnableHex
<< b << " baz " << c << END_THINGY;

will all be wrapped up within an "if (enableDebug)" condition, which
we would only turn on when we need the log output, so the cost of the
streaming/printing can be virtually eliminated even if we decide to
land the code.

This also allows us to do more complex things like:

START_THINGY << "Selectively printing variables:";
    if (isSet(someVar1))
        thingy << " someVar1 = '" << someVar1 << "'";
    if (isSet(someVar2))
        thingy << " someVar2 = '" << someVar2 << "'";

    if (someArray.length()) {
        thingy << " someArray.items = [";
        for (int i = 0; i < someArray.length(); ++i) {
            thingy << someArray[i] << ", ";
        }
        thingy << "]";
    }
thingy << END_THINGY;

Often when I'm debugging something, I really want to selectively craft
the log message so that I only need to see the variables I'm
interested in, depending on the state of the program *at that time*.
This reduces the clutter in the log message, and since this whole
block is wrapped within "if (enableDebug)", the cost of any additional
work used to generate the log message is completely eliminated if
debug mode is disabled.

And because I'm in the middle of debugging a particular issue, I don't
really want to create a new type that wraps up those (potentially
unrelated) variables, just in order to create an overloaded "debug()"
function that does the pretty printing.

-shez-


More information about the webkit-dev mailing list