can anyone tell me why this isn't working?
Code:
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
void myLogger(int log_level, char* logMessage, const char *format, ...)
{
FILE *file = fopen("c:\file.txt", "a+");
va_list args;
time_t now;
int debugValue = 1;
if ( time(&now) != (time_t)(-1) )
{
struct tm *local = localtime(&now);
if ( local )
{
char timestamp[20];
if ( strftime(timestamp, sizeof timestamp, "%Y-%m-%d %X", local) )
{
fprintf(file, "%s : ", timestamp);
}
}
}
switch (log_level)
{
case 0:
if ( 0 < debugValue )
{
fprintf(file, "%s, we got %d as debug_level ", debugValue, logMessage);
va_start(args, format);
vfprintf(file, format, args);
va_end(args);
fputc('\n', file);
fclose(file);
}
break;
default:
if ( log_level <= debugValue )
{
fprintf(file, "%s, we got %d as debug_level ", debugValue, logMessage);
va_start(args, format);
vfprintf(file, format, args);
va_end(args);
fputc('\n', file);
fclose(file);
}
break;
}
}
int main(void)
{
int i = 42;
double d = 123.456;
//myLogger(1, "This is a test: %d", debugValue);
myLogger(1,"%s", "ok");
myLogger(1,"i = %d, d = %g", i, d);
return 0;
}