1 /****************************************************************************
5 * Debugging and logging component (specification).
7 * Copyright (C) 1996-2023 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
17 * IMPORTANT: A description of FreeType's debugging support can be
18 * found in 'docs/DEBUG.TXT'. Read it if you need to use or
19 * understand this code.
29 #include FT_CONFIG_CONFIG_H
30 #include <freetype/freetype.h>
32 #include "compiler-macros.h"
34 #ifdef FT_DEBUG_LOGGING
36 #include <dlg/output.h>
39 #include <freetype/ftlogging.h>
40 #endif /* FT_DEBUG_LOGGING */
45 /* force the definition of FT_DEBUG_LEVEL_TRACE if FT_DEBUG_LOGGING is */
46 /* already defined. */
48 #ifdef FT_DEBUG_LOGGING
49 #undef FT_DEBUG_LEVEL_TRACE
50 #define FT_DEBUG_LEVEL_TRACE
53 /* force the definition of FT_DEBUG_LEVEL_ERROR if FT_DEBUG_LEVEL_TRACE */
54 /* is already defined; this simplifies the following #ifdefs */
56 #ifdef FT_DEBUG_LEVEL_TRACE
57 #undef FT_DEBUG_LEVEL_ERROR
58 #define FT_DEBUG_LEVEL_ERROR
62 /**************************************************************************
64 * Define the trace enums as well as the trace levels array when they are
69 #ifdef FT_DEBUG_LEVEL_TRACE
71 #define FT_TRACE_DEF( x ) trace_ ## x ,
73 /* defining the enumeration */
74 typedef enum FT_Trace_
76 #include <freetype/internal/fttrace.h>
82 /* a pointer to the array of trace levels, */
83 /* provided by `src/base/ftdebug.c' */
84 extern int* ft_trace_levels;
88 #endif /* FT_DEBUG_LEVEL_TRACE */
91 /**************************************************************************
93 * Define the FT_TRACE macro
97 * Each component must define the macro FT_COMPONENT to a valid FT_Trace
98 * value before using any TRACE macro.
100 * To get consistent logging output, there should be no newline character
101 * (i.e., '\n') or a single trailing one in the message string of
102 * `FT_TRACEx` and `FT_ERROR`.
106 /*************************************************************************
108 * If FT_DEBUG_LOGGING is enabled, tracing messages are sent to dlg's API.
109 * If FT_DEBUG_LOGGING is disabled, tracing messages are sent to
110 * `FT_Message` (defined in ftdebug.c).
112 #ifdef FT_DEBUG_LOGGING
114 /* we need two macros to convert the names of `FT_COMPONENT` to a string */
115 #define FT_LOGGING_TAG( x ) FT_LOGGING_TAG_( x )
116 #define FT_LOGGING_TAG_( x ) #x
118 /* we need two macros to convert the component and the trace level */
119 /* to a string that combines them */
120 #define FT_LOGGING_TAGX( x, y ) FT_LOGGING_TAGX_( x, y )
121 #define FT_LOGGING_TAGX_( x, y ) #x ":" #y
124 #define FT_LOG( level, varformat ) \
127 const char* dlg_tag = FT_LOGGING_TAGX( FT_COMPONENT, level ); \
130 ft_add_tag( dlg_tag ); \
131 if ( ft_trace_levels[FT_TRACE_COMP( FT_COMPONENT )] >= level ) \
133 if ( custom_output_handler != NULL ) \
134 FT_Logging_Callback varformat; \
136 dlg_trace varformat; \
138 ft_remove_tag( dlg_tag ); \
141 #else /* !FT_DEBUG_LOGGING */
143 #define FT_LOG( level, varformat ) \
146 if ( ft_trace_levels[FT_TRACE_COMP( FT_COMPONENT )] >= level ) \
147 FT_Message varformat; \
150 #endif /* !FT_DEBUG_LOGGING */
153 #ifdef FT_DEBUG_LEVEL_TRACE
155 /* we need two macros here to make cpp expand `FT_COMPONENT' */
156 #define FT_TRACE_COMP( x ) FT_TRACE_COMP_( x )
157 #define FT_TRACE_COMP_( x ) trace_ ## x
159 #define FT_TRACE( level, varformat ) FT_LOG( level, varformat )
161 #else /* !FT_DEBUG_LEVEL_TRACE */
163 #define FT_TRACE( level, varformat ) do { } while ( 0 ) /* nothing */
165 #endif /* !FT_DEBUG_LEVEL_TRACE */
168 /**************************************************************************
174 * Return the number of available trace components.
177 * The number of trace components. 0 if FreeType 2 is not built with
178 * FT_DEBUG_LEVEL_TRACE definition.
181 * This function may be useful if you want to access elements of the
182 * internal trace levels array by an index.
185 FT_Trace_Get_Count( void );
188 /**************************************************************************
194 * Return the name of a trace component.
197 * The index of the trace component.
200 * The name of the trace component. This is a statically allocated
201 * C~string, so do not free it after use. `NULL` if FreeType is not
202 * built with FT_DEBUG_LEVEL_TRACE definition.
205 * Use @FT_Trace_Get_Count to get the number of available trace
208 FT_BASE( const char* )
209 FT_Trace_Get_Name( FT_Int idx );
212 /**************************************************************************
218 * Switch off tracing temporarily. It can be activated again with
222 FT_Trace_Disable( void );
225 /**************************************************************************
231 * Activate tracing. Use it after tracing has been switched off with
235 FT_Trace_Enable( void );
238 /**************************************************************************
240 * You need two opening and closing parentheses!
242 * Example: FT_TRACE0(( "Value is %i", foo ))
244 * Output of the FT_TRACEX macros is sent to stderr.
248 #define FT_TRACE0( varformat ) FT_TRACE( 0, varformat )
249 #define FT_TRACE1( varformat ) FT_TRACE( 1, varformat )
250 #define FT_TRACE2( varformat ) FT_TRACE( 2, varformat )
251 #define FT_TRACE3( varformat ) FT_TRACE( 3, varformat )
252 #define FT_TRACE4( varformat ) FT_TRACE( 4, varformat )
253 #define FT_TRACE5( varformat ) FT_TRACE( 5, varformat )
254 #define FT_TRACE6( varformat ) FT_TRACE( 6, varformat )
255 #define FT_TRACE7( varformat ) FT_TRACE( 7, varformat )
258 /**************************************************************************
260 * Define the FT_ERROR macro.
262 * Output of this macro is sent to stderr.
266 #ifdef FT_DEBUG_LEVEL_ERROR
268 /**************************************************************************
270 * If FT_DEBUG_LOGGING is enabled, error messages are sent to dlg's API.
271 * If FT_DEBUG_LOGGING is disabled, error messages are sent to `FT_Message`
272 * (defined in ftdebug.c).
275 #ifdef FT_DEBUG_LOGGING
277 #define FT_ERROR( varformat ) \
280 const char* dlg_tag = FT_LOGGING_TAG( FT_COMPONENT ); \
283 ft_add_tag( dlg_tag ); \
284 dlg_trace varformat; \
285 ft_remove_tag( dlg_tag ); \
288 #else /* !FT_DEBUG_LOGGING */
290 #define FT_ERROR( varformat ) FT_Message varformat
292 #endif /* !FT_DEBUG_LOGGING */
295 #else /* !FT_DEBUG_LEVEL_ERROR */
297 #define FT_ERROR( varformat ) do { } while ( 0 ) /* nothing */
299 #endif /* !FT_DEBUG_LEVEL_ERROR */
302 /**************************************************************************
304 * Define the FT_ASSERT and FT_THROW macros. The call to `FT_Throw` makes
305 * it possible to easily set a breakpoint at this function.
309 #ifdef FT_DEBUG_LEVEL_ERROR
311 #define FT_ASSERT( condition ) \
314 if ( !( condition ) ) \
315 FT_Panic( "assertion failed on line %d of file %s\n", \
316 __LINE__, __FILE__ ); \
319 #define FT_THROW( e ) \
320 ( FT_Throw( FT_ERR_CAT( FT_ERR_PREFIX, e ), \
323 FT_ERR_CAT( FT_ERR_PREFIX, e ) )
325 #else /* !FT_DEBUG_LEVEL_ERROR */
327 #define FT_ASSERT( condition ) do { } while ( 0 )
329 #define FT_THROW( e ) FT_ERR_CAT( FT_ERR_PREFIX, e )
331 #endif /* !FT_DEBUG_LEVEL_ERROR */
334 /**************************************************************************
336 * Define `FT_Message` and `FT_Panic` when needed.
340 #ifdef FT_DEBUG_LEVEL_ERROR
342 #include "stdio.h" /* for vfprintf() */
344 /* print a message */
346 FT_Message( const char* fmt,
349 /* print a message and exit */
351 FT_Panic( const char* fmt,
354 /* report file name and line number of an error */
356 FT_Throw( FT_Error error,
360 #endif /* FT_DEBUG_LEVEL_ERROR */
364 ft_debug_init( void );
367 #ifdef FT_DEBUG_LOGGING
369 /**************************************************************************
371 * 'dlg' uses output handlers to control how and where log messages are
372 * printed. Therefore we need to define a default output handler for
376 ft_log_handler( const struct dlg_origin* origin,
381 /**************************************************************************
383 * 1. `ft_default_log_handler` stores the function pointer that is used
384 * internally by FreeType to print logs to a file.
386 * 2. `custom_output_handler` stores the function pointer to the callback
387 * function provided by the user.
389 * It is defined in `ftdebug.c`.
391 extern dlg_handler ft_default_log_handler;
392 extern FT_Custom_Log_Handler custom_output_handler;
395 /**************************************************************************
397 * If FT_DEBUG_LOGGING macro is enabled, FreeType needs to initialize and
398 * un-initialize `FILE*`.
400 * These functions are defined in `ftdebug.c`.
403 ft_logging_init( void );
406 ft_logging_deinit( void );
409 /**************************************************************************
411 * For printing the name of `FT_COMPONENT` along with the actual log we
412 * need to add a tag with the name of `FT_COMPONENT`.
414 * These functions are defined in `ftdebug.c`.
417 ft_add_tag( const char* tag );
420 ft_remove_tag( const char* tag );
423 /**************************************************************************
425 * A function to print log data using a custom callback logging function
426 * (which is set using `FT_Set_Log_Handler`).
428 * This function is defined in `ftdebug.c`.
431 FT_Logging_Callback( const char* fmt,
434 #endif /* FT_DEBUG_LOGGING */
439 #endif /* FTDEBUG_H_ */