MOAB: Mesh Oriented datABase
(version 5.4.1)
|
Utility class for printing error output. More...
#include <ErrorOutput.hpp>
Public Member Functions | |
ErrorOutput (FILE *str) | |
ErrorOutput (std::ostream &str) | |
~ErrorOutput () | |
bool | have_rank () const |
Check if MPI rank has been set. | |
int | get_rank () const |
Get MPI rank. | |
void | set_rank (int rank) |
Set MPI rank. | |
void | use_world_rank () |
Set MPI rank to the rank of this process in MPI_COMM_WORLD, if MOAB is built with MPI and MPI_Init has been called. | |
void | print (const char *str) |
Output the specified string. | |
void | print (const std::string &str) |
Output the specified string. | |
void | printf (const char *fmt,...) MB_PRINTF(1) |
Output the specified printf-formatted output. | |
Private Member Functions | |
void | print_real (const char *buffer) |
void | print_real (const std::string &str) |
void | print_real (const char *buffer, va_list args1, va_list args2) |
void | process_line_buffer () |
Private Attributes | |
ErrorOutputStream * | outputImpl |
int | mpiRank |
std::vector< char > | lineBuffer |
Utility class for printing error output.
This class implements line-oriented output. That is, it buffers output data until a newline is encountered, at which point it sends the output to the output stream followed by an explicit flush, and optionally prefixed with the MPI rank.
Any output not terminated with an newline character or followed by later output containing a newline character will not be flushed until the destructor is invoked.
Definition at line 28 of file ErrorOutput.hpp.
moab::ErrorOutput::ErrorOutput | ( | FILE * | str | ) |
str | Output stream to which to flush output |
Definition at line 62 of file ErrorOutput.cpp.
References lineBuffer.
: outputImpl( new FILEErrorStream( impl ) ), mpiRank( -1 ) { lineBuffer.reserve( 1024 ); }
moab::ErrorOutput::ErrorOutput | ( | std::ostream & | str | ) |
str | Output stream to which to flush output |
Definition at line 67 of file ErrorOutput.cpp.
References lineBuffer.
: outputImpl( new CxxErrorStream( str ) ), mpiRank( -1 ) { lineBuffer.reserve( 1024 ); }
Destructor flushes any remaining output that wasn't followed by a newline character.
Definition at line 72 of file ErrorOutput.cpp.
References lineBuffer, outputImpl, and process_line_buffer().
{ if( !lineBuffer.empty() ) { lineBuffer.push_back( '\n' ); process_line_buffer(); } if( NULL != outputImpl ) { delete outputImpl; outputImpl = NULL; } }
int moab::ErrorOutput::get_rank | ( | ) | const [inline] |
Get MPI rank.
Definition at line 53 of file ErrorOutput.hpp.
References mpiRank.
Referenced by moab::MBTraceBackErrorHandler(), and process_line_buffer().
{ return mpiRank; }
bool moab::ErrorOutput::have_rank | ( | ) | const [inline] |
Check if MPI rank has been set.
Definition at line 48 of file ErrorOutput.hpp.
References mpiRank.
Referenced by moab::MBTraceBackErrorHandler(), and process_line_buffer().
{ return mpiRank >= 0; }
void moab::ErrorOutput::print | ( | const char * | str | ) | [inline] |
Output the specified string.
Definition at line 67 of file ErrorOutput.hpp.
References print_real().
Referenced by moab::MBTraceBackErrorHandler().
{ print_real( str ); }
void moab::ErrorOutput::print | ( | const std::string & | str | ) | [inline] |
Output the specified string.
Definition at line 73 of file ErrorOutput.hpp.
References print_real().
{ print_real( str ); }
void moab::ErrorOutput::print_real | ( | const char * | buffer | ) | [private] |
Definition at line 98 of file ErrorOutput.cpp.
References buffer, lineBuffer, and process_line_buffer().
Referenced by print(), and printf().
{ lineBuffer.insert( lineBuffer.end(), buffer, buffer + strlen( buffer ) ); process_line_buffer(); }
void moab::ErrorOutput::print_real | ( | const std::string & | str | ) | [private] |
Definition at line 104 of file ErrorOutput.cpp.
References lineBuffer, and process_line_buffer().
{ lineBuffer.insert( lineBuffer.end(), str.begin(), str.end() ); process_line_buffer(); }
void moab::ErrorOutput::print_real | ( | const char * | buffer, |
va_list | args1, | ||
va_list | args2 | ||
) | [private] |
Definition at line 110 of file ErrorOutput.cpp.
References lineBuffer, process_line_buffer(), and size.
{ size_t idx = lineBuffer.size(); #ifdef MOAB_HAVE_VSNPRINTF // try once with remaining space in buffer lineBuffer.resize( lineBuffer.capacity() ); unsigned size = vsnprintf( &lineBuffer[idx], lineBuffer.size() - idx, fmt, args1 ); ++size; // trailing null // if necessary, increase buffer size and retry if( size > ( lineBuffer.size() - idx ) ) { lineBuffer.resize( idx + size ); size = vsnprintf( &lineBuffer[idx], lineBuffer.size() - idx, fmt, args2 ); ++size; // trailing null } #else // Guess how much space might be required. // If every character is a format code then there are len/3 format codes. // Guess a random large value of num_chars characters per formatted argument. const unsigned num_chars = 180; unsigned exp_size = ( num_chars / 3 ) * strlen( fmt ); lineBuffer.resize( idx + exp_size ); unsigned size = vsprintf( &lineBuffer[idx], fmt, args1 ); ++size; // trailing null // check if we overflowed the buffer if( size > exp_size ) { // crap! fprintf( stderr, "ERROR: Buffer overflow at %s:%d\n", __FILE__, __LINE__ ); lineBuffer.resize( idx + exp_size ); size = vsprintf( &lineBuffer[idx], fmt, args2 ); ++size; // trailing null } #endif // less one because we don't want the trailing '\0' lineBuffer.resize( idx + size - 1 ); process_line_buffer(); }
void moab::ErrorOutput::printf | ( | const char * | fmt, |
... | |||
) | [inline] |
Output the specified printf-formatted output.
Definition at line 98 of file ErrorOutput.hpp.
References print_real().
Referenced by moab::MBTraceBackErrorHandler().
{ va_list args1, args2; va_start( args1, fmt ); va_start( args2, fmt ); print_real( fmt, args1, args2 ); va_end( args2 ); va_end( args1 ); }
void moab::ErrorOutput::process_line_buffer | ( | ) | [private] |
Definition at line 150 of file ErrorOutput.cpp.
References get_rank(), have_rank(), lineBuffer, outputImpl, and moab::ErrorOutputStream::println().
Referenced by print_real(), and ~ErrorOutput().
{ size_t last_idx = 0; std::vector< char >::iterator i; for( i = std::find( lineBuffer.begin(), lineBuffer.end(), '\n' ); i != lineBuffer.end(); i = std::find( i, lineBuffer.end(), '\n' ) ) { *i = '\0'; if( have_rank() ) outputImpl->println( get_rank(), &lineBuffer[last_idx] ); else outputImpl->println( &lineBuffer[last_idx] ); ++i; last_idx = i - lineBuffer.begin(); } if( last_idx ) { i = std::copy( lineBuffer.begin() + last_idx, lineBuffer.end(), lineBuffer.begin() ); lineBuffer.erase( i, lineBuffer.end() ); } }
void moab::ErrorOutput::set_rank | ( | int | rank | ) | [inline] |
void moab::ErrorOutput::use_world_rank | ( | ) |
Set MPI rank to the rank of this process in MPI_COMM_WORLD, if MOAB is built with MPI and MPI_Init has been called.
Definition at line 87 of file ErrorOutput.cpp.
References MPI_COMM_WORLD, and mpiRank.
Referenced by moab::MBErrorHandler_Init().
{ #ifdef MOAB_HAVE_MPI int flag1; MPI_Initialized( &flag1 ); int flag2; MPI_Finalized( &flag2 ); if( flag1 && !flag2 ) MPI_Comm_rank( MPI_COMM_WORLD, &mpiRank ); #endif }
std::vector< char > moab::ErrorOutput::lineBuffer [private] |
Definition at line 95 of file ErrorOutput.hpp.
Referenced by ErrorOutput(), print_real(), process_line_buffer(), and ~ErrorOutput().
int moab::ErrorOutput::mpiRank [private] |
Definition at line 83 of file ErrorOutput.hpp.
Referenced by get_rank(), have_rank(), set_rank(), and use_world_rank().
ErrorOutputStream* moab::ErrorOutput::outputImpl [private] |
Definition at line 82 of file ErrorOutput.hpp.
Referenced by process_line_buffer(), and ~ErrorOutput().