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.cpp < prev    next >
Text File  |  1993-01-15  |  5KB  |  143 lines

  1. /*
  2. ** File - tracer.cxx
  3. */
  4.  
  5. #ifndef TRACER_HXX
  6. #include "tracer.h"
  7. #endif
  8.  
  9. #include <iostream.h>
  10. #include <string.h>
  11.  
  12. char*    Tracer::prog;           // program being worked on
  13. int      Tracer::mode;           // global mode of Tracer
  14. char*    Tracer::watch;          // function to watch for and trace
  15. int     Tracer::level;
  16. ostream* Tracer::str;
  17.  
  18. // ****************************************************************:
  19. //      Tracer
  20. //
  21. // Description:
  22. //      Initialize local as well as static information.  Set up
  23. //      for total cleanup at destruction.  Also outputs Tracer
  24. //      to stream using operator<<.  Implicit assumption is that
  25. //      this is only called once per run of a program.
  26. // ****************************************************************:
  27. Tracer::Tracer(char* s, const int n, ostream &st)
  28. {
  29.         str= &st;
  30.         prog = func = s;        // Set program name
  31.         mode = n;               // Initialize the tracing mode (no default).
  32.         lmode = 1;              // Recognize this as top-level.
  33.     level = 0;
  34.         if (mode)               // Should we output?
  35.                 *str << *this << ": entered" << endl;
  36. }
  37.  
  38. // ****************************************************************:
  39. //      Tracer
  40. //
  41. // Description:
  42. //      Initialize only local information.  No total cleanup
  43. //      at destuction.  Also outputs Tracer to stream using
  44. //      operator<<.  Also checks to see if input character
  45. //      string contains information that Tracer is watching
  46. //      for and turns tracing on if it does.  Implicit assumption
  47. //      is that this is the way Tracer will be set up when needed.
  48. // ****************************************************************:
  49. Tracer::Tracer(char* s, int l)
  50. {
  51.         func = s;               // Leave program alone, but set function
  52.         emode= l;        // entry/exit mode
  53.     level++;
  54.         if (watch != 0)                 // Watching something?
  55.         {
  56.                 if (strstr(func, watch) != 0)   // Watching for this?
  57.                 {                       // yes...
  58.                         lmode = mode;   // remember global mode locally
  59.                         mode = -1;      // turn tracing on
  60.                 }
  61.         }
  62.         else                            // no...
  63.                 lmode = 0;              // set local mode to not top level
  64.  
  65.         if (mode & emode){                  // Should we output?
  66.                 *str << *this << ": entered" << endl;
  67.         }
  68. }
  69.  
  70.  
  71. // ****************************************************************:
  72. //      ~Tracer
  73. //
  74. // Description:
  75. //      Check for totalcleanup flag and cleanup.
  76. //      Also outputs Tracer to stream using operator<<.
  77. // ****************************************************************:
  78. Tracer::~Tracer()
  79. {
  80.         if (mode & emode){                       // Should we output?
  81.                 *str << *this << ": exitted" << endl;
  82.         }
  83.         level--;
  84.  
  85.         if (watch != 0)                 // Watching something?
  86.         {
  87.                 if (strstr(func, watch) != 0)   // Watching for the function?
  88.                 {                       // yes...
  89.                         mode = lmode;   // reset remembered global mode
  90.                         lmode = 0;      // this is not the top level
  91.                 }
  92.         }
  93.  
  94.         if (lmode)                      // Is this top level?
  95.                 TotalCleanup();
  96. }
  97.  
  98.  
  99. // ****************************************************************:
  100. //              TotalCleanup
  101. //
  102. // Description:
  103. //      Do what's needed to finally cleanup Tracer.
  104. //      For instance, might be a close of a log file.
  105. // ****************************************************************:
  106. void
  107. Tracer::TotalCleanup()
  108. {
  109.         if (watch)
  110.                 delete watch;
  111. }
  112.  
  113.  
  114. // ****************************************************************:
  115. //              SetWatch
  116. //
  117. // Description:
  118. //      Set a function name to watch trace on.
  119. // ****************************************************************:
  120. void
  121. Tracer::SetWatch(char* name)
  122. {
  123.         if (watch)
  124.                 delete watch;
  125.  
  126.         watch = new char[strlen(name)+1];
  127.         strcpy(watch, name);
  128. }
  129.  
  130. // ****************************************************************:
  131. //              Operator<<
  132. //
  133. // Description:
  134. //      Puts Tracer into an output stream.
  135. // ****************************************************************:
  136. ostream&
  137. operator<<(ostream& str, Tracer& x)
  138. {
  139.     for(int i=0;i<x.level;i++) str << '.';
  140.         str << x.func;   // output the Tracer object to stream
  141.         return str;
  142. }
  143.