+#include <stdio.h>
#include "logging.h"
+
void get_timestamp( char buf_36chars[]) {
struct timeb tb;
free(localtime);
}
-static FILE* log_file = NULL;
+static char* lf = NULL;
static int log_level = -1;
static int logging = 0;
-void log_free() { if( log_file != NULL ) fclose(log_file ); }
+void log_free() { if( lf != NULL ) free(lf); }
int fatal_handler( char* msg, ... ) {
-
+
+ FILE * log_file;
+
char buf[36];
memset( buf, 0, 36 );
get_timestamp( buf );
va_list args;
if( logging ) {
-
if( log_level < LOG_ERROR )
return -1;
- fprintf( log_file, "[%s %d] [%s] ", buf, pid, "ERR " );
+ log_file = fopen( lf, "a" );
+ if( log_file == NULL ) {
+ perror( "Unable to open log file for appending\n" );
+ } else {
+
+ fprintf( log_file, "[%s %d] [%s] ", buf, pid, "ERR " );
- va_start(args, msg);
- vfprintf(log_file, msg, args);
- va_end(args);
+ va_start(args, msg);
+ vfprintf(log_file, msg, args);
+ va_end(args);
- fprintf(log_file, "\n");
- fflush( log_file );
+ fprintf(log_file, "\n");
+ fflush( log_file );
+ fclose(log_file);
+ }
}
/* also log to stderr for ERRORS*/
int warning_handler( char* msg, ... ) {
+ FILE * log_file;
+
char buf[36];
memset( buf, 0, 36 );
get_timestamp( buf );
pid_t pid = getpid();
va_list args;
- if(logging) {
+ if( log_level < LOG_WARNING )
+ return -1;
- if( log_level < LOG_WARNING )
- return -1;
+ if(logging) {
- fprintf( log_file, "[%s %d] [%s] ", buf, pid, "WARN" );
+ log_file = fopen( lf, "a" );
+ if( log_file == NULL ) {
+ perror( "Unable to open log file for appending\n" );
+ fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" );
+ va_start(args, msg);
+ vfprintf(stderr, msg, args);
+ va_end(args);
+ fprintf( stderr, "\n" );
+ } else {
+
+ fprintf( log_file, "[%s %d] [%s] ", buf, pid, "WARN" );
- va_start(args, msg);
- vfprintf(log_file, msg, args);
- va_end(args);
+ va_start(args, msg);
+ vfprintf(log_file, msg, args);
+ va_end(args);
- fprintf(log_file, "\n");
- fflush( log_file );
+ fprintf(log_file, "\n");
+ fflush( log_file );
+ fclose(log_file);
+ }
} else {
fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" );
int info_handler( char* msg, ... ) {
+ FILE * log_file;
+
char buf[36];
memset( buf, 0, 36 );
get_timestamp( buf );
pid_t pid = getpid();
va_list args;
- if(logging) {
+ if( log_level < LOG_INFO )
+ return -1;
- if( log_level < LOG_INFO )
- return -1;
- fprintf( log_file, "[%s %d] [%s] ", buf, pid, "INFO" );
+ if(logging) {
- va_start(args, msg);
- vfprintf(log_file, msg, args);
- va_end(args);
+ log_file = fopen( lf, "a" );
+ if( log_file == NULL ) {
+ perror( "Unable to open log file for appending\n" );
+ fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" );
+ va_start(args, msg);
+ vfprintf(stderr, msg, args);
+ va_end(args);
+ fprintf( stderr, "\n" );
+ fflush(stderr);
+ } else {
+
+ fprintf( log_file, "[%s %d] [%s] ", buf, pid, "INFO" );
+
+ va_start(args, msg);
+ vfprintf(log_file, msg, args);
+ va_end(args);
- fprintf(log_file, "\n");
- fflush( log_file );
-
+ fprintf(log_file, "\n");
+ fflush( log_file );
+ fclose(log_file);
+ }
} else {
fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" );
int debug_handler( char* msg, ... ) {
+ FILE * log_file;
+
char buf[36];
memset( buf, 0, 36 );
get_timestamp( buf );
pid_t pid = getpid();
va_list args;
- if(logging) {
+ if( log_level < LOG_DEBUG )
+ return -1;
- if( log_level < LOG_DEBUG )
- return -1;
+ if(logging) {
- fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" );
+ log_file = fopen( lf, "a" );
+ if( log_file == NULL ) {
+ perror( "Unable to open log file for appending\n" );
+ fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
+ va_start(args, msg);
+ vfprintf(stderr, msg, args);
+ va_end(args);
+ fprintf( stderr, "\n" );
+ } else {
+ fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" );
- va_start(args, msg);
- vfprintf(log_file, msg, args);
- va_end(args);
+ va_start(args, msg);
+ vfprintf(log_file, msg, args);
+ va_end(args);
- fprintf(log_file, "\n");
- fflush( log_file );
-
+ fprintf(log_file, "\n");
+ fflush( log_file );
+
+ fclose(log_file);
+ }
} else {
fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
return 0;
}
- log_level = llevel;
-
/* log to stderr */
if(lfile == NULL) return 0;
- log_file = fopen( lfile, "a" );
- if( log_file == NULL ) {
- fprintf( stderr, "Unable to open log file %s for appending\n", lfile );
- return 0;
- }
+ log_level = llevel;
+ lf = strdup(lfile);
+
logging = 1;
return 1;