Selective printf utility?

Discussion to talk about software related topics only.
Post Reply
Ridgeglider
Posts: 513
Joined: Sat Apr 26, 2008 7:14 am

Selective printf utility?

Post by Ridgeglider »

I printf to stdio from many tasks to display status info about how my application is running. However I find that with many tasks doing this, the info sent to stdout can get very confusing. It would be nice to be able to dynmically control what gets sent to stdout. Is there a utility (perhaps built with vsnprintf?) that allows me to 1) specify an output string and format, and then specify 2) a mask register (memory location, or OS flag?) and 3) a bit position in that regester to monitor. If the bit is set, the statement prints, otherwise it's ignored. Then you could set the bits in the mask (say via the web interface or a simple command processor) to control what status info is printed to stdout at any given time.

Something like:
void SelectivePrintf( int * MaskLocation, int TriggerBitPositionInMask, const char * restrict format, ...);

This mask/bit-field approach to printf would be sort of like select() or OS_Flags that trigger output on things of interest specified during runtime.

It's hard to believe something like this doesn't exist already...Just trying to avoid reinventing the wheel...

Thanks
thomastaranowski
Posts: 82
Joined: Sun May 11, 2008 2:17 pm
Location: Los Angeles, CA
Contact:

Re: Selective printf utility?

Post by thomastaranowski »

FWIW, this is what I use, patterned off the netburner DBPRINT, plus some other stuff I've used in the past. I don't think I've ever run across a de facto standard.

To use, have a debug flag (debugVar) that gets toggled on or off for each area of functionality you want to enable. I wrote a command shell that allows me to toggle variables dynamically, so I can change the output on the fly.

//Debug printf. Prints out if the debugVar is set to >0.
#define APPDPRINTF(debugVar,...) ( (debugVar) && platformDiag(__VA_ARGS__))

#define WARN(...) APPDPRINTF(WarnEnabled,__VA_ARGS__)

//Standard debug log
#define LOG(fmt,...) (platformDiag(fmt, ##__VA_ARGS__))


unsigned platformDiag(const char *fmt,...) {
va_list vl;

va_start(vl,fmt);
char str[250];
vsnprintf(str,sizeof(str),fmt,vl);
iprintf(str);
SysLog(str);
va_end(vl);

return 1;

}
Post Reply