OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Inform.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef INFORM_H
12 #define INFORM_H
13 
14 /*
15  * Inform - takes messages and displays them to the given ostream.
16  * A message is sent to an Inform object by treating it as an ostream,
17  * then ending the message by sending the 'inform' manipulator. In
18  * fact, Inform works much like an ostream, although it may actually
19  * just use stdio for I/O.
20  *
21  * Each message is assigned the current 'level of interest'; the lower
22  * the level, the more important it is. Each Inform object is also
23  * set for a current level; messages with a level <= the current level
24  * are displayed. Levels run from 1 ... 5. Thus, setting the level of
25  * Inform object to 0 will turn off printing of all messages.
26  *
27  * By default, a new Inform object will only print out the message on
28  * node 0. You may change the node on which this prints with the
29  * 'setPrintNode(int)' method; if the argument is 'INFORM_ALL_NODES',
30  * the message will be printed on ALL nodes, not just one. The final
31  * argument to the constructor may also be set to the node to print on.
32  */
33 
34 #include <iostream>
35 #include <iomanip>
36 #include <sstream>
37 
38 #define INFORM_ALL_NODES (-1)
39 
40 
41 class Inform {
42 
43 public:
44  // enumeration listing the ways in which a file may be opened for writing
46 
47 public:
48  // constructor: arguments = name, print node
49  Inform(const char * = 0, int = 0);
50 
51  // second constructor: this specifies the name of a file as well as
52  // a prefix and a mode for opening the file (i.e. OVERWRITE or APPEND).
53  // The final argument is the print node.
54  Inform(const char *prefix, const char *fname, const WriteMode, int = 0);
55 
56  // third constructor: this specifies the prefix and an ostream object
57  // to write to, as well as as the print node
58  Inform(const char *, std::ostream&, int = 0);
59 
60  // fourth constructor: this specifies the prefix and an Inform instance
61  // from which the ostream object is copied, as well as as the print node
62  Inform(const char *myname, const Inform& os, int pnode = 0);
63 
64  // destructor
65  ~Inform();
66 
67  // turn messages on/off
68  void on(const bool o) { On = o; }
69  bool isOn() const { return On; }
70 
71  // change output destination
72  void setDestination(std::ostream &dest);
73  std::ostream& getDestination() { return *MsgDest; }
74 
75  // get/set the current output level
76  Inform& setOutputLevel(const int);
77  int getOutputLevel(void) const { return OutputLevel; }
78 
79  // get/set the current message level
80  Inform& setMessageLevel(const int);
81  int getMessageLevel(void) const { return MsgLevel; }
82 
83  // get/set the printing node. If set to a value < 0, all nodes print.
84  int getPrintNode() const { return PrintNode; }
85  void setPrintNode(int n = (-1)) { PrintNode = n; }
86 
87  // return a reference to the internal ostream used to print messages
88  std::ostream& getStream() { return FormatBuf; }
89 
90  // Was the stream opened successfully on construction?
92 
93  // the signal has been given, print out the message. Return ref to object.
94  Inform& outputMessage(void);
95 
96  // functions used to change format state; used just as for iostreams
97 
98 #ifdef IPPL_IOFLAG_BUG
99  typedef long FmtFlags_t;
100 #else
101  typedef std::ios_base::fmtflags FmtFlags_t;
102 #endif
103 
105  { return FormatBuf.setf(setbits,field); }
106 
107  FmtFlags_t setf(FmtFlags_t f) { return FormatBuf.setf(f); }
108  void /*long*/ unsetf(FmtFlags_t f) { FormatBuf.unsetf(f); }
109  FmtFlags_t flags() const { return FormatBuf.flags(); }
110  FmtFlags_t flags(FmtFlags_t f) { return FormatBuf.flags(f); }
111  int width() const { return FormatBuf.width(); }
112  int width(int w) { return FormatBuf.width(w); }
113  char fill() const { return FormatBuf.fill(); }
114  char fill(char c) { return FormatBuf.fill(c); }
115  int precision() const { return FormatBuf.precision(); }
116  int precision(int p) { return FormatBuf.precision(p); }
117  void flush() { MsgDest->flush();}
118 private:
119  // name of this object; put at the start of each message.
120  char *Name;
121 
122  // storage for the message text
123  std::string MsgBuf;
124  // an ostringstream used to format the messages
125  std::ostringstream FormatBuf;
126 
127  // where to put the messages; can be changed, by default = cout
128  std::ostream *MsgDest;
129 
130  // do we need to close the destination stream?
131  bool NeedClose;
132 
133  // Was the stream opened successfully on construction?
135 
136  // do we output the message?
137  bool On;
138 
139  // limit printing only to this node (if < 0, all nodes print)
141 
142  // output level of this Inform object; messages with a level <= the output
143  // level are printed. Setting this to < 1 turns off messages.
145 
146  // current message level; this is set by the 'levelN' manipulators, or
147  // by the routine setMsgLevel(int). After a message is printed, the current
148  // message level is reset to the minimum.
149  int MsgLevel;
150 
151  // print out the message in the given buffer. Will modify the string,
152  // so beware. Arguments: string
153  void display_message(char *);
154 
155  // print out just a single line of the message.
156  void display_single_line(char *);
157 
158  // perform initialization for this object; called by the constructors.
159  // arguments = prefix string, print node
160  void setup(const char *, int);
161 };
162 
163 
164 // manipulator for signaling we want to send the message.
165 extern Inform& endl(Inform&);
166 
167 // manipulators for setting the current msg level
168 extern Inform& level1(Inform&);
169 extern Inform& level2(Inform&);
170 extern Inform& level3(Inform&);
171 extern Inform& level4(Inform&);
172 extern Inform& level5(Inform&);
173 
174 
175 // templated version of operator<< for Inform objects
176 template<class T>
177 inline
178 Inform& operator<<(Inform& o, const T& val) {
179  o.getStream() << val;
180  return o;
181 }
182 
183 
184 // specialized version of operator<< to handle Inform-specific manipulators
185 inline
187  return d(o);
188 }
189 
190 
191 // specialized version of operator<< to handle void * arguments
192 inline
193 Inform& operator<<(Inform& o, const void *val) {
194  Inform::FmtFlags_t oldformat = o.setf(std::ios::hex, std::ios::basefield);
195  o.getStream() << "0x" << (long)val;
196  o.setf(oldformat, std::ios::basefield);
197  return o;
198 }
199 
200 // specialized version of operator<< to handle long long type (KCC workaround)
201 inline
202 Inform& operator<<(Inform& o, const long long& val) {
203  o.getStream() << val;
204  return o;
205 }
206 
207 // specialized function for sending strings to Inform object
208 inline Inform& operator<<(Inform& out, const std::string& s) {
209  out << s.c_str();
210  return out;
211 }
212 
213 
214 #endif // INFORM_H
215 
216 /***************************************************************************
217  * $RCSfile: Inform.h,v $ $Author: adelmann $
218  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
219  * IPPL_VERSION_ID: $Id: Inform.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
220  ***************************************************************************/
std::ostream & operator<<(std::ostream &os, const Attribute &attr)
Definition: Attribute.cpp:167
std::string MsgBuf
Definition: Inform.h:123
Inform(const char *=0, int=0)
Definition: Inform.cpp:77
Definition: rbendmap.h:8
Inform & level5(Inform &inf)
Definition: Inform.cpp:49
int getOutputLevel(void) const
Definition: Inform.h:77
std::ostream & getStream()
Definition: Inform.h:88
void on(const bool o)
Definition: Inform.h:68
int getMessageLevel(void) const
Definition: Inform.h:81
char * Name
Definition: Inform.h:120
std::ostringstream FormatBuf
Definition: Inform.h:125
int precision(int p)
Definition: Inform.h:116
Inform & setMessageLevel(const int)
Definition: Inform.cpp:238
FmtFlags_t setf(FmtFlags_t setbits, FmtFlags_t field)
Definition: Inform.h:104
Inform & level4(Inform &inf)
Definition: Inform.cpp:48
FmtFlags_t flags(FmtFlags_t f)
Definition: Inform.h:110
bool openedSuccessfully()
Definition: Inform.h:91
Inform & outputMessage(void)
Definition: Inform.cpp:248
void setDestination(std::ostream &dest)
Definition: Inform.cpp:216
int MsgLevel
Definition: Inform.h:149
Inform & level2(Inform &inf)
Definition: Inform.cpp:46
void display_single_line(char *)
Definition: Inform.cpp:159
FmtFlags_t flags() const
Definition: Inform.h:109
int width(int w)
Definition: Inform.h:112
FmtFlags_t setf(FmtFlags_t f)
Definition: Inform.h:107
int width() const
Definition: Inform.h:111
void setPrintNode(int n=(-1))
Definition: Inform.h:85
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
std::ostream & getDestination()
Definition: Inform.h:73
bool NeedClose
Definition: Inform.h:131
void display_message(char *)
Definition: Inform.cpp:186
char fill() const
Definition: Inform.h:113
Inform & setOutputLevel(const int)
Definition: Inform.cpp:228
int getPrintNode() const
Definition: Inform.h:84
int precision() const
Definition: Inform.h:115
void setup(const char *, int)
Definition: Inform.cpp:55
bool isOn() const
Definition: Inform.h:69
char fill(char c)
Definition: Inform.h:114
~Inform()
Definition: Inform.cpp:150
std::ios_base::fmtflags FmtFlags_t
Definition: Inform.h:101
int PrintNode
Definition: Inform.h:140
bool OpenedSuccessfully
Definition: Inform.h:134
Inform & level3(Inform &inf)
Definition: Inform.cpp:47
std::ostream * MsgDest
Definition: Inform.h:128
int OutputLevel
Definition: Inform.h:144
Definition: Inform.h:41
WriteMode
Definition: Inform.h:45
void unsetf(FmtFlags_t f)
Definition: Inform.h:108
void flush()
Definition: Inform.h:117
Inform & level1(Inform &inf)
Definition: Inform.cpp:45
bool On
Definition: Inform.h:137
Inform & endl(Inform &inf)
Definition: Inform.cpp:42