MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
InfoOut_streamBuffer Class Reference

Customized string buffer to prepend cout/cerr with the domain id. More...

#include <infoout.h>

Inheritance diagram for InfoOut_streamBuffer:
[legend]
Collaboration diagram for InfoOut_streamBuffer:
[legend]

Public Member Functions

 InfoOut_streamBuffer ()
 Default constructor does basically nothing. More...
 
 InfoOut_streamBuffer (std::ostream *os, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool printDomainId=true)
 Constructor passes the parameters to initialize(). More...
 
void initialize (std::ostream *os, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool printDomainId=true)
 Initializes the buffer to make it ready for use. More...
 
- Public Member Functions inherited from InfoOut_buffer
 InfoOut_buffer ()
 Generic constructor is used when no information is provided during declaration. More...
 
virtual MBool setRootOnly (MBool rootOnly=true)
 Sets interal state of whether only the root domain (rank 0) should write to file. More...
 
virtual MInt setMinFlushSize (MInt minFlushSize)
 Sets the minimum buffer length that has to be reached before the buffer is flushed. More...
 

Protected Member Functions

virtual MInt sync ()
 Flushes the buffer by writing the contents to the asssociated output stream. More...
 
virtual void flushBuffer ()
 This function does nothing for this class, since as of now there is no intermediate buffering. More...
 
virtual MString addPrefix (const MString &str)
 
- Protected Member Functions inherited from InfoOut_buffer
virtual MString encodeXml (const std::string &str)
 Parses the string input and returns the string with XML entities escaped. More...
 
virtual MString getXmlHeader ()
 Return an XML header that should written at the beginning of each log file. More...
 
virtual MString getXmlFooter ()
 Return an XML footer that should written at the end of each log file. More...
 
virtual void createPrefixMessage ()
 Creates an XML prefix using the domain id that is prepended to each message. More...
 
virtual void createSuffixMessage ()
 Creates an XML suffix that is appended to each message. More...
 
virtual void flushBuffer ()=0
 

Private Attributes

MBool m_isInitialized
 Stores whether a stream was already associated with this buffer. More...
 
MBool m_printDomainId
 Stores whether the domain id should be prepended to each output. More...
 
std::ostream * m_output
 Stores the output stream (usually cout or cerr) that will be modified. More...
 
MPI_Comm m_mpiComm
 MPI communicator group. More...
 
MBool m_isDisabled
 Stores wether output in streamBuffer should be written. More...
 

Additional Inherited Members

- Protected Attributes inherited from InfoOut_buffer
MBool m_rootOnly
 Stores whether only the root domain writes a log file. More...
 
MInt m_domainId
 Contains the MPI rank (= domain id) of this process. More...
 
MInt m_noDomains
 Contains the MPI rank count (= number of domains) More...
 
MInt m_minFlushSize
 Minimum length of the internal buffer before flushing. More...
 
MString m_prefixMessage
 Stores the prefix that is prepended to each output. More...
 
MString m_suffixMessage
 Stores the suffix that is apended to each output. More...
 
std::ostringstream m_tmpBuffer
 Temporary buffer to hold string until flushing. More...
 
MString m_projectName
 Name of the current Project. More...
 
std::vector< std::pair< MString, MString > > m_prefixAttributes
 
- Static Protected Attributes inherited from InfoOut_buffer
static const MInt m_fileFormatVersion = 1
 

Detailed Description

Author
Michael Schlottke
Date
AJune 2012

This class takes an exisiting output stream such as cout or cerr and adds an additonal layer of buffering. For cerr this effectively means that cerr is now thread-safe, i.e. multiple processors writing to cerr at once should not result in mixed up strings anymore. Furthermore, each message is prepended with a prefix consisting of the rank of the specified MPI communicator. This behavior is optional and may be changed at runtime by subsequent calls to initialize().

Definition at line 185 of file infoout.h.

Constructor & Destructor Documentation

◆ InfoOut_streamBuffer() [1/2]

InfoOut_streamBuffer::InfoOut_streamBuffer ( )
Author
Michael Schlottke
Date
April 2012

Definition at line 789 of file infoout.cpp.

790 : m_isInitialized(false), m_printDomainId(false), m_output(0), m_mpiComm(), m_isDisabled(false) {
791 // Nothing here
792}
std::ostream * m_output
Stores the output stream (usually cout or cerr) that will be modified.
Definition: infoout.h:189
MPI_Comm m_mpiComm
MPI communicator group.
Definition: infoout.h:190
MBool m_isInitialized
Stores whether a stream was already associated with this buffer.
Definition: infoout.h:187
MBool m_isDisabled
Stores wether output in streamBuffer should be written.
Definition: infoout.h:191
MBool m_printDomainId
Stores whether the domain id should be prepended to each output.
Definition: infoout.h:188

◆ InfoOut_streamBuffer() [2/2]

InfoOut_streamBuffer::InfoOut_streamBuffer ( std::ostream *  os,
MPI_Comm  mpiComm = MPI_COMM_WORLD,
MBool  printDomainId = true 
)
Author
Michael Schlottke
Date
April 2012

After an object is instantiated using this constructor, the buffer is ready to use. For detailed information on what is done, please refer to initialize().

Parameters
[in]osOutput stream that should be used for output.
[in]mpiCommMPI communicator for which the domain information should be gathered.
[in]printDomainIdDetermines whether the domain id should be prepended to each message.

Definition at line 803 of file infoout.cpp.

