moab
moab::DebugOutput Class Reference

Utility class for printing debug output. More...

#include <DebugOutput.hpp>

List of all members.

Public Member Functions

 DebugOutput (DebugOutputStream *str, unsigned verbosity=0)
 DebugOutput (DebugOutputStream *str, int rank, unsigned verbosity=0)
 DebugOutput (FILE *str, unsigned verbosity=0)
 DebugOutput (FILE *str, int rank, unsigned verbosity=0)
 DebugOutput (std::ostream &str, unsigned verbosity=0)
 DebugOutput (std::ostream &str, int rank, unsigned verbosity=0)
 DebugOutput (const char *pfx, DebugOutputStream *str, unsigned verbosity=0)
 DebugOutput (const char *pfx, DebugOutputStream *str, int rank, unsigned verbosity=0)
 DebugOutput (const char *pfx, FILE *str, unsigned verbosity=0)
 DebugOutput (const char *pfx, FILE *str, int rank, unsigned verbosity=0)
 DebugOutput (const char *pfx, std::ostream &str, unsigned verbosity=0)
 DebugOutput (const char *pfx, std::ostream &str, int rank, unsigned verbosity=0)
 DebugOutput (const DebugOutput &copy)
DebugOutputoperator= (const DebugOutput &copy)
 ~DebugOutput ()
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 proccess in MPI_COMM_WORLD, or zero if MOAB is build w/out MPI.
void limit_output_to_first_N_procs (int N)
 Only print debug output from N processes.
unsigned get_verbosity () const
 Get verbosity limit.
void set_verbosity (unsigned val)
 Set verbosity limit.
const std::string & get_prefix () const
 Get line prefix.
void set_prefix (const std::string &str)
 Set line prefix.
void print (int verbosity, const char *str)
 Output the specified string iff output is enabled.
void print (int verbosity, const std::string &str)
 Output the specified string iff output is enabled.
void printf (int verbosity, const char *fmt,...) MB_PRINTF(2)
 Output the specified printf-formatted output iff output is enabled.
void tprint (int verbosity, const char *str)
 Output the specified string iff output is enabled.
void tprint (int verbosity, const std::string &str)
 Output the specified string iff output is enabled.
void tprintf (int verbosity, const char *fmt,...) MB_PRINTF(2)
 Output the specified printf-formatted output iff output is enabled.
void print (int verbosity, const char *pfx, const Range &range)
 Print the contents of a moab::Range.
void print (int verbosity, const Range &range)
 Print the contents of a moab::Range.
void print_ints (int verbosity, const char *pfx, const Range &range)
 Print the contents of a moab::Range as numerical values only.
void print_ints (int verbosity, const Range &range)
 Print the contents of a moab::Range as numerical values only.

Private Member Functions

void tprint ()
void list_range_real (const char *pfx, const Range &range)
void list_ints_real (const char *pfx, const Range &range)
void print_real (const char *buffer)
void print_real (const std::string &str)
void tprint_real (const char *buffer)
void tprint_real (const std::string &str)
void print_real (const char *buffer, va_list args1, va_list args2)
void tprint_real (const char *buffer, va_list args1, va_list args2)
void process_line_buffer ()
bool check (unsigned verbosity)

Private Attributes

std::string linePfx
DebugOutputStreamoutputImpl
int mpiRank
unsigned verbosityLimit
double initTime
std::vector< char > lineBuffer

Detailed Description

Utility class for printing debug 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.

This class also implements a verbosity filter for all output. The class instance has a verbosity limit. Each request for output has an associated verbosity level. If the verbosity level for the output is is less greater than the limit then the output is discarded. By convetion a verbosity limit of zero should indicate no output. Therefore all requests for output should have an associated verbosity level greater than or equal to one.

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. C++-style IO (i.e. std::ostream) is not supported because it is necessarily inefficient for debug-type output. All formatting (e.g. converting arguments to strings, etc.) must be done even when output is disabled.

Definition at line 41 of file DebugOutput.hpp.


Constructor & Destructor Documentation

moab::DebugOutput::DebugOutput ( DebugOutputStream str,
unsigned  verbosity = 0 
)
Parameters:
strOutput stream to which to flush output
verbosityVerbosity limit.

