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

  1. /****************************************************************************
  2.     $Id: log.cpp 501.0 1995/03/07 12:26:16 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 class TLLog for message logging.
  10.  
  11.     $Log: log.cpp $
  12.     Revision 501.0  1995/03/07 12:26:16  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.7  1995/01/31 16:30:16  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.6  1995/01/06  15:58:02  ron
  18.     Corrected Revision keyword
  19.  
  20.     Revision 1.5  1994/11/16  15:40:55  ron
  21.     Added module info; rearranged #include directives
  22.  
  23.     Revision 1.4  1994/10/13  11:52:10  ron
  24.     Corrected calculation of minutes in relative log time
  25.  
  26.     Revision 1.3  1994/10/10  16:51:51  ron
  27.     Added initialization function
  28.     Added entry formatting options
  29.  
  30.     Revision 1.2  1994/10/06  17:44:24  ron
  31.     Made most mmber functions const
  32.  
  33.     Revision 1.1  1994/10/05  18:40:20  ron
  34.     Initial revision
  35.  
  36. ****************************************************************************/
  37.  
  38. #include <tlx\501\_build.h>
  39.  
  40. TLX_MODULE_INFO("$Revision: 501.0 $");
  41.  
  42. #include <iostream.h>
  43. #include <stdarg.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46.  
  47. #include <tlx\501\log.h>
  48.  
  49. /*---------------------------------------------------------------------------
  50.     Global objects
  51. ---------------------------------------------------------------------------*/
  52.  
  53. TLLog *        TLLog::sIt = 0;
  54. static TLLog     _gTheLog;
  55.  
  56. /*---------------------------------------------------------------------------
  57.     Global functions
  58. ---------------------------------------------------------------------------*/
  59.  
  60. #ifdef _TLXDBG
  61. TLLog &tlLog()
  62. {
  63.     TLX_ASSERT_PTR(TLLog::sIt);
  64.     return *TLLog::sIt;
  65. }
  66. #endif
  67.  
  68. /*-------------------------------------------------------------------------*/
  69.     TLLog::TLLog(bool aFlush)
  70.  
  71. /*  Default constructor. Connects to cout.
  72. ---------------------------------------------------------------------------*/
  73. {
  74.     Init(aFlush);
  75.     mOStream = &cout;
  76. }
  77.  
  78. /*-------------------------------------------------------------------------*/
  79.     TLLog::TLLog(ostream &os, bool aFlush)
  80.  
  81. /*  Constructor that links to the given stream.
  82. ---------------------------------------------------------------------------*/
  83. {
  84.     Init(aFlush);
  85.     mOStream = &os;
  86. }
  87.  
  88. /*-------------------------------------------------------------------------*/
  89.     TLLog::TLLog(const TLLog &aLog)
  90.  
  91. /*  Copy constructor. Links to the same output stream.
  92. ---------------------------------------------------------------------------*/
  93. {
  94.     Init(aLog.mFlushAll);
  95.     mOStream = aLog.mOStream;
  96. }
  97.  
  98. /*-------------------------------------------------------------------------*/
  99.     TLLog::~TLLog()
  100.  
  101. /*  Destructor flushes the output stream and unlinks the instance from
  102.     the stack of log instances.
  103. ---------------------------------------------------------------------------*/
  104. {
  105.     TLX_ASSERT_PTR(mOStream);
  106.     mOStream->flush();
  107.  
  108.     if (sIt == this)
  109.     sIt = mPrev;
  110.     else {
  111.     TLX_ASSERT_PTR(sIt);
  112.     TLLog *log;
  113.     for (log = sIt; log; log = log->mPrev)
  114.         if (log->mPrev == this) {
  115.         log->mPrev = mPrev;
  116.         break;
  117.         }
  118.     TLX_ASSERT_PTR(log);
  119.     }
  120.     mPrev = 0;
  121. }
  122.  
  123. /*-------------------------------------------------------------------------*/
  124.     ostream &TLLog::BeginEntry()
  125.  
  126. /*  Starts a new log entry by outputting a date/time stamp and some space.
  127. ---------------------------------------------------------------------------*/
  128. {
  129.     mEntryCount++;
  130.  
  131.     switch (mEntryFormat) {
  132.     case etAbsTime: {
  133.             time_t t;
  134.             time(&t);
  135.             struct tm *tmptr = localtime(&t);
  136.             return Printf("%02d-%02d-%02d %02d:%02d:%02d  ",
  137.                tmptr->tm_year, tmptr->tm_mon+1, tmptr->tm_mday,
  138.                tmptr->tm_hour, tmptr->tm_min, tmptr->tm_sec);
  139.     }
  140.     case etRelTime: {
  141.             time_t t;
  142.             time(&t);
  143.         t -= mStartTime;
  144.             return Printf("%03d:%02d:%02d  ", t / 3600, (t / 60) % 60, t % 60);
  145.     }
  146.     case etCount:
  147.             return Printf("%05ld  ", mEntryCount);
  148.  
  149.     default:
  150.         TLX_ASSERT_UNREACHABLE;
  151.             return Printf("***Log error***  ");
  152.     }
  153. }
  154.  
  155. /*-------------------------------------------------------------------------*/
  156.     ostream &TLLog::EndEntry() const
  157.  
  158. /*  Completes an entry by appending a newline and optionally flushing the
  159.     output stream.
  160. ---------------------------------------------------------------------------*/
  161. {
  162.     if (mFlushAll)
  163.         return operator ostream &() << endl;
  164.     else
  165.         return operator ostream &() << "\n";
  166. }
  167.  
  168. /*-------------------------------------------------------------------------*/
  169.     void TLLog::Init(bool aFlush)
  170.  
  171. /*  Initialization routine, to be called only from constructors.
  172. ---------------------------------------------------------------------------*/
  173. {
  174.     // Link to previous entry on log stack (if any)
  175.     mPrev = sIt;
  176.     sIt   = this;
  177.  
  178.     time(&mStartTime);
  179.     mEntryFormat = etRelTime;
  180.     mEntryCount  = 0;
  181.     mFlushAll      = aFlush;
  182. }
  183.  
  184. #ifdef _TLXDBG
  185. /*-------------------------------------------------------------------------*/
  186.     TLLog::operator ostream &() const
  187.  
  188. /*  Conversion operator to ostream &.
  189. ---------------------------------------------------------------------------*/
  190. {
  191.     TLX_ASSERT_PTR(mOStream);
  192.     return *mOStream;
  193. }
  194. #endif
  195.  
  196. /*-------------------------------------------------------------------------*/
  197.     TLLog &TLLog::operator =(ostream &os)
  198.  
  199. /*  Overloading of assignment operator that accepts ostream &. The
  200.     previous ostream is flushed, then the log is linked to the new
  201.     stream.
  202. ---------------------------------------------------------------------------*/
  203. {
  204.     if (mOStream != &os) {
  205.     TLX_ASSERT_PTR(mOStream);
  206.     mOStream->flush();
  207.     mOStream = &os;
  208.     }
  209.     return *this;
  210. }
  211.  
  212. /*-------------------------------------------------------------------------*/
  213.     TLLog &TLLog::operator =(const TLLog &aLog)
  214.  
  215. /*  Overloading of assignment operator that will link to the same ostream
  216.     as the other log. The previous log is flushed first.
  217. ---------------------------------------------------------------------------*/
  218. {
  219.     if (this != &aLog) {
  220.     TLX_ASSERT_PTR(mOStream);
  221.     mOStream->flush();
  222.     mOStream = aLog.mOStream;
  223.     }
  224.     return *this;
  225. }
  226.  
  227. /*-------------------------------------------------------------------------*/
  228.     void __cdecl TLLog::PrintEntry(const char *aFmt, ...)
  229.  
  230. /*  Outputs a printf-style message on the log, preceded by a date/time
  231.     stamp, and followed by a newline.
  232. ---------------------------------------------------------------------------*/
  233. {
  234.     va_list args;
  235.     va_start(args, aFmt);
  236.  
  237.     BeginEntry();
  238.     VPrintf(aFmt, args);
  239.     EndEntry();
  240.  
  241.     va_end(args);
  242. }
  243.  
  244. /*-------------------------------------------------------------------------*/
  245.     ostream & __cdecl TLLog::Printf(const char *aFmt, ...) const
  246.  
  247. /*  Outputs a printf-style message on the log, preceded by a date/time
  248.     stamp, and followed by a newline.
  249. ---------------------------------------------------------------------------*/
  250. {
  251.     va_list args;
  252.     va_start(args, aFmt);
  253.     VPrintf(aFmt, args);
  254.     va_end(args);
  255.     return Stream();
  256. }
  257.  
  258. /*-------------------------------------------------------------------------*/
  259.     int TLLog::SetEntryFormat(int aFmt)
  260.  
  261. /*  Sets a new entry format, while returning the previous one.
  262. ---------------------------------------------------------------------------*/
  263. {
  264.     int oldfmt      = mEntryFormat;
  265.     mEntryFormat = aFmt;
  266.     return oldfmt;
  267. }
  268.  
  269. /*-------------------------------------------------------------------------*/
  270.     void TLLog::VPrintf(const char *aFmt, va_list args) const
  271.  
  272. /*  Outputs a printf-style message on the log.
  273. ---------------------------------------------------------------------------*/
  274. {
  275.     static char _gBuffer[kMaxLogMessage];
  276.     vsprintf(_gBuffer, aFmt, args);
  277.     TLX_ASSERT(strlen(_gBuffer) < kMaxLogMessage);
  278.     Stream() << _gBuffer;
  279. }
  280.  
  281. ostream & _TLXFUNC operator <<(ostream &os, const TLLog &aLog)
  282. {
  283.     TLX_ASSERT(&os == &aLog.Stream());
  284.     return aLog.EndEntry();
  285. }
  286.