IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
Inform.cpp
Go to the documentation of this file.
1// -*- C++ -*-
2/***************************************************************************
3 *
4 * The IPPL Framework
5 *
6 * This program was prepared by PSI.
7 * All rights in the program are reserved by PSI.
8 * Neither PSI nor the author(s)
9 * makes any warranty, express or implied, or assumes any liability or
10 * responsibility for the use of this software
11 *
12 * Visit www.amas.web.psi for more details
13 *
14 ***************************************************************************/
15
16// -*- C++ -*-
17/***************************************************************************
18 *
19 * The IPPL Framework
20 *
21 *
22 * Visit http://people.web.psi.ch/adelmann/ for more details
23 *
24 ***************************************************************************/
25
26// include files
27#include "Ippl.h"
28
29#include "Utility/Inform.h"
30
31#include <cstring>
32#include <fstream>
33
34// range of Inform message levels
35constexpr int MIN_INFORM_LEVEL = 1;
36constexpr int MAX_INFORM_LEVEL = 5;
37
39// manipulator functions
40
41// signal we wish to send the message
43 inf << '\n';
44 return inf.outputMessage();
45}
46
47// set the current msg level
49 return inf.setMessageLevel(1);
50}
52 return inf.setMessageLevel(2);
53}
55 return inf.setMessageLevel(3);
56}
58 return inf.setMessageLevel(4);
59}
61 return inf.setMessageLevel(5);
62}
63
65// perform initialization for this object; called by the constructors.
66// arguments = prefix string, print node
67void Inform::setup(const char* myname, int pnode) {
68 On = true;
69
70 if (ippl::Info != NULL) {
71 OutputLevel = ippl::Info->getOutputLevel();
72 } else {
74 }
76 PrintNode = pnode;
77
78 if (myname != 0) {
79 Name = strcpy(new char[strlen(myname) + 1], myname);
80 } else {
81 Name = 0;
82 }
83}
84
86// class constructor
87Inform::Inform(const char* myname, int pnode)
88 : FormatBuf(std::ios::out)
89 , OpenedSuccessfully(true) {
90 // in this case, the default destination stream is cout
91 NeedClose = false;
92 MsgDest = &std::cout;
93
94 // perform all other needed initialization
95 setup(myname, pnode);
96}
97
99// class constructor specifying a file to open
100Inform::Inform(const char* myname, const char* fname, const WriteMode opnmode, int pnode)
101 : FormatBuf(std::ios::out)
102 , OpenedSuccessfully(true) {
103 // only open a file if we're on the proper node
104 MsgDest = 0;
105 if (pnode >= 0 && pnode == ippl::Comm->rank()) {
106 if (opnmode == OVERWRITE)
107 MsgDest = new std::ofstream(fname, std::ios::out);
108 else
109 MsgDest = new std::ofstream(fname, std::ios::app);
110 }
111
112 // make sure it was opened properly
113 if (MsgDest == 0 || !(*MsgDest)) {
114 if (pnode >= 0 && pnode == ippl::Comm->rank()) {
115 std::cerr << "Inform: Cannot open file '" << fname << "'." << std::endl;
116 }
117 NeedClose = false;
118 MsgDest = &std::cout;
119 OpenedSuccessfully = false;
120 } else {
121 NeedClose = true;
122 }
123
124 // perform all other needed initialization
125 setup(myname, pnode);
126}
127
129// class constructor specifying an output stream to use
130Inform::Inform(const char* myname, std::ostream& os, int pnode)
131 : FormatBuf(std::ios::out)
132 , OpenedSuccessfully(true) {
133 // just store a ref to the provided stream
134 NeedClose = false;
135 MsgDest = &os;
136
137 // perform all other needed initialization
138 setup(myname, pnode);
139}
140
142// class constructor specifying an other Inform instance
143Inform::Inform(const char* myname, const Inform& os, int pnode)
144 : FormatBuf(std::ios::out)
145 , MsgDest(os.MsgDest)
146 , OpenedSuccessfully(true) {
147 // just store a ref to the provided stream
148 NeedClose = false;
149
150 // perform all other needed initialization
151 setup(myname, pnode);
152}
153
155// class destructor ... frees up space
157 delete[] Name;
158 if (NeedClose)
159 delete MsgDest;
160}
161
162// print out just a single line, from the given buffer
164 // output the prefix name if necessary ... if no name was given, do
165 // not print any prefix at all
166 if (Name != 0) {
167 *MsgDest << Name;
168
169 // output the node number if necessary
170 if (ippl::Comm->size() > 1)
171 *MsgDest << "{" << ippl::Comm->rank() << "}";
172
173 // output the message level number if necessary
174 if (MsgLevel > 1)
175 *MsgDest << "[" << MsgLevel << "]";
176
177 // output the end of the prefix string if necessary
178 if (Name != 0)
179 *MsgDest << "> ";
180 }
181
182 // finally, print out the message itself
183 *MsgDest << buf << std::endl;
184}
185
187// Print out the message in the given buffer.
188void Inform::display_message(char* buf) {
189 // check if we should even print out the message
190 if (On && MsgLevel <= OutputLevel && buf != 0) {
191 // get location of final string term char
192 char* stend = buf + strlen(buf);
193
194 // print blank lines for leading endlines
195 while (*buf == '\n') {
196 *buf = '\0';
197 display_single_line(buf++);
198 }
199
200 // print out all lines in the string now
201 while ((buf = strtok(buf, "\n")) != 0) {
203 buf += strlen(buf);
204 if (buf < stend)
205 buf++;
206
207 // print out contiguous blank lines, if any
208 while (*buf == '\n') {
209 *buf = '\0';
210 display_single_line(buf++);
211 }
212 }
213 }
215}
216
217void Inform::setDestination(std::ostream& dest) {
218 if (NeedClose)
219 delete MsgDest;
220
221 MsgDest = &dest;
222
223 NeedClose = false;
224}
225
227// Set the current output level for this Inform object.
229 if (ol >= (MIN_INFORM_LEVEL - 1) && ol <= MAX_INFORM_LEVEL)
230 OutputLevel = ol;
231 return *this;
232}
233
235// Set the current message level for the current message in this Inform object.
237 if (ol >= MIN_INFORM_LEVEL && ol <= MAX_INFORM_LEVEL)
238 MsgLevel = ol;
239 return *this;
240}
241
243// the signal has been given ... process the message. Return ref to object.
245 // print out the message (only if this is the master node)
247 FormatBuf << std::ends;
248 // extract C string and display
249 MsgBuf = FormatBuf.str();
250 char* cstring = const_cast<char*>(MsgBuf.c_str());
251 display_message(cstring);
252 // clear buffer contents
253 // MsgBuf = string("");
254 // FormatBuf.str(MsgBuf);
255 }
256
257 // reset this ostrstream to the start
258 FormatBuf.seekp(0, std::ios::beg);
259 return *this;
260}
261
263// test program
264
265#ifdef DEBUG_INFORM_CLASS
266
267int main(int argc, char* argv[]) {
268 int i;
269
270 // create an Inform instance
271 Inform inf("Inform Test");
272
273 // copy in the argv's ... then print them out
274 for (i = 0; i < argc; i++)
275 inf << "Argument " << i << " = " << argv[i] << "\n";
276 inf << endl << endl;
277
278 // do another one to make sure
279 inf.setOutputLevel(3);
280 inf << level2 << "This is the second test." << endl;
281
282 return 0;
283}
284
285#endif
286
287/***************************************************************************
288 * $RCSfile: Inform.cpp,v $ $Author: adelmann $
289 * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
290 * IPPL_VERSION_ID: $Id: Inform.cpp,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
291 ***************************************************************************/
int main(int argc, char *argv[])
Inform & level2(Inform &inf)
Definition Inform.cpp:51
Inform & level4(Inform &inf)
Definition Inform.cpp:57
constexpr int MAX_INFORM_LEVEL
Definition Inform.cpp:36
Inform & endl(Inform &inf)
Definition Inform.cpp:42
Inform & level1(Inform &inf)
Definition Inform.cpp:48
Inform & level3(Inform &inf)
Definition Inform.cpp:54
constexpr int MIN_INFORM_LEVEL
Definition Inform.cpp:35
Inform & level5(Inform &inf)
Definition Inform.cpp:60
STL namespace.
std::unique_ptr< Inform > Info
Definition Ippl.h:29
std::unique_ptr< mpi::Communicator > Comm
Definition Ippl.h:22
int OutputLevel
Definition Inform.h:141
std::string MsgBuf
Definition Inform.h:120
Inform & outputMessage(void)
Definition Inform.cpp:244
void display_single_line(char *)
Definition Inform.cpp:163
int PrintNode
Definition Inform.h:137
std::ostream * MsgDest
Definition Inform.h:125
Inform(const char *=0, int=0)
Definition Inform.cpp:87
WriteMode
Definition Inform.h:43
@ OVERWRITE
Definition Inform.h:44
bool On
Definition Inform.h:134
void setDestination(std::ostream &dest)
Definition Inform.cpp:217
Inform & setOutputLevel(const int)
Definition Inform.cpp:228
bool NeedClose
Definition Inform.h:128
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
bool OpenedSuccessfully
Definition Inform.h:131
int MsgLevel
Definition Inform.h:146
char * Name
Definition Inform.h:117
void setup(const char *, int)
Definition Inform.cpp:67