Objects of this class may be used to handle messages in a standardized
way. Messages may be prefixed with order numbers and labels and/or line
numbers. Messages generated by Mstream objects may optionally end by
throwing an FBB::Exception (which is an std::exception), allowing
simple implementation of, e.g., fatal error messages.
Four message streams are provided by the BOBCAT library and may be
used after including the mstream header file. They are all defined in the
FBB namespace:
FBB::emsg for generating standard (labeled and numbered) error
messages, e.g.
[Error 1] this is an error message
FBB::fmsg for generating (labeled) fatal error messages, ending in an
exception, e.g.,
[Fatal] this fatal message is followed by an exception
The exception's what member contains FBB::fmsg's id value (see
member id below).
FBB::imsg for generating plain informational messages, e.g.,
this is a plain message
FBB::wmsg for generating (labeled) warning messages;
[Warning] this is a warning
By default all messages are inserted into the standard output stream, but
other destinations (standard error, a named file, etc.) can easily be
configured.
Mstream objects themselves are std::ostream objects, so they
can be used as objects passed to functions expecting ostream
arguments. Messages inserted into Mstream objects are buffered until
the buffer is flushed by their std::ostream parts or when either the
FBB::endl or FBB::flush is inserted into the message stream. Since
these manipulators act like std::endl and std::flush when inserted
into another kind of std::ostream object, using FBB::endl and using
FBB::flush might be worth considering. Also, when using namespace stdandusing namespace FBB is specified the FBB::endl and
FBB::flush manipulator will automatically be used with Mstream
objects.
Messages inserted into Mstream objects are considered completed
at the insertion of the FBB::endl or FBB::flush manipulators. Message
labels, line number labels and line numbers will only be shown for the next
line after these manipulators have been inserted and exceptions are, if
needed, thrown from these manipulators.
NAMESPACE
FBB
All elements mentioned in this man-page, are defined in the namespace
FBB.
INHERITS FROM
std::ostream
CONSTRUCTORS
Mstream():
The default constructor generates messages using the
std::streambuf used by std::cout. There is no limit to the number of
messages that may be inserted. No message or line numbers are shown, no
exceptions are thrown when inserting messages.
explicit Mstream(std::ostream &ostr, size_t maxCount =
std::numeric_limits<size_t>::max(), std::string const &tag = "", bool
throwing = false):
This constructor uses the std::streambuf that is also used by the
ostream ostr. By default (using the default argument values) there
is no limit to the number of messages that may be inserted. No message
or line numbers are shown, no exceptions are thrown when inserting
messages. Specifying any other value than
std::numeric_limits<size_t>::max() will set the maximum number of
messages that can be inserted to that value. The tag defines the
text of the message label (e.g., Error).
When throwing is true an exception is thrown after completing
a message. The generated exception's what member shows the
FBB::Mstream object's id (see below for the member id) of the
Mstream object throwing the exception.
explicit Mstream(std::streambuf &buf, size_t maxCount =
std::numeric_limits<size_t>::max(), std::string const &tag = "", bool
throwing = false):
This constructor uses buf to insert messages into. The remaining
parameters are identical to those of the previous constructor.
explicit Mstream(std::string const &name, size_t maxCount =
std::numeric_limits<size_t>::max(), std::string const &tag = "", bool
throwing = false):
This constructor creates a std::ofstream from the provided name
parameter. It throws an exception if the stream cannot be opened for
writing. If a file by that name already exists it is rewritten. The
remaining parameters are identical to those of the previous two
constructors.
As Mstream inherits from std::ostream the copy constructor is
not available. Neither is the move constructor.
MEMBER FUNCTIONS
size_t count() const:
returns the number of inserted messages (if setCount has been
called: the value set by the last setCount call plus the number of
inserted messages since that call).
bool isActive() const:
returns true if messages can actually be inserted into the
FBB::MStream object, and false if inserted messages are ignored. When
ignoring messages the message count is not updated.
int id() const:
returns the unique id of the Mstream object.
bool lineExcess() const:
returns true after attempting to insert an additional message
after maxCount number of messages have been inserted.
std::string const &lineTag() const:
returns the currently used line-tag (by default `Line').
size_t maxCount() const:
returns the maximum number of messages that can be inserted. If the
returned value equals std::numeric_limits<size_t>::max() then there is no limit to the number of
messages that can be inserted.
void noLineNr():
calling this member will suppress the display of a line number if it
is called after calling setLineNr (see below) but before a message is
being (or has been) inserted.
void off():
after calling off messages inserted into the Mstream
object are silently ignored. After calling off the internal message
counter is not incremented when messages are inserted.
void on():
by default and after calling on messages inserted into the
Mstream object are sent to their destination stream.
void reset(std::ostream &ostr):
messages inserted into the Mstream object are handled by the
std::streambuf also used by ostr. Other parameters (e.g.,
maxCount, the message label) are kept as-is.
void reset(std::streambuf *buf):
messages inserted into the Mstream object are handled by
std::streambuf buf. Other parameters (e.g., maxCount, the message
label) are kept as-is.
void reset(FBB::Mstream const &mstream):
the current object is reset using the parameters of the mstream
parameter.
void reset(std::string const &name, size_t maxCount,
std::string const &tag, bool throwing):
messages inserted into Mstream objects are handled by a
std::ofstream created using the provided name parameter. An exception
is thrown if the stream cannot be opened for writing. If a file by that name
already exists it is rewritten. Other parameters (e.g., maxCount, the
message label) are kept as-is.
void reset(std::ostream &ostr, size_t maxCount,
std::string const &tag, bool throwing):
messages inserted into Mstream objects are handled by the
std::streambuf also used by ostr. By specifying
std::numer