5 void get_timestamp( char buf_36chars[]) {
9 char* localtime = strdup( ctime( &(tb.time) ) );
12 sprintf(mil," (%d)",tb.millitm);
13 strcpy( buf_36chars, localtime );
14 buf_36chars[ strlen(localtime)-1] = '\0'; // remove newline
15 strcat(buf_36chars,mil);
19 static char* lf = NULL;
20 static int log_level = -1;
21 static int logging = 0;
23 void log_free() { if( lf != NULL ) free(lf); }
25 int fatal_handler( char* msg, ... ) {
36 if( log_level < LOG_ERROR )
39 log_file = fopen( lf, "a" );
40 if( log_file == NULL ) {
41 perror( "Unable to open log file for appending\n" );
44 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "ERR " );
47 vfprintf(log_file, msg, args);
50 fprintf(log_file, "\n");
57 /* also log to stderr for ERRORS*/
58 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "ERR " );
60 vfprintf(stderr, msg, args);
62 fprintf( stderr, "\n" );
65 return -1; /* for consistency */
68 int warning_handler( char* msg, ... ) {
78 if( log_level < LOG_WARNING )
83 log_file = fopen( lf, "a" );
84 if( log_file == NULL ) {
85 perror( "Unable to open log file for appending\n" );
86 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" );
88 vfprintf(stderr, msg, args);
90 fprintf( stderr, "\n" );
93 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "WARN" );
96 vfprintf(log_file, msg, args);
99 fprintf(log_file, "\n");
106 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" );
108 vfprintf(stderr, msg, args);
110 fprintf( stderr, "\n" );
116 int info_handler( char* msg, ... ) {
121 memset( buf, 0, 36 );
122 get_timestamp( buf );
123 pid_t pid = getpid();
126 if( log_level < LOG_INFO )
131 log_file = fopen( lf, "a" );
132 if( log_file == NULL ) {
133 perror( "Unable to open log file for appending\n" );
134 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" );
136 vfprintf(stderr, msg, args);
138 fprintf( stderr, "\n" );
142 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "INFO" );
145 vfprintf(log_file, msg, args);
148 fprintf(log_file, "\n");
154 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" );
156 vfprintf(stderr, msg, args);
158 fprintf( stderr, "\n" );
166 int debug_handler( char* msg, ... ) {
171 memset( buf, 0, 36 );
172 get_timestamp( buf );
173 pid_t pid = getpid();
176 if( log_level < LOG_DEBUG )
181 log_file = fopen( lf, "a" );
182 if( log_file == NULL ) {
183 perror( "Unable to open log file for appending\n" );
184 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
186 vfprintf(stderr, msg, args);
188 fprintf( stderr, "\n" );
190 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" );
193 vfprintf(log_file, msg, args);
196 fprintf(log_file, "\n");
203 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
205 vfprintf(stderr, msg, args);
207 fprintf( stderr, "\n" );
214 int log_init( int llevel, char* lfile ) {
223 if(lfile == NULL) return 0;