Mesh Oriented datABase  (version 5.5.1)
An array-based unstructured mesh library
moab::ErrorOutput Class Reference

Utility class for printing error output. More...

#include <ErrorOutput.hpp>

+ Collaboration diagram for moab::ErrorOutput:

Public Member Functions

 ErrorOutput (FILE *str)
 
 ErrorOutput (std::ostream &str)
 
 ~ErrorOutput ()
 
bool have_rank () const
 Check if MPI rank has been set. More...
 
int get_rank () const
 Get MPI rank. More...
 
void set_rank (int rank)
 Set MPI rank. More...
 
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. More...
 
void print (const char *str)
 Output the specified string. More...
 
void print (const std::string &str)
 Output the specified string. More...
 
void printf (const char *fmt,...) MB_PRINTF(1)
 Output the specified printf-formatted output. More...
 

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

ErrorOutputStreamoutputImpl
 
int mpiRank
 
std::vector< char > lineBuffer
 

Detailed Description

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.

\Note 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.

Constructor & Destructor Documentation

◆ ErrorOutput() [1/2]

moab::ErrorOutput::ErrorOutput ( FILE *  str)
Parameters
strOutput stream to which to flush output

Definition at line 62 of file ErrorOutput.cpp.

62  : outputImpl( new FILEErrorStream( impl ) ), mpiRank( -1 )
63 {
64  lineBuffer.reserve( 1024 );
65 }

References lineBuffer.

◆ ErrorOutput() [2/2]

moab::ErrorOutput::ErrorOutput ( std::ostream &  str)
Parameters
strOutput stream to which to flush output

Definition at line 67 of file ErrorOutput.cpp.

67  : outputImpl( new CxxErrorStream( str ) ), mpiRank( -1 )
68 {
69  lineBuffer.reserve( 1024 );
70 }

References lineBuffer.

◆ ~ErrorOutput()

moab::ErrorOutput::~ErrorOutput ( )

Destructor flushes any remaining output that wasn't followed by a newline character.

Definition at line 72 of file ErrorOutput.cpp.

73 {
74  if( !lineBuffer.empty() )
75  {
76  lineBuffer.push_back( '\n' );
78  }
79 
80  if( NULL != outputImpl )
81  {
82  delete outputImpl;
83  outputImpl = NULL;
84  }
85 }

References lineBuffer, outputImpl, and process_line_buffer().

Member Function Documentation

◆ get_rank()

int moab::ErrorOutput::get_rank ( ) const
inline

Get MPI rank.

Definition at line 53 of file ErrorOutput.hpp.

54  {
55  return mpiRank;
56  }

References mpiRank.

Referenced by moab::MBTraceBackErrorHandler(), and process_line_buffer().

◆ have_rank()

bool moab::ErrorOutput::have_rank ( ) const
inline

Check if MPI rank has been set.

Definition at line 48 of file ErrorOutput.hpp.

49  {
50  return mpiRank >= 0;
51  }

References mpiRank.

Referenced by moab::MBTraceBackErrorHandler(), and process_line_buffer().

◆ print() [1/2]

void moab::ErrorOutput::print ( const char *  str)
inline

Output the specified string.

Definition at line 67 of file ErrorOutput.hpp.

68  {
69  print_real( str );
70  }

References print_real().

Referenced by moab::MBTraceBackErrorHandler().

◆ print() [2/2]

void moab::ErrorOutput::print ( const std::string &  str)
inline

Output the specified string.

Definition at line 73 of file ErrorOutput.hpp.

74  {
75  print_real( str );
76  }

References print_real().

◆ print_real() [1/3]

void moab::ErrorOutput::print_real ( const char *  buffer)
private

Definition at line 98 of file ErrorOutput.cpp.

99 {
100  lineBuffer.insert( lineBuffer.end(), buffer, buffer + strlen( buffer ) );
102 }

References buffer, lineBuffer, and process_line_buffer().

Referenced by print(), and printf().