Definition at line 65 of file DebugOutput.cpp.

  : outputImpl(impl), mpiRank(-1), verbosityLimit(verbosity), initTime(CURTIME)
    { impl->referenceCount++; assert(impl->referenceCount > 1); }
moab::DebugOutput::DebugOutput ( DebugOutputStream str,
int  rank,
unsigned  verbosity = 0 
)
Parameters:
strOutput stream to which to flush output
rankMPI rank with which to prefix output.
verbosityVerbosity limit.

Definition at line 68 of file DebugOutput.cpp.

  : outputImpl(impl), mpiRank(rank), verbosityLimit(verbosity), initTime(CURTIME)
    { impl->referenceCount++; assert(impl->referenceCount > 1); }
moab::DebugOutput::DebugOutput ( FILE *  str,
unsigned  verbosity = 0 
)
Parameters:
strOutput stream to which to flush output
enabledEnable output: if not true, all output operations to nothing.

Definition at line 71 of file DebugOutput.cpp.

  : outputImpl(new FILEDebugStream(impl)),
    mpiRank(-1), verbosityLimit(verbosity), initTime(CURTIME) {}
moab::DebugOutput::DebugOutput ( FILE *  str,
int  rank,
unsigned  verbosity = 0 
)
Parameters:
strOutput stream to which to flush output
rankMPI rank with which to prefix output.
verbosityVerbosity limit.

Definition at line 74 of file DebugOutput.cpp.

  : outputImpl(new FILEDebugStream(impl)),
    mpiRank(rank), verbosityLimit(verbosity), initTime(CURTIME) {}
moab::DebugOutput::DebugOutput ( std::ostream &  str,
unsigned  verbosity = 0 
)
Parameters:
strOutput stream to which to flush output
verbosityVerbosity limit.

Definition at line 77 of file DebugOutput.cpp.

  : outputImpl(new CxxDebugStream(str)),
    mpiRank(-1), verbosityLimit(verbosity), initTime(CURTIME) {}
moab::DebugOutput::DebugOutput ( std::ostream &  str,
int  rank,
unsigned  verbosity = 0 
)
Parameters:
strOutput stream to which to flush output
rankMPI rank with which to prefix output.
verbosityVerbosity limit.

Definition at line 80 of file DebugOutput.cpp.

  : outputImpl(new CxxDebugStream(str)),
    mpiRank(rank), verbosityLimit(verbosity), initTime(CURTIME) {}
moab::DebugOutput::DebugOutput ( const char *  pfx,
DebugOutputStream str,
unsigned  verbosity = 0 
)
Parameters:
pfxPrefix for output
strOutput stream to which to flush output
verbosityVerbosity limit.

Definition at line 83 of file DebugOutput.cpp.

  : linePfx(pfx), outputImpl(impl), mpiRank(-1), verbosityLimit(verbosity) , initTime(CURTIME)
  { impl->referenceCount++; assert(impl->referenceCount > 1); }
moab::DebugOutput::DebugOutput ( const char *  pfx,
DebugOutputStream str,
int  rank,
unsigned  verbosity = 0 
)
Parameters:
pfxPrefix for output
strOutput stream to which to flush output
rankMPI rank with which to prefix output.
verbosityVerbosity limit.

Definition at line 86 of file DebugOutput.cpp.

  : linePfx(pfx), outputImpl(impl), mpiRank(rank), verbosityLimit(verbosity), initTime(CURTIME)
  { impl->referenceCount++; assert(impl->referenceCount > 1); }
moab::DebugOutput::DebugOutput ( const char *  pfx,
FILE *  str,
unsigned  verbosity = 0 
)
Parameters:
pfxPrefix for output
strOutput stream to which to flush output
enabledEnable output: if not true, all output operations to nothing.

Definition at line 89 of file DebugOutput.cpp.

  : linePfx(pfx), outputImpl(new FILEDebugStream(impl)),
    mpiRank(-1), verbosityLimit(verbosity), initTime(CURTIME) {}
moab::DebugOutput::DebugOutput ( const char *  pfx,
FILE *  str,
int  rank,
unsigned  verbosity = 0 
)
Parameters:
pfxPrefix for output
strOutput stream to which to flush output
rankMPI rank with which to prefix output.
verbosityVerbosity limit.

