home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / actlib12.zip / TRACE.H < prev    next >
C/C++ Source or Header  |  1993-02-25  |  3KB  |  112 lines

  1. /***
  2.  *  Copyright (C) 1993   Marc Stern  (internet: stern@mble.philips.be)
  3.  *
  4.  *  File    : trace.h
  5.  *
  6.  *  Description    : General macros for tracing
  7.  *                Print all the begining and ending of functions
  8.  *                (indented) with return values.
  9.  *
  10.  *  OS/Compiler : All
  11.  *
  12.  *  Usage       : - Add the line BEGIN( function_name, return_format );
  13.  *                  just after all the declaration of the function.
  14.  *                  return_format is one of the C standard format
  15.  *                  ( "%d", "%s", "%f", ... )
  16.  *
  17.  *                - To trace a value call the function 'trace( (...) );'
  18.  *                  where (...) will be given as is to printf.
  19.  *
  20.  *                - Define 'TRACE' to actually perform tracing,
  21.  *                  do not define it to suppress tracing.
  22.  *
  23.  ***/
  24.  
  25.  
  26. #ifndef _TRACE_H
  27. #define _TRACE_H
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32.  
  33. #ifdef TRACE    /*  For debugging  */
  34.  
  35. extern int    G_nesting;
  36.  
  37. #define main \
  38.     extern int G_nesting = 0; \
  39.     main
  40.  
  41. #define trace( args ) \
  42.     printf( "%*d : ", G_nesting, G_nesting );    \
  43.     printf args
  44.  
  45. #define BEGIN( function, ret_format ) \
  46.     char _FUNCTION_[] = #function;                \
  47. /*    char _RET_FORMAT_[] = #ret_format;*/              \
  48.     char _RET_FORMAT_[] = ret_format;              \
  49.     G_nesting ++;                                            \
  50.     printf( "\n" );                                \
  51.     trace( ("<<< Begin of function " #function "\n" ) );    \
  52.     trace( (" << in file " __FILE__ "\n") );        \
  53.     trace( (" << at line %d\n\n", __LINE__) )
  54.  
  55. #define exit( status ) \
  56.     { printf( "\n" );                        \
  57.       trace( (">>> Exit of function %s\n", _FUNCTION_) );        \
  58.       trace( (" >> in file " __FILE__ "\n") );            \
  59.       trace( (" >> at line %d\n\n", __LINE__) );            \
  60.       trace( (" >> exit code : '" #status "' = %d", status) );    \
  61.       G_nesting --;                                \
  62.       exit( status );                        \
  63.     }
  64.  
  65. #define _exit( status ) \
  66.     { printf( "\n" );                        \
  67.       trace( (">>> Exit of function %s\n", _FUNCTION_) );        \
  68.       trace( (" >> in file " __FILE__ "\n") );            \
  69.       trace( (" >> at line %d\n\n", __LINE__) );            \
  70.       trace( (" >> exit code : '" #status "' = %d", status) );    \
  71.       G_nesting --;                                \
  72.       _exit( status );                        \
  73.     }
  74.  
  75. #define return( value ) \
  76.     { char line[256];                            \
  77.       printf( "\n" );                            \
  78.       trace( (">>> Return from function %s\n", _FUNCTION_) );        \
  79.       trace( (" >> in file " __FILE__ "\n") );                \
  80.       trace( (" >> at line %d\n\n", __LINE__) );                \
  81.       sprintf( line, " >> return code : '" #value "' = %s\n\n", _RET_FORMAT_ ); \
  82.       trace( (line, value) );                        \
  83.       G_nesting --;                                    \
  84.       return( value );                            \
  85.     }
  86.  
  87. #define syserror( error ) \
  88.     { printf( "\n!!! %s\n", strerror(errno) );        \
  89.       printf( " !! in file     : " __FILE__ "\n" );            \
  90.       printf( " !! in function : %s\n", _FUNCTION_ );    \
  91.       printf( " !! at line     : %d\n", __LINE__ );            \
  92.       printf( " !! instruction : " #error "\n\n" );            \
  93.     }
  94.  
  95. #else
  96.  
  97. #define BEGIN( function, ret_format )
  98.  
  99. #define trace( args )
  100.  
  101. #define syserror( error ) \
  102.     { printf( "\n!!! %s\n", strerror(errno) );        \
  103.       printf( " !! in file     : " __FILE__ "\n" );            \
  104.       printf( " !! at line     : %d\n", __LINE__ );            \
  105.       printf( " !! instruction : " #error "\n\n" );            \
  106.     }
  107.  
  108. #endif
  109.  
  110.  
  111. #endif
  112.