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