Definition at line 92 of file DebugOutput.cpp.

  : linePfx(pfx), outputImpl(new FILEDebugStream(impl)),
    mpiRank(rank), verbosityLimit(verbosity), initTime(CURTIME) {}
moab::DebugOutput::DebugOutput ( const char *  pfx,
std::ostream &  str,
unsigned  verbosity = 0 
)
Parameters:
pfxPrefix for output
strOutput stream to which to flush output
verbosityVerbosity limit.

Definition at line 95 of file DebugOutput.cpp.

  : linePfx(pfx), outputImpl(new CxxDebugStream(str)),
    mpiRank(-1), verbosityLimit(verbosity), initTime(CURTIME) {}
moab::DebugOutput::DebugOutput ( const char *  pfx,
std::ostream &  str,
int  rank,
unsigned  verbosity = 0 
)
Parameters:
pfxPrefix for output
strOutput stream to which to flush output
rankMPI rank with which to prefix output.
verbosityVerbosity limit.

Definition at line 98 of file DebugOutput.cpp.

  : linePfx(pfx), outputImpl(new CxxDebugStream(str)),
    mpiRank(rank), verbosityLimit(verbosity), initTime(CURTIME) {}

Definition at line 102 of file DebugOutput.cpp.

  : linePfx(copy.linePfx), 
    outputImpl(copy.outputImpl),
    mpiRank(copy.mpiRank),
    verbosityLimit(copy.verbosityLimit)
{
  outputImpl->referenceCount++; 
  assert(outputImpl->referenceCount > 1); 
}

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

Definition at line 123 of file DebugOutput.cpp.

{ 
  if (!lineBuffer.empty()) {
    lineBuffer.push_back('\n');
    process_line_buffer();
  }
  if (outputImpl) {
    assert(outputImpl->referenceCount > 0);
    if (!--outputImpl->referenceCount)
      delete outputImpl;
    outputImpl = 0;
  }
}

Member Function Documentation

bool moab::DebugOutput::check ( unsigned  verbosity) [inline, private]

Definition at line 224 of file DebugOutput.hpp.

    { return verbosity <= verbosityLimit; }
const std::string& moab::DebugOutput::get_prefix ( ) const [inline]

Get line prefix.

Definition at line 148 of file DebugOutput.hpp.

{ return linePfx; }
int moab::DebugOutput::get_rank ( ) const [inline]

Get MPI rank.

Definition at line 131 of file DebugOutput.hpp.

{ return mpiRank; }
unsigned moab::DebugOutput::get_verbosity ( ) const [inline]

Get verbosity limit.

Definition at line 143 of file DebugOutput.hpp.

{ return verbosityLimit; }
bool moab::DebugOutput::have_rank ( ) const [inline]

Check if MPI rank has been set.

Definition at line 129 of file DebugOutput.hpp.

{ return mpiRank >= 0; }

Only print debug output from N processes.

Definition at line 139 of file DebugOutput.hpp.

    { if (mpiRank >= N) verbosityLimit = 0; }
void moab::DebugOutput::list_ints_real ( const char *  pfx,
const Range range 
) [private]

Definition at line 279 of file DebugOutput.cpp.

{
  if (range.empty()) {
    print_real("<empty>\n");
    return;
  }

  if (pfx) {
    lineBuffer.insert( lineBuffer.end(), pfx, pfx+strlen(pfx) );
    lineBuffer.push_back(' ');
  }

  char numbuf[48]; // unsigned 64 bit integer can't have more than 20 decimal digits
  Range::const_pair_iterator i;
  for (i = range.const_pair_begin(); i != range.const_pair_end(); ++i) {
    if (i->first == i->second) 
      sprintf(numbuf, " %lu,", (unsigned long)(i->first));
    else
      print_range(numbuf, (unsigned long)(i->first), (unsigned long)(i->second));
    lineBuffer.insert( lineBuffer.end(), numbuf, numbuf+strlen(numbuf) );
  }

  lineBuffer.push_back('\n');
  process_line_buffer();
}
void moab::DebugOutput::list_range_real ( const char *  pfx,
const Range range 
) [private]

