Hey guys,
I am trying to write a function that will have an optional parameter, well maybe you guys can give me a better option. I have a function that will log data (text string, etc.) to a file. I have it reading a file for the debug level 0 means only syslog 1 means debug log. Now in the debug log I need to print out everything, so there are certain things like "Entering functionName()", "Exiting functionName()", etc. that need to be printed only if the debug is set to 1; Otherwise only error are printed to the syslog. This is what I have now:
Code:
int logData(char* logMessage, int debugLevel)
{
FILE *pFile = NULL;
char logMsg[1024];
char s[30];
size_t i;
struct tm tim;
time_t now;
now = time(NULL);
tim = *(localtime(&now));
i = strftime(s,30,"%D %H:%M:%S",&tim);
switch (debugLevel)
{
case 1:
/* Create a new file we can write */
pFile = fopen(DEBUGFILE, "a+");
if ( pFile == NULL )
{
syslog(LOG_ERR,"Unable to open DEBUGFILE for reading");
return 1;
}
fprintf(pFile,"%s [%d] %s\n",s,getpid(),logMessage);
fclose(pFile); /* Close the file */
break;
case 0:
default:
//Write to var log messages
syslog(LOG_NOTICE,logMessage);
break;
}
return 0;
}
and I call it like so:
Code:
if (getDebugLevel(DEBUGGING_LEVEL, value) == 1)
{
syslog(LOG_NOTICE, "Unable to get the Debugging Level, assuming 1 (LOGGING TO SYSLOG)");
return 1;
}
logData("Entering thisFunction()", debugValue);
I guess, what I was thinking was adding a specialDebug, and if this was not set, then I would not print, if it was, then I would print to the debug aswell.
Thanks a ton!