ftdebug.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /***************************************************************************/
  2. /* */
  3. /* ftdebug.c */
  4. /* */
  5. /* Debugging and logging component (body). */
  6. /* */
  7. /* Copyright 1996-2001 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. /* This component contains various macros and functions used to ease the */
  20. /* debugging of the FreeType engine. Its main purpose is in assertion */
  21. /* checking, tracing, and error detection. */
  22. /* */
  23. /* There are now three debugging modes: */
  24. /* */
  25. /* - trace mode */
  26. /* */
  27. /* Error and trace messages are sent to the log file (which can be the */
  28. /* standard error output). */
  29. /* */
  30. /* - error mode */
  31. /* */
  32. /* Only error messages are generated. */
  33. /* */
  34. /* - release mode: */
  35. /* */
  36. /* No error message is sent or generated. The code is free from any */
  37. /* debugging parts. */
  38. /* */
  39. /*************************************************************************/
  40. #include <ft2build.h>
  41. #include FT_FREETYPE_H
  42. #include FT_INTERNAL_DEBUG_H
  43. #if defined( FT_DEBUG_LEVEL_ERROR )
  44. FT_EXPORT_DEF( void )
  45. FT_Message( const char* fmt, ... )
  46. {
  47. va_list ap;
  48. va_start( ap, fmt );
  49. vprintf( fmt, ap );
  50. va_end( ap );
  51. }
  52. FT_EXPORT_DEF( void )
  53. FT_Panic( const char* fmt, ... )
  54. {
  55. va_list ap;
  56. va_start( ap, fmt );
  57. vprintf( fmt, ap );
  58. va_end( ap );
  59. exit( EXIT_FAILURE );
  60. }
  61. #endif /* FT_DEBUG_LEVEL_ERROR */
  62. #ifdef FT_DEBUG_LEVEL_TRACE
  63. /* array of trace levels, initialized to 0 */
  64. int ft_trace_levels[trace_count];
  65. /* define array of trace toggle names */
  66. #define FT_TRACE_DEF(x) #x ,
  67. static const char* ft_trace_toggles[trace_count + 1] =
  68. {
  69. #include FT_INTERNAL_TRACE_H
  70. NULL
  71. };
  72. #undef FT_TRACE_DEF
  73. /*************************************************************************/
  74. /* */
  75. /* Initialize the tracing sub-system. This is done by retrieving the */
  76. /* value of the "FT2_DEBUG" environment variable. It must be a list of */
  77. /* toggles, separated by spaces, `;' or `,'. Example: */
  78. /* */
  79. /* "any:3 memory:6 stream:5" */
  80. /* */
  81. /* This will request that all levels be set to 3, except the trace level */
  82. /* for the memory and stream components which are set to 6 and 5, */
  83. /* respectively. */
  84. /* */
  85. /* See the file <freetype/internal/fttrace.h> for details of the */
  86. /* available toggle names. */
  87. /* */
  88. /* The level must be between 0 and 6; 0 means quiet (except for serious */
  89. /* runtime errors), and 6 means _very_ verbose. */
  90. /* */
  91. FT_BASE_DEF( void )
  92. ft_debug_init( void )
  93. {
  94. const char* ft2_debug = getenv( "FT2_DEBUG" );
  95. if ( ft2_debug )
  96. {
  97. const char* p = ft2_debug;
  98. const char* q;
  99. for ( ; *p; p++ )
  100. {
  101. /* skip leading whitespace and separators */
  102. if ( *p == ' ' || *p == '\t' || *p == ',' || *p == ';' || *p == '=' )
  103. continue;
  104. /* read toggle name, followed by ':' */
  105. q = p;
  106. while ( *p && *p != ':' )
  107. p++;
  108. if ( *p == ':' && p > q )
  109. {
  110. FT_Int n, i, len = (FT_Int)(p - q);
  111. FT_Int level = -1, found = -1;
  112. for ( n = 0; n < trace_count; n++ )
  113. {
  114. const char* toggle = ft_trace_toggles[n];
  115. for ( i = 0; i < len; i++ )
  116. {
  117. if ( toggle[i] != q[i] )
  118. break;
  119. }
  120. if ( i == len && toggle[i] == 0 )
  121. {
  122. found = n;
  123. break;
  124. }
  125. }
  126. /* read level */
  127. p++;
  128. if ( *p )
  129. {
  130. level = *p++ - '0';
  131. if ( level < 0 || level > 6 )
  132. level = -1;
  133. }
  134. if ( found >= 0 && level >= 0 )
  135. {
  136. if ( found == trace_any )
  137. {
  138. /* special case for "any" */
  139. for ( n = 0; n < trace_count; n++ )
  140. ft_trace_levels[n] = level;
  141. }
  142. else
  143. ft_trace_levels[found] = level;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. #else /* !FT_DEBUG_LEVEL_TRACE */
  150. FT_BASE_DEF( void )
  151. ft_debug_init( void )
  152. {
  153. /* nothing */
  154. }
  155. #endif /* !FT_DEBUG_LEVEL_TRACE */
  156. /* END */