Definition at line 247 of file DebugOutput.cpp.

{
  if (pfx) {
    lineBuffer.insert( lineBuffer.end(), pfx, pfx+strlen(pfx) );
    lineBuffer.push_back(' ');
  }

  if (range.empty()) {
    print_real("<empty>\n");
    return;
  }

  char numbuf[48]; // unsigned 64 bit integer can't have more than 20 decimal digits
  Range::const_pair_iterator i;
  EntityType type = MBMAXTYPE;
  for (i = range.const_pair_begin(); i != range.const_pair_end(); ++i) {
    if (TYPE_FROM_HANDLE(i->first) != type) {
      type = TYPE_FROM_HANDLE(i->first);
      const char* name = CN::EntityTypeName(type);
      lineBuffer.insert( lineBuffer.end(), name, name+strlen(name) );
    }
    if (i->first == i->second) 
      sprintf(numbuf, " %lu,", (unsigned long)(ID_FROM_HANDLE(i->first)));
    else
      print_range(numbuf, ID_FROM_HANDLE(i->first), ID_FROM_HANDLE(i->second) );
    lineBuffer.insert( lineBuffer.end(), numbuf, numbuf+strlen(numbuf) );
  }

  lineBuffer.push_back('\n');
  process_line_buffer();
}
DebugOutput & moab::DebugOutput::operator= ( const DebugOutput copy)

Definition at line 112 of file DebugOutput.cpp.

{
  linePfx = copy.linePfx;
  outputImpl = copy.outputImpl;
  mpiRank = copy.mpiRank;
  verbosityLimit = copy.verbosityLimit;
  outputImpl->referenceCount++; 
  assert(outputImpl->referenceCount > 1); 
  return *this;
}
void moab::DebugOutput::print ( int  verbosity,
const char *  str 
) [inline]

Output the specified string iff output is enabled.

Definition at line 153 of file DebugOutput.hpp.

    { if (check(verbosity)) print_real(str); }
void moab::DebugOutput::print ( int  verbosity,
const std::string &  str 
) [inline]

Output the specified string iff output is enabled.

Definition at line 157 of file DebugOutput.hpp.

    { if (check(verbosity)) print_real(str); }
void moab::DebugOutput::print ( int  verbosity,
const char *  pfx,
const Range range 
) [inline]

Print the contents of a moab::Range.

Parameters:
pfxString to print after default class prefix and before range contents

Definition at line 183 of file DebugOutput.hpp.

    { if (check(verbosity)) list_range_real( pfx, range ); }
void moab::DebugOutput::print ( int  verbosity,
const Range range 
) [inline]

Print the contents of a moab::Range.

Definition at line 186 of file DebugOutput.hpp.

    { if (check(verbosity)) list_range_real( 0, range ); }
void moab::DebugOutput::print_ints ( int  verbosity,
const char *  pfx,
const Range range 
) [inline]

Print the contents of a moab::Range as numerical values only.

Parameters:
pfxString to print after default class prefix and before range contents

Definition at line 191 of file DebugOutput.hpp.

    { if (check(verbosity)) list_ints_real( pfx, range ); }
void moab::DebugOutput::print_ints ( int  verbosity,
const Range range 
) [inline]

Print the contents of a moab::Range as numerical values only.

Definition at line 194 of file DebugOutput.hpp.

    { if (check(verbosity)) list_ints_real( 0, range ); }
void moab::DebugOutput::print_real ( const char *  buffer) [private]

Definition at line 145 of file DebugOutput.cpp.

{
  lineBuffer.insert( lineBuffer.end(), buffer, buffer + strlen(buffer) );
  process_line_buffer();
}
void moab::DebugOutput::print_real ( const std::string &  str) [private]

Definition at line 157 of file DebugOutput.cpp.

{
  lineBuffer.insert( lineBuffer.end(), str.begin(), str.end() );
  process_line_buffer();
}
void moab::DebugOutput::print_real ( const char *  buffer,
va_list  args1,
va_list  args2 
) [private]

Definition at line 169 of file DebugOutput.cpp.

