00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <eh_utils.h>
00022 #include <stdarg.h>
00023 #include <stdlib.h>
00024
00025 GHashTable* _log_files_;
00026 long int _log_file_code_;
00027
00028 #include <time.h>
00029
00030 FILE*
00031 eh_open_log( const char *log_name )
00032 {
00033 FILE *fp = NULL;
00034 char *log_file_name;
00035 GPtrArray *new_vec, *log_fp_vec;
00036 time_t current_time;
00037
00038 current_time = time( ¤t_time );
00039
00040 if ( !_log_files_ )
00041 {
00042 _log_files_ = g_hash_table_new(&g_str_hash,&eh_str_case_equal);
00043 new_vec = g_ptr_array_new();
00044 g_ptr_array_add(new_vec,NULL);
00045 g_hash_table_insert(_log_files_,g_strdup("/dev/null"),new_vec);
00046 new_vec = g_ptr_array_new();
00047 g_ptr_array_add(new_vec,stdout);
00048 g_hash_table_insert(_log_files_,g_strdup("stdout"),new_vec);
00049 new_vec = g_ptr_array_new();
00050 g_ptr_array_add(new_vec,stderr);
00051 g_hash_table_insert(_log_files_,g_strdup("stderr"),new_vec);
00052 _log_file_code_ = random();
00053 }
00054
00055 if ( log_name )
00056 {
00057
00058 log_fp_vec = g_hash_table_lookup( _log_files_ , log_name );
00059 if ( log_fp_vec )
00060 fp = (FILE*)g_ptr_array_index(log_fp_vec,log_fp_vec->len-1);
00061 else
00062 {
00063
00064 log_file_name = g_strconcat( log_name , ".log" , NULL );
00065 g_strstrip(log_file_name);
00066 while ( strstr(log_file_name," ") != NULL )
00067 strstr(log_file_name," ")[0] = '_';
00068
00069
00070 fp = fopen( log_file_name , "w" );
00071 fprintf( fp , "# Creation data : %s" , ctime( ¤t_time ) );
00072 fprintf( fp , "# Log code : %ld\n" , _log_file_code_ );
00073 fflush( fp );
00074 new_vec = g_ptr_array_new();
00075 g_ptr_array_add(new_vec,fp);
00076 g_hash_table_insert(_log_files_,g_strdup(log_name),new_vec);
00077
00078 eh_free( log_file_name );
00079 }
00080 }
00081
00082 return fp;
00083 }
00084
00085 FILE *eh_open_log_file(const char *log_name)
00086 {
00087 FILE *fp;
00088 char *good_name;
00089
00090
00091
00092 fp = eh_open_log( log_name );
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 if ( !fp )
00103 {
00104 fprintf(stderr,"Could not open log file, %s\n",good_name);
00105 eh_exit( EXIT_FAILURE );
00106 }
00107
00108
00109
00110 return fp;
00111 }
00112
00113 void eh_print_log(const char *log_name, const char *message,...)
00114 {
00115 GPtrArray *log_fp_vec;
00116 FILE *fp;
00117 char *stripped_msg;
00118 va_list ap;
00119 if ( !_log_files_ )
00120 eh_open_log( log_name );
00121 if ( (log_fp_vec=(GPtrArray*)g_hash_table_lookup(_log_files_,log_name)) )
00122 {
00123 stripped_msg = g_strconcat(log_name," : ",message,NULL);
00124 g_strchomp(stripped_msg);
00125 va_start(ap,message);
00126 fp = (FILE*)g_ptr_array_index(log_fp_vec,log_fp_vec->len-1);
00127 if ( fp )
00128 {
00129 vfprintf(fp,g_strconcat(stripped_msg,"\n",NULL),ap);
00130 fflush(fp);
00131 }
00132 va_end(ap);
00133 eh_free(stripped_msg);
00134 }
00135 }
00136
00137 void eh_close_log(const char *log_name)
00138 {
00139 GPtrArray *log_fp_vec;
00140 if ( log_name && (log_fp_vec=(GPtrArray*)g_hash_table_lookup(_log_files_,log_name)) )
00141 {
00142 fclose((FILE*)g_ptr_array_index(log_fp_vec,log_fp_vec->len-1));
00143 g_ptr_array_free(log_fp_vec,TRUE);
00144 g_hash_table_remove(_log_files_,log_name);
00145 }
00146 }
00147
00148 void eh_redirect_log(const char *log_file1,const char *log_file2)
00149 {
00150 GPtrArray *log_fp_vec1;
00151 GPtrArray *log_fp_vec2;
00152 if ( !_log_files_ )
00153 eh_open_log( log_file1 );
00154 log_fp_vec1 = (GPtrArray*)g_hash_table_lookup(_log_files_,log_file1);
00155 log_fp_vec2 = (GPtrArray*)g_hash_table_lookup(_log_files_,log_file2);
00156 if ( log_fp_vec1 && log_fp_vec2 )
00157 g_ptr_array_add(log_fp_vec2,g_ptr_array_index(log_fp_vec1,log_fp_vec1->len-1));
00158 }
00159
00160 void eh_reset_log(const char *log_file)
00161 {
00162 GPtrArray *log_fp_vec;
00163 log_fp_vec = (GPtrArray*)g_hash_table_lookup(_log_files_,log_file);
00164 if ( log_fp_vec && log_fp_vec->len > 1 )
00165 g_ptr_array_remove_index(log_fp_vec,log_fp_vec->len-1);
00166 }
00167
00168 static GLogLevelFlags eh_ignore_log_level = 0;
00169
00170 void
00171 eh_set_ignore_log_level( GLogLevelFlags ignore )
00172 {
00173 eh_ignore_log_level |= ignore;
00174 }
00175
00176 GLogLevelFlags
00177 eh_set_verbosity_level( gint verbosity )
00178 {
00179 GLogLevelFlags ignore = 0;
00180
00181 if ( verbosity<0 )
00182 verbosity = 0;
00183
00184 if ( verbosity>6 )
00185 verbosity = 6;
00186
00187 switch ( verbosity )
00188 {
00189 case 0:
00190 ignore |= G_LOG_LEVEL_ERROR;
00191 case 1:
00192 ignore |= G_LOG_LEVEL_CRITICAL;
00193 case 2:
00194 ignore |= G_LOG_LEVEL_WARNING;
00195 case 3:
00196 ignore |= G_LOG_LEVEL_MESSAGE;
00197 case 4:
00198 ignore |= G_LOG_LEVEL_INFO;
00199 case 5:
00200 ignore |= G_LOG_LEVEL_DEBUG;
00201 }
00202
00203 eh_set_ignore_log_level( ignore );
00204
00205 return ignore;
00206 }
00207
00208 void eh_logger( const gchar* log_domain ,
00209 GLogLevelFlags log_level ,
00210 const gchar* message ,
00211 gpointer user_data )
00212 {
00213 FILE **fp_list;
00214 gchar *log_label = NULL;
00215 gboolean is_fatal = (log_level & G_LOG_FLAG_FATAL) != 0;
00216 int i;
00217
00218 if ( eh_ignore_log_level & log_level )
00219 return;
00220
00221 if ( !message )
00222 message = "(NULL) message";
00223
00224 if ( user_data && !(log_level & G_LOG_LEVEL_DEBUG) )
00225 fp_list = (FILE**)user_data;
00226 else
00227 {
00228 fp_list = eh_new( FILE* , 2 );
00229 fp_list[0] = stderr;
00230 fp_list[1] = NULL;
00231 }
00232
00233 if ( log_level & G_LOG_LEVEL_WARNING )
00234 log_label = "Warning";
00235 else if ( log_level & G_LOG_LEVEL_ERROR )
00236 log_label = "Error";
00237
00238 for ( i=0 ; fp_list[i]!=NULL ; i++ )
00239 {
00240 if ( log_label )
00241 fprintf( fp_list[i] , "%s: " , log_label );
00242 if ( log_domain && !(log_level&EH_LOG_LEVEL_DATA) )
00243 fprintf( fp_list[i] , "%s: " , log_domain );
00244 fprintf( fp_list[i] , "%s\n" , message );
00245
00246 fflush ( fp_list[i] );
00247 }
00248
00249 if ( is_fatal )
00250 eh_exit( EXIT_FAILURE );
00251
00252 if ( !(user_data && !(log_level & G_LOG_LEVEL_DEBUG)) )
00253 eh_free( fp_list );
00254 }
00255
00256