home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / hpp.z / WDEBUG.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-24  |  10.2 KB  |  371 lines

  1. /**********************************************************************
  2.  *
  3.  * WDebug -- Debugging utilties.
  4.  *
  5.  **********************************************************************/
  6.  
  7. #ifndef _WDEBUG_HPP_INCLUDED
  8. #define _WDEBUG_HPP_INCLUDED
  9.  
  10. #ifndef _WNO_PRAGMA_PUSH
  11. #pragma pack(push,4);
  12. #pragma enum int;
  13. #endif
  14.  
  15. #ifndef _WDEF_HPP_INCLUDED
  16. #   include "wdef.hpp"
  17. #endif
  18. #ifndef _WLLIST_HPP_INCLUDED
  19. #   include "wllist.hpp"
  20. #endif
  21.  
  22. //
  23. // WDBG macro -- Use like: WDBG(( "This is number %d", i ));
  24. //
  25.  
  26. #ifdef _DEBUG
  27. #define WDBG(x)    WDebug::Printf x
  28. #else
  29. #define WDBG(x)
  30. #endif
  31.  
  32. //
  33. // WAssert macro -- Use to check that a given condition is true.
  34. //
  35.  
  36. #define WVerify(__x)\
  37.         (((int)(__x))? (void)0                                          \
  38.                      : WDebug::Assert( NULL, __WTEXT(#__x), NULL, \
  39.                                        __WTEXT(__FUNCTION__), \
  40.                                        __WTEXT(__FILE__), __LINE__ ))
  41.  
  42. #define WVerifyEx(__x,__msg)\
  43.         (((int)(__x))? (void)0                                          \
  44.                      : WDebug::Assert( NULL, __WTEXT(#__x), __WTEXT(__msg),\
  45.                                        __WTEXT(__FUNCTION__), \
  46.                                        __WTEXT(__FILE__), __LINE__ ))
  47.  
  48. #ifdef _DEBUG
  49. #define WAssert(__x)\
  50.         (((int)(__x))? (void)0                                          \
  51.                      : WDebug::Assert( NULL, __WTEXT(#__x), NULL, \
  52.                                        __WTEXT(__FUNCTION__), \
  53.                                        __WTEXT(__FILE__), __LINE__ ))
  54.  
  55. #define WAssertEx(__x,__msg)\
  56.         (((int)(__x))? (void)0                                          \
  57.                      : WDebug::Assert( NULL, __WTEXT(#__x), __WTEXT(__msg),\
  58.                                        __WTEXT(__FUNCTION__), \
  59.                                        __WTEXT(__FILE__), __LINE__ ))
  60.  
  61. #define WWarning(__msg)\
  62.             WDebug::Assert( __WTEXT("Warning"),__WTEXT(__msg),NULL,\
  63.                             __WTEXT(__FUNCTION__),\
  64.                                        __WTEXT(__FILE__), __LINE__ )
  65. #else
  66. #define WAssert(__x)
  67. #define WAssertEx(__x,__msg)
  68. #define WWarning(__msg)
  69. #endif
  70.  
  71. #define WVERIFY(__x)          WVerify(__x)
  72. #define WVERIFYEX(__x,__msg)  WVerifyEx(__x,__msg)
  73. #define WASSERT(__x)          WAssert(__x)
  74. #define WASSERTEX(__x,__msg)  WAssertEx(__x,__msg)
  75. #define WWARNING(__msg)       WWarning(__msg)
  76.  
  77.  
  78. #define WInternalError(__msg) WDebug::InternalError( __msg,\
  79.                                               __WTEXT(__FUNCTION__),\
  80.                                               __WTEXT(__FILE__),__LINE__)
  81.  
  82.  
  83. #ifndef _DEBUG
  84. #define __NO_WCM_TRACING__
  85. #endif
  86.  
  87. //
  88. // WTraceMember macro -- Declare at beginning of a WObject member function.
  89. //
  90.  
  91. #ifndef __NO_WCM_TRACING__
  92. #define WTraceMember()    static WTraceMarker __marker(__WTEXT(__FUNCTION__),\
  93.                                                   __WTEXT(__FILE__),\
  94.                                                   __LINE__);\
  95.                           WTrace __trace( __marker );\
  96.                           WValidateObject(this);
  97. #else
  98. #define WTraceMember()
  99. #endif
  100.  
  101. //
  102. // WTraceFunction macro -- Declare at beginning of a non-member function
  103. //                         or of a non-WObject member.
  104. //
  105.  
  106. #ifndef __NO_WCM_TRACING__
  107. #define WTraceFunction()  static WTraceMarker __marker(__WTEXT(__FUNCTION__),\
  108.                                                   __WTEXT(__FILE__),\
  109.                                                   __LINE__);\
  110.                           WTrace __trace( __marker );
  111. #else
  112. #define WTraceFunction()
  113. #endif
  114.  
  115. //
  116. // WValidateObject macro -- Use to check if object is valid or not.
  117. //
  118.  
  119. #ifdef _DEBUG
  120. #pragma warning 549 9
  121. #define WValidateObject(__x) \
  122.              ((WDebug::GetObjectFlags() == 0) ? (void)0 : \
  123.                  WDebug::ValidateObject( (WObject*)__x,\
  124.                                      (((__x)!=NULL)?sizeof(*__x):0),\
  125.                                      __WTEXT(__FUNCTION__), \
  126.                                      __WTEXT(__FILE__),__LINE__))
  127.  
  128. #else
  129. #define WValidateObject(__x)
  130. #endif
  131.  
  132. #define WVALIDATEWOBJECT(__x) WValidateObject(__x)
  133. #define WVALIDATETHIS()       WValidateObject(this)
  134.  
  135. class WObject;
  136.  
  137. //
  138. // WNoDefault macro -- Use to catch unexpected switch values.
  139. //
  140.  
  141. #ifdef _DEBUG
  142. #define WNoDefault() WDebug::NoDefault(__WTEXT(__FUNCTION__),\
  143.                                        __WTEXT(__FILE__),__LINE__ )
  144. #else
  145. #define WNoDefault()
  146. #endif
  147.  
  148. #define WNODEFAULT WNoDefault()
  149.  
  150. #ifdef _DEBUG
  151. #define CHECKGDI(__x)  (WDebug::GetCheckGDIHandles()?WASSERT(__x):(void)0)
  152. #else
  153. #define CHECKGDI(__x)
  154. #endif
  155.  
  156. //
  157. // System error checking
  158. //
  159.  
  160. #ifdef _DEBUG
  161. #define WUPDATEERRORLOGFILE() WDebug::CheckErrorCode( __WTEXT(__FUNCTION__),\
  162.                                  __WTEXT(__FILE__), __LINE__ )
  163. #else
  164. #define WUPDATEERRORLOGFILE() ((void)0)
  165. #endif
  166.  
  167. #define WUPDATESYSTEMERRORLOGFILE() WUPDATEERRORLOGFILE()
  168.  
  169. //
  170. // Tracing facilities
  171. //
  172. //    Declare a WTraceMarker statically at the beginning of a function.
  173. //    Then declare a WTrace on the stack, passing in the address of the
  174. //    trace marker.
  175. //
  176.  
  177. class WCMCLASS WTraceMarker {
  178.  
  179.     public:
  180.  
  181.         struct WTraceMarkerEntry {
  182.             wllist_link  link;
  183.  
  184.             const WChar *name;
  185.             const WChar *fileName;
  186.             WULong       line;
  187.         };
  188.  
  189.     public:
  190.         WTraceMarker( const WChar *name, const WChar *fileName,
  191.                       WULong line );
  192.  
  193.         // ~WTraceMarker();   no destructor is needed....
  194.  
  195.         //
  196.         // Data
  197.         //
  198.  
  199.         WTraceMarkerEntry _markerInfo;
  200. };
  201.  
  202. class WCMCLASS WTrace {
  203.  
  204.     public:
  205.  
  206.         typedef WTraceMarker::WTraceMarkerEntry wtm_entry;
  207.  
  208.         struct WTraceEntry {
  209.             wllist_link            link;
  210.             wtm_entry             *marker;
  211.         };
  212.          
  213.     public:
  214.  
  215.         WTrace( WTraceMarker & marker );
  216.  
  217.         ~WTrace();
  218.  
  219.         WTraceEntry _traceInfo;
  220. };
  221.  
  222. //
  223. // Debugging control
  224. //
  225. //    Used to turn on/off debugging options.
  226. //
  227.  
  228. class WCMCLASS WDebug {
  229.  
  230.     public:
  231.  
  232.         struct WDebugEntry {
  233.             wllist_link link;
  234.             WULong      memoryFlags;
  235.             WBool       checkGDI;
  236.             WBool       checkStrings;
  237.         };
  238.  
  239.     public:
  240.  
  241.         WDebug();
  242.  
  243.         ~WDebug();
  244.  
  245.         /**************************************************************
  246.          * Properties
  247.          **************************************************************/
  248.  
  249.         // CheckErrors
  250.  
  251.         static WBool GetCheckErrors();
  252.         static WBool SetCheckErrors( WBool check );
  253.  
  254.         // CheckGDIHandles
  255.  
  256.         static WBool GetCheckGDIHandles();
  257.         static WBool SetCheckGDIHandles( WBool check );
  258.  
  259.         // CheckStrings
  260.  
  261.         static WBool GetCheckStrings();
  262.         static WBool SetCheckStrings( WBool check );
  263.  
  264.         // MemoryTracking
  265.  
  266.         static WULong GetMemoryTracking();
  267.         static WBool  SetMemoryTracking( WULong trackFlags );
  268.  
  269.         // ObjectFlags
  270.  
  271.         static WULong GetObjectFlags();
  272.         static WBool  SetObjectFlags( WULong flags );
  273.  
  274.         /**************************************************************
  275.          * Methods
  276.          **************************************************************/
  277.  
  278.         //
  279.         // Assert
  280.         //
  281.  
  282.         static void Assert( const WChar *line1, const WChar *line2,
  283.                             const WChar *line3, const WChar *name,
  284.                             const WChar *fileName, WULong lineNumber );
  285.  
  286.         // BreakIntoDebugger
  287.         //
  288.         //    Enter the debugger if it is present.  Returns FALSE
  289.         //    if the debugger was not entered.  The parameter
  290.         //    is a message to display (it can be NULL).  Only works
  291.         //    in debugging version.
  292.  
  293.         static WBool BreakIntoDebugger( const WAnsiChar *message,
  294.                                         WBool startWDW );
  295.  
  296.         // CheckErrorCode
  297.         //
  298.         //    Used for debugging.  Checks to see if the last error
  299.         //    code is non-zero, and if so prints a debugging message.
  300.         //    It then calls ::SetErrorCode( 0 ) to reset the error.
  301.         //    Returns the error code that was detected.  Fails without
  302.         //    checking the error code if GetCheckErrors() is FALSE.
  303.  
  304.         static WDWord CheckErrorCode( const WChar *functionName,
  305.                                       const WChar *fileName,
  306.                                       WULong line );
  307.  
  308.         //
  309.         // InternalError
  310.         //
  311.  
  312.         static void InternalError( const WChar *text, const WChar *name,
  313.                                    const WChar *fileName, WULong line );
  314.  
  315.         //
  316.         // IsDebuggerPresent
  317.         //
  318.  
  319.         static WBool IsDebuggerPresent();
  320.  
  321.         //
  322.         // NoDefault
  323.         //
  324.  
  325.         static void NoDefault( const WChar *name,
  326.                                const WChar *fileName, WULong lineNumber );
  327.  
  328.         //
  329.         // Printf
  330.         //
  331.  
  332.         static void Printf( const WChar *format, ... );
  333.  
  334.         //
  335.         // ValidateObject
  336.         //
  337.  
  338.         static void ValidateObject( WObject *ptr, WULong len,
  339.                                     const WChar *name,
  340.                                     const WChar *fileName, WULong line );
  341.                                     
  342.  
  343.         /**************************************************************
  344.          * Data
  345.          **************************************************************/
  346.  
  347.     private:
  348.         WDebugEntry _debugInfo;
  349.  
  350.         static WBool                   CheckStrings;
  351.         static WBool                   CheckGDIHandles;
  352.         static WULong                  MemoryTracking;
  353.         static WULong                  ObjectFlags;
  354.         static WBool                   CheckErrors;
  355. };
  356.  
  357. #define WDEBUG_CHECK_OBJECT_FLAGS 0x01
  358. #define WDEBUG_CHECK_OBJECT_WRITE 0x02
  359. #define WDEBUG_CHECK_OBJECT_READ  0x04
  360.  
  361. #define WDEBUG_CHECK_OBJECT_NONE  0x00
  362. #define WDEBUG_CHECK_OBJECT_ALL   0xFF
  363.  
  364.  
  365. #ifndef _WNO_PRAGMA_PUSH
  366. #pragma enum pop;
  367. #pragma pack(pop);
  368. #endif
  369.  
  370. #endif // _WDEBUG_HPP_INCLUDED
  371.