casacore
|
ostream-like interface to creating log messages. More...
#include <LogIO.h>
Public Types | |
enum | Command { POST, EXCEPTION, SEVERE, WARN, NORMAL, NORMAL1, NORMAL2, NORMAL3, NORMAL4, NORMAL5, DEBUG1, DEBUG2, DEBUGGING } |
Special commands to the LogIO object. More... | |
Public Member Functions | |
LogIO () | |
Attach this LogIO object to the global sink with no origin information. More... | |
LogIO (LogSink &sink) | |
Attach this LogIO object to the supplied sink. More... | |
LogIO (const LogOrigin &OR) | |
Attach this LogIO object to the supplied origin and global sink. More... | |
LogIO (const LogOrigin &OR, LogSink &sink) | |
Attach this LogIO object to the supplied origin and sink. More... | |
LogIO (const LogIO &other) | |
Copying uses reference semantics, i.e. More... | |
LogIO & | operator= (const LogIO &other) |
~LogIO () | |
The destructor will post any accumulated message that has not already been posted. More... | |
void | post () |
Post the accumulated message. More... | |
void | post (LogMessage &amess) |
void | postLocally () |
Post the accumulated message locally. More... | |
template<typename EXC > | |
void | postThenThrow (const EXC &exc) |
Post the accumulated message at SEVERE priority and then throw an exception. More... | |
void | priority (LogMessage::Priority which) |
Change the priority of the message. More... | |
LogMessage::Priority | priority () |
void | sourceLocation (const SourceLocation *where) |
Change the location in the origin. More... | |
void | origin (const LogOrigin &origin) |
Change the origin of the accumulated message. More... | |
ostream & | output () |
Acumulate output in this ostream. More... | |
LogSinkInterface & | localSink () |
Occasionally it is useful to interrogate the local log sink. More... | |
const LogSinkInterface & | localSink () const |
Private Member Functions | |
void | preparePostThenThrow (const AipsError &x) |
Prepare message stream for postThenThrow function. More... | |
Private Attributes | |
LogSink | sink_p |
LogMessage | msg_p |
ostringstream * | text_p |
ostream-like interface to creating log messages.
Public interface
Log
message, I
nput/O
utput.
LogIO is intended to be used in a way similar to the ostream class. However, rather than sending it's output to a file or stdout, it bundles its output up into LogMessage objects and posts them to a LogSink.
When you use the "<<" operator on a LogIO, you are building up a log message inside the LogIO object. The message is posted when:
LogIO::POST()
is called LogIO::POST
or LogIO::EXCEPTION
commands to the LogIO with the shift ( <<
) command. Note that log messages may span multiple lines, so sending the LogIO a newline (via "\n" or endl) does not force the message to be emitted.
A LogIO may be created in the following ways:
Here, os
is attached to the global log sink, and no origin information is set.
Here, os
is attached to tab
(and also to the global log sink since every sink's post
also calls the global sink's post
).
Here, os
is attached to the global sink and the origin information is set to class::func(args)
and the line number and source file information is set (with WHERE
).
Here all the above information is set.
Once you have a LogIO
, using it is pretty simple:
This accumulates the message but does not send it. If you want to force it to be sent you can do so with either of the following methods:
Note that after a post the priority is reset to NORMAL.
If you want to change the level of the message you can also do so with the shift operator:
Note that changing the priority changes the priority of the entire message. The message does not get posted until the POST is done. So in the above example the DEBUGGING priority does not do anything because the priority is overwritten by the SEVERE one.
You can also change the origin information with the << operator:
A class which has an operator<< to std::ostream but not LogIO can be handled as follows:
The earlier method of creating log messages solely through LogSink and LogMessage required the programmer to type in more lines of code than this solution. Also, this interface makes it easy to drop log messages into existing code that uses ostreams.
Special commands to the LogIO object.
Enumerator | |
---|---|
POST |
Post the accumulated message. Equivalent to calling LogIO::post(). |
EXCEPTION |
Post the accumulated message then throw an exception. Always posts the message at SEVERE priority. Equivalent to calling LogIO::postThenThrow(). |
SEVERE |
Change the message priority to SEVERE. |
WARN |
Change the message priority to WARN. |
NORMAL |
Change the message priority to NORMAL. |
NORMAL1 | |
NORMAL2 | |
NORMAL3 | |
NORMAL4 | |
NORMAL5 | |
DEBUG1 |
Change the message priority to DEBUGGING. |
DEBUG2 | |
DEBUGGING |
casacore::LogIO::LogIO | ( | ) |
Attach this LogIO object to the global sink with no origin information.
casacore::LogIO::LogIO | ( | LogSink & | sink | ) |
casacore::LogIO::LogIO | ( | const LogOrigin & | OR | ) |
Attach this LogIO object to the supplied origin and global sink.
Attach this LogIO object to the supplied origin and sink.
casacore::LogIO::LogIO | ( | const LogIO & | other | ) |
Copying uses reference semantics, i.e.
the same sink will be shared by both copies.
casacore::LogIO::~LogIO | ( | ) |
The destructor will post any accumulated message that has not already been posted.
|
inline |
Occasionally it is useful to interrogate the local log sink.
Definition at line 303 of file LogIO.h.
References casacore::LogSink::localSink(), and sink_p.
|
inline |
Definition at line 308 of file LogIO.h.
References casacore::LogSink::localSink(), and sink_p.
void casacore::LogIO::origin | ( | const LogOrigin & | origin | ) |
Change the origin of the accumulated message.
ostream& casacore::LogIO::output | ( | ) |
Acumulate output in this ostream.
Referenced by casacore::STLIO_global_functions_Container_IO::operator<<().
void casacore::LogIO::post | ( | ) |
Post the accumulated message.
If you wish, you can post the messages only locally to the sink. After the post the priority is reset to NORMAL.
void casacore::LogIO::post | ( | LogMessage & | amess | ) |
void casacore::LogIO::postLocally | ( | ) |
Post the accumulated message locally.
After the post the priority is reset to NORMAL.
|
inline |
Post the accumulated message at SEVERE priority and then throw an exception.
After the post the priority is reset to NORMAL.
Definition at line 229 of file LogIO.h.
References msg_p, casacore::LogSink::postThenThrow(), preparePostThenThrow(), and sink_p.
|
private |
Prepare message stream for postThenThrow function.
Referenced by postThenThrow().
void casacore::LogIO::priority | ( | LogMessage::Priority | which | ) |
Change the priority of the message.
It does NOT post the accumulated message at the old priority first.
LogMessage::Priority casacore::LogIO::priority | ( | ) |
void casacore::LogIO::sourceLocation | ( | const SourceLocation * | where | ) |
Change the location in the origin.
Almost always this is called with the macro WHERE as its argument.
|
private |
Definition at line 254 of file LogIO.h.
Referenced by postThenThrow().
|
private |
Definition at line 253 of file LogIO.h.
Referenced by localSink(), and postThenThrow().