Blame src/base/ftdebug.c

Packit cf904d
/***************************************************************************/
Packit cf904d
/*                                                                         */
Packit cf904d
/*  ftdebug.c                                                              */
Packit cf904d
/*                                                                         */
Packit cf904d
/*    Debugging and logging component (body).                              */
Packit cf904d
/*                                                                         */
Packit cf904d
/*  Copyright 1996-2017 by                                                 */
Packit cf904d
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
Packit cf904d
/*                                                                         */
Packit cf904d
/*  This file is part of the FreeType project, and may only be used,       */
Packit cf904d
/*  modified, and distributed under the terms of the FreeType project      */
Packit cf904d
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
Packit cf904d
/*  this file you indicate that you have read the license and              */
Packit cf904d
/*  understand and accept it fully.                                        */
Packit cf904d
/*                                                                         */
Packit cf904d
/***************************************************************************/
Packit cf904d
Packit cf904d
Packit cf904d
  /*************************************************************************/
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* This component contains various macros and functions used to ease the */
Packit cf904d
  /* debugging of the FreeType engine.  Its main purpose is in assertion   */
Packit cf904d
  /* checking, tracing, and error detection.                               */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* There are now three debugging modes:                                  */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* - trace mode                                                          */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /*   Error and trace messages are sent to the log file (which can be the */
Packit cf904d
  /*   standard error output).                                             */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* - error mode                                                          */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /*   Only error messages are generated.                                  */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* - release mode:                                                       */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /*   No error message is sent or generated.  The code is free from any   */
Packit cf904d
  /*   debugging parts.                                                    */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /*************************************************************************/
Packit cf904d
Packit cf904d
Packit cf904d
#include <ft2build.h>
Packit cf904d
#include FT_FREETYPE_H
Packit cf904d
#include FT_INTERNAL_DEBUG_H
Packit cf904d
Packit cf904d
Packit cf904d
#ifdef FT_DEBUG_LEVEL_ERROR
Packit cf904d
Packit cf904d
  /* documentation is in ftdebug.h */
Packit cf904d
Packit cf904d
  FT_BASE_DEF( void )
Packit cf904d
  FT_Message( const char*  fmt,
Packit cf904d
              ... )
Packit cf904d
  {
Packit cf904d
    va_list  ap;
Packit cf904d
Packit cf904d
Packit cf904d
    va_start( ap, fmt );
Packit cf904d
    vfprintf( stderr, fmt, ap );
Packit cf904d
    va_end( ap );
Packit cf904d
  }
Packit cf904d
Packit cf904d
Packit cf904d
  /* documentation is in ftdebug.h */
Packit cf904d
Packit cf904d
  FT_BASE_DEF( void )
Packit cf904d
  FT_Panic( const char*  fmt,
Packit cf904d
            ... )
Packit cf904d
  {
Packit cf904d
    va_list  ap;
Packit cf904d
Packit cf904d
Packit cf904d
    va_start( ap, fmt );
Packit cf904d
    vfprintf( stderr, fmt, ap );
Packit cf904d
    va_end( ap );
Packit cf904d
Packit cf904d
    exit( EXIT_FAILURE );
Packit cf904d
  }
Packit cf904d
Packit cf904d
Packit cf904d
  /* documentation is in ftdebug.h */
Packit cf904d
Packit cf904d
  FT_BASE_DEF( int )
Packit cf904d
  FT_Throw( FT_Error     error,
Packit cf904d
            int          line,
Packit cf904d
            const char*  file )
Packit cf904d
  {
Packit cf904d
    FT_UNUSED( error );
Packit cf904d
    FT_UNUSED( line );
Packit cf904d
    FT_UNUSED( file );
Packit cf904d
Packit cf904d
    return 0;
Packit cf904d
  }
Packit cf904d
Packit cf904d
#endif /* FT_DEBUG_LEVEL_ERROR */
Packit cf904d
Packit cf904d
Packit cf904d
Packit cf904d
#ifdef FT_DEBUG_LEVEL_TRACE
Packit cf904d
Packit cf904d
  /* array of trace levels, initialized to 0 */
Packit cf904d
  int  ft_trace_levels[trace_count];
Packit cf904d
Packit cf904d
Packit cf904d
  /* define array of trace toggle names */
Packit cf904d
#define FT_TRACE_DEF( x )  #x ,
Packit cf904d
Packit cf904d
  static const char*  ft_trace_toggles[trace_count + 1] =
Packit cf904d
  {
Packit cf904d
#include FT_INTERNAL_TRACE_H
Packit cf904d
    NULL
Packit cf904d
  };
Packit cf904d
Packit cf904d
#undef FT_TRACE_DEF
Packit cf904d
Packit cf904d
Packit cf904d
  /* documentation is in ftdebug.h */
Packit cf904d
Packit cf904d
  FT_BASE_DEF( FT_Int )
Packit cf904d
  FT_Trace_Get_Count( void )
Packit cf904d
  {
Packit cf904d
    return trace_count;
Packit cf904d
  }
Packit cf904d
Packit cf904d
Packit cf904d
  /* documentation is in ftdebug.h */
Packit cf904d
Packit cf904d
  FT_BASE_DEF( const char * )
Packit cf904d
  FT_Trace_Get_Name( FT_Int  idx )
