Created
October 6, 2011 11:58
-
-
Save dmatveev/1267238 to your computer and use it in GitHub Desktop.
Simple logging code snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <stdarg.h> | |
static CRITICAL_SECTION crit; | |
void log_init() | |
{ | |
static volatile LONG done = 0; | |
if (done == 0) { | |
if (InterlockedIncrement(&done) == 1) { | |
InitializeCriticalSection(&crit); | |
} | |
} | |
} | |
void log_on(const char *format, ...) | |
{ | |
FILE *fp; | |
va_list va; | |
log_init(); | |
EnterCriticalSection(&crit); | |
fp = fopen("logfile.txt", "a+"); | |
if (fp != NULL) { | |
va_start(va, format); | |
vfprintf(fp, format, va); | |
fputs("\n", fp); | |
va_end(va); | |
fclose (fp); | |
} | |
LeaveCriticalSection(&crit); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment