casacore
Loading...
Searching...
No Matches
LogIO.h
Go to the documentation of this file.
1//# LogIO.h: ostream-like interface to creating log messages.
2//# Copyright (C) 1997,1999,2000,2001,2003
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_LOGIO_H
27#define CASA_LOGIO_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Logging/LogMessage.h>
32#include <casacore/casa/Logging/LogSink.h>
33#include <casacore/casa/BasicSL/Complex.h>
34#include <casacore/casa/iosstrfwd.h>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38//# Forward Declarations
39class LogSink;
40class LogOrigin;
41
42// <summary>
43// ostream-like interface to creating log messages.
44// </summary>
45
46// <use visibility=export>
47
48// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tLogIO.cc" demos="dLogging.cc">
49// </reviewed>
50
51// <prerequisite>
52// <li> <linkto class=LogSink>LogSink</linkto> class
53// <li> <linkto class=LogMessage>LogMessage</linkto> class
54// <li> <linkto class=LogMessage>LogOrigin</linkto> class
55// </prerequisite>
56//
57// <etymology>
58// <src>Log</src> message, <src>I</src>nput/<src>O</src>utput.
59// </etymology>
60//
61// <synopsis>
62// LogIO is intended to be used in a way similar to the ostream class.
63// However, rather than sending it's output to a file or stdout, it bundles
64// its output up into <linkto class=LogMessage>LogMessage</linkto> objects
65// and posts them to a <linkto class=LogSink>LogSink</linkto>.
66//
67// When you use the "<<" operator on a LogIO, you are building up a log message
68// inside the LogIO object. The message is posted when:
69// <ol>
70// <li> <src>LogIO::POST()</src> is called
71// <li> You send the <src>LogIO::POST</src> or <src>LogIO::EXCEPTION</src>
72// commands to the LogIO with the shift (<src> << </src>) command.
73// <li> The LogIO object is destructed.
74// </ol>
75// Note that log messages may span multiple lines, so sending the LogIO a
76// newline (via "\n" or endl) does not force the message to be emitted.
77// </synopsis>
78//
79// <example>
80// A LogIO may be created in the following ways:
81// <srcblock>
82// LogIO os;
83// </srcblock>
84// Here, <src>os</src> is attached to the global log sink, and no origin
85// information is set.
86//
87// <srcblock>
88// TableLogSink tab(...);
89// LogIO os(tab);
90// </srcblock>
91// Here, <src>os</src> is attached to <src>tab</src> (and also to the global
92// log sink since every sink's <src>post</src> also calls the global sink's
93// <src>post</src>).
94//
95//
96// <srcblock>
97// LogIO os(LogOrigin("class", "func(args)", WHERE));
98// </srcblock>
99// Here, <src>os</src> is attached to the global sink and the origin
100// information is set to <src>class::func(args)</src> and the line number and
101// source file information is set (with <src>WHERE</src>).
102//
103// <srcblock>
104// TableLogSink tab(...);
105// LogIO os(LogOrigin("class", "func(args)", WHERE), tab);
106// </srcblock>
107// Here all the above information is set.
108//
109// Once you have a <src>LogIO</src>, using it is pretty simple:
110// <srcblock>
111// os << "Every good boy deserves" << 5 << " pieces of fudge!";
112// </srcblock>
113//
114// This accumulates the message but does not send it. If you want to force it
115// to be sent you can do so with either of the following methods:
116// <srcblock>
117// os << LogIO::POST; // From the Commands enum
118// os.post(); // Member function
119// </srcblock>
120// Note that after a post the priority is reset to NORMAL.
121//
122// If you want to change the level of the message you can also do so with the
123// shift operator:
124// <srcblock>
125// os << LogIO::DEBUGGING << "Boring message" <<
126// LogIO::SEVERE << "Error!" << LogIO::POST;
127// </srcblock>
128// Note that changing the priority changes the priority of the entire
129// message. The message does not get posted until the POST is done.
130// So in the above example the DEBUGGING priority does not do anything
131// because the priority is overwritten by the SEVERE one.
132//
133// You can also change the origin information with the << operator:
134// <srcblock>
135// os << LogOrigin("class", "func(args)");
136// os << WHERE;
137// </srcblock>
138//
139// A class which has an operator<< to std::ostream but not LogIO can be handled
140// as follows:
141// <srcblock>
142// os << LogIO::SEVERE << " at ";
143// os.output() << MEpoch::Convert(time_p, MEpoch::Ref(MEpoch::UTC))();
144// os << LogIO::POST;
145// </srcblock>
146// </example>
147//
148// <motivation>
149// The earlier method of creating log messages solely through LogSink and
150// LogMessage required the programmer to type in more lines of code than
151// this solution. Also, this interface makes it easy to drop log messages
152// into existing code that uses ostreams.
153// </motivation>
154//
155// <todo asof="1997/01/29">
156// <li> Add << operators for all classes that have ostream<< defined.
157// (We could probably do it with a template, but might result
158// in ambiguity).
159// <li> Have a function for changing the LogSink only? (You can get
160// much the same effect with operator=).
161// them?
162// </todo>
163
164class LogIO
165{
166public:
167 // Special commands to the LogIO object
168 enum Command {
169 // Post the accumulated message. Equivalent to calling LogIO::post().
171 // Post the accumulated message then throw an exception.
172 // Always posts the message at SEVERE priority. Equivalent to calling
173 // LogIO::postThenThrow().
175 // Change the message priority to SEVERE.
177 // Change the message priority to WARN.
179 // Change the message priority to NORMAL.
186 // Change the message priority to DEBUGGING.
190
191 // Attach this LogIO object to the global sink with no origin information.
193 // Attach this LogIO object to the supplied sink. A referencing copy of
194 // the sink is made inside the LogIO object, so you do not need to worry
195 // about memory management.
197 // Attach this LogIO object to the supplied origin and global sink.
198 LogIO(const LogOrigin &OR);
199 // Attach this LogIO object to the supplied origin and sink.
200 LogIO(const LogOrigin &OR, LogSink &sink);
201
202 // Copying uses reference semantics, i.e. the same sink will be shared
203 // by both copies.
204 // <group>
205 LogIO(const LogIO &other);
206 LogIO &operator=(const LogIO &other);
207 // </group>
208
209 // The destructor will post any accumulated message that has not already
210 // been posted.
212
213 // Post the accumulated message. If you wish, you can post the messages
214 // only locally to the sink.
215 // After the post the priority is reset to NORMAL.
216 void post();
217 void post(LogMessage &amess);
218
219 // Post the accumulated message locally.
220 // After the post the priority is reset to NORMAL.
222
223 // Post the accumulated message at SEVERE priority and then throw an
224 // exception.
225 // After the post the priority is reset to NORMAL.
226 template<typename EXC> void postThenThrow (const EXC& exc)
228
229 // Change the priority of the message. It does NOT post the accumulated
230 // message at the old priority first.
233 // Change the location in the origin. Almost always this is called with the
234 // macro WHERE as its argument.
235 void sourceLocation(const SourceLocation *where);
236 // Change the origin of the accumulated message.
237 void origin(const LogOrigin &origin);
238
239 // Acumulate output in this ostream.
240 ostream& output();
241
242 // Occasionally it is useful to interrogate the local log sink.
244 const LogSinkInterface &localSink() const;
245
246private:
247 // Prepare message stream for postThenThrow function.
249
252 ostringstream *text_p;
253
254};
255
256// <summary>
257// Functions to send commands to a LogIO object.
258// </summary>
259// The following commands don't change the accumulated message, rather they
260// send commands to the LogIO object, either to:
261// <ol>
262// <li>post the current message: <src>os << "message" << LogIO::POST;</src>
263// <li>post the current message and then throw an exception:
264// <src>os << "error" << LogIO::EXCEPTION;</src>
265// <li> Change the priority of the current message:
266// <src>os << LogIO::DEBUGGING;</src>
267// <li> Change the origin of the message:
268// <srcblock>
269// os << LogOrigin(...);
270// os << WHERE; // Changes only source file/line number
271// </srcblock>
272// </ol>
273// <group name=command>
275LogIO &operator<<(LogIO &os, const SourceLocation *item);
277// </group>
278
279// <summary>
280// Functions to accumulate text in the output message.
281// </summary>
282// Accumulate text in the output message. The last entry is for things like
283// <src>endl</src>.
284// <group name=output>
285LogIO &operator<<(LogIO &os, const String &item);
286LogIO &operator<<(LogIO &os, const char *item);
288LogIO &operator<<(LogIO &os, Complex item);
289LogIO &operator<<(LogIO &os, DComplex item);
297LogIO &operator<<(LogIO &os, ostream &(*item)(ostream &));
298// </group>
299
301{
302 return sink_p.localSink();
303}
304
306{
307 return sink_p.localSink();
308}
309
310
311} //# NAMESPACE CASACORE - END
312
313#endif
LogIO()
Attach this LogIO object to the global sink with no origin information.
LogIO(const LogIO &other)
Copying uses reference semantics, i.e.
Command
Special commands to the LogIO object.
Definition LogIO.h:168
@ DEBUG1
Change the message priority to DEBUGGING.
Definition LogIO.h:187
@ SEVERE
Change the message priority to SEVERE.
Definition LogIO.h:176
@ EXCEPTION
Post the accumulated message then throw an exception.
Definition LogIO.h:174
@ NORMAL
Change the message priority to NORMAL.
Definition LogIO.h:180
@ POST
Post the accumulated message.
Definition LogIO.h:170
@ WARN
Change the message priority to WARN.
Definition LogIO.h:178
ostringstream * text_p
Definition LogIO.h:252
LogIO(const LogOrigin &OR)
Attach this LogIO object to the supplied origin and global sink.
ostream & output()
Acumulate output in this ostream.
void postThenThrow(const EXC &exc)
Post the accumulated message at SEVERE priority and then throw an exception.
Definition LogIO.h:226
void post()
Post the accumulated message.
LogMessage msg_p
Definition LogIO.h:251
void preparePostThenThrow(const AipsError &x)
Prepare message stream for postThenThrow function.
~LogIO()
The destructor will post any accumulated message that has not already been posted.
LogSinkInterface & localSink()
Occasionally it is useful to interrogate the local log sink.
Definition LogIO.h:300
void priority(LogMessage::Priority which)
Change the priority of the message.
LogMessage::Priority priority()
LogSink sink_p
Definition LogIO.h:250
void sourceLocation(const SourceLocation *where)
Change the location in the origin.
LogIO(LogSink &sink)
Attach this LogIO object to the supplied sink.
LogIO & operator=(const LogIO &other)
LogIO(const LogOrigin &OR, LogSink &sink)
Attach this LogIO object to the supplied origin and sink.
void post(LogMessage &amess)
void postLocally()
Post the accumulated message locally.
void origin(const LogOrigin &origin)
Change the origin of the accumulated message.
Priority
An "importance" which is assigned to each LogMessage.
Definition LogMessage.h:102
void postThenThrow(const LogMessage &message, const EXC &exc)
Post message and then throw an AipsError exception containing message.toString().
Definition LogSink.h:202
const LogSinkInterface & localSink() const
Change the sink that this LogSink actually uses.
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
unsigned long uLong
Definition aipstype.h:51
long Long
Definition aipstype.h:50
unsigned int uInt
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53
unsigned long long uInt64
Definition aipsxtype.h:37
Functions to send commands to a LogIO object.
Definition LogIO.h:274
LogIO & operator<<(LogIO &os, const LogOrigin &OR)
LogIO & operator<<(LogIO &os, LogIO::Command item)
LogIO & operator<<(LogIO &os, const SourceLocation *item)
Functions to accumulate text in the output message.
Definition LogIO.h:285
LogIO & operator<<(LogIO &os, uInt64 item)
LogIO & operator<<(LogIO &os, const char *item)
LogIO & operator<<(LogIO &os, uInt item)
LogIO & operator<<(LogIO &os, Double item)
LogIO & operator<<(LogIO &os, Long item)
LogIO & operator<<(LogIO &os, Int item)
LogIO & operator<<(LogIO &os, DComplex item)
LogIO & operator<<(LogIO &os, ostream &(*item)(ostream &))
LogIO & operator<<(LogIO &os, Complex item)
LogIO & operator<<(LogIO &os, uLong item)
LogIO & operator<<(LogIO &os, Bool item)
LogIO & operator<<(LogIO &os, Int64 item)
LogIO & operator<<(LogIO &os, const String &item)