home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / splash / tracer.h < prev    next >
Text File  |  1993-01-15  |  5KB  |  134 lines

  1. /*
  2. ** Tracer object
  3. **
  4. ** Purpose:
  5. **      Mostly for tracing function calls, but may be adapted for
  6. **      logging a trace of a program to somewhere else.
  7. */
  8.  
  9. #ifndef TRACER_HXX
  10. #define TRACER_HXX
  11.  
  12. // .NAME Tracer - class to manage providing a trace of functions.
  13. // .LIBRARY util
  14. // .HEADER Utility Classes
  15. // .INCLUDE tracer.hxx
  16. // .FILE tracer.hxx
  17. // .FILE tracer.cxx
  18.  
  19. // .SECTION Description
  20. // This class makes use of some of the semantics of C++ to provide
  21. // a convenient way of tracing function calls.  This code was initially
  22. // suggested by Bjarne Stroustrup, but has now been modified to allow
  23. // control of when tracing takes place.  Tracing messages are sprinkled
  24. // about the user's code and, at the user's choosing, the type of
  25. // tracing is enabled.  Users of this object class will rarely (if
  26. // ever) directly see the class.  Instead, they will use the macro
  27. // interface defined with this class.
  28.  
  29. // .SECTION Tracing Types
  30. // Tracer may be set to output its tracing information according to the
  31. // type of mode it has been put into.  At the simplest level, Tracer may
  32. // be set to output all tracing information by setting its type to -1
  33. // (which is what TRACE_ON does).  At the next level, messages that
  34. // Tracer is told about may be given a positive integer type < 32.
  35. // Tracer may also be set to output only messages that are of certain
  36. // types (via bitwise AND of current type with message type).  Users
  37. // can, therefore, monitor all types of messages, some of the types,
  38. // or just one of the types depending on the user's interest.  The final
  39. // level is that a user may turn on monitoring of a function call by
  40. // telling Tracer the name of the function to WATCH.  When Tracer hits
  41. // the function in question, it turns TRACE_ON and turns TRACE_OFF when
  42. // it leaves the function (so, all lower functions are also traced).
  43.  
  44. #include <iostream.h>
  45.  
  46.  
  47. class Tracer
  48. {
  49. public:
  50.         Tracer(char*, const int, ostream &st);   // Full tracer invocation
  51.         Tracer(char*, int= -1);                  // Lower level invocation
  52.  
  53.         ~Tracer();                      // Close tracer invocation
  54.  
  55.         void    TotalCleanup();         // Clean up when fully done
  56.  
  57.         void    SetMode(const int n){mode = n;} // Set current mode of tracing
  58.         int     GetMode(){ return mode;}        // Determine current tracing mode
  59.  
  60.         void    SetWatch(char*);            // Function name to begin tracing in
  61.         char*   GetWatch(){return watch;};      // What we are watching for?
  62.  
  63.         ostream& GetStream(void){ return *str; }
  64.         friend ostream& operator<<(ostream&, Tracer&);
  65.                                         // Output tracing information
  66.  
  67. private:
  68.         char*           func;           // function being worked on
  69.         int             lmode;          // local mode of Tracer
  70.         int        emode;        // entry/exit mode, determines if printed
  71.         static char*    prog;           // program being worked on
  72.         static int      mode;           // global mode of Tracer
  73.         static char*    watch;          // function to watch for and trace
  74.     static int    level;        // function call nesting
  75.     static ostream*    str;        // stream to output onto
  76. };
  77.  
  78.  
  79. // ****************************************************************
  80. //              Macro Interface to Tracer Object
  81. // ****************************************************************
  82. #ifdef DOTRACER
  83.  
  84. #define FTRACER(s,n,st)    /* Trace program s with type n */\
  85.         Tracer  _trace(s, n, st);
  86.  
  87. #define TRACER(s)       /* Trace function s */\
  88.         Tracer  _trace(s);
  89.  
  90. #define LTRACER(s, l)       /* Trace function s with type n */\
  91.         Tracer  _trace(s, l);
  92.  
  93. #define TRACE(s)        /* If TRACE_ON then output s */\
  94.         if (_trace.GetMode()) _trace.GetStream() << _trace << ": " << s << endl;
  95.  
  96. #define TRACEF(f)       /* If TRACE_ON then call function f */\
  97.         if (_trace.GetMode()) f;
  98.  
  99. #define TRACE_ON        /* Turn tracing on */\
  100.         _trace.SetMode(-1);
  101.  
  102. #define TRACE_OFF       /* Turn tracing off */\
  103.         _trace.SetMode(0);
  104.  
  105. #define LTRACE(l,s)     /* If (type & l) then output s */\
  106.         if (_trace.GetMode() & l) \
  107.                 _trace.GetStream() << _trace << ": " << s << endl;
  108.  
  109. #define LTRACEF(l,f)    /* If (type & l) then call function f */\
  110.         if (_trace.GetMode() & l) f;
  111.  
  112. #define STRACE(m)       /* Set tracing to type m */\
  113.         _trace.SetMode(m);
  114.  
  115. #define WATCH(s)        /* Set function to watch to s */\
  116.         _trace.SetWatch(s);
  117.  
  118. #else
  119.  
  120. #define FTRACER(s,n,st) /**/
  121. #define TRACER(s)       /**/
  122. #define LTRACER(s,l)    /**/
  123. #define TRACE(s)        /**/
  124. #define TRACE_ON        /**/
  125. #define TRACE_OFF       /**/
  126. #define LTRACE(l,s)     /**/
  127. #define LTRACEF(l,s)    /**/
  128. #define STRACE(m)       /**/
  129. #define WATCH(s)        /**/
  130.  
  131. #endif
  132.  
  133. #endif
  134.