4 void get_timestamp( char buf_36chars[]) {
8 char* localtime = strdup( ctime( &(tb.time) ) );
11 sprintf(mil," (%d)",tb.millitm);
12 strcpy( buf_36chars, localtime );
13 buf_36chars[ strlen(localtime)-1] = '\0'; // remove newline
14 strcat(buf_36chars,mil);
18 static char* lf = NULL;
19 static int log_level = -1;
20 static int logging = 0;
22 void log_free() { if( lf != NULL ) free(lf); }
24 int fatal_handler( char* msg, ... ) {
35 if( log_level < LOG_ERROR )
38 log_file = fopen( lf, "a" );
39 if( log_file == NULL ) {
40 perror( "Unable to open log file for appending\n" );
43 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "ERR " );
46 vfprintf(log_file, msg, args);
49 fprintf(log_file, "\n");
56 /* also log to stderr for ERRORS*/
57 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "ERR " );
59 vfprintf(stderr, msg, args);
61 fprintf( stderr, "\n" );
64 return -1; /* for consistency */
67 int warning_handler( char* msg, ... ) {
77 if( log_level < LOG_WARNING )
82 log_file = fopen( lf, "a" );
83 if( log_file == NULL ) {
84 perror( "Unable to open log file for appending\n" );
85 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" );
87 vfprintf(stderr, msg, args);
89 fprintf( stderr, "\n" );
92 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "WARN" );
95 vfprintf(log_file, msg, args);
98 fprintf(log_file, "\n");
105 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" );
107 vfprintf(stderr, msg, args);
109 fprintf( stderr, "\n" );
115 int info_handler( char* msg, ... ) {
120 memset( buf, 0, 36 );
121 get_timestamp( buf );
122 pid_t pid = getpid();
125 if( log_level < LOG_INFO )
130 log_file = fopen( lf, "a" );
131 if( log_file == NULL ) {
132 perror( "Unable to open log file for appending\n" );
133 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" );
135 vfprintf(stderr, msg, args);
137 fprintf( stderr, "\n" );
141 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "INFO" );
144 vfprintf(log_file, msg, args);
147 fprintf(log_file, "\n");
153 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" );
155 vfprintf(stderr, msg, args);
157 fprintf( stderr, "\n" );
165 int debug_handler( char* msg, ... ) {
170 memset( buf, 0, 36 );
171 get_timestamp( buf );
172 pid_t pid = getpid();
175 if( log_level < LOG_DEBUG )
180 log_file = fopen( lf, "a" );
181 if( log_file == NULL ) {
182 perror( "Unable to open log file for appending\n" );
183 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
185 vfprintf(stderr, msg, args);
187 fprintf( stderr, "\n" );
189 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" );
192 vfprintf(log_file, msg, args);
195 fprintf(log_file, "\n");
202 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
204 vfprintf(stderr, msg, args);
206 fprintf( stderr, "\n" );
213 int log_init( int llevel, char* lfile ) {
222 if(lfile == NULL) return 0;