IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
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 <iomanip>
35#include <iostream>
36#include <sstream>
37
38#define INFORM_ALL_NODES (-1)
39
40class Inform {
41public:
42 // 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
101 FmtFlags_t setf(FmtFlags_t setbits, FmtFlags_t field) { return FormatBuf.setf(setbits, field); }
102
103 FmtFlags_t setf(FmtFlags_t f) { return FormatBuf.setf(f); }
104 void /*long*/ unsetf(FmtFlags_t f) { FormatBuf.unsetf(f); }
105 FmtFlags_t flags() const { return FormatBuf.flags(); }
106 FmtFlags_t flags(FmtFlags_t f) { return FormatBuf.flags(f); }
107 int width() const { return FormatBuf.width(); }
108 int width(int w) { return FormatBuf.width(w); }
109 char fill() const { return FormatBuf.fill(); }
110 char fill(char c) { return FormatBuf.fill(c); }
111 int precision() const { return FormatBuf.precision(); }
112 int precision(int p) { return FormatBuf.precision(p); }
113 void flush() { MsgDest->flush(); }
114
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// manipulator for signaling we want to send the message.
161extern Inform& endl(Inform&);
162
163// manipulators for setting the current msg level
164extern Inform& level1(Inform&);
165extern Inform& level2(Inform&);
166extern Inform& level3(Inform&);
167extern Inform& level4(Inform&);
168extern Inform& level5(Inform&);
169
170// templated version of operator<< for Inform objects
171template <class T>
172inline Inform& operator<<(Inform& o, const T& val) {
173 o.getStream() << val;
174 return o;
175}
176
177// specialized version of operator<< to handle Inform-specific manipulators
178inline Inform& operator<<(Inform& o, Inform& (*d)(Inform&)) {
179 return d(o);
180}
181
182// specialized version of operator<< to handle void * arguments
183inline Inform& operator<<(Inform& o, const void* val) {
184 Inform::FmtFlags_t oldformat = o.setf(std::ios::hex, std::ios::basefield);
185 o.getStream() << "0x" << (long)val;
186 o.setf(oldformat, std::ios::basefield);
187 return o;
188}
189
190// specialized version of operator<< to handle long long type (KCC workaround)
191inline Inform& operator<<(Inform& o, const long long& val) {
192 o.getStream() << val;
193 return o;
194}
195
196// specialized function for sending strings to Inform object
197inline Inform& operator<<(Inform& out, const std::string& s) {
198 out << s.c_str();
199 return out;
200}
201
202#endif // INFORM_H
203
204/***************************************************************************
205 * $RCSfile: Inform.h,v $ $Author: adelmann $
206 * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
207 * IPPL_VERSION_ID: $Id: Inform.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
208 ***************************************************************************/
Inform & level3(Inform &)
Definition Inform.cpp:54
Inform & level4(Inform &)
Definition Inform.cpp:57
Inform & level5(Inform &)
Definition Inform.cpp:60
Inform & endl(Inform &)
Definition Inform.cpp:42
Inform & operator<<(Inform &o, const T &val)
Definition Inform.h:172
Inform & level2(Inform &)
Definition Inform.cpp:51
Inform & level1(Inform &)
Definition Inform.cpp:48
int OutputLevel
Definition Inform.h:141
std::string MsgBuf
Definition Inform.h:120
char fill(char c)
Definition Inform.h:110
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:244
int width() const
Definition Inform.h:107
void display_single_line(char *)
Definition Inform.cpp:163
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:87
WriteMode
Definition Inform.h:43
@ APPEND
Definition Inform.h:45
@ OVERWRITE
Definition Inform.h:44
bool On
Definition Inform.h:134
bool openedSuccessfully()
Definition Inform.h:92
void setDestination(std::ostream &dest)
Definition Inform.cpp:217
Inform & setOutputLevel(const int)
Definition Inform.cpp:228
bool NeedClose
Definition Inform.h:128
FmtFlags_t flags(FmtFlags_t f)
Definition Inform.h:106
FmtFlags_t flags() const
Definition Inform.h:105
void unsetf(FmtFlags_t f)
Definition Inform.h:104
FmtFlags_t setf(FmtFlags_t setbits, FmtFlags_t field)
Definition Inform.h:101
void display_message(char *)
Definition Inform.cpp:188
std::ostringstream FormatBuf
Definition Inform.h:122
~Inform()
Definition Inform.cpp:156
Inform & setMessageLevel(const int)
Definition Inform.cpp:236
void flush()
Definition Inform.h:113
bool OpenedSuccessfully
Definition Inform.h:131
int precision() const
Definition Inform.h:111
int MsgLevel
Definition Inform.h:146
int precision(int p)
Definition Inform.h:112
char * Name
Definition Inform.h:117
FmtFlags_t setf(FmtFlags_t f)
Definition Inform.h:103
int width(int w)
Definition Inform.h:108
char fill() const
Definition Inform.h:109
void setup(const char *, int)
Definition Inform.cpp:67
void setPrintNode(int n=(-1))
Definition Inform.h:86
int getOutputLevel(void) const
Definition Inform.h:78
std::ostream & getStream()
Definition Inform.h:89