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

Customized buffer to facilitate of a regular physical file for each processor within an MPI communicator. More...

#include <infoout.h>

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

Public Member Functions

 InfoOut_simpleFileBuffer ()
 Generic constructor is used when no information is provided during declaration. More...
 
 InfoOut_simpleFileBuffer (const MString &filename, const MString &projectName, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool rootOnlyHardwired=false)
 This constructor yields a new instance that can immediately be used to write messages to a regular file. More...
 
 ~InfoOut_simpleFileBuffer ()
 Destructor calls close() to close the file. More...
 
void open (const MString &filename, const MString &projectName, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool rootOnlyHardwired=false)
 Initialization of the file I/O environment. More...
 
void close (MBool forceClose=false)
 Closes the file. 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 file. More...
 
virtual void flushBuffer ()
 Flushes the buffer by writing the contents to the file. More...
 
- 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_isOpen
 Stores whether the file(s) were already opened. More...
 
MBool m_rootOnlyHardwired
 If true, only domain 0 opens and uses a file. More...
 
MString m_filename
 Filename on disk. More...
 
std::ofstream m_file
 File stream tied to physical file on disk. More...
 
MPI_Comm m_mpiComm
 MPI communicator group. 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
June 2012

This class can be used as a regular string buffer, as it inherits from stringbuf. On flushing the buffer, the contents of the buffer are written to a file using an ofstream. This is mainly for cases where logging speed is crucial, as the implementation is very lightweight and since for each process an individual file is maintained. There is an option to use this buffer but to create only one file for the MPI root domain (see m_rootOnlyHardwired).

Definition at line 152 of file infoout.h.

Constructor & Destructor Documentation

◆ InfoOut_simpleFileBuffer() [1/2]

InfoOut_simpleFileBuffer::InfoOut_simpleFileBuffer ( )
Author
Michael Schlottke
Date
June 2012

Definition at line 331 of file infoout.cpp.

332 : m_isOpen(false), m_rootOnlyHardwired(false), m_filename(), m_file(), m_mpiComm() {
333 // Nothing here
334}
MString m_filename
Filename on disk.
Definition: infoout.h:156
MBool m_isOpen
Stores whether the file(s) were already opened.
Definition: infoout.h:154
MBool m_rootOnlyHardwired
If true, only domain 0 opens and uses a file.
Definition: infoout.h:155
MPI_Comm m_mpiComm
MPI communicator group.
Definition: infoout.h:158
std::ofstream m_file
File stream tied to physical file on disk.
Definition: infoout.h:157

◆ InfoOut_simpleFileBuffer() [2/2]

InfoOut_simpleFileBuffer::InfoOut_simpleFileBuffer ( const MString filename,
const MString projectName,
MPI_Comm  mpiComm = MPI_COMM_WORLD,
MBool  rootOnlyHardwired = false 
)
Author
Michael Schlottke
Date
June 2012

Internally, this constructor just passes the parameters to open() (see open() for more details).

Parameters
[in]filenameFilename that should be used for the file.
[in]mpiCommMPI communicator which is used to determine rank/domain information.
[in]rootOnlyHardwiredIf true, only rank 0 creates a file and writes to it. On all other processors, no file is opened and at each flushing of the buffer, the buffer content is discarded.
[in]projectNameProjectname that should be used for the executed program.

Definition at line 348 of file infoout.cpp.

350 : m_isOpen(false), m_rootOnlyHardwired(false), m_filename(), m_file(), m_mpiComm() {
351 open(filename, projectName, mpiComm, rootOnlyHardwired);
352}
void open(const MString &filename, const MString &projectName, MPI_Comm mpiComm=MPI_COMM_WORLD, MBool rootOnlyHardwired=false)
Initialization of the file I/O environment.
Definition: infoout.cpp:376

◆ ~InfoOut_simpleFileBuffer()

InfoOut_simpleFileBuffer::~InfoOut_simpleFileBuffer ( )
Author
Michael Schlottke
Date
June 2012

Definition at line 359 of file infoout.cpp.

359{ close(); }
void close(MBool forceClose=false)
Closes the file.
Definition: infoout.cpp:424

Member Function Documentation

◆ close()

void InfoOut_simpleFileBuffer::close ( MBool  forceClose = false)
Author
Michael Schlottke
Date
June 2012

Any subsequent write statements to the file stream are discarded after this method is called. After close() is called, an XML footer is written to the file. Then the file is closed.

Definition at line 424 of file infoout.cpp.

424 {
425 // forceClose is not needed here (only kept for interface consistency reasons)
426 static_cast<void>(forceClose);
427
428 // Only close file if was opened before
429 if(m_isOpen) {
430 // Force flushing of the internal buffer
431 flushBuffer();
432
433 // Write XML footer to file and flush stream
434 m_file << getXmlFooter() << flush;
435
436 // Close file stream
437 m_file.close();
438
439 // Set state variable
440 m_isOpen = false;
441 }
442}
virtual MString getXmlFooter()
Return an XML footer that should written at the end of each log file.
Definition: infoout.cpp:299
virtual void flushBuffer()
Flushes the buffer by writing the contents to the file.
Definition: infoout.cpp:487

◆ flushBuffer()

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

Sync is called automatically when an "endl" is sent to the stream. At first the buffer content is wrapped in the prefix and suffix messages, then the entire string is written to the file. Finally, the internal buffers are reset.

Returns
Zero by default.

