Ternary
Ternary

Reputation: 2421

Custom Debug printf Used at Runtime

For awhile I've used something like the following to do my debugging.

#ifdef DEBUG
#       define Log(fmt, ...) printf(("[%s:%d] %s: " fmt "\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__);
#else
#       define Log(...)
#endif

This works fine such that when I compile with something like g++ -DDEBUG=1, I get all the prints I expect.

My question (or challenge) is to come up with a way that this debugging can occur with a runtime command instead of build time (e.g. ./myprocess -d) without complicating (or really changing at all) the client side code.

Upvotes: 0

Views: 1380

Answers (2)

Kos
Kos

Reputation: 72279

Why not take it simple?

extern boolean GLOBAL_LOGGING_ENABLED = false; // you'll need a single definition in any .cpp
#define LOG(fmt, ...) if (GLOBAL_LOGGING_ENABLED) {printf( // etc etc

// at startup read command line and set the flag to true if asked for

Or anything analogous.

Upvotes: 2

shuvalov
shuvalov

Reputation: 4923

You should declare an abstract class Logger and create an appropriate implementation (that is a child of Logger) during a runtime.

Upvotes: 3

Related Questions