MOAB: Mesh Oriented datABase  (version 5.2.1)
MsqDebug.hpp
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 #ifndef MSQ_DEBUG_HPP
00026 #define MSQ_DEBUG_HPP
00027 
00028 #include "Mesquite.hpp"
00029 
00030 #include <vector>
00031 #include <iostream>
00032 #include <cstdio>
00033 
00034 namespace MBMesquite
00035 {
00036 
00037 /**
00038  *\defgroup debug Mesquite debug output
00039  */
00040 /*@{*/
00041 
00042 /** \def MSQ_ENABLE_DEBUG
00043  *\brief Enable debug output and optionally set initial debug flags
00044  *
00045  *If this preprocessor constant is undefined, all debug output will
00046  *be disabled.  It is expected that this will not be defined for
00047  *release builds of Mesquite.
00048  *
00049  *If MSQ_ENABLE_DEBUG is defined, it may optionally be set to a
00050  *comma-separated list of positive, non-zero integers.  This list
00051  *will be interpreted as a list of debug flags that should be
00052  *activated automatically.
00053  *
00054  *To enable debug output but leave all debug flags deactivated:
00055  *#define  MSQ_ENABLE_DEBUG
00056  *
00057  *To enable debug output and activated the first three debug flags:
00058  *#define  MSQ_ENABLE_DEBUG 1,2,3
00059  */
00060 
00061 /**
00062  *\class MsqDebug
00063  *\brief Run-time activation/deactivation of debug flags
00064  *\author Jason Kraftcheck
00065  *\date 2004-10-18
00066  *
00067  *Wrap static functions for managing debug flags and associated
00068  *output streams.  Output is expected do be done using the
00069  *provided macros MSQ_DBGOUT(), MSQ_PRINT(), and MSQ_DBG().
00070  *
00071  *The default output stream for all flags is cout.
00072  */
00073 class MsqDebug
00074 {
00075   public:
00076     // some pre-defined meanings of debug flags
00077     enum
00078     {
00079         WARN = 1,
00080         INFO = 2
00081     };
00082 
00083     /**\brief Enable a debug flag */
00084     static void enable( unsigned flag )
00085     {
00086         set( flag, true );
00087     }
00088     /**\brief Disable a debug flag */
00089     static void disable( unsigned flag )
00090     {
00091         set( flag, false );
00092     }
00093     /**\brief Set a debug flag */
00094     static void set( unsigned flag, bool state );
00095     /**\brief Check a debug flag */
00096     static bool get( unsigned flag );
00097 
00098     /**\brief Disable all debug streams */
00099     static void disable_all();
00100 
00101     /**\brief Get the output stream to be used for a given debug flag */
00102     static std::ostream& get_stream( unsigned flag );
00103     /**\brief Set the output stream to be used for a given debug flag */
00104     static void set_stream( unsigned flag, std::ostream& stream );
00105 
00106     // Work around limitations of preprocessor macros.
00107     // You probably don't want to use this directly.  See
00108     // MSQ_PRINT macro below.
00109     class FormatPrinter
00110     {
00111       public:
00112         FormatPrinter( unsigned flag ) : myFlag( flag ) {}
00113         void print( const char* format, ... ) const
00114 #ifdef __GNUC__
00115             __attribute__( ( format( printf, 2, 3 ) ) )
00116 #endif
00117             ;
00118         const unsigned myFlag;
00119     };
00120 
00121     // Static initialize function (declare a static instance of this
00122     // such that the constructor can be used as a function to initialize
00123     // static data.)
00124     class InitializeFlags
00125     {
00126       public:
00127         InitializeFlags();
00128     };
00129 
00130   private:
00131     static std::vector< std::ostream* > streams;
00132     static std::vector< bool > flags;
00133     static InitializeFlags init;
00134 };
00135 
00136 /** \brief Check if a debug flag is activated - evaluates to a bool.
00137  */
00138 #ifdef MSQ_ENABLE_DEBUG
00139 #define MSQ_DBG( flag ) MBMesquite::MsqDebug::get( flag )
00140 #else
00141 #define MSQ_DBG( flag ) false
00142 #endif
00143 
00144 /**
00145  *\brief Check debug flag and return ostream associated with flag.
00146  *
00147  * Evaluates to a conditional calling of an ostream depending on the
00148  * debug flag.  Example:
00149  *   MSQ_DBGOUT(f) << "Debug flag " << f << " is activated" << endl;
00150  */
00151 #define MSQ_DBGOUT( flag ) \
00152     if( MSQ_DBG( flag ) ) MBMesquite::MsqDebug::get_stream( flag )
00153 
00154 /**
00155  *\brief Check debug flag and print printf-style formatted output.
00156  *
00157  * Evaluatues to a conditional print depending on the debug flag.
00158  * Example:
00159  *  MSQ_PRINT(f)("Debug flag %d is activated", f);
00160  */
00161 #define MSQ_PRINT( flag ) \
00162     if( MSQ_DBG( flag ) ) MBMesquite::MsqDebug::FormatPrinter( flag ).print
00163 
00164 /*@}*/
00165 
00166 }  // namespace MBMesquite
00167 
00168 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines