1 /***************************************************************************/
2 /*                                                                         */
3 /*  ftdebug.c                                                              */
4 /*                                                                         */
5 /*    Debugging and logging component for WinCE (body).                    */
6 /*                                                                         */
7 /*  Copyright 1996-2018 by                                                 */
8 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
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 #include <ft2build.h>
45 #include FT_INTERNAL_DEBUG_H
46 
47 
48 #ifdef FT_DEBUG_LEVEL_ERROR
49 
50 
51 #include <stdarg.h>
52 #include <stdlib.h>
53 #include <string.h>
54 
55 #include <windows.h>
56 
57 
58   void
OutputDebugStringEx(const char * str)59   OutputDebugStringEx( const char*  str )
60   {
61     static WCHAR  buf[8192];
62 
63 
64     int sz = MultiByteToWideChar( CP_ACP, 0, str, -1, buf,
65                                   sizeof ( buf ) / sizeof ( *buf ) );
66     if ( !sz )
67       lstrcpyW( buf, L"OutputDebugStringEx: MultiByteToWideChar failed" );
68 
69     OutputDebugStringW( buf );
70   }
71 
72 
73   FT_BASE_DEF( void )
FT_Message(const char * fmt,...)74   FT_Message( const char*  fmt,
75               ... )
76   {
77     static char  buf[8192];
78     va_list      ap;
79 
80 
81     va_start( ap, fmt );
82     vfprintf( stderr, fmt, ap );
83     /* send the string to the debugger as well */
84     vsprintf( buf, fmt, ap );
85     OutputDebugStringEx( buf );
86     va_end( ap );
87   }
88 
89 
90   FT_BASE_DEF( void )
FT_Panic(const char * fmt,...)91   FT_Panic( const char*  fmt,
92             ... )
93   {
94     static char  buf[8192];
95     va_list      ap;
96 
97 
98     va_start( ap, fmt );
99     vsprintf( buf, fmt, ap );
100     OutputDebugStringEx( buf );
101     va_end( ap );
102 
103     exit( EXIT_FAILURE );
104   }
105 
106 
107   /* documentation is in ftdebug.h */
108 
109   FT_BASE_DEF( int )
FT_Throw(FT_Error error,int line,const char * file)110   FT_Throw( FT_Error     error,
111             int          line,
112             const char*  file )
113   {
114     FT_UNUSED( error );
115     FT_UNUSED( line );
116     FT_UNUSED( file );
117 
118     return 0;
119   }
120 
121 #ifdef FT_DEBUG_LEVEL_TRACE
122 
123 
124   /* array of trace levels, initialized to 0 */
125   int  ft_trace_levels[trace_count];
126 
127   /* define array of trace toggle names */
128 #define FT_TRACE_DEF( x )  #x ,
129 
130   static const char*  ft_trace_toggles[trace_count + 1] =
131   {
132 #include FT_INTERNAL_TRACE_H
133     NULL
134   };
135 
136 #undef FT_TRACE_DEF
137 
138 
139   /*************************************************************************/
140   /*                                                                       */
141   /* Initialize the tracing sub-system.  This is done by retrieving the    */
142   /* value of the "FT2_DEBUG" environment variable.  It must be a list of  */
143   /* toggles, separated by spaces, `;' or `,'.  Example:                   */
144   /*                                                                       */
145   /*    "any:3 memory:6 stream:5"                                          */
146   /*                                                                       */
147   /* This will request that all levels be set to 3, except the trace level */
148   /* for the memory and stream components which are set to 6 and 5,        */
149   /* respectively.                                                         */
150   /*                                                                       */
151   /* See the file `include/freetype/internal/fttrace.h' for details of the */
152   /* available toggle names.                                               */
153   /*                                                                       */
154   /* The level must be between 0 and 6; 0 means quiet (except for serious  */
155   /* runtime errors), and 6 means _very_ verbose.                          */
156   /*                                                                       */
157   FT_BASE_DEF( void )
ft_debug_init(void)158   ft_debug_init( void )
159   {
160     /* Windows Mobile doesn't have environment API:           */
161     /* GetEnvironmentStrings, GetEnvironmentVariable, getenv. */
162     /*                                                        */
163     /* FIXME!!! How to set debug mode?                        */
164 
165     /* const char*  ft2_debug = getenv( "FT2_DEBUG" ); */
166 
167     const char*  ft2_debug = 0;
168 
169 
170     if ( ft2_debug )
171     {
172       const char*  p = ft2_debug;
173       const char*  q;
174 
175 
176       for ( ; *p; p++ )
177       {
178         /* skip leading whitespace and separators */
179         if ( *p == ' ' || *p == '\t' || *p == ',' || *p == ';' || *p == '=' )
180           continue;
181 
182         /* read toggle name, followed by ':' */
183         q = p;
184         while ( *p && *p != ':' )
185           p++;
186 
187         if ( !*p )
188           break;
189 
190         if ( *p == ':' && p > q )
191         {
192           int  n, i, len = (int)( p - q );
193           int  level = -1, found = -1;
194 
195 
196           for ( n = 0; n < trace_count; n++ )
197           {
198             const char*  toggle = ft_trace_toggles[n];
199 
200 
201             for ( i = 0; i < len; i++ )
202             {
203               if ( toggle[i] != q[i] )
204                 break;
205             }
206 
207             if ( i == len && toggle[i] == 0 )
208             {
209               found = n;
210               break;
211             }
212           }
213 
214           /* read level */
215           p++;
216           if ( *p )
217           {
218             level = *p - '0';
219             if ( level < 0 || level > 7 )
220               level = -1;
221           }
222 
223           if ( found >= 0 && level >= 0 )
224           {
225             if ( found == trace_any )
226             {
227               /* special case for "any" */
228               for ( n = 0; n < trace_count; n++ )
229                 ft_trace_levels[n] = level;
230             }
231             else
232               ft_trace_levels[found] = level;
233           }
234         }
235       }
236     }
237   }
238 
239 
240 #else  /* !FT_DEBUG_LEVEL_TRACE */
241 
242 
243   FT_BASE_DEF( void )
ft_debug_init(void)244   ft_debug_init( void )
245   {
246     /* nothing */
247   }
248 
249 
250 #endif /* !FT_DEBUG_LEVEL_TRACE */
251 
252 #endif /* FT_DEBUG_LEVEL_ERROR */
253 
254 
255 /* END */
256