Util.hpp

Go to the documentation of this file.
00001 
00015 #ifndef OA_Util_H
00016 #define OA_Util_H
00017 
00018 #include <cstdlib>
00019 #include <string>
00020 
00021 // Could use templates within OA namespace
00022 
00023 #ifndef OA_MIN
00024 # define OA_MIN(a,b) (((a) < (b)) ? (a) : (b))
00025 #endif
00026 
00027 #ifndef OA_MAX
00028 # define OA_MAX(a,b) (((a) > (b)) ? (a) : (b))
00029 #endif
00030 
00031 /* 
00032 Debugging macro
00033 
00034 OA_DEBUG_CTRL_MACRO(ModuleNameListDEBUG, DeBugDEBUG)
00035 
00036 This .hpp file sets up the macro OA_DEBUG_CTRL_MACRO.  This macro 
00037 at runtime retrieves the ':' colon-separated  list from the environment 
00038 variable OA_DEBUG and compares this list to the list of ':' separate 
00039 names passed to it in ModuleNameListDEBUG.  If it finds an exact name 
00040 match for any of the elements it sets the bool variable true, otherwise 
00041 it set it false.  This variable can then be used to control printing 
00042 of debug information.  
00043 
00044 OA_DEBUG_CTRL_MACRO was written as a macro so it would always be 
00045 inlined.  The only library call the macro uses is getenv(). All 
00046 the string compares are done inline without any library calls, 
00047 so the code is a bit ugly, but should be safe. 
00048 
00049 String case does matter, so I recommend using upper-case 
00050 charters for both the call and in the environment OA_DEBUG variable!
00051 
00052 No memory is allocated other then a couple pointers taken from the stack. 
00053 Everything is wrapped within a { } pair to keep all the variables in a 
00054 local scope.
00055 
00056 To use, within the program code (must be executed), declare a bool variable
00057 and call the macro:
00058 
00059         bool debug;
00060         OA_DEBUG_CTRL ( "THISMODNAME:GROUPMODNAME:GLOBALMODNAME:ALL", debug) ;
00061 
00062 Where the text between the "" is one or more names, separated with ':' 
00063 that will be compared to the environment variable returned with 
00064 getenv("OA_DEBUG"). The parameter "debug" is the name of the bool variable that 
00065 will be set to true of false depending on if the environment variable is 
00066 matched. 
00067 
00068         WARNING: This is a macro, so do not do:
00069 
00070         OA_DEBUG_CTRL ( "MYMODULE:ALL", bool debug) ;
00071 
00072         As "bool debug" will get expanded more then once 
00073         in the macro and your code will not compile
00074 
00075 The reason ModuleNameListDEBUG takes a list is so you can place 
00076 a specific entry for this one module, and then add in zero or 
00077 more group names, allowing you to turn on debugging for a group 
00078 of modules by only setting a single environment variable
00079 
00080 When you want to output debugging information, use something like this 
00081 within your code:
00082 
00083  if (debug) {
00084           std::cout << "This is a debug message << std::endl ;
00085  }
00086 
00087 To turn debugging on, under the bash shell use the command:
00088 
00089     export OA_DEBUG=FREDSMODULE:THISMODULENAME:SOMEOTHERMODUAL
00090 
00091 and then run your code.
00092 
00093 To turn debugging off, issued the command:
00094 
00095     export OA_DEBUG=
00096 
00097 E. Eastman
00098 Feb 2006
00099 
00100 */
00101 #define OA_GLOBAL_DEBUG_ON
00102 
00103 #if defined OA_GLOBAL_DEBUG_ON
00104 
00105 #define OA_DEBUG_CTRL_MACRO(ModuleNameListDEBUG, DeBugDEBUG)\
00106     {\
00107         DeBugDEBUG = false;\
00108         char *ept = getenv("OA_DEBUG");\
00109         if( ept != NULL ) {\
00110             while (*ept == ':') {++ept;}\
00111             char *ept1 = ept;\
00112             char *mpt1;\
00113             char *mpt;\
00114             mpt1 = ModuleNameListDEBUG;\
00115             while (*mpt1 == ':') {++mpt1;}\
00116             mpt = mpt1;\
00117             while(*mpt1 != '\0') {\
00118                 while(*ept != '\0'){\
00119                     if( *ept == *mpt ){\
00120                         *ept++;\
00121                         *mpt++;\
00122                         if ((*ept == '\0' || *ept == ':' ) && \
00123                           (*mpt == '\0' || *mpt == ':')) {\
00124                             DeBugDEBUG = true;\
00125                             break;\
00126                         }\
00127                     }\
00128                     else {\
00129                         mpt = mpt1;\
00130                         while ((*ept != ':') && (*ept != '\0')){++ept;}\
00131                         while (*ept == ':') {++ept;}\
00132                     }\
00133                 }\
00134                 if (DeBugDEBUG) {break;}\
00135                 while( (*mpt1 != ':') && (*mpt1 != '\0') ){++mpt1;}\
00136                 while (*mpt1 == ':') {++mpt1;}\
00137                 mpt = mpt1; ept = ept1;\
00138             }\
00139         }\
00140     }\
00141 
00142 #else
00143 /* 
00144 *    If OA_GLOBAL_DEBUG_ON is not set, we still need to define DeBugDEBUG  
00145 */ 
00146 #define OA_DEBUG_CTRL_MACRO(ModuleNameListDEBUG, DeBugDEBUG)\
00147     DeBugDEBUG = false;\
00148 
00149 #endif
00150 
00151 
00152 #endif

Generated on Sat Oct 31 05:21:26 2009 for OpenAnalysis by  doxygen 1.6.1