Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MsgStream.h

Go to the documentation of this file.
00001 
00002 // $Id: MsgStream.h,v 1.26 2008/08/19 19:02:59 gmieg Exp $
00003 //
00004 // MsgStream
00005 //
00006 // Allows messages to be filtered, formatted and send to one or many
00007 // output streams 
00008 //
00009 // messier@huhepl.harvard.edu
00011 #ifndef MSGSTREAM_H
00012 #define MSGSTREAM_H
00013 #ifndef IOSFWD
00014 #include <iosfwd>
00015 #define IOSFWD
00016 #endif
00017 #ifndef IOSTREAM
00018 #include <iostream> // Needed for __omanip and __manip and others
00019 #define IOSTREAM
00020 #endif
00021 #ifndef IOMANIP
00022 #include <iomanip> // Needed for smanip
00023 #define IOMANIP
00024 #endif
00025 #ifndef VECTOR
00026 #include <vector>
00027 #define VECTOR
00028 #endif
00029 #ifndef MAP
00030 #include <map>
00031 #endif
00032 #ifndef STRING
00033 #define STRING
00034 #include <string> // Needed for string << operator
00035 #endif
00036 #ifndef STDEXCEPT
00037 #define STDEXCEPT
00038 #include <stdexcept>
00039 #endif
00040 #ifndef MSG_H
00041 #include "MessageService/Msg.h"
00042 #endif
00043 #ifndef MSGOSTREAM_H
00044 #include "MessageService/MsgOStream.h"
00045 #endif
00046 #ifndef MSGSTATISTIC_H
00047 #include "MessageService/MsgStatistic.h"
00048 #endif
00049 
00050 using namespace std;
00051 
00052 class MsgBoundFormat;
00053 class MsgOStream;
00054 
00055 //......................................................................
00056 
00057 class MsgStream
00058 {
00059   friend ostream& operator<<(ostream& os, const MsgStream& s);
00060 public:
00061   MsgStream();
00062   MsgStream(const char* name);
00063 
00064   bool            IsActive(Msg::LogLevel_t lvl) const { return lvl>=fLogLevel; }
00065   Msg::LogLevel_t GetLogLevel() const { return fLogLevel; };
00066   void            PrintStatistics(ostream& os);
00067 
00068   void AttachOStream(Msg::LogLevel_t lvl, const char *name);
00069   void DetachOStream(Msg::LogLevel_t lvl, const char *name);
00070   void Close();
00071   void Flush();
00072   void AddFormat(Msg::LogLevel_t lvl, int fmt);
00073   void RemoveFormat(Msg::LogLevel_t lvl, int fmt) { fFormat[lvl]&=(~fmt); }
00074   void SetLogLevel(Msg::LogLevel_t lvl);
00075   void SetFormat(Msg::LogLevel_t lvl, int fmt)    { fFormat[lvl] = fmt;   }
00076 
00077   MsgStream& operator()(Msg::LogLevel_t priority, const char* file, 
00078                         const char* cvsid, int line);
00079   void LogPrint(Msg::LogLevel_t priority, const char* file);
00080 
00081   //====================================================================
00082   // The set of << operators - basically these just loop over the
00083   // output streams attached to the message stream an use the <<
00084   // operator defined for that stream
00085   //
00086   // << operator for most normal classes
00087   template<class TP> MsgStream& operator<<(const TP& t) {
00088     if (fCurrentLogLevel >= fLogLevel) {
00089       vector<MsgOStream*>::iterator vend(fMsgOStream[fCurrentLogLevel].end());
00090       for (vector<MsgOStream*>::iterator 
00091              iter(fMsgOStream[fCurrentLogLevel].begin());
00092            iter != vend;
00093            ++iter) {
00094         (*iter)->Os() << t;
00095       }
00096     }
00097     return (*this);
00098   }
00099 
00100   // << operator for omanips...
00101   typedef std::ostream& (*__MSGomanip)(std::ostream&);
00102   MsgStream& operator<<(__MSGomanip func) {
00103     if (fCurrentLogLevel >= fLogLevel) {
00104       vector<MsgOStream*>::iterator vend(fMsgOStream[fCurrentLogLevel].end());
00105       for (vector<MsgOStream*>::iterator 
00106              iter(fMsgOStream[fCurrentLogLevel].begin());
00107            iter != vend;
00108            ++iter) {
00109         (*iter)->Os() << func;
00110       }
00111     }
00112     return (*this);
00113   }
00114 
00115   //====================================================================
00116   // Handle differences between gcc V3 and eariler versions here
00117   //====================================================================
00118 #if ( __GNUC__ >= 3)
00119   // << operator for ios_base...
00120   typedef ios_base& (*__MSGios_base)(ios_base&);
00121   MsgStream& operator<<(__MSGios_base iosb) {
00122     if (fCurrentLogLevel >= fLogLevel) {
00123       vector<MsgOStream*>::iterator vend(fMsgOStream[fCurrentLogLevel].end());
00124       for (vector<MsgOStream*>::iterator
00125              iter = fMsgOStream[fCurrentLogLevel].begin();
00126            iter != vend;
00127            ++iter) {
00128         (*iter)->Os() << iosb;
00129       }
00130     }
00131     return (*this);
00132   }
00133 #else // (__GNUC__ < 3)
00134   // << operator for manips...
00135   typedef ios& (*__MSGmanip)(ios&);
00136   MsgStream& operator<<(__MSGmanip func) {
00137     if (fCurrentLogLevel >= fLogLevel) {
00138       vector<MsgOStream*>::iterator vend(fMsgOStream[fCurrentLogLevel].end());
00139       for (vector<MsgOStream*>::iterator
00140              iter(fMsgOStream[fCurrentLogLevel].begin());
00141            iter != vend;
00142            ++iter) {
00143         (*iter)->Os() << func;
00144       }
00145     }
00146     return (*this);
00147   }
00148 #endif // __GNUC__
00149 
00150 private:
00151   void Init();
00152   void StatPrint(ostream& os, const char* label, int n) const;
00153   void SetCurrentDateString();
00154   void SetCVSVersion(const char* CVSId);
00155 
00156   enum { kMaxNameSize = 8 };
00157   char                fName[kMaxNameSize+1];        // Name of this MsgStream
00158   Msg::LogLevel_t     fLogLevel;                    // Threshold for print
00159   int                 fFormat[Msg::kNLogLevel];     // Formatting flags
00160   vector<MsgOStream*> fMsgOStream[Msg::kNLogLevel]; // Output streams
00161   map<string,MsgStatistic> fFileStat;          // Print stats by file name
00162   
00163   Msg::LogLevel_t fCurrentLogLevel; // The log level of messages being printed
00164   char fCurrentDate[32];            // Time stamp for current message
00165   char fCVSVersion[128];            // CVS version for current message
00166 };
00167 
00168 #endif // MSGSTREAM_H
00169 

Generated on Mon Nov 23 05:27:26 2009 for loon by  doxygen 1.3.9.1