Skip to content

Instantly share code, notes, and snippets.

@dmatveev
Created October 6, 2011 11:58
Show Gist options
  • Save dmatveev/1267238 to your computer and use it in GitHub Desktop.
Save dmatveev/1267238 to your computer and use it in GitHub Desktop.
Simple logging code snippet
#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