hitcounter
dpointer
Friday, July 02, 2004
 
Simple Logging Facility

If you're familiar with programming, you'll know why it's "1% coding, 99% debugging". In order to ease debugging sessions, it's useful to be able to track what's going on in the debugged application. In C, you can have very simple, yet effective poor-man's logging using the following code snippet:

#include <stdarg.h>
#include <stdio.h>

static void log( char* string, ... )
{
  va_list parameter;
  char output[256];
  FILE *f;

  va_start( parameter, string );
  vsnprintf( output, 255, string, parameter );
  
  f = fopen( "output.log", "at" );
  fprintf( f, "%s\n", output );
  fclose( f );
}
Then, wherever you want to record something, call the function log above, for example log("writing to file %s", filename). Pretty easy, isn't it? Of course, there's still enough room for improvement. It's left as an exercise for the reader :-P

Comments: Post a Comment

<< Home

Powered by Blogger