{
  size_t idx = lineBuffer.size();
#ifdef 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 81 characters per formatted argument.
  unsigned exp_size = 27*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::DebugOutput::printf ( int  verbosity,
const char *  fmt,
  ... 
) [inline]

Output the specified printf-formatted output iff output is enabled.

Definition at line 240 of file DebugOutput.hpp.

{
  if (check(verbosity)) {
    va_list args1, args2;
    va_start(args1, fmt);
    va_start(args2, fmt);
    print_real(fmt, args1, args2);
    va_end(args2);
    va_end(args1);
  }
}

Definition at line 305 of file DebugOutput.cpp.

{
  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(), linePfx.c_str(), &lineBuffer[last_idx] );
    else
      outputImpl->println( linePfx.c_str(), &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::DebugOutput::set_prefix ( const std::string &  str) [inline]

Set line prefix.

Definition at line 150 of file DebugOutput.hpp.

{ linePfx = str; }
void moab::DebugOutput::set_rank ( int  rank) [inline]

Set MPI rank.

Definition at line 133 of file DebugOutput.hpp.

{ mpiRank  = rank; }
void moab::DebugOutput::set_verbosity ( unsigned  val) [inline]

Set verbosity limit.

Definition at line 145 of file DebugOutput.hpp.

{ verbosityLimit = val; }
void moab::DebugOutput::tprint ( int  verbosity,
const char *  str 
) [inline]

Output the specified string iff output is enabled.

Include current CPU time (as returned by clock()) in output.

Definition at line 166 of file DebugOutput.hpp.

    { if (check(verbosity)) tprint_real(str); }
void moab::DebugOutput::tprint ( int  verbosity,
const std::string &  str 
) [inline]

Output the specified string iff output is enabled.

Include current CPU time (as returned by clock()) in output.

Definition at line 172 of file DebugOutput.hpp.

    { if (check(verbosity)) tprint_real(str); }
void moab::DebugOutput::tprint ( ) [private]

Definition at line 326 of file DebugOutput.cpp.

{
  size_t s = lineBuffer.size();
  lineBuffer.resize( s + 64 );
  size_t ss = sprintf(&lineBuffer[s],"(%.2f s) ", CURTIME-initTime );
  lineBuffer.resize( s + ss );
}
void moab::DebugOutput::tprint_real ( const char *  buffer) [private]

Definition at line 151 of file DebugOutput.cpp.

{
  tprint();
  print_real( buffer );
}
void moab::DebugOutput::tprint_real ( const std::string &  str) [private]

Definition at line 163 of file DebugOutput.cpp.

{
  tprint();
  print_real( str );
}
void moab::DebugOutput::tprint_real ( const char *  buffer,
va_list  args1,
va_list  args2 
) [private]

Definition at line 206 of file DebugOutput.cpp.

{
  tprint();
  print_real( fmt, args1, args2 );
}
void moab::DebugOutput::tprintf ( int  verbosity,
const char *  fmt,
  ... 
) [inline]

Output the specified printf-formatted output iff output is enabled.

Include current CPU time (as returned by clock()) in output.

Definition at line 252 of file DebugOutput.hpp.

{
  if (check(verbosity)) {
    va_list args1, args2;
    va_start(args1, fmt);
    va_start(args2, fmt);
    tprint_real(fmt, args1, args2);
    va_end(args2);
    va_end(args1);
  }
}

Set MPI rank to the rank of this proccess in MPI_COMM_WORLD, or zero if MOAB is build w/out MPI.

Definition at line 137 of file DebugOutput.cpp.

{
  mpiRank = 0;
#ifdef USE_MPI
  MPI_Comm_rank( MPI_COMM_WORLD, &mpiRank );
#endif
}   

Member Data Documentation

double moab::DebugOutput::initTime [private]

Definition at line 203 of file DebugOutput.hpp.

std::vector<char> moab::DebugOutput::lineBuffer [private]

Definition at line 222 of file DebugOutput.hpp.

std::string moab::DebugOutput::linePfx [private]

Definition at line 199 of file DebugOutput.hpp.

Definition at line 201 of file DebugOutput.hpp.

Definition at line 200 of file DebugOutput.hpp.

Definition at line 202 of file DebugOutput.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines