Imported Upstream version 2.13.2
[platform/upstream/freetype2.git] / builds / amiga / src / base / ftdebug.c
1 /****************************************************************************
2  *
3  * ftdebug.c
4  *
5  *   Debugging and logging component for amiga (body).
6  *
7  * Copyright (C) 1996-2023 by
8  * David Turner, Robert Wilhelm, Werner Lemberg, and Detlef Wuerkner.
9  *
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.
15  *
16  */
17
18
19   /**************************************************************************
20    *
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.
24    *
25    * There are now three debugging modes:
26    *
27    * - trace mode
28    *
29    *   Error and trace messages are sent to the log file (which can be the
30    *   standard error output).
31    *
32    * - error mode
33    *
34    *   Only error messages are generated.
35    *
36    * - release mode:
37    *
38    *   No error message is sent or generated.  The code is free from any
39    *   debugging parts.
40    *
41    */
42
43
44   /*
45    * Based on the default `ftdebug.c' file,
46    * replaced `vprintf' with `KVPrintF',
47    * commented out `exit',
48    * replaced `getenv' with `GetVar'.
49    */
50
51 #include <exec/types.h>
52 #include <utility/tagitem.h>
53 #include <dos/exall.h>
54 #include <dos/var.h>
55
56 #define __NOLIBBASE__
57 #define __NOLOBALIFACE__
58 #define __USE_INLINE__
59
60 #include <proto/dos.h>
61 #include <clib/debug_protos.h>
62
63 #ifndef __amigaos4__
64   extern struct Library*  DOSBase;
65 #else
66   extern struct DOSIFace*  IDOS;
67 #endif
68
69
70 #include <ft2build.h>
71 #include <freetype/freetype.h>
72 #include <freetype/internal/ftdebug.h>
73
74
75 #ifdef FT_DEBUG_LEVEL_ERROR
76
77   /* documentation is in ftdebug.h */
78
79   FT_BASE_DEF( void )
80   FT_Message( const char*  fmt,
81               ... )
82   {
83     va_list  ap;
84
85
86     va_start( ap, fmt );
87     KVPrintF( fmt, ap );
88     va_end( ap );
89   }
90
91
92   /* documentation is in ftdebug.h */
93
94   FT_BASE_DEF( void )
95   FT_Panic( const char*  fmt,
96             ... )
97   {
98     va_list  ap;
99
100
101     va_start( ap, fmt );
102     KVPrintF( fmt, ap );
103     va_end( ap );
104
105     /* exit( EXIT_FAILURE ); */
106   }
107
108
109   /* documentation is in ftdebug.h */
110
111   FT_BASE_DEF( int )
112   FT_Throw( FT_Error     error,
113             int          line,
114             const char*  file )
115   {
116 #if 0
117     /* activating the code in this block makes FreeType very chatty */
118     fprintf( stderr,
119              "%s:%d: error 0x%02x: %s\n",
120              file,
121              line,
122              error,
123              FT_Error_String( error ) );
124 #else
125     FT_UNUSED( error );
126     FT_UNUSED( line );
127     FT_UNUSED( file );
128 #endif
129
130     return 0;
131   }
132
133 #endif /* FT_DEBUG_LEVEL_ERROR */
134
135
136
137 #ifdef FT_DEBUG_LEVEL_TRACE
138
139   /* array of trace levels, initialized to 0; */
140   /* this gets adjusted at run-time           */
141   static int  ft_trace_levels_enabled[trace_count];
142
143   /* array of trace levels, always initialized to 0 */
144   static int  ft_trace_levels_disabled[trace_count];
145
146   /* a pointer to either `ft_trace_levels_enabled' */
147   /* or `ft_trace_levels_disabled'                 */
148   int*  ft_trace_levels;
149
150   /* define array of trace toggle names */
151 #define FT_TRACE_DEF( x )  #x ,
152
153   static const char*  ft_trace_toggles[trace_count + 1] =
154   {
155 #include <freetype/internal/fttrace.h>
156     NULL
157   };
158
159 #undef FT_TRACE_DEF
160
161
162   /* documentation is in ftdebug.h */
163
164   FT_BASE_DEF( FT_Int )
165   FT_Trace_Get_Count( void )
166   {
167     return trace_count;
168   }
169
170
171   /* documentation is in ftdebug.h */
172
173   FT_BASE_DEF( const char * )
174   FT_Trace_Get_Name( FT_Int  idx )
175   {
176     int  max = FT_Trace_Get_Count();
177
178
179     if ( idx < max )
180       return ft_trace_toggles[idx];
181     else
182       return NULL;
183   }
184
185
186   /* documentation is in ftdebug.h */
187
188   FT_BASE_DEF( void )
189   FT_Trace_Disable( void )
190   {
191     ft_trace_levels = ft_trace_levels_disabled;
192   }
193
194
195   /* documentation is in ftdebug.h */
196
197   FT_BASE_DEF( void )
198   FT_Trace_Enable( void )
199   {
200     ft_trace_levels = ft_trace_levels_enabled;
201   }
202
203
204   /**************************************************************************
205    *
206    * Initialize the tracing sub-system.  This is done by retrieving the
207    * value of the `FT2_DEBUG' environment variable.  It must be a list of
208    * toggles, separated by spaces, `;', or `,'.  Example:
209    *
210    *   export FT2_DEBUG="any:3 memory:7 stream:5"
211    *
212    * This requests that all levels be set to 3, except the trace level for
213    * the memory and stream components which are set to 7 and 5,
214    * respectively.
215    *
216    * See the file `include/freetype/internal/fttrace.h' for details of
217    * the available toggle names.
218    *
219    * The level must be between 0 and 7; 0 means quiet (except for serious
220    * runtime errors), and 7 means _very_ verbose.
221    */
222   FT_BASE_DEF( void )
223   ft_debug_init( void )
224   {
225     /* const char*  ft2_debug = ft_getenv( "FT2_DEBUG" ); */
226     char         buf[256];
227     const char*  ft2_debug = &buf[0];
228
229
230     /* if ( ft2_debug ) */
231     if ( GetVar( "FT2_DEBUG", (STRPTR)ft2_debug, 256, LV_VAR ) > 0 )
232     {
233       const char*  p = ft2_debug;
234       const char*  q;
235
236
237       for ( ; *p; p++ )
238       {
239         /* skip leading whitespace and separators */
240         if ( *p == ' ' || *p == '\t' || *p == ',' || *p == ';' || *p == '=' )
241           continue;
242
243         /* read toggle name, followed by ':' */
244         q = p;
245         while ( *p && *p != ':' )
246           p++;
247
248         if ( !*p )
249           break;
250
251         if ( *p == ':' && p > q )
252         {
253           FT_Int  n, i, len = (FT_Int)( p - q );
254           FT_Int  level = -1, found = -1;
255
256
257           for ( n = 0; n < trace_count; n++ )
258           {
259             const char*  toggle = ft_trace_toggles[n];
260
261
262             for ( i = 0; i < len; i++ )
263             {
264               if ( toggle[i] != q[i] )
265                 break;
266             }
267
268             if ( i == len && toggle[i] == 0 )
269             {
270               found = n;
271               break;
272             }
273           }
274
275           /* read level */
276           p++;
277           if ( *p )
278           {
279             level = *p - '0';
280             if ( level < 0 || level > 7 )
281               level = -1;
282           }
283
284           if ( found >= 0 && level >= 0 )
285           {
286             if ( found == trace_any )
287             {
288               /* special case for `any' */
289               for ( n = 0; n < trace_count; n++ )
290                 ft_trace_levels_enabled[n] = level;
291             }
292             else
293               ft_trace_levels_enabled[found] = level;
294           }
295         }
296       }
297     }
298
299     ft_trace_levels = ft_trace_levels_enabled;
300   }
301
302
303 #else  /* !FT_DEBUG_LEVEL_TRACE */
304
305
306   FT_BASE_DEF( void )
307   ft_debug_init( void )
308   {
309     /* nothing */
310   }
311
312
313   FT_BASE_DEF( FT_Int )
314   FT_Trace_Get_Count( void )
315   {
316     return 0;
317   }
318
319
320   FT_BASE_DEF( const char * )
321   FT_Trace_Get_Name( FT_Int  idx )
322   {
323     FT_UNUSED( idx );
324
325     return NULL;
326   }
327
328
329   FT_BASE_DEF( void )
330   FT_Trace_Disable( void )
331   {
332     /* nothing */
333   }
334
335
336   /* documentation is in ftdebug.h */
337
338   FT_BASE_DEF( void )
339   FT_Trace_Enable( void )
340   {
341     /* nothing */
342   }
343
344
345 #endif /* !FT_DEBUG_LEVEL_TRACE */
346
347
348 /* END */