Added support of callback functions to handle logs outside FreeType

This commit is contained in:
Priyeshkkumar 2020-07-04 13:09:33 +05:30
parent b378812f22
commit bfe6d5218a
4 changed files with 58 additions and 40 deletions

View File

@ -1,3 +1,16 @@
2020-07-04 Priyesh Kumar <priyeshkkumar@gmail.com>
* include/freetype/ftlogging.h: Added two functions `FT_Set_Log_Handler()`
and `FT_Set_Default_Log_Handler()` to enable the callback functionality.
* include/freetype/internal/ftdebug.h: Some Code Cleanup
* src/base/ftdebug.c:
1. Added definitions for functions:
`FT_Set_Default_Log_Handler()` and `FT_Set_Log_Handler()`.
2. Added support for callback function.
2020-07-03 Priyesh Kumar <priyeshkkumar@gmail.com> 2020-07-03 Priyesh Kumar <priyeshkkumar@gmail.com>
* Code Cleanup and Added more comments * Code Cleanup and Added more comments

View File

@ -22,20 +22,24 @@ FT_BEGIN_HEADER
/* An external callback function to be used to define an output handler */ /* An external callback function to be used to define an output handler */
typedef void typedef void
(*ft_custom_output_handler)( const char* string ); (*ft_custom_log_handler)( const char* fmt, va_list args );
/************************************************************************** /**************************************************************************
* *
* comments on callback * If FT_LOGGING is enabled user can provide their own function to handle
* the log messages using the function `FT_Set_Log_Handler()` by passing
* the function name which they want to use.
* User could also revert back to use FreeType's inbuilt function to
* handle logs using function `FT_Set_Default_Log_Handler()`
* Defined in src/base/ftdebug.c
* *
*/ */
/*
FT_EXPORT( void ) FT_EXPORT( void )
FT_Trace_Set_Output( ft_ouput_handler handler ); FT_Set_Log_Handler( ft_custom_log_handler handler );
FT_EXPORT( void ) FT_EXPORT( void )
FT_Trace_Set_Default_Output(); FT_Set_Default_Log_Handler();
*/
FT_END_HEADER FT_END_HEADER

View File

