Wednesday, July 26, 2006

Verbose options and logging levels/priority

Today I did something really cool with the 7DS logging system that enabled me to turn the "verbose" option off and instead introduce something more advanced: a level system that outputs log messages to the log file or screen based on the user setting.

To set this up, I basically made added two functions like this, one for on-screen and one for log files:


int SdsLogSetPrintLevel (int level)
{
print_level = level;
}



and then changed the code so that it prints only the higher priority messages to log file or screen, depending on user settings.


/* If level is greater than specified ,
* then print to stderr. */
if (level >= print_level)
fprintf(stderr, logstatement);

/* If level is greater than specified log level, write to logfile */
if (level >= log_level)
fprintf(logfp, "%s", logstatement);


Of course, I had to set the default values appropriately, so that the log file would get all messages.


int log_level = SDS_LOG_DEBUG, /* File should have debug messages */
print_level = SDS_LOG_INFO; /* Screen should have info messages */

No comments: