3 void get_timestamp( char buf_36chars[]) {
7 char* localtime = strdup( ctime( &(tb.time) ) );
10 sprintf(mil," (%d)",tb.millitm);
11 strcpy( buf_36chars, localtime );
12 buf_36chars[ strlen(localtime)-1] = '\0'; // remove newline
13 strcat(buf_36chars,mil);
17 static FILE* log_file = NULL;
18 static int log_level = -1;
19 static int logging = 0;
21 void log_free() { if( log_file != NULL ) fclose(log_file ); }
23 int fatal_handler( char* msg, ... ) {
33 if( log_level < LOG_ERROR )
36 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "ERR " );
39 vfprintf(log_file, msg, args);
42 fprintf(log_file, "\n");
47 /* also log to stderr for ERRORS*/
48 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "ERR " );
50 vfprintf(stderr, msg, args);
52 fprintf( stderr, "\n" );
55 return -1; /* for consistency */
58 int warning_handler( char* msg, ... ) {
68 if( log_level < LOG_WARNING )
71 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "WARN" );
74 vfprintf(log_file, msg, args);
77 fprintf(log_file, "\n");
82 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" );
84 vfprintf(stderr, msg, args);
86 fprintf( stderr, "\n" );
92 int info_handler( char* msg, ... ) {
102 if( log_level < LOG_INFO )
104 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "INFO" );
107 vfprintf(log_file, msg, args);
110 fprintf(log_file, "\n");
115 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" );
117 vfprintf(stderr, msg, args);
119 fprintf( stderr, "\n" );
127 int debug_handler( char* msg, ... ) {
130 memset( buf, 0, 36 );
131 get_timestamp( buf );
132 pid_t pid = getpid();
137 if( log_level < LOG_DEBUG )
140 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" );
143 vfprintf(log_file, msg, args);
146 fprintf(log_file, "\n");
151 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
153 vfprintf(stderr, msg, args);
155 fprintf( stderr, "\n" );
162 int log_init( int llevel, char* lfile ) {
173 if(lfile == NULL) return 0;
175 log_file = fopen( lfile, "a" );
176 if( log_file == NULL ) {
177 fprintf( stderr, "Unable to open log file %s for appending\n", lfile );