1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
...
#define SILENT 0
#define QUIET 1
#define NORMAL 2
#define LOUD 3
..
// vfprintf needs to be used instead of fprintf so that you can pass the va_list to it instead of worrying about the specific arguments
int toLog(FILE *f, int functionPrintSetting, int currentPrintSetting, const char *buffer, ...){
if(currentPrintSetting >= functionPrintSetting){
va_list arguments;
va_start(arguments, buffer);
int r = vfprintf(f, buffer, arguments);
va_end(arguments);
return r;
}else{
return 1;
}
}
...
void myFunction(int printSetting){
int value1 = 6;
toLog (file, printSetting, QUIET, "\n\nSome log text I only want to print if the current mode is QUIET or higher - %u", value1);
int value2 = 23;
toLog(file, printSetting, LOUD, "\n\nSome text I only want to print if the current mode is LOUD or higher - %u", value2);
}
...
int main(){
// Now I call myFunction() in four different ways so it prints to my log in any amount of detail that I want :)
myFunction(LOUD);
myFunction(NORMAL);
myFunction(QUIET);
myFunction(SILENT);
}
|