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

  1. /****************************************************************************
  2.     $Id: log.h 501.0 1995/03/07 12:26:44 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.     Declarations for message logging support.
  10.  
  11.     $Log: log.h $
  12.     Revision 501.0  1995/03/07 12:26:44  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.4  1995/01/31 16:29:22  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.3  1994/10/10  17:02:20  ron
  18.     Added entry formatting options
  19.  
  20.     Revision 1.2  1994/10/06  17:48:23  ron
  21.     Made most member functions public
  22.  
  23.     Revision 1.1  1994/10/05  18:23:00  ron
  24.     Initial revision
  25.  
  26. ****************************************************************************/
  27.  
  28. #ifndef _TLX_LOG_H
  29. #define _TLX_LOG_H
  30.  
  31. #include <stdarg.h>
  32. #include <time.h>
  33.  
  34. #ifndef _TLX_TLX_H
  35. #include <tlx\501\tlx.h>
  36. #endif
  37.  
  38. class _RTLCLASS ostream;
  39.  
  40. /*---------------------------------------------------------------------------
  41.     TLLog -
  42.  
  43.     Message log class. It provides the means for message logging with
  44.     date/time stamps. It can also be used wherever an ostream & argument
  45.     is required.
  46.  
  47.     There is normally only a single instance of this class in a given
  48.     program.
  49. ---------------------------------------------------------------------------*/
  50.  
  51. const size_t kMaxLogMessage = 255;    // Maximum size of single log message
  52.  
  53. class _TLXCLASS TLLog {
  54.     ostream *        mOStream;    // Current output stream
  55.     TLLog *        mPrev;        // Previously active message log
  56.     bool        mFlushAll;    // Flush output after each entry
  57.     time_t        mStartTime;    // Time of log creation
  58.     uint32        mEntryCount;    // Number of entries
  59.     int            mEntryFormat;    // Entry prefix type (see below)
  60.  
  61. public:
  62.     // Log entries may be prefixed by a full date/time stamp, a relative
  63.     // time, or an entry count.
  64.  
  65.     enum {
  66.     etAbsTime,            // Absolute date + time
  67.     etRelTime,            // Relative time
  68.     etCount                // Count of the entry
  69.     };
  70.  
  71.     // The (presumably) only log instance is accessible to all
  72.  
  73.     static TLLog *    sIt;
  74.  
  75. public:
  76.     // Default constructor links to cout; other constructor allows
  77.     // specification of initial ostream. Destructor flushes log stream
  78.     // and disconnects from log stack.
  79.  
  80.     TLLog(bool = true);
  81.     TLLog(ostream &, bool = true);
  82.     TLLog(const TLLog &);
  83.     ~TLLog();
  84.  
  85.     // Conversion from and to ostream &
  86.  
  87.             operator ostream &() const;
  88.     ostream &        Stream() const;
  89.     TLLog &        operator =(ostream &);
  90.     TLLog &        operator =(const TLLog &);
  91.  
  92.     // Ways to set the entry format
  93.  
  94.     int            GetEntryFormat() const;
  95.     int            SetEntryFormat(int);
  96.     void        SetFlush(bool = true);
  97.  
  98.     // Message output: can be ostream-style or printf-style. When using
  99.     // ostream-style, log entries must be explicitly started and terminated:
  100.     //
  101.     //       BeginEntry();
  102.     //      ...use ostream functions and/or Printf()...
  103.     //      EndEntry();
  104.     //
  105.     // or:
  106.     //
  107.     //    PrintEntry();
  108.  
  109.     ostream &        BeginEntry();
  110.     ostream &        EndEntry() const;
  111.     ostream & __cdecl    Printf(const char *, ...) const;
  112.     void __cdecl    PrintEntry(const char *, ...);
  113.  
  114.     // Dummy output operator which will terminate the log
  115.  
  116.     friend ostream &    _TLXFUNC operator <<(ostream &, const TLLog &);
  117.  
  118. private:
  119.     void        Init(bool);
  120.     void        VPrintf(const char *, va_list) const;
  121. };
  122.  
  123. /*---------------------------------------------------------------------------
  124.     Helper functions and macros.
  125.  
  126.     Log()        - returns reference to the current log instance
  127.  
  128.     The following macros all return an (ostream &):
  129.  
  130.     TLX_LOG        - accesses current log stream
  131.     TLX_LOG_BEGIN    - starts a log entry
  132.     TLX_LOG_END        - ends a log entry
  133.  
  134.     TLX_LOG_ENTRY()    - use as TLX_LOG_ENTRY("format", arg1, arg2, ...)
  135.     TLX_LOG_PRINT()    - use as TLX_LOG_PRINT("format", arg1, arg2, ...)
  136. ---------------------------------------------------------------------------*/
  137.  
  138. TLLog &            tlLog();
  139.  
  140. #define TLX_LOG        tlLog().Stream()
  141. #define TLX_LOG_BEGIN    tlLog().BeginEntry()
  142. #define TLX_LOG_END    tlLog()
  143. #define TLX_LOG_PRINT    tlLog().Printf
  144. #define TLX_LOG_ENTRY    tlLog().PrintEntry
  145.  
  146. /*---------------------------------------------------------------------------
  147.     Inline definitions
  148. ---------------------------------------------------------------------------*/
  149.  
  150. //-----    TLLog
  151.  
  152. #ifndef _TLXDBG
  153. inline TLLog::operator ostream &() const
  154.     { return *mOStream; }
  155. #endif
  156.  
  157. inline ostream &TLLog::Stream() const
  158.     { return operator ostream &(); }
  159.  
  160. inline void TLLog::SetFlush(bool aFlag)
  161.     { mFlushAll = aFlag; }
  162.  
  163. inline int TLLog::GetEntryFormat() const
  164.     { return mEntryFormat; }
  165.  
  166. //-----    Helper functions
  167.  
  168. #ifndef _TLXDBG
  169. inline TLLog &tlLog()
  170.     { return *TLLog::sIt; }
  171. #endif
  172.  
  173. #endif    // _TLX_LOG_H
  174.