MOAB: Mesh Oriented datABase
(version 5.4.1)
|
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 <cstdio> 00028 #include <cstdlib> 00029 #include <cstdarg> 00030 #include <cstring> 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() ) 00091 { 00092 flags.resize( flag + 1 ); 00093 } 00094 flags[flag] = true; 00095 } 00096 else 00097 { 00098 if( flag < flags.size() ) flags[flag] = false; 00099 } 00100 } 00101 00102 void MsqDebug::disable_all() 00103 { 00104 flags.clear(); 00105 } 00106 00107 std::ostream& MsqDebug::get_stream( unsigned flag ) 00108 { 00109 if( flag < streams.size() ) 00110 return *streams[flag]; 00111 else 00112 return std::cout; 00113 } 00114 00115 void MsqDebug::set_stream( unsigned flag, std::ostream& stream ) 00116 { 00117 if( flag >= streams.size() ) 00118 { 00119 size_t old_size = streams.size(); 00120 streams.resize( flag ); 00121 for( unsigned i = old_size; i < flag; ++i ) 00122 streams[i] = &std::cout; 00123 } 00124 streams[flag] = &stream; 00125 } 00126 00127 void MsqDebug::FormatPrinter::print( const char* format, ... ) const 00128 { 00129 if( !MsqDebug::get( myFlag ) ) return; 00130 00131 char buffer[512]; 00132 00133 #if defined( HAVE_VSNPRINTF ) 00134 va_list args; 00135 va_start( args, format ); 00136 vsnprintf( buffer, sizeof( buffer ), format, args ); 00137 va_end( args ); 00138 #elif defined( HAVE__VSNPRINTF ) 00139 va_list args; 00140 va_start( args, format ); 00141 _vsnprintf( buffer, sizeof( buffer ), format, args ); 00142 va_end( args ); 00143 #elif defined( HAVE_VSPRINTF ) 00144 va_list args; 00145 va_start( args, format ); 00146 vsprintf( buffer, format, args ); 00147 va_end( args ); 00148 #else 00149 strncpy( buffer, format, sizeof( buffer ) ); 00150 buffer[sizeof( buffer ) - 1] = '\0'; 00151 #endif 00152 00153 MsqDebug::get_stream( myFlag ) << buffer; 00154 } 00155 00156 } // namespace MBMesquite