src/Utility/Inform.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  *
00007  * Visit http://people.web.psi.ch/adelmann/ for more details
00008  *
00009  ***************************************************************************/
00010 
00011 #ifndef INFORM_H
00012 #define INFORM_H
00013 
00014 /*
00015  * Inform - takes messages and displays them to the given ostream.
00016  *      A message is sent to an Inform object by treating it as an ostream,
00017  *      then ending the message by sending the 'inform' manipulator.  In
00018  *      fact, Inform works much like an ostream, although it may actually
00019  *      just use stdio for I/O.
00020  *      
00021  *      Each message is assigned the current 'level of interest'; the lower
00022  *      the level, the more important it is.  Each Inform object is also
00023  *      set for a current level; messages with a level <= the current level
00024  *      are displayed.  Levels run from 1 ... 5.  Thus, setting the level of
00025  *      Inform object to 0 will turn off printing of all messages.
00026  *
00027  *      By default, a new Inform object will only print out the message on
00028  *      node 0.  You may change the node on which this prints with the
00029  *      'setPrintNode(int)' method; if the argument is 'INFORM_ALL_NODES',
00030  *      the message will be printed on ALL nodes, not just one.  The final
00031  *      argument to the constructor may also be set to the node to print on.
00032  */
00033 
00034 // include files
00035 #ifdef IPPL_USE_STANDARD_HEADERS
00036 #include <iostream>
00037 #include <iomanip>
00038 using namespace std;
00039 #else
00040 #include <iostream.h>
00041 #include <iomanip.h>
00042 #endif
00043 
00044 #ifdef IPPL_NO_STRINGSTREAM
00045 #include <sstream>
00046 //using std::ostringstream;
00047 using namespace std;
00048 #else
00049 #include <strstream.h>
00050 #endif
00051 
00052 #include "Utility/Pstring.h"
00053 
00054 
00055 // range of Inform message levels
00056 #define MIN_INFORM_LEVEL        1
00057 #define MAX_INFORM_LEVEL        5
00058 
00059 // how large is the buffer for this object?
00060 #define MAX_INFORM_MSG_SIZE     32000
00061 #define INFORM_ALL_NODES        (-1)
00062 
00063 
00064 class Inform {
00065 
00066 public:
00067   // enumeration listing the ways in which a file may be opened for writing
00068   enum WriteMode { OVERWRITE, APPEND };
00069 
00070 public:
00071   // constructor: arguments = name, print node
00072   Inform(const char * = 0, int = 0);
00073 
00074   // second constructor: this specifies the name of a file as well as
00075   // a prefix and a mode for opening the file (i.e. OVERWRITE or APPEND).
00076   // The final argument is the print node.
00077   Inform(const char *prefix, const char *fname, const WriteMode, int = 0);
00078 
00079   // third constructor: this specifies the prefix and an ostream object
00080   // to write to, as well as as the print node
00081   Inform(const char *, ostream&, int = 0);
00082 
00083   // destructor
00084   ~Inform();
00085   
00086   // turn messages on/off
00087   void on(const bool o) { On = o; }
00088   bool isOn() const { return On; }
00089 
00090   // get/set the current output level
00091   Inform& setOutputLevel(const int);
00092   int getOutputLevel(void) const { return OutputLevel; }
00093 
00094   // get/set the current message level
00095   Inform& setMessageLevel(const int);
00096   int getMessageLevel(void) const { return MsgLevel; }
00097 
00098   // get/set the printing node.  If set to a value < 0, all nodes print.
00099   int getPrintNode() const { return PrintNode; }
00100   void setPrintNode(int n = (-1)) { PrintNode = n; }
00101 
00102   // return a reference to the internal ostream used to print messages
00103   ostream& getStream() { return FormatBuf; }
00104 
00105   // Was the stream opened successfully on construction?
00106   bool openedSuccessfully() { return OpenedSuccessfully; }
00107 
00108   // the signal has been given, print out the message.  Return ref to object.
00109   Inform& outputMessage(void);
00110 
00111   // functions used to change format state; used just as for iostreams
00112 
00113 #if (IPPL_IOFLAG_BUG)
00114   typedef long FmtFlags_t;
00115 #else
00116   typedef std::ios_base::fmtflags FmtFlags_t;
00117 #endif
00118 
00119   FmtFlags_t setf(FmtFlags_t setbits, FmtFlags_t field) 
00120   { return FormatBuf.setf(setbits,field); }
00121   
00122   FmtFlags_t setf(FmtFlags_t f) { return FormatBuf.setf(f); }
00123   void /*long*/ unsetf(FmtFlags_t f) { FormatBuf.unsetf(f); }
00124   long flags() const { return FormatBuf.flags(); }
00125   long flags(FmtFlags_t f) { return FormatBuf.flags(f); }
00126   int width() const { return FormatBuf.width(); }
00127   int width(int w) { return FormatBuf.width(w); }
00128   char fill() const { return FormatBuf.fill(); }
00129   char fill(char c) { return FormatBuf.fill(c); }
00130   int precision() const { return FormatBuf.precision(); }
00131   int precision(int p) { return FormatBuf.precision(p); }
00132 
00133 private:
00134   // name of this object; put at the start of each message.
00135   char *Name;
00136 
00137 #ifdef IPPL_NO_STRINGSTREAM
00138   // storage for the message text
00139   string MsgBuf;
00140   // an ostringstream used to format the messages
00141   ostringstream FormatBuf;
00142 #else
00143   // storage for the message text
00144   char MsgBuf[MAX_INFORM_MSG_SIZE];
00145   // an ostrstream used to format the messages
00146   ostrstream FormatBuf;
00147 #endif
00148 
00149   // where to put the messages; can be changed, by default = cout
00150   ostream *MsgDest;
00151 
00152   // do we need to close the destination stream?
00153   bool NeedClose;
00154 
00155   // Was the stream opened successfully on construction?
00156   bool OpenedSuccessfully;
00157 
00158   // do we output the message?
00159   bool On;
00160 
00161   // limit printing only to this node (if < 0, all nodes print)
00162   int PrintNode;
00163 
00164   // output level of this Inform object; messages with a level <= the output
00165   // level are printed.  Setting this to < 1 turns off messages.
00166   int OutputLevel;
00167 
00168   // current message level; this is set by the 'levelN' manipulators, or
00169   // by the routine setMsgLevel(int).  After a message is printed, the current
00170   // message level is reset to the minimum.
00171   int MsgLevel;
00172 
00173   // print out the message in the given buffer.  Will modify the string,
00174   // so beware.  Arguments: string
00175   void display_message(char *);
00176 
00177   // print out just a single line of the message.
00178   void display_single_line(char *);
00179 
00180   // perform initialization for this object; called by the constructors.
00181   // arguments = prefix string, print node
00182   void setup(const char *, int);
00183 };
00184 
00185 
00186 // manipulator for signaling we want to send the message.
00187 extern Inform& endl(Inform&);
00188 
00189 // manipulators for setting the current msg level
00190 extern Inform& level1(Inform&);
00191 extern Inform& level2(Inform&);
00192 extern Inform& level3(Inform&);
00193 extern Inform& level4(Inform&);
00194 extern Inform& level5(Inform&);
00195 
00196 
00197 // templated version of operator<< for Inform objects
00198 template<class T>
00199 inline
00200 Inform& operator<<(Inform& o, const T& val) {
00201   o.getStream() << val;
00202   return o;
00203 }
00204 
00205 
00206 // specialized version of operator<< to handle Inform-specific manipulators
00207 inline
00208 Inform& operator<<(Inform& o, Inform& (*d)(Inform&)) {
00209   return d(o);
00210 }
00211 
00212 
00213 // specialized version of operator<< to handle void * arguments
00214 inline
00215 Inform& operator<<(Inform& o, const void *val) {
00216   Inform::FmtFlags_t oldformat = o.setf(ios::hex, ios::basefield);
00217   o.getStream() << "0x" << (long)val;
00218   o.setf(oldformat, ios::basefield);
00219   return o;
00220 }
00221 
00222 #if defined(IPPL_LONGLONG)
00223 // specialized version of operator<< to handle long long type (KCC workaround)
00224 inline
00225 Inform& operator<<(Inform& o, const long long& val) {
00226 #if ( defined(IPPL_KAI) || defined(IPPL_PGI) )
00227   // cast to long double before sending to ostream
00228   o.getStream() << static_cast<long double>(val);
00229 #else
00230   o.getStream() << val;
00231 #endif
00232   return o;
00233 }
00234 #endif // IPPL_LONGLONG
00235 
00236 // specialized function for sending strings to Inform object
00237 inline Inform& operator<<(Inform& out, const string& s) {
00238   out << s.c_str();
00239   return out;
00240 }
00241 
00242 
00243 #endif // INFORM_H
00244 
00245 /***************************************************************************
00246  * $RCSfile: Inform.h,v $   $Author: adelmann $
00247  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00248  * IPPL_VERSION_ID: $Id: Inform.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $ 
00249  ***************************************************************************/

Generated on Mon Jan 16 13:23:58 2006 for IPPL by  doxygen 1.4.6