debug.h

00001 
00017 #ifndef AS_DEBUG_H
00018 #define AS_DEBUG_H
00019 
00020 /* -------------------------------------------------------------------------
00021    Define DEBUG as a compiler flag in all source files to enable debugging
00022    messages.  For example:
00023 
00024       gcc -DDEBUG -c debug.cc
00025 
00026    Provided macros are:
00027 
00028       ASSERT             - assert replacement with debug support
00029       STREAM             - used for writing to ostream with debug support
00030       WARN               - similar to ASSERT but does not exit
00031       DB                 - used for printing messages to cerr
00032       DB_ASSERT          - same as ASSERT, but only included in DEBUG mode
00033       DB_ENTER           - call this when entering a function
00034       DB_REDIRECT        - redirect debug output to file and/or cerr
00035       DB_SET_EXIT_DEPTH  - override exit level for subsequent calls
00036       DB_SET_PRINT_DEPTH - override print level for subsequent calls
00037       DB_STAT            - useful for printing values of variables/functions
00038       DB_STREAM          - same as STREAM, but only included in DEBUG mode
00039       DB_TIME            - print out the current date and time accurate
00040                            to the hundredths of a second
00041       DB_WARN            - similar to DB_ASSERT but does not exit
00042 
00043    Typical usage might be something like:
00044 
00045       #include "debug.h"
00046 
00047       void main(void)
00048       {
00049          int i = 22;
00050 
00051          DB_ENTER(main);
00052          DB_SET_PRINT_DEPTH(2);
00053          DB_SET_EXIT_DEPTH(10);
00054 
00055          DB("Hello world, i = " << i);
00056       }
00057 
00058    If DEBUG is not defined, default is no debugging and macros with DB_
00059    prefix are expanded to nothing, meaning no code is included.  Macros
00060    without this prefix such as ASSERT expand to code in both cases, but
00061    will not include debugging support unless DEBUG is defined.
00062    ------------------------------------------------------------------------- */
00063 
00064 #ifdef _DEBUG
00065 #define DEBUG
00066 #endif
00067 
00068 // Temporarily disable NDEBUG to make sure we get a definition for assert()
00069 #ifdef WIN32
00070         #ifdef NDEBUG
00071                 #define NDEBUG_BACKUP NDEBUG
00072         #endif
00073 
00074         #undef NDEBUG
00075 #endif
00076 
00077 #include <assert.h>
00078 
00079 // Restore NDEBUG to its previous setting
00080 #ifdef WIN32
00081         #ifdef NDEBUG_BACKUP
00082                 #define NDEBUG NDEBUG_BACKUP
00083                 #undef NDEBUG_BACKUP
00084         #endif
00085 #endif
00086 
00087 #ifndef DEBUG
00088 
00089 #ifndef ASSERT
00090 #define ASSERT(X) \
00091         assert(X)
00092 #endif
00093 
00094 #define STREAM(S, X) \
00095         S << X
00096 
00097 #define WARN(X) \
00098         (X)
00099 
00100 // Debug mode off, so disable all DB macros
00101 #define DB(X)
00102 #define DB_ASSERT(X)
00103 #define DB_DELAY()
00104 #define DB_ENTER(X)
00105 #define DB_REDIRECT(X, F)
00106 #define DB_SET_EXIT_DEPTH(X)
00107 #define DB_SET_PRINT_DEPTH(X)
00108 #define DB_STAT(X)
00109 #define DB_STREAM(S, X)
00110 #define DB_TIME()
00111 #define DB_WARN(X)
00112 
00113 #else // DEBUG
00114 
00115 #ifndef __cplusplus
00116 
00117 // Provide minimal support for C programs
00118 #define ASSERT(X) \
00119         assert(X)
00120 
00121 #define DB_ASSERT(X) \
00122         ASSERT(X)
00123 
00124 #ifdef WIN32
00125 #define DB(X) \
00126         OutputDebugString(X)
00127 #else
00128 #define DB(X) \
00129         fprintf(stderr, X);
00130 #endif
00131 
00132 #else // __cplusplus
00133 
00134 // Compile time options
00135 //#define DEBUG_THREADS
00136 //#define DEBUG_TIME_ACCURACY
00137 
00138 #ifdef WIN32
00139         #include <strstrea.h>
00140         #include <process.h>
00141 
00142         #define getpid() \
00143                 "0x" << hex << GetCurrentProcessId()
00144 
00145         #define pthread_self() \
00146                 "0x" << hex << GetCurrentThreadId()
00147 #else
00148         #include <iostream>
00149         #include <ostream>
00150         #include <unistd.h>                     // getpid()
00151 #endif
00152 
00153 #ifdef DEBUG_THREADS
00154         #ifdef __linux__
00155         #include <bits/sigset.h>
00156         #endif
00157 
00158 #include "critsec.h"
00159 #endif
00160 
00161 #ifdef WIN32
00162         #ifdef DLL_VERSION
00163                 #define DllImport __declspec(dllimport)
00164                 #define DllExport __declspec(dllexport)
00165         #else
00166                 #define DllImport
00167                 #define DllExport
00168         #endif
00169 
00170         #ifdef SVVIDEO_EXPORTS
00171                 #define CLASS_DECL_DEBUG DllExport
00172         #else
00173                 #define CLASS_DECL_DEBUG DllImport
00174         #endif
00175 #else
00176         #define CLASS_DECL_DEBUG
00177 #endif
00178 
00179 #include <string>
00180 
00181 class CLASS_DECL_DEBUG Debug
00182 {
00183 private:
00184         char *func_name;
00185         int prev_exit_depth, prev_print_depth;
00186 
00187 public:
00188         static int exit_depth;
00189         static int indent_depth;
00190         static int print_depth;
00191 
00192         static bool cerr_enable;
00193         static std::ostream *os_ptr;
00194 
00195 #ifdef DEBUG_THREADS
00196         static Mutex mutex;
00197 #endif
00198 
00199         Debug(char *_func_name);
00200         ~Debug();
00201 
00202         static void Assert(char *expr, char *filename, int line);
00203         static void delay(int num_seconds = 5);
00204         static void indent(void);
00205         static void preline(void);
00206         static void print_time(void);
00207         static void redirect(const char *_filename = NULL, bool _cerr_enable = true);
00208         static void set_exit_depth(int _exit_depth);
00209         static void set_print_depth(int _print_depth);
00210         static void warn(char *expr, char *filename, int line);
00211 
00212         static std::string localtime(void);
00213 };
00214 
00215 // Private helper macros
00216 
00217 #ifdef WIN32
00218         static char db_stream_buf[2048];
00219 
00220         #define DB_STREAM_PRINT(S, X) \
00221                 { \
00222                         ostrstream s(db_stream_buf, sizeof(db_stream_buf)); \
00223                         s << X; \
00224                         db_stream_buf[s.pcount()] = '\0'; \
00225                         OutputDebugString(db_stream_buf); \
00226                 }
00227 #else
00228         #define DB_STREAM_PRINT(S, X) \
00229                 S << X
00230 #endif
00231 
00232 #ifdef DEBUG_THREADS
00233         #define DB_ACQUIRE_CRITSEC() \
00234                 Critical_Section critsec(Debug::mutex)
00235         #define DB_STREAM_PRINT_THREAD(S) \
00236                 DB_STREAM_PRINT(S, "," << pthread_self())
00237 #else
00238         #define DB_ACQUIRE_CRITSEC()
00239         #define DB_STREAM_PRINT_THREAD(S)
00240 #endif
00241 
00242 #define DB_PRINT(X) \
00243         { \
00244                 DB_ACQUIRE_CRITSEC(); \
00245                 if (Debug::cerr_enable) \
00246                         DB_STREAM_PRINT(cerr, X); \
00247                 if (Debug::os_ptr) \
00248                         DB_STREAM_PRINT(*Debug::os_ptr, X); \
00249         }
00250 
00251 // Removed for now since server is single-threaded, time is more useful
00252 //DB_STREAM_PRINT(S, getpid());
00253 //DB_STREAM_PRINT_THREAD(S);
00254 
00255 #define DB_STREAM_INDENT(S) \
00256         { \
00257                 DB_STREAM_PRINT(S, Debug::localtime()); \
00258                 DB_STREAM_PRINT(S, " "); \
00259                 for (int i=0; i < Debug::indent_depth; i++) \
00260                 { \
00261                         DB_STREAM_PRINT(S, " "); \
00262                 } \
00263         }
00264 
00265 #define DB_INDENT() \
00266         Debug::indent()
00267 
00268 // Macros defined in both debug and normal modes
00269 
00270 #ifndef ASSERT
00271 #define ASSERT(X) \
00272         if ((X) == 0) \
00273                 Debug::Assert(#X, __FILE__, __LINE__)
00274 #endif
00275 
00276 #define STREAM(S, X) \
00277         { \
00278                 DB_STREAM_INDENT(S); \
00279                 DB_STREAM_PRINT(S, X); \
00280         }
00281 
00282 #define WARN(X) \
00283         if ((X) == 0) \
00284                 Debug::warn(#X, __FILE__, __LINE__)
00285 
00286 // Macros defined only if debugging is enabled
00287 
00288 #define DB(X) \
00289         if (Debug::print_depth > 0) \
00290         { \
00291                 DB_INDENT(); \
00292                 DB_PRINT(X << endl); \
00293         }
00294 
00295 #define DB_ASSERT(X) \
00296         ASSERT(X)
00297 
00298 #define DB_DELAY() \
00299         Debug::delay();
00300 
00301 #define DB_ENTER(X) \
00302         Debug debug(#X)
00303 
00304 #define DB_REDIRECT(X, F) \
00305         Debug::redirect(X, F)
00306 
00307 #define DB_SET_EXIT_DEPTH(X) \
00308         Debug::set_exit_depth(X)
00309 
00310 #define DB_SET_PRINT_DEPTH(X) \
00311         Debug::set_print_depth(X)
00312 
00313 #define DB_STAT(X) \
00314         DB(#X << " = " << (X))
00315 
00316 #define DB_STREAM(S, X) \
00317         STREAM(S, X)
00318 
00319 #define DB_TIME() \
00320         Debug::print_time()
00321 
00322 #define DB_WARN(X) \
00323         WARN(X)
00324 
00325 #endif // __cplusplus
00326 
00327 #endif // DEBUG
00328 
00329 #endif // AS_DEBUG_H
00330 

Generated on Thu Sep 16 11:51:44 2010 for Communication Component by  doxygen 1.4.7