Packit cf904d
  {
Packit cf904d
    int  max = FT_Trace_Get_Count();
Packit cf904d
Packit cf904d
Packit cf904d
    if ( idx < max )
Packit cf904d
      return ft_trace_toggles[idx];
Packit cf904d
    else
Packit cf904d
      return NULL;
Packit cf904d
  }
Packit cf904d
Packit cf904d
Packit cf904d
  /*************************************************************************/
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* Initialize the tracing sub-system.  This is done by retrieving the    */
Packit cf904d
  /* value of the `FT2_DEBUG' environment variable.  It must be a list of  */
Packit cf904d
  /* toggles, separated by spaces, `;', or `,'.  Example:                  */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /*    export FT2_DEBUG="any:3 memory:7 stream:5"                         */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* This requests that all levels be set to 3, except the trace level for */
Packit cf904d
  /* the memory and stream components which are set to 7 and 5,            */
Packit cf904d
  /* respectively.                                                         */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* See the file `include/freetype/internal/fttrace.h' for details of     */
Packit cf904d
  /* the available toggle names.                                           */
Packit cf904d
  /*                                                                       */
Packit cf904d
  /* The level must be between 0 and 7; 0 means quiet (except for serious  */
Packit cf904d
  /* runtime errors), and 7 means _very_ verbose.                          */
Packit cf904d
  /*                                                                       */
Packit cf904d
  FT_BASE_DEF( void )
Packit cf904d
  ft_debug_init( void )
Packit cf904d
  {
Packit cf904d
    const char*  ft2_debug = getenv( "FT2_DEBUG" );
Packit cf904d
Packit cf904d
Packit cf904d
    if ( ft2_debug )
Packit cf904d
    {
Packit cf904d
      const char*  p = ft2_debug;
Packit cf904d
      const char*  q;
Packit cf904d
Packit cf904d
Packit cf904d
      for ( ; *p; p++ )
Packit cf904d
      {
Packit cf904d
        /* skip leading whitespace and separators */
Packit cf904d
        if ( *p == ' ' || *p == '\t' || *p == ',' || *p == ';' || *p == '=' )
Packit cf904d
          continue;
Packit cf904d
Packit cf904d
        /* read toggle name, followed by ':' */
Packit cf904d
        q = p;
Packit cf904d
        while ( *p && *p != ':' )
Packit cf904d
          p++;
Packit cf904d
Packit cf904d
        if ( !*p )
Packit cf904d
          break;
Packit cf904d
Packit cf904d
        if ( *p == ':' && p > q )
Packit cf904d
        {
Packit cf904d
          FT_Int  n, i, len = (FT_Int)( p - q );
Packit cf904d
          FT_Int  level = -1, found = -1;
Packit cf904d
Packit cf904d
Packit cf904d
          for ( n = 0; n < trace_count; n++ )
Packit cf904d
          {
Packit cf904d
            const char*  toggle = ft_trace_toggles[n];
Packit cf904d
Packit cf904d
Packit cf904d
            for ( i = 0; i < len; i++ )
Packit cf904d
            {
Packit cf904d
              if ( toggle[i] != q[i] )
Packit cf904d
                break;
Packit cf904d
            }
Packit cf904d
Packit cf904d
            if ( i == len && toggle[i] == 0 )
Packit cf904d
            {
Packit cf904d
              found = n;
Packit cf904d
              break;
Packit cf904d
            }
Packit cf904d
          }
Packit cf904d
Packit cf904d
          /* read level */
Packit cf904d
          p++;
Packit cf904d
          if ( *p )
Packit cf904d
          {
Packit cf904d
            level = *p - '0';
Packit cf904d
            if ( level < 0 || level > 7 )
Packit cf904d
              level = -1;
Packit cf904d
          }
Packit cf904d
Packit cf904d
          if ( found >= 0 && level >= 0 )
Packit cf904d
          {
Packit cf904d
            if ( found == trace_any )
Packit cf904d
            {
Packit cf904d
              /* special case for `any' */
Packit cf904d
              for ( n = 0; n < trace_count; n++ )
Packit cf904d
                ft_trace_levels[n] = level;
Packit cf904d
            }
Packit cf904d
            else
Packit cf904d
              ft_trace_levels[found] = level;
Packit cf904d
          }
Packit cf904d
        }
Packit cf904d
      }
Packit cf904d
    }
Packit cf904d
  }
Packit cf904d
Packit cf904d
Packit cf904d
#else  /* !FT_DEBUG_LEVEL_TRACE */
Packit cf904d
Packit cf904d
Packit cf904d
  FT_BASE_DEF( void )
Packit cf904d
  ft_debug_init( void )
Packit cf904d
  {
Packit cf904d
    /* nothing */
Packit cf904d
  }
Packit cf904d
Packit cf904d
Packit cf904d
  FT_BASE_DEF( FT_Int )
Packit cf904d
  FT_Trace_Get_Count( void )
Packit cf904d
  {
Packit cf904d
    return 0;
Packit cf904d
  }
Packit cf904d
Packit cf904d
Packit cf904d
  FT_BASE_DEF( const char * )
Packit cf904d
  FT_Trace_Get_Name( FT_Int  idx )
Packit cf904d
  {
Packit cf904d
    FT_UNUSED( idx );
Packit cf904d
Packit cf904d
    return NULL;
Packit cf904d
  }
Packit cf904d
Packit cf904d
Packit cf904d
#endif /* !FT_DEBUG_LEVEL_TRACE */
Packit cf904d
Packit cf904d
Packit cf904d
/* END */