1 /****************************************************************************
5 * Debugging and logging component (body).
7 * Copyright (C) 1996-2020 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.
19 /**************************************************************************
21 * This component contains various macros and functions used to ease the
22 * debugging of the FreeType engine. Its main purpose is in assertion
23 * checking, tracing, and error detection.
25 * There are now three debugging modes:
29 * Error and trace messages are sent to the log file (which can be the
30 * standard error output).
34 * Only error messages are generated.
38 * No error message is sent or generated. The code is free from any
44 #include <freetype/freetype.h>
45 #include <freetype/internal/ftdebug.h>
48 #ifdef FT_DEBUG_LEVEL_ERROR
50 /* documentation is in ftdebug.h */
53 FT_Message( const char* fmt,
60 vfprintf( stderr, fmt, ap );
65 /* documentation is in ftdebug.h */
68 FT_Panic( const char* fmt,
75 vfprintf( stderr, fmt, ap );
82 /* documentation is in ftdebug.h */
85 FT_Throw( FT_Error error,
90 /* activating the code in this block makes FreeType very chatty */
92 "%s:%d: error 0x%02x: %s\n",
96 FT_Error_String( error ) );
106 #endif /* FT_DEBUG_LEVEL_ERROR */
110 #ifdef FT_DEBUG_LEVEL_TRACE
112 /* array of trace levels, initialized to 0; */
113 /* this gets adjusted at run-time */
114 static int ft_trace_levels_enabled[trace_count];
116 /* array of trace levels, always initialized to 0 */
117 static int ft_trace_levels_disabled[trace_count];
119 /* a pointer to either `ft_trace_levels_enabled' */
120 /* or `ft_trace_levels_disabled' */
121 int* ft_trace_levels;
123 /* define array of trace toggle names */
124 #define FT_TRACE_DEF( x ) #x ,
126 static const char* ft_trace_toggles[trace_count + 1] =
128 #include <freetype/internal/fttrace.h>
135 /* documentation is in ftdebug.h */
137 FT_BASE_DEF( FT_Int )
138 FT_Trace_Get_Count( void )
144 /* documentation is in ftdebug.h */
146 FT_BASE_DEF( const char * )
147 FT_Trace_Get_Name( FT_Int idx )
149 int max = FT_Trace_Get_Count();
153 return ft_trace_toggles[idx];
159 /* documentation is in ftdebug.h */
162 FT_Trace_Disable( void )
164 ft_trace_levels = ft_trace_levels_disabled;
168 /* documentation is in ftdebug.h */
171 FT_Trace_Enable( void )
173 ft_trace_levels = ft_trace_levels_enabled;
177 /**************************************************************************
179 * Initialize the tracing sub-system. This is done by retrieving the
180 * value of the `FT2_DEBUG' environment variable. It must be a list of
181 * toggles, separated by spaces, `;', or `,'. Example:
183 * export FT2_DEBUG="any:3 memory:7 stream:5"
185 * This requests that all levels be set to 3, except the trace level for
186 * the memory and stream components which are set to 7 and 5,
189 * See the file `include/freetype/internal/fttrace.h' for details of
190 * the available toggle names.
192 * The level must be between 0 and 7; 0 means quiet (except for serious
193 * runtime errors), and 7 means _very_ verbose.
196 ft_debug_init( void )
198 const char* ft2_debug = ft_getenv( "FT2_DEBUG" );
203 const char* p = ft2_debug;
209 /* skip leading whitespace and separators */
210 if ( *p == ' ' || *p == '\t' || *p == ',' || *p == ';' || *p == '=' )
213 /* read toggle name, followed by ':' */
215 while ( *p && *p != ':' )
221 if ( *p == ':' && p > q )
223 FT_Int n, i, len = (FT_Int)( p - q );
224 FT_Int level = -1, found = -1;
227 for ( n = 0; n < trace_count; n++ )
229 const char* toggle = ft_trace_toggles[n];
232 for ( i = 0; i < len; i++ )
234 if ( toggle[i] != q[i] )
238 if ( i == len && toggle[i] == 0 )
250 if ( level < 0 || level > 7 )
254 if ( found >= 0 && level >= 0 )
256 if ( found == trace_any )
258 /* special case for `any' */
259 for ( n = 0; n < trace_count; n++ )
260 ft_trace_levels_enabled[n] = level;
263 ft_trace_levels_enabled[found] = level;
269 ft_trace_levels = ft_trace_levels_enabled;
273 #else /* !FT_DEBUG_LEVEL_TRACE */
277 ft_debug_init( void )
283 FT_BASE_DEF( FT_Int )
284 FT_Trace_Get_Count( void )
290 FT_BASE_DEF( const char * )
291 FT_Trace_Get_Name( FT_Int idx )
300 FT_Trace_Disable( void )
306 /* documentation is in ftdebug.h */
309 FT_Trace_Enable( void )
315 #endif /* !FT_DEBUG_LEVEL_TRACE */