50#ifdef FT_DEBUG_LOGGING
78 static const char* ft_default_trace_level = NULL;
79 static FILE* ft_fileptr = NULL;
80 static const char* ft_component = NULL;
81 static FT_Bool ft_component_flag = FALSE;
82 static FT_Bool ft_timestamp_flag = FALSE;
83 static FT_Bool ft_have_newline_char = TRUE;
84 static const char* ft_custom_trace_level = NULL;
88 dlg_handler ft_default_log_handler = NULL;
89 FT_Custom_Log_Handler custom_output_handler = NULL;
94#ifdef FT_DEBUG_LEVEL_ERROR
99 FT_Message(
const char* fmt,
106 vfprintf( stderr, fmt, ap );
114 FT_Panic(
const char* fmt,
121 vfprintf( stderr, fmt, ap );
124 exit( EXIT_FAILURE );
131 FT_Throw( FT_Error error,
138 "%s:%d: error 0x%02x: %s\n",
142 FT_Error_String( error ) );
155#ifdef FT_DEBUG_LEVEL_TRACE
159 static int ft_trace_levels_enabled[trace_count];
162 static int ft_trace_levels_disabled[trace_count];
166 int* ft_trace_levels;
169#define FT_TRACE_DEF( x ) #x ,
171 static const char* ft_trace_toggles[trace_count + 1] =
182 FT_BASE_DEF( FT_Int )
183 FT_Trace_Get_Count(
void )
191 FT_BASE_DEF(
const char * )
192 FT_Trace_Get_Name( FT_Int idx )
194 int max = FT_Trace_Get_Count();
198 return ft_trace_toggles[idx];
207 FT_Trace_Disable(
void )
209 ft_trace_levels = ft_trace_levels_disabled;
216 FT_Trace_Enable(
void )
218 ft_trace_levels = ft_trace_levels_enabled;
241 ft_debug_init(
void )
243 const char* ft2_debug = NULL;
246#ifdef FT_DEBUG_LOGGING
247 if ( ft_custom_trace_level != NULL )
248 ft2_debug = ft_custom_trace_level;
250 ft2_debug = ft_default_trace_level;
252 ft2_debug = ft_getenv(
"FT2_DEBUG" );
257 const char* p = ft2_debug;
264 if ( *p ==
' ' || *p ==
'\t' || *p ==
',' || *p ==
';' || *p ==
'=' )
267#ifdef FT_DEBUG_LOGGING
280 ft_component_flag = TRUE;
284 ft_timestamp_flag = TRUE;
291 else if ( *r ==
't' )
296 ft_timestamp_flag = TRUE;
300 ft_component_flag = TRUE;
312 while ( *p && *p !=
':' )
318 if ( *p ==
':' && p > q )
320 FT_Int n, i, len = (FT_Int)( p - q );
321 FT_Int level = -1, found = -1;
324 for ( n = 0; n < trace_count; n++ )
326 const char* toggle = ft_trace_toggles[n];
329 for ( i = 0; i < len; i++ )
331 if ( toggle[i] != q[i] )
335 if ( i == len && toggle[i] == 0 )
347 if ( level < 0 || level > 7 )
351 if ( found >= 0 && level >= 0 )
353 if ( found == trace_any )
356 for ( n = 0; n < trace_count; n++ )
357 ft_trace_levels_enabled[n] = level;
360 ft_trace_levels_enabled[found] = level;
366 ft_trace_levels = ft_trace_levels_enabled;
374 ft_debug_init(
void )
380 FT_BASE_DEF( FT_Int )
381 FT_Trace_Get_Count(
void )
387 FT_BASE_DEF(
const char * )
388 FT_Trace_Get_Name( FT_Int idx )
397 FT_Trace_Disable(
void )
406 FT_Trace_Enable(
void )
414#ifdef FT_DEBUG_LOGGING
423 ft_logging_init(
void )
425 ft_default_log_handler = ft_log_handler;
426 ft_default_trace_level = ft_getenv(
"FT2_DEBUG" );
428 if ( ft_getenv(
"FT_LOGGING_FILE" ) )
429 ft_fileptr = ft_fopen( ft_getenv(
"FT_LOGGING_FILE" ),
"w" );
436 dlg_set_handler( ft_default_log_handler, NULL );
441 ft_logging_deinit(
void )
443 if ( ft_fileptr != stderr )
444 ft_fclose( ft_fileptr );
454 ft_log_handler(
const struct dlg_origin* origin,
458 char features_buf[128];
459 char* bufp = features_buf;
464 if ( ft_have_newline_char )
466 const char* features = NULL;
467 size_t features_length = 0;
470#define FEATURES_TIMESTAMP "[%h:%m] "
471#define FEATURES_COMPONENT "[%t] "
472#define FEATURES_TIMESTAMP_COMPONENT "[%h:%m %t] "
474 if ( ft_timestamp_flag && ft_component_flag )
476 features = FEATURES_TIMESTAMP_COMPONENT;
477 features_length =
sizeof ( FEATURES_TIMESTAMP_COMPONENT );
479 else if ( ft_timestamp_flag )
481 features = FEATURES_TIMESTAMP;
482 features_length =
sizeof ( FEATURES_TIMESTAMP );
484 else if ( ft_component_flag )
486 features = FEATURES_COMPONENT;
487 features_length =
sizeof ( FEATURES_COMPONENT );
490 if ( ft_component_flag || ft_timestamp_flag )
492 ft_strncpy( features_buf, features, features_length );
493 bufp += features_length - 1;
496 if ( ft_component_flag )
498 size_t tag_length = ft_strlen( *origin->tags );
506 i < FT_MAX_TRACE_LEVEL_LENGTH - tag_length;
517 dlg_generic_outputf_stream( ft_fileptr,
518 (
const char*)features_buf,
521 dlg_default_output_styles,
524 if ( ft_strrchr(
string,
'\n' ) )
525 ft_have_newline_char = TRUE;
527 ft_have_newline_char = FALSE;
533 ft_add_tag(
const char* tag )
537 dlg_add_tag( tag, NULL );
543 ft_remove_tag(
const char* tag )
545 dlg_remove_tag( tag, NULL );
551 FT_EXPORT_DEF(
void )
552 FT_Trace_Set_Level(
const char* level )
554 ft_component_flag = FALSE;
555 ft_timestamp_flag = FALSE;
556 ft_custom_trace_level = level;
564 FT_EXPORT_DEF(
void )
565 FT_Trace_Set_Default_Level(
void )
567 ft_component_flag = FALSE;
568 ft_timestamp_flag = FALSE;
569 ft_custom_trace_level = NULL;
583 FT_EXPORT_DEF(
void )
584 FT_Set_Log_Handler( FT_Custom_Log_Handler handler )
586 custom_output_handler = handler;
592 FT_EXPORT_DEF(
void )
593 FT_Set_Default_Log_Handler(
void )
595 custom_output_handler = NULL;
601 FT_Logging_Callback(
const char* fmt,
608 custom_output_handler( ft_component, fmt, ap );
614 FT_EXPORT_DEF(
void )
615 FT_Trace_Set_Level(
const char* level )
621 FT_EXPORT_DEF(
void )
622 FT_Trace_Set_Default_Level(
void )
628 FT_EXPORT_DEF(
void )
629 FT_Set_Log_Handler( FT_Custom_Log_Handler handler )
631 FT_UNUSED( handler );
635 FT_EXPORT_DEF(
void )
636 FT_Set_Default_Log_Handler(
void )