Implements InfoOut_buffer.

Definition at line 487 of file infoout.cpp.

487 {
488 // Only write if the file was already opened
489 if(m_isOpen) {
490 // Write the string to the file and flush the stream
491 m_file << m_tmpBuffer.str() << flush;
492
493 // Reset temporary buffer
494 m_tmpBuffer.str("");
495 }
496}
std::ostringstream m_tmpBuffer
Temporary buffer to hold string until flushing.
Definition: infoout.h:52

◆ open()

void InfoOut_simpleFileBuffer::open ( const MString filename,
const MString projectName,
MPI_Comm  mpiComm = MPI_COMM_WORLD,
MBool  rootOnlyHardwired = false 
)
Author
Michael Schlottke
Date
June 2012

After a successful call to this method the file stream is ready to use. Any previous information written to the buffer is lost when open is called. This function creates a new file as specified in filename (deleting any existing files with the same name), and creates the XML prefixes and suffixes used for each message. It also writes the necessary XML header information to the file.

Parameters
[in]filenameFilename that should be used for the file.
[in]mpiCommMPI communicator which is used to determine rank/domain information.
[in]rootOnlyHardwiredIf true, only rank 0 creates a file and writes to it. On all other processors, no file is opened and at each flushing of the buffer, the buffer content is discarded.
[in]projectNameProjectname that should be used for the executed program.

Definition at line 376 of file infoout.cpp.

377 {
378 // Open file only if it was not yet done
379 if(!m_isOpen) {
380 // Set MPI communicator group
381 m_mpiComm = mpiComm;
382
383 // Get domain id and number of domains
384 MPI_Comm_rank(m_mpiComm, &m_domainId);
385 MPI_Comm_size(m_mpiComm, &m_noDomains);
386
387 // Set whether only domain 0 should do any writing (including the creation of a file)
388 m_rootOnlyHardwired = rootOnlyHardwired;
389
390 // Only open the file if m_rootOnlyHardwired was not set as true. Otherwise the file state remains closed.
391 if(!(m_rootOnlyHardwired && m_domainId != 0)) {
392 // Set filename
393 m_filename = filename;
394
395 // Set projectName
396 m_projectName = projectName;
397
398 // Open file
399 m_file.open(m_filename.c_str());
400
401 // Clear internal buffer in order to dismiss any previous input
402 str("");
403
404 // Create prefix and suffix messages
407
408 // Write root and meta information to file
409 m_file << getXmlHeader() << flush;
410
411 // Set state variable
412 m_isOpen = true;
413 }
414 }
415}
MInt m_domainId
Contains the MPI rank (= domain id) of this process.
Definition: infoout.h:47
virtual void createSuffixMessage()
Creates an XML suffix that is appended to each message.
Definition: infoout.cpp:162
virtual MString getXmlHeader()
Return an XML header that should written at the beginning of each log file.
Definition: infoout.cpp:199
virtual void createPrefixMessage()
Creates an XML prefix using the domain id that is prepended to each message.
Definition: infoout.cpp:138
MInt m_noDomains
Contains the MPI rank count (= number of domains)
Definition: infoout.h:48
MString m_projectName
Name of the current Project.
Definition: infoout.h:53

◆ sync()

MInt InfoOut_simpleFileBuffer::sync ( )
protectedvirtual
Author
Michael Schlottke
Date
June 2012

Sync is called automatically when an "endl" is sent to the stream. At first the buffer content is wrapped in the prefix and suffix messages, then the entire string is written to the file by calling flushBuffer(). Finally, the internal buffers are reset.

Returns
Zero by default.

Definition at line 454 of file infoout.cpp.

454 {
455 // Only write if the file was already opened
456 if(m_isOpen) {
457 // Create formatted string, escape any XML entities in the message, and save to temporary buffer
459
460 // Only write to file if current buffer length exceeds the minimum size for flushing
461 if(m_tmpBuffer.str().length() >= (unsigned)m_minFlushSize) {
462 // Write the string to the file and flush the stream
463 m_file << m_tmpBuffer.str() << flush;
464
465 // Reset temporary buffer
466 m_tmpBuffer.str("");
467 }
468 }
469
470 // Reset internal buffer
471 str("");
472
473 // Default return value for sync()
474 return 0;
475}
MInt m_minFlushSize
Minimum length of the internal buffer before flushing.
Definition: infoout.h:49
MString m_prefixMessage
Stores the prefix that is prepended to each output.
Definition: infoout.h:50
MString m_suffixMessage
Stores the suffix that is apended to each output.
Definition: infoout.h:51
virtual MString encodeXml(const std::string &str)
Parses the string input and returns the string with XML entities escaped.
Definition: infoout.cpp:95

Member Data Documentation

◆ m_file

std::ofstream InfoOut_simpleFileBuffer::m_file
private

Definition at line 157 of file infoout.h.

◆ m_filename

MString InfoOut_simpleFileBuffer::m_filename
private

Definition at line 156 of file infoout.h.

◆ m_isOpen

MBool InfoOut_simpleFileBuffer::m_isOpen
private

Definition at line 154 of file infoout.h.

◆ m_mpiComm

MPI_Comm InfoOut_simpleFileBuffer::m_mpiComm
private

Definition at line 158 of file infoout.h.

◆ m_rootOnlyHardwired

MBool InfoOut_simpleFileBuffer::m_rootOnlyHardwired
private

Definition at line 155 of file infoout.h.


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