namespace olb {
/// userdefined stream buffer for OstreamManager
/**that prefixes each line with a user specified string in squared brackets*/
class OMBuf : public std::stringbuf {
private:
std::ostream* output;
std::string text;
static bool multiOutput;
public:
OMBuf();
~OMBuf() override;
OMBuf(const OMBuf& rhs);
OMBuf& operator=(const OMBuf& rhs);
void swap(OMBuf& rhs);
OMBuf(std::ostream& str, std::string classname);
void setMultiOutput(bool b);
/// sync the stream with the output:
/** 1) first Output "[text] ", then the buffer,
* 2) reset the buffer
* 3) and flush the actual output stream*/
int sync() override;
};
/// class for marking output with some text
/** The principle of this class consists of writing all output first in a userdefined Buffer of type OMBuf. On a flush it spits out at first the userdefined text in squared brackets and afterwards everything from the buffer.
An object of this class can be used (almost) exactly like a normal std::cout with the <<-Operator and std::endl.
How to implement in the code of a class with some output:
#include
ExampleClass {
private:
OstreamManager clout;
public:
ExampleClass() : clout(std::cout,"ExampleClass")
{}
showOutput() {
clout << "where am I?" << std::endl;
}
}
A call of the function showOutput() will show in terminal:
[ExampleClass] Where am I?
Please note, that the control character \\n - in contrast to std::endl - will not force a flush of the outstream. As consequence, the new line won't be prefixed with a user specified text. Other control character might not work either (untested).
*/
class OstreamManager : public std::ostream {
private:
// OstreamManager clout should use it's own special buffer
/// special, overloaded buffer
OMBuf buffer;
public:
// standard constructor (should be unnecessary due to absence of classname-text)
// OstreamManager();
/// constructor that uses std::cout by default
OstreamManager(std::string classname);
/// constructor for default usage
OstreamManager(std::ostream& str, std::string classname);
// Copy construction
OstreamManager(const OstreamManager& rhs);
// Copy assignment
OstreamManager& operator=(const OstreamManager& rhs);
// Destructor
~OstreamManager() override;
/// enable message output for all MPI processes, disabled by default
void setMultiOutput(bool b);
};
} // namespace olb
#endif