home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / SRC / TRACE.CPP < prev    next >
C/C++ Source or Header  |  1996-07-08  |  9KB  |  290 lines

  1. /****************************************************************************
  2.     $Id: trace.cpp 501.0 1995/03/07 12:26:24 RON Exp $
  3.  
  4.     Copyright (c) 1991-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Implementation of tracing functions.
  10.  
  11.     $Log: trace.cpp $
  12.     Revision 501.0  1995/03/07 12:26:24  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.11  1995/02/28 15:15:22  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.10  1995/01/18  19:04:13  ron
  18.     Added support for GUI/non-GUI output
  19.  
  20.     Revision 1.9  1995/01/06  15:58:38  ron
  21.     Corrected Revision keyword
  22.  
  23.     Revision 1.8  1995/01/05  15:31:04  ron
  24.     Changes in tracing output format
  25.  
  26.     Revision 1.7  1994/11/16  15:45:06  ron
  27.     Made global tracing functions into static members of TLTracer
  28.     Added module info; rearranged #include directives
  29.  
  30.     Revision 1.6  1994/10/05  18:45:11  ron
  31.     Renamed TLx...() functions to tl...()
  32.  
  33.     Revision 1.5  1994/09/28  14:46:55  ron
  34.     Removed Macintosh-style #include references
  35.  
  36.     Revision 1.4  1994/09/27  20:23:16  ron
  37.     Changed path separator from / to \
  38.  
  39.     Revision 1.3  1994/09/26  15:49:45  ron
  40.     Small formatting changes
  41.  
  42.     Revision 1.2  1994/09/12  14:57:34  ron
  43.     Changed wording and layout of diagnostic trace messages slightly
  44.  
  45.     Revision 1.1  1994/08/16  18:13:19  ron
  46.     Initial revision
  47.  
  48. ****************************************************************************/
  49.  
  50. #include <tlx\501\_build.h>
  51.  
  52. TLX_MODULE_INFO("$Revision: 501.0 $");
  53.  
  54. #include <iostream.h>        // For ostream
  55. #include <stdio.h>        // For vsprintf()
  56. #include <stdlib.h>        // For getenv()
  57. #include <string.h>        // For strlen()/
  58.  
  59. #if OS_WINXXX && defined(_GUI)
  60.   #define STRICT
  61.   #include <windows.h>
  62. #endif
  63.  
  64. #ifndef TLX_MAX_MSGLEN
  65. // TLX_MAX_MSGLEN is defined in <tlx\tracelib.h> for Windows
  66. #define TLX_MAX_MSGLEN    512
  67. #endif
  68.  
  69. /*---------------------------------------------------------------------------
  70.     Global variables
  71. ---------------------------------------------------------------------------*/
  72.  
  73. // Try TLTRACE environment variable for trace level
  74.  
  75. static char *    _gEnv           = getenv("TLTRACE");
  76. int        _gTraceLevel  = _gEnv ? atoi(_gEnv) : INT_MAX;
  77. ostream *    _gTraceStream = &cerr;
  78.  
  79. bool        TLTracer::sEnabled = true;
  80. TLTracer    gTraceGrpTLX("TLX");
  81.  
  82. /*-------------------------------------------------------------------------*/
  83.     TLTracer::TLTracer(const char *aName)
  84.  
  85. /*  Constructor. Creates a tracing group with the given name.
  86. ---------------------------------------------------------------------------*/
  87. {
  88.     TLX_ASSERT_PTR(aName);
  89.  
  90.     mGroupName = aName;
  91.     mMaxLevel  = INT_MAX;
  92.     mEnabled   = true;
  93.     mOStream   = &cerr;
  94.     mFileName  = "";
  95.     mLineNo    = 0;
  96. }
  97.  
  98. /*-------------------------------------------------------------------------*/
  99.     TLTracer::~TLTracer()
  100.  
  101. /*  Destructor. Does nothing currently.
  102. ---------------------------------------------------------------------------*/
  103. {
  104. }
  105.  
  106. /*-------------------------------------------------------------------------*/
  107.     void TLTracer::Disable()
  108.  
  109. /*  Disables tracing output for the current group.
  110. ---------------------------------------------------------------------------*/
  111. {
  112.     mEnabled = false;
  113. }
  114.  
  115. /*-------------------------------------------------------------------------*/
  116.     void TLTracer::DisableAll()
  117.  
  118. /*  Disables tracing output for all tracing groups at once.
  119. ---------------------------------------------------------------------------*/
  120. {
  121.     sEnabled = false;
  122. }
  123.  
  124. /*-------------------------------------------------------------------------*/
  125.     void TLTracer::Enable()
  126.  
  127. /*  Enables tracing output for the current group.
  128. ---------------------------------------------------------------------------*/
  129. {
  130.     mEnabled = true;
  131. }
  132.  
  133. /*-------------------------------------------------------------------------*/
  134.     void TLTracer::EnableAll()
  135.  
  136. /*  Restores the individual tracing on/off settings for all groups.
  137. ---------------------------------------------------------------------------*/
  138. {
  139.     sEnabled = true;
  140. }
  141.  
  142. /*-------------------------------------------------------------------------*/
  143.     void __cdecl TLTracer::ErrorFmt(const char *aFmt, ...)
  144.  
  145. /*  Prints a formatted error message, provided the current maximum tracing
  146.     level >= 1 and tracing is enabled both globally and locally.
  147. ---------------------------------------------------------------------------*/
  148. {
  149.     if (!CanTrace(1))
  150.     return;
  151.  
  152.     va_list args;
  153.     va_start(args, aFmt);
  154.     VPrintf("Err", aFmt, args);
  155.     va_end(args);
  156. }
  157.  
  158. /*-------------------------------------------------------------------------*/
  159.     void __cdecl TLTracer::FatalFmt(const char *aFmt, ...)
  160.  
  161. /*  Prints a formatted fatal message, provided the current maximum tracing
  162.     level >= 0 and tracing is enabled both globally and locally.
  163. ---------------------------------------------------------------------------*/
  164. {
  165.     if (!CanTrace(0))
  166.     return;
  167.  
  168.     va_list args;
  169.     va_start(args, aFmt);
  170.     VPrintf("Fatal", aFmt, args);
  171.     va_end(args);
  172. }
  173.  
  174. /*-------------------------------------------------------------------------*/
  175.     void __cdecl TLTracer::InfoFmt(const char *aFmt, ...)
  176.  
  177. /*  Prints a formatted info message, provided the current maximum tracing
  178.     is >= 3 level and tracing is enabled both globally and locally.
  179. ---------------------------------------------------------------------------*/
  180. {
  181.     if (!CanTrace(3))
  182.     return;
  183.  
  184.     va_list args;
  185.     va_start(args, aFmt);
  186.     VPrintf("Info", aFmt, args);
  187.     va_end(args);
  188. }
  189.  
  190. /*-------------------------------------------------------------------------*/
  191.     TLTracer &TLTracer::SetLocus(const char *aFile, int aLine)
  192.  
  193. /*  Helper function to store locus information prior to the output of a
  194.     trace message.
  195. ---------------------------------------------------------------------------*/
  196. {
  197.     mFileName = aFile;
  198.     mLineNo   = aLine;
  199.     return *this;
  200. }
  201.  
  202. /*-------------------------------------------------------------------------*/
  203.     void TLTracer::SetMaxLevel(int aLevel)
  204.  
  205. /*  Sets the maximum level of trace output for the current group.
  206. ---------------------------------------------------------------------------*/
  207. {
  208.     mMaxLevel = aLevel;
  209. }
  210.  
  211. /*-------------------------------------------------------------------------*/
  212.     void TLTracer::SetStream(ostream &os)
  213.  
  214. /*  Installs the given stream as the output stream for the tracing group.
  215. ---------------------------------------------------------------------------*/
  216. {
  217.     mOStream = &os;
  218. }
  219.  
  220. /*-------------------------------------------------------------------------*/
  221.     void __cdecl TLTracer::TraceFmt(int aLevel, const char *aFmt, ...)
  222.  
  223. /*  Prints a formatted trace message, provided its level is <= the current
  224.     maximum tracing level and tracing is enabled both globally and locally.
  225. ---------------------------------------------------------------------------*/
  226. {
  227.     if (!CanTrace(aLevel))
  228.     return;
  229.  
  230.     char buf[20];
  231.     itoa(aLevel, buf, 10);
  232.  
  233.     va_list args;
  234.     va_start(args, aFmt);
  235.     VPrintf(buf, aFmt, args);
  236.     va_end(args);
  237. }
  238.  
  239. /*-------------------------------------------------------------------------*/
  240.     void TLTracer::VPrintf(const char *aPre, const char *aFmt, va_list args)
  241.  
  242. /*  Prints a tracing message consisting of a the group's name, a prefix,
  243.     the actual vprintf-style formatted message, and the locus of the
  244.     message originator.
  245. ---------------------------------------------------------------------------*/
  246. {
  247.     TLX_ASSERT_PTR(aPre);
  248.     TLX_ASSERT_PTR(mOStream);
  249.     TLX_ASSERT_PTR(mGroupName);
  250.     TLX_ASSERT_PTR(mFileName);
  251.  
  252.     // Build the trace message in a local buffer
  253.  
  254.     char buffer[TLX_MAX_MSGLEN];
  255.  
  256.     sprintf(buffer, "%s %s %04d: [%s] ", aPre, mFileName, mLineNo, mGroupName);
  257.     vsprintf(&buffer[strlen(buffer)], aFmt, args);
  258.  
  259.     size_t msglen = strlen(buffer);
  260.     TLX_ASSERT(msglen < TLX_MAX_MSGLEN - 1);
  261.  
  262.     if (buffer[msglen - 1] != '\n')
  263.     strcat(buffer, "\n");
  264.  
  265.   #ifndef _GUI
  266.     *mOStream << buffer;
  267.   #elif OS_WINXXX
  268.     OutputDebugString(buffer);          // Windows debug output
  269.   #else
  270.     #error Unsupported platform
  271.   #endif
  272. }
  273.  
  274. /*-------------------------------------------------------------------------*/
  275.     void __cdecl TLTracer::WarningFmt(const char *aFmt, ...)
  276.  
  277. /*  Prints a formatted warning message, provided the current maximum tracing
  278.     level >= 2 and tracing is enabled both globally and locally.
  279. ---------------------------------------------------------------------------*/
  280. {
  281.     if (!CanTrace(2))
  282.     return;
  283.  
  284.     va_list args;
  285.     va_start(args, aFmt);
  286.     VPrintf("Warn", aFmt, args);
  287.     va_end(args);
  288. }
  289.  
  290.