@ -109,18 +109,6 @@ FT_BEGIN_HEADER
* *
*/ */
#ifdef FT_LOGGING
#undef FT_Log
#define FT_Log dlg_trace
#else
#undef FT_Log
#define FT_Log FT_Message
#endif /* FT_LOGGING */
#ifdef FT_DEBUG_LEVEL_TRACE #ifdef FT_DEBUG_LEVEL_TRACE
/* we need two macros here to make cpp expand `FT_COMPONENT' */ /* we need two macros here to make cpp expand `FT_COMPONENT' */
@ -131,7 +119,7 @@ FT_BEGIN_HEADER
do \ do \
{ \ { \
if ( ft_trace_levels[FT_TRACE_COMP( FT_COMPONENT )] >= level ) \ if ( ft_trace_levels[FT_TRACE_COMP( FT_COMPONENT )] >= level ) \
FT_Log varformat; \ FT_Message varformat; \
} while ( 0 ) } while ( 0 )
#else /* !FT_DEBUG_LEVEL_TRACE */ #else /* !FT_DEBUG_LEVEL_TRACE */
@ -341,7 +329,7 @@ FT_BEGIN_HEADER
*/ */
FT_BASE( void ) FT_BASE( void )
ft_output_handler( const struct dlg_origin* origin, const char* string, ft_log_handler( const struct dlg_origin* origin, const char* string,
void* data ); void* data );
@ -360,14 +348,14 @@ FT_BEGIN_HEADER
* 4. ft_default_output_handler: stores the function pointer which is used * 4. ft_default_output_handler: stores the function pointer which is used
* internally by FreeType to print logs to file. * internally by FreeType to print logs to file.
* *
* 5. fileptr: store the FILE* * 5. ft_fileptr: store the FILE*
* *
*/ */
static const char* ft_default_trace_level = NULL; static const char* ft_default_trace_level = NULL;
static const char* ft_custom_trace_level = NULL; static const char* ft_custom_trace_level = NULL;
static ft_custom_output_handler custom_output_handler = NULL; static ft_custom_log_handler custom_output_handler = NULL;
static dlg_handler ft_default_output_handler = NULL; static dlg_handler ft_default_log_handler = NULL;
static FILE* fileptr = NULL; static FILE* ft_fileptr = NULL;
/************************************************************************** /**************************************************************************
* *

View File

@ -53,12 +53,25 @@
FT_Message( const char* fmt, FT_Message( const char* fmt,
... ) ... )
{ {
#ifdef FT_LOGGING
if( custom_output_handler != NULL )
{
va_list ap;
va_start( ap, fmt );
custom_output_handler( fmt, ap );
va_end( ap );
}
else
dlg_trace( fmt );
#else
va_list ap; va_list ap;
va_start( ap, fmt ); va_start( ap, fmt );
vfprintf( stderr, fmt, ap ); vfprintf( stderr, fmt, ap );
va_end( ap ); va_end( ap );
#endif /* FT_LOGGING */
} }
@ -340,20 +353,20 @@ else
FT_BASE_DEF( void ) FT_BASE_DEF( void )
ft_logging_init( void ) ft_logging_init( void )
{ {
ft_default_output_handler = &ft_output_handler; ft_default_log_handler = ft_log_handler;
ft_default_trace_level = ft_getenv( "FT2_DEBUG" ); ft_default_trace_level = ft_getenv( "FT2_DEBUG" );
fileptr = fopen( "freetype2.log", "w" ); ft_fileptr = fopen( "freetype2.log", "w" );
ft_debug_init(); ft_debug_init();
/* We need to set the default FreeType specific dlg's output handler */ /* We need to set the default FreeType specific dlg's output handler */
dlg_set_handler( ft_default_output_handler, NULL ); dlg_set_handler( ft_default_log_handler, NULL );
} }
FT_BASE_DEF( void ) FT_BASE_DEF( void )
ft_logging_deinit( void ) ft_logging_deinit( void )
{ {
fclose( fileptr ); fclose( ft_fileptr );
} }
/************************************************************************* /*************************************************************************
@ -363,9 +376,9 @@ else
* *
*/ */
FT_BASE_DEF( void ) FT_BASE_DEF( void )
ft_output_handler( const struct dlg_origin* origin, ft_log_handler( const struct dlg_origin* origin,
const char* string, void* data ) const char* string, void* data )
{ {
unsigned int features; unsigned int features;
if( origin->tags[0] == "error_log" ) if( origin->tags[0] == "error_log" )
{ {
@ -376,9 +389,9 @@ else
features = dlg_output_threadsafe /*| dlg_output_tags*/ ; features = dlg_output_threadsafe /*| dlg_output_tags*/ ;
} }
dlg_generic_output_stream( fileptr, features, origin, string, dlg_generic_output_stream( ft_fileptr, features, origin, string,
dlg_default_output_styles ); dlg_default_output_styles );
} }
/************************************************************************** /**************************************************************************
* *
@ -408,22 +421,22 @@ else
/**************************************************************************** /****************************************************************************
* *
* comments on callback * Functions to handle custom log handler:
* *
*/ */
/*
FT_EXPORT_DEF( void ) FT_EXPORT_DEF( void )
FT_Trace_Set_Output( ft_ouput_handler handler ) FT_Set_Log_Handler( ft_custom_log_handler handler )
{ {
ft_custom_output_handler = handler; custom_output_handler = handler;
} }
FT_EXPORT_DEF( void ) FT_EXPORT_DEF( void )
FT_Trace_Set_Default_Output() FT_Set_Default_Log_Handler()
{ {
ft_custom_output_handler = NULL; custom_output_handler = NULL;
} }
*/
#endif /* FT_LOGGING */ #endif /* FT_LOGGING */
/* END */ /* END */