◆ print_real() [2/3]

void moab::ErrorOutput::print_real ( const char *  buffer,
va_list  args1,
va_list  args2 
)
private

Definition at line 110 of file ErrorOutput.cpp.

111 {
112  size_t idx = lineBuffer.size();
113  // try once with remaining space in buffer
114  lineBuffer.resize( lineBuffer.capacity() );
115  unsigned size = vsnprintf( &lineBuffer[idx], lineBuffer.size() - idx, fmt, args1 );
116  ++size; // trailing null
117  // if necessary, increase buffer size and retry
118  if( size > ( lineBuffer.size() - idx ) )
119  {
120  lineBuffer.resize( idx + size );
121  size = vsnprintf( &lineBuffer[idx], lineBuffer.size() - idx, fmt, args2 );
122  ++size; // trailing null
123  }
124 
125  // less one because we don't want the trailing '\0'
126  lineBuffer.resize( idx + size - 1 );
128 }

References lineBuffer, process_line_buffer(), and size.

◆ print_real() [3/3]

void moab::ErrorOutput::print_real ( const std::string &  str)
private

Definition at line 104 of file ErrorOutput.cpp.

105 {
106  lineBuffer.insert( lineBuffer.end(), str.begin(), str.end() );
108 }

References lineBuffer, and process_line_buffer().

◆ printf()

void moab::ErrorOutput::printf ( const char *  fmt,
  ... 
)
inline

Output the specified printf-formatted output.

Definition at line 98 of file ErrorOutput.hpp.

99 {
100  va_list args1, args2;
101  va_start( args1, fmt );
102  va_start( args2, fmt );
103  print_real( fmt, args1, args2 );
104  va_end( args2 );
105  va_end( args1 );
106 }

References print_real().

Referenced by moab::MBTraceBackErrorHandler().

◆ process_line_buffer()

void moab::ErrorOutput::process_line_buffer ( )
private

Definition at line 130 of file ErrorOutput.cpp.

131 {
132  size_t last_idx = 0;
133  std::vector< char >::iterator i;
134  for( i = std::find( lineBuffer.begin(), lineBuffer.end(), '\n' ); i != lineBuffer.end();
135  i = std::find( i, lineBuffer.end(), '\n' ) )
136  {
137  *i = '\0';
138  if( have_rank() )
139  outputImpl->println( get_rank(), &lineBuffer[last_idx] );
140  else
141  outputImpl->println( &lineBuffer[last_idx] );
142  ++i;
143  last_idx = i - lineBuffer.begin();
144  }
145 
146  if( last_idx )
147  {
148  i = std::copy( lineBuffer.begin() + last_idx, lineBuffer.end(), lineBuffer.begin() );
149  lineBuffer.erase( i, lineBuffer.end() );
150  }
151 }

References get_rank(), have_rank(), lineBuffer, outputImpl, and moab::ErrorOutputStream::println().

Referenced by print_real(), and ~ErrorOutput().

◆ set_rank()

void moab::ErrorOutput::set_rank ( int  rank)
inline

Set MPI rank.

Definition at line 58 of file ErrorOutput.hpp.

59  {
60  mpiRank = rank;
61  }

References mpiRank.

◆ use_world_rank()

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.

88 {
89 #ifdef MOAB_HAVE_MPI
90  int flag1;
91  MPI_Initialized( &flag1 );
92  int flag2;
93  MPI_Finalized( &flag2 );
94  if( flag1 && !flag2 ) MPI_Comm_rank( MPI_COMM_WORLD, &mpiRank );
95 #endif
96 }

References mpiRank.

Referenced by moab::MBErrorHandler_Init().

Member Data Documentation

◆ lineBuffer

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().

◆ mpiRank

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().

◆ outputImpl

ErrorOutputStream* moab::ErrorOutput::outputImpl
private

Definition at line 82 of file ErrorOutput.hpp.

Referenced by process_line_buffer(), and ~ErrorOutput().


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