Aussie AI

Dynamic Debug Tracing Flag

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

Dynamic Debug Tracing Flag

Instead of using “#if DEBUG”, it can be desirable to have the debug tracing dynamically controlled at runtime. This allows you to turn it on and off without a rebuild, such as via a command-line argument. And you can decide whether or not you want to ship it to production with the tracing available to be used.

This idea can use a single Boolean flag:

   extern bool g_aussie_debug_enabled;

We can add some macros to control it:

    #define aussie_debug_off()  ( g_aussie_debug_enabled = false )
    #define aussie_debug_on()  ( g_aussie_debug_enabled = true )

And then the basic debug tracing macros simply need to check it:

    #define ydbg(fmt,...)  ( g_aussie_debug_enabled && \
        fprintf(stderr, (fmt), __VA_ARGS__ ))

So, this adds some runtime cost of testing a global flag every time this line of code is executed.

Here's the version with file, line, and function context:

    #define ydbg(fmt,...)  \
        ( g_aussie_debug_enabled && \
        ( fprintf(stderr, "DEBUG [%s:%d:%s]: ", \
                __FILE__, __LINE__, __func__ ), \
        fprintf(stderr, (fmt), __VA_ARGS__ )))

And here's the courtesy newline-optional version:

    #define ydbg(fmt,...)  \
        ( g_aussie_debug_enabled && \
        (fprintf(stderr, "DEBUG [%s:%d:%s]: ", \
                __FILE__, __LINE__, __func__ ), \
        fprintf(stderr, (fmt), __VA_ARGS__ ), \
        (strchr((fmt), '\n') != NULL \
                || fprintf(stderr, "\n"))))

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++