MOAB: Mesh Oriented datABase  (version 5.2.1)
MsqDebug.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Sandia Corporation and Argonne National
00005     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
00006     with Sandia Corporation, the U.S. Government retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023   ***************************************************************** */
00024 
00025 #include "MsqDebug.hpp"
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <stdarg.h>
00030 #include <string.h>
00031 
00032 namespace MBMesquite
00033 {
00034 
00035 /* IMPORTANT: Be careful to initialize the InitializeFlags object
00036  *LAST* as it will access the other static data. */
00037 
00038 std::vector< std::ostream* > MsqDebug::streams;
00039 std::vector< bool > MsqDebug::flags;
00040 
00041 #ifdef MSQ_ENABLE_DEBUG
00042 static void parse_flag_string( const char* str, bool flag_val )
00043 {
00044     char* s            = strdup( str );
00045     const char delim[] = ";,";
00046     for( const char* p = strtok( s, delim ); p; p = strtok( 0, delim ) )
00047     {
00048         int beg, end;
00049         switch( sscanf( p, "%u - %u", &beg, &end ) )
00050         {
00051             case 1:
00052                 end = beg;
00053                 // fall through
00054             case 2:
00055                 for( ; beg <= end; ++beg )
00056                     MsqDebug::set( beg, flag_val );
00057                 break;
00058         }
00059     }
00060     free( s );
00061 }
00062 
00063 MsqDebug::InitializeFlags::InitializeFlags()
00064 {
00065     const unsigned flag_array[] = { MSQ_ENABLE_DEBUG, 0 };
00066     size_t length               = sizeof( flag_array ) / sizeof( unsigned ) - 1;
00067     while( length > 0 )
00068         MsqDebug::set( flag_array[--length], true );
00069 
00070     const char* envstr = getenv( "MESQUITE_DEBUG" );
00071     if( envstr ) parse_flag_string( envstr, true );
00072     envstr = getenv( "MESQUITE_NO_DEBUG" );
00073     if( envstr ) parse_flag_string( envstr, false );
00074 }
00075 #else
00076 MsqDebug::InitializeFlags::InitializeFlags() {}
00077 #endif
00078 
00079 MsqDebug::InitializeFlags MsqDebug::init;
00080 
00081 bool MsqDebug::get( unsigned flag )
00082 {
00083     return flag < flags.size() && flags[flag];
00084 }
00085 
00086 void MsqDebug::set( unsigned flag, bool state )
00087 {
00088     if( state )
00089     {
00090         if( flag >= flags.size() ) { flags.resize( flag + 1 ); }
00091         flags[flag] = true;
00092     }
00093     else
00094     {
00095         if( flag < flags.size() ) flags[flag] = false;
00096     }
00097 }
00098 
00099 void MsqDebug::disable_all()
00100 {
00101     flags.clear();
00102 }
00103 
00104 std::ostream& MsqDebug::get_stream( unsigned flag )
00105 {
00106     if( flag < streams.size() )
00107         return *streams[flag];
00108     else
00109         return std::cout;
00110 }
00111 
00112 void MsqDebug::set_stream( unsigned flag, std::ostream& stream )
00113 {
00114     if( flag >= streams.size() )
00115     {
00116         size_t old_size = streams.size();
00117         streams.resize( flag );
00118         for( unsigned i = old_size; i < flag; ++i )
00119             streams[i] = &std::cout;
00120     }
00121     streams[flag] = &stream;
00122 }
00123 
00124 void MsqDebug::FormatPrinter::print( const char* format, ... ) const
00125 {
00126     if( !MsqDebug::get( myFlag ) ) return;
00127 
00128     char buffer[512];
00129 
00130 #if defined( HAVE_VSNPRINTF )
00131     va_list args;
00132     va_start( args, format );
00133     vsnprintf( buffer, sizeof( buffer ), format, args );
00134     va_end( args );
00135 #elif defined( HAVE__VSNPRINTF )
00136     va_list args;
00137     va_start( args, format );
00138     _vsnprintf( buffer, sizeof( buffer ), format, args );
00139     va_end( args );
00140 #elif defined( HAVE_VSPRINTF )
00141     va_list args;
00142     va_start( args, format );
00143     vsprintf( buffer, format, args );
00144     va_end( args );
00145 #else
00146     strncpy( buffer, format, sizeof( buffer ) );
00147     buffer[sizeof( buffer ) - 1] = '\0';
00148 #endif
00149 
00150     MsqDebug::get_stream( myFlag ) << buffer;
00151 }
00152 
00153 }  // namespace MBMesquite
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines