LCOV - code coverage report
Current view: top level - src/mesquite/Misc - MsqDebug.cpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 5 33 15.2 %
Date: 2020-07-18 00:09:26 Functions: 3 9 33.3 %
Branches: 2 34 5.9 %

           Branch data     Line data    Source code
       1                 :            : /* *****************************************************************
       2                 :            :     MESQUITE -- The Mesh Quality Improvement Toolkit
       3                 :            : 
       4                 :            :     Copyright 2004 Sandia Corporation and Argonne National
       5                 :            :     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
       6                 :            :     with Sandia Corporation, the U.S. Government retains certain
       7                 :            :     rights in this software.
       8                 :            : 
       9                 :            :     This library is free software; you can redistribute it and/or
      10                 :            :     modify it under the terms of the GNU Lesser General Public
      11                 :            :     License as published by the Free Software Foundation; either
      12                 :            :     version 2.1 of the License, or (at your option) any later version.
      13                 :            : 
      14                 :            :     This library is distributed in the hope that it will be useful,
      15                 :            :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :            :     Lesser General Public License for more details.
      18                 :            : 
      19                 :            :     You should have received a copy of the GNU Lesser General Public License
      20                 :            :     (lgpl.txt) along with this library; if not, write to the Free Software
      21                 :            :     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      22                 :            : 
      23                 :            :   ***************************************************************** */
      24                 :            : 
      25                 :            : #include "MsqDebug.hpp"
      26                 :            : 
      27                 :            : #include <stdio.h>
      28                 :            : #include <stdlib.h>
      29                 :            : #include <stdarg.h>
      30                 :            : #include <string.h>
      31                 :            : 
      32                 :            : namespace MBMesquite
      33                 :            : {
      34                 :            : 
      35                 :            : /* IMPORTANT: Be careful to initialize the InitializeFlags object
      36                 :            :  *LAST* as it will access the other static data. */
      37                 :            : 
      38                 :         30 : std::vector< std::ostream* > MsqDebug::streams;
      39                 :         30 : std::vector< bool > MsqDebug::flags;
      40                 :            : 
      41                 :            : #ifdef MSQ_ENABLE_DEBUG
      42                 :            : static void parse_flag_string( const char* str, bool flag_val )
      43                 :            : {
      44                 :            :     char* s            = strdup( str );
      45                 :            :     const char delim[] = ";,";
      46                 :            :     for( const char* p = strtok( s, delim ); p; p = strtok( 0, delim ) )
      47                 :            :     {
      48                 :            :         int beg, end;
      49                 :            :         switch( sscanf( p, "%u - %u", &beg, &end ) )
      50                 :            :         {
      51                 :            :             case 1:
      52                 :            :                 end = beg;
      53                 :            :                 // fall through
      54                 :            :             case 2:
      55                 :            :                 for( ; beg <= end; ++beg )
      56                 :            :                     MsqDebug::set( beg, flag_val );
      57                 :            :                 break;
      58                 :            :         }
      59                 :            :     }
      60                 :            :     free( s );
      61                 :            : }
      62                 :            : 
      63                 :            : MsqDebug::InitializeFlags::InitializeFlags()
      64                 :            : {
      65                 :            :     const unsigned flag_array[] = { MSQ_ENABLE_DEBUG, 0 };
      66                 :            :     size_t length               = sizeof( flag_array ) / sizeof( unsigned ) - 1;
      67                 :            :     while( length > 0 )
      68                 :            :         MsqDebug::set( flag_array[--length], true );
      69                 :            : 
      70                 :            :     const char* envstr = getenv( "MESQUITE_DEBUG" );
      71                 :            :     if( envstr ) parse_flag_string( envstr, true );
      72                 :            :     envstr = getenv( "MESQUITE_NO_DEBUG" );
      73                 :            :     if( envstr ) parse_flag_string( envstr, false );
      74                 :            : }
      75                 :            : #else
      76                 :         30 : MsqDebug::InitializeFlags::InitializeFlags() {}
      77                 :            : #endif
      78                 :            : 
      79                 :         30 : MsqDebug::InitializeFlags MsqDebug::init;
      80                 :            : 
      81                 :          0 : bool MsqDebug::get( unsigned flag )
      82                 :            : {
      83 [ #  # ][ #  # ]:          0 :     return flag < flags.size() && flags[flag];
         [ #  # ][ #  # ]
                 [ #  # ]
      84                 :            : }
      85                 :            : 
      86                 :          0 : void MsqDebug::set( unsigned flag, bool state )
      87                 :            : {
      88         [ #  # ]:          0 :     if( state )
      89                 :            :     {
      90         [ #  # ]:          0 :         if( flag >= flags.size() ) { flags.resize( flag + 1 ); }
      91                 :          0 :         flags[flag] = true;
      92                 :            :     }
      93                 :            :     else
      94                 :            :     {
      95         [ #  # ]:          0 :         if( flag < flags.size() ) flags[flag] = false;
      96                 :            :     }
      97                 :          0 : }
      98                 :            : 
      99                 :          0 : void MsqDebug::disable_all()
     100                 :            : {
     101                 :          0 :     flags.clear();
     102                 :          0 : }
     103                 :            : 
     104                 :          0 : std::ostream& MsqDebug::get_stream( unsigned flag )
     105                 :            : {
     106         [ #  # ]:          0 :     if( flag < streams.size() )
     107                 :          0 :         return *streams[flag];
     108                 :            :     else
     109                 :          0 :         return std::cout;
     110                 :            : }
     111                 :            : 
     112                 :          0 : void MsqDebug::set_stream( unsigned flag, std::ostream& stream )
     113                 :            : {
     114         [ #  # ]:          0 :     if( flag >= streams.size() )
     115                 :            :     {
     116                 :          0 :         size_t old_size = streams.size();
     117                 :          0 :         streams.resize( flag );
     118         [ #  # ]:          0 :         for( unsigned i = old_size; i < flag; ++i )
     119                 :          0 :             streams[i] = &std::cout;
     120                 :            :     }
     121                 :          0 :     streams[flag] = &stream;
     122                 :          0 : }
     123                 :            : 
     124                 :          0 : void MsqDebug::FormatPrinter::print( const char* format, ... ) const
     125                 :            : {
     126 [ #  # ][ #  # ]:          0 :     if( !MsqDebug::get( myFlag ) ) return;
     127                 :            : 
     128                 :            :     char buffer[512];
     129                 :            : 
     130                 :            : #if defined( HAVE_VSNPRINTF )
     131                 :            :     va_list args;
     132                 :            :     va_start( args, format );
     133                 :            :     vsnprintf( buffer, sizeof( buffer ), format, args );
     134                 :            :     va_end( args );
     135                 :            : #elif defined( HAVE__VSNPRINTF )
     136                 :            :     va_list args;
     137                 :            :     va_start( args, format );
     138                 :            :     _vsnprintf( buffer, sizeof( buffer ), format, args );
     139                 :            :     va_end( args );
     140                 :            : #elif defined( HAVE_VSPRINTF )
     141                 :            :     va_list args;
     142                 :            :     va_start( args, format );
     143                 :            :     vsprintf( buffer, format, args );
     144                 :            :     va_end( args );
     145                 :            : #else
     146                 :          0 :     strncpy( buffer, format, sizeof( buffer ) );
     147                 :          0 :     buffer[sizeof( buffer ) - 1] = '\0';
     148                 :            : #endif
     149                 :            : 
     150 [ #  # ][ #  # ]:          0 :     MsqDebug::get_stream( myFlag ) << buffer;
     151                 :            : }
     152                 :            : 
     153 [ +  - ][ +  - ]:        120 : }  // namespace MBMesquite

Generated by: LCOV version 1.11