#include "ichatserv.h" static FILE* logfile = NULL; int open_log() { logfile = fopen( OPT_LOGFILE, "a+t" ); if( ! logfile ) { perror( "Error opening log file\n" ); return 0; } return 1; } void close_log() { if( logfile ) { fclose( logfile ); logfile = NULL; } } void print2log( const char* msg, ... ) { va_list arglist; if( logfile ) { time_t tp; char* ts; // get the time and clip finishing \n symbol tp = time( NULL ); ts = ctime( &tp ); ts[strlen( ts )-1] = 0; // write header fprintf( logfile, "%s iChatSrv[%d]: ", ts, getpid() ); // print the message va_start( arglist, msg ); vfprintf( logfile, msg, arglist ); va_end( arglist ); // print the footer fputc( '\n', logfile ); // force log to be updated fflush( logfile ); } }