1 #include "opensrf/generic_utils.h"
6 void get_timestamp( char buf_36chars[]) {
10 char* localtime = strdup( ctime( &(tb.time) ) );
13 sprintf(mil," (%d)",tb.millitm);
14 strcpy( buf_36chars, localtime );
15 buf_36chars[ strlen(localtime)-1] = '\0'; // remove newline
16 strcat(buf_36chars,mil);
21 inline void* safe_malloc( int size ) {
22 void* ptr = (void*) malloc( size );
24 fatal_handler("safe_malloc(): Out of Memory" );
25 memset( ptr, 0, size );
29 // ---------------------------------------------------------------------------------
30 // Here we define how we want to handle various error levels.
31 // ---------------------------------------------------------------------------------
34 static FILE* log_file = NULL;
35 static int log_level = -1;
36 static int logging = 0;
37 pthread_mutex_t mutex;
39 void log_free() { if( log_file != NULL ) fclose(log_file ); }
41 void fatal_handler( char* msg, ... ) {
51 if( log_level < LOG_ERROR )
54 pthread_mutex_lock( &(mutex) );
55 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "ERR " );
58 vfprintf(log_file, msg, args);
61 fprintf(log_file, "\n");
63 pthread_mutex_unlock( &(mutex) );
67 /* also log to stderr for ERRORS*/
68 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "ERR " );
70 vfprintf(stderr, msg, args);
72 fprintf( stderr, "\n" );
77 void warning_handler( char* msg, ... ) {
87 if( log_level < LOG_WARNING )
90 pthread_mutex_lock( &(mutex) );
91 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "WARN" );
94 vfprintf(log_file, msg, args);
97 fprintf(log_file, "\n");
99 pthread_mutex_unlock( &(mutex) );
103 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" );
105 vfprintf(stderr, msg, args);
107 fprintf( stderr, "\n" );
112 void info_handler( char* msg, ... ) {
115 memset( buf, 0, 36 );
116 get_timestamp( buf );
117 pid_t pid = getpid();
122 if( log_level < LOG_INFO )
124 pthread_mutex_lock( &(mutex) );
125 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "INFO" );
128 vfprintf(log_file, msg, args);
131 fprintf(log_file, "\n");
133 pthread_mutex_unlock( &(mutex) );
137 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" );
139 vfprintf(stderr, msg, args);
141 fprintf( stderr, "\n" );
148 void debug_handler( char* msg, ... ) {
151 memset( buf, 0, 36 );
152 get_timestamp( buf );
153 pid_t pid = getpid();
158 if( log_level < LOG_DEBUG )
161 pthread_mutex_lock( &(mutex) );
162 fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" );
165 vfprintf(log_file, msg, args);
168 fprintf(log_file, "\n");
170 pthread_mutex_unlock( &(mutex) );
174 fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" );
176 vfprintf(stderr, msg, args);
178 fprintf( stderr, "\n" );
184 int log_init( int llevel, char* lfile ) {
193 log_file = fopen( lfile, "a" );
194 if( log_file == NULL ) {
195 fprintf( stderr, "Unable to open log file %s for appending\n", lfile );
204 // ---------------------------------------------------------------------------------
205 // Flesh out a ubiqitous growing string buffer
206 // ---------------------------------------------------------------------------------
208 growing_buffer* buffer_init(int num_initial_bytes) {
210 if( num_initial_bytes > BUFFER_MAX_SIZE ) {
215 size_t len = sizeof(growing_buffer);
217 growing_buffer* gb = (growing_buffer*) safe_malloc(len);
219 gb->n_used = 0;/* nothing stored so far */
220 gb->size = num_initial_bytes;
221 gb->buf = (char *) safe_malloc(gb->size + 1);
226 int buffer_add(growing_buffer* gb, char* data) {
229 if( ! gb || ! data ) { return 0; }
230 int data_len = strlen( data );
232 if( data_len == 0 ) { return 0; }
233 int total_len = data_len + gb->n_used;
235 while( total_len >= gb->size ) {
239 if( gb->size > BUFFER_MAX_SIZE ) {
240 warning_handler( "Buffer reached MAX_SIZE of %d", BUFFER_MAX_SIZE );
245 char* new_data = (char*) safe_malloc( gb->size );
247 strcpy( new_data, gb->buf );
251 strcat( gb->buf, data );
252 gb->n_used = total_len;
257 int buffer_reset( growing_buffer *gb){
258 if( gb == NULL ) { return 0; }
259 if( gb->buf == NULL ) { return 0; }
260 memset( gb->buf, 0, gb->size );
265 int buffer_free( growing_buffer* gb ) {
273 char* buffer_data( growing_buffer *gb) {
274 return strdup( gb->buf );
281 // ---------------------------------------------------------------------------------
283 // ---------------------------------------------------------------------------------
286 // ---------------------------------------------------------------------------------
287 // Allocate and build the conf_reader. This only has to happen once in a given
288 // system. Repeated calls are ignored.
289 // ---------------------------------------------------------------------------------
291 void config_reader_init( char* config_file ) {
292 if( conf_reader == NULL ) {
294 if( config_file == NULL || strlen(config_file) == 0 ) {
295 fatal_handler( "config_reader_init(): No config file specified" );
299 size_t len = sizeof( config_reader );
300 conf_reader = (config_reader*) safe_malloc( len );
302 conf_reader->config_doc = xmlParseFile( config_file );
303 conf_reader->xpathCx = xmlXPathNewContext( conf_reader->config_doc );
304 if( conf_reader->xpathCx == NULL ) {
305 fatal_handler( "config_reader_init(): Unable to create xpath context");
312 void config_reader_init( char* name, char* config_file ) {
314 if( name == NULL || config_file == NULL || strlen(config_file) == 0 ) {
315 fatal_handler( "config_reader_init(): No config file specified" );
319 config_reader* reader =
320 (config_reader*) safe_malloc(sizeof(config_reader));
322 reader->config_doc = xmlParseFile( config_file );
323 reader->xpathCx = xmlXPathNewContext( reader->config_doc );
324 reader->name = strdup(name);
327 if( reader->xpathCx == NULL ) {
328 fprintf( stderr, "config_reader_init(): Unable to create xpath context\n");
332 if( conf_reader == NULL ) {
333 conf_reader = reader;
335 config_reader* tmp = conf_reader;
336 conf_reader = reader;
342 char* config_value( const char* config_name, const char* xp_query, ... ) {
344 if( conf_reader == NULL || xp_query == NULL ) {
345 fatal_handler( "config_value(): NULL conf_reader or NULL param(s)" );
349 config_reader* reader = conf_reader;
350 while( reader != NULL ) {
351 if( !strcmp(reader->name, config_name))
353 reader = reader->next;
356 if( reader == NULL ) {
357 fprintf(stderr, "No Config file with name %s\n", config_name );
361 int slen = strlen(xp_query) + 512;/* this is unsafe ... */
362 char xpath_query[ slen ];
363 memset( xpath_query, 0, slen );
365 va_start(va_args, xp_query);
366 vsprintf(xpath_query, xp_query, va_args);
371 int len = strlen(xpath_query) + 100;
372 char alert_buffer[len];
373 memset( alert_buffer, 0, len );
375 // build the xpath object
376 xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression( BAD_CAST xpath_query, reader->xpathCx );
378 if( xpathObj == NULL ) {
379 sprintf( alert_buffer, "Could not build xpath object: %s", xpath_query );
380 fatal_handler( alert_buffer );
385 if( xpathObj->type == XPATH_NODESET ) {
387 // ----------------------------------------------------------------------------
388 // Grab nodeset from xpath query, then first node, then first text node and
389 // finaly the text node's value
390 // ----------------------------------------------------------------------------
391 xmlNodeSet* node_list = xpathObj->nodesetval;
392 if( node_list == NULL ) {
393 sprintf( alert_buffer, "Could not build xpath object: %s", xpath_query );
394 warning_handler(alert_buffer);
398 if( node_list->nodeNr == 0 ) {
399 sprintf( alert_buffer, "Config XPATH query returned 0 results: %s", xpath_query );
400 warning_handler(alert_buffer);
405 xmlNodePtr element_node = *(node_list)->nodeTab;
406 if( element_node == NULL ) {
407 sprintf( alert_buffer, "Config XPATH query returned 0 results: %s", xpath_query );
408 warning_handler(alert_buffer);
412 xmlNodePtr text_node = element_node->children;
413 if( text_node == NULL ) {
414 sprintf( alert_buffer, "Config variable has no value: %s", xpath_query );
415 warning_handler(alert_buffer);
419 val = text_node->content;
421 sprintf( alert_buffer, "Config variable has no value: %s", xpath_query );
422 warning_handler(alert_buffer);
428 sprintf( alert_buffer, "Xpath evaluation failed: %s", xpath_query );
429 warning_handler(alert_buffer);
433 char* value = strdup(val);
434 if( value == NULL ) { warning_handler( "config_value(): Empty config value or Out of Memory!" ); }
436 // Free XPATH structures
437 if( xpathObj != NULL ) xmlXPathFreeObject( xpathObj );
443 void config_reader_free() {
444 while( conf_reader != NULL ) {
445 xmlXPathFreeContext( conf_reader->xpathCx );
446 xmlFreeDoc( conf_reader->config_doc );
447 free(conf_reader->name );
448 config_reader* tmp = conf_reader->next;