804 : m_isInitialized(false), m_printDomainId(false), m_output(0), m_mpiComm(), m_isDisabled(false) {
805 initialize(os, mpiComm, printDomainId);
806}
void initialize(std::ostream *os, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool printDomainId=true)
Initializes the buffer to make it ready for use.
Definition: infoout.cpp:820

Member Function Documentation

◆ addPrefix()

MString InfoOut_streamBuffer::addPrefix ( const MString inputStr)
protectedvirtual

\Writes the domain_Id in front of every line. \If
is written in the middle of a stream it will be replaced with the domain_Id \and written in the following line so you have the expected output.

Date
November 2012

Definition at line 867 of file infoout.cpp.

867 {
868 MChar c;
869 ostringstream tmpEncodeStreambuffer;
870
871 for(MString::const_iterator iter = inputStr.begin(); iter < inputStr.end(); iter++) {
872 c = (MChar)*iter;
873 tmpEncodeStreambuffer << c;
874
875 if(c == '\n' && iter != inputStr.end() - 1) {
876 tmpEncodeStreambuffer << m_prefixMessage;
877 }
878 }
879
880 return tmpEncodeStreambuffer.str();
881}
MString m_prefixMessage
Stores the prefix that is prepended to each output.
Definition: infoout.h:50
char MChar
Definition: maiatypes.h:56

◆ flushBuffer()

void InfoOut_streamBuffer::flushBuffer ( )
inlineprotectedvirtual
Author
Michael Schlottke
Date
June 2012

Implements InfoOut_buffer.

Definition at line 919 of file infoout.cpp.

919 {
920 // Nothing here
921}

◆ initialize()

void InfoOut_streamBuffer::initialize ( std::ostream *  os,
MPI_Comm  mpiComm = MPI_COMM_WORLD,
MBool  printDomainId = true 
)
Author
Michael Schlottke
Date
April 2012

The output stream that will be written to is assigned and the domain id determined. If specified, a prefix is generated containing the domain id. This method may be called multiple times to change the desired behavior of this buffer.

Parameters
[in]osOutput stream that should be used for output.
[in]mpiCommMPI communicator for which the domain information should be gathered.
[in]printDomainIdDetermines whether the domain id should be prepended to each message.

Definition at line 820 of file infoout.cpp.

820 {
821 // Delete any previous (non-empty) strings stored in the buffer that were not flushed yet by calling sync
822 if(!str().empty()) {
823 sync();
824 }
825
826 // Assign ostream to m_output
827 m_output = os;
828
829 // Set MPI communicator group
830 m_mpiComm = mpiComm;
831
832 // Save whether the domain id should be prepended to each message
833 m_printDomainId = printDomainId;
834
835 // Get domain id
836 MPI_Comm_rank(m_mpiComm, &m_domainId);
837
838 // Create prefix string including the domain id if specified
839 if(m_printDomainId) {
840 // Create temporary stream
841 ostringstream tmpStream;
842
843 // Fill tmpStream with formatted domain id
844 tmpStream << "[" << setfill('0') << setw(7) << m_domainId << "] ";
845
846 // Set prefix message to tmpStream string
847 m_prefixMessage = tmpStream.str();
848 } else {
849 // Otherwise use an empty prefix message
850 m_prefixMessage = "";
851 }
852
853 // Set state variable
854 m_isInitialized = true;
855
856 // Set isDisabled
857 m_isDisabled = false;
858}
MInt m_domainId
Contains the MPI rank (= domain id) of this process.
Definition: infoout.h:47
virtual MInt sync()
Flushes the buffer by writing the contents to the asssociated output stream.
Definition: infoout.cpp:894

◆ sync()

MInt InfoOut_streamBuffer::sync ( )
protectedvirtual
Author
Michael Schlottke
Date
April 2012

Sync is called automatically when an "endl" is sent to the stream. It preprends each message with the optional message prefix and writes the contents of the buffer to the output stream, which is then flushed itself. Finally, all internal buffers are reset.

Returns
Zero by default.

Definition at line 894 of file infoout.cpp.

894 {
895 // Only write actual output if streamBuffer is initialized, and if we are supposed to on this processor
896 if(m_isInitialized && !(m_rootOnly && m_domainId != 0) && !m_isDisabled) {
897 // Create formatted string
899
900 // Write temporary buffer to output stream and flush it
901 *m_output << m_tmpBuffer.str() << flush;
902 }
903
904 // Reset stringbuf of InfoOut_streamBuffer
905 str("");
906
907 // Reset temporary buffer
908 m_tmpBuffer.str("");
909
910 // Default return value for sync()
911 return 0;
912}
std::ostringstream m_tmpBuffer
Temporary buffer to hold string until flushing.
Definition: infoout.h:52
MBool m_rootOnly
Stores whether only the root domain writes a log file.
Definition: infoout.h:46
virtual MString addPrefix(const MString &str)
Definition: infoout.cpp:867

Member Data Documentation

◆ m_isDisabled

MBool InfoOut_streamBuffer::m_isDisabled
private

Definition at line 191 of file infoout.h.

◆ m_isInitialized

MBool InfoOut_streamBuffer::m_isInitialized
private

Definition at line 187 of file infoout.h.

◆ m_mpiComm

MPI_Comm InfoOut_streamBuffer::m_mpiComm
private

Definition at line 190 of file infoout.h.

◆ m_output

std::ostream* InfoOut_streamBuffer::m_output
private

Definition at line 189 of file infoout.h.

◆ m_printDomainId

MBool InfoOut_streamBuffer::m_printDomainId
private

Definition at line 188 of file infoout.h.


The documentation for this class was generated from the following files: