home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / hpp.z / WDEBUG.HPP < prev    next >
C/C++ Source or Header  |  1996-12-12  |  11KB  |  391 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,8);
  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 __BUILDING_WCM__
  120. #define WGETOBJECTFLAGS WDebug::ObjectFlags
  121. #define WGETCHECKGDIHANDLES WDebug::CheckGDIHandles
  122. #define WGETCHECKSTRINGS WDebug::CheckStrings
  123. #define WGETMEMORYTRACKING WDebug::MemoryTracking
  124. #else
  125. #define WGETOBJECTFLAGS WDebug::GetObjectFlags()
  126. #define WGETCHECKGDIHANDLES WDebug::GetCheckGDIHandles()
  127. #define WGETCHECKSTRINGS WDebug::GetCheckStrings()
  128. #define WGETMEMORYTRACKING WDebug::GetMemoryTracking()
  129. #endif
  130.  
  131. #ifdef _DEBUG
  132. #pragma warning 549 9
  133. #define WValidateObject(__x) \
  134.              ((WGETOBJECTFLAGS == 0) ? (void)0 : \
  135.                  WDebug::ValidateObject( (WObject*)__x,\
  136.                                      (((__x)!=NULL)?sizeof(*__x):0),\
  137.                                      __WTEXT(__FUNCTION__), \
  138.                                      __WTEXT(__FILE__),__LINE__))
  139.  
  140. #else
  141. #define WValidateObject(__x)
  142. #endif
  143.  
  144. #define WVALIDATEWOBJECT(__x) WValidateObject(__x)
  145. #define WVALIDATETHIS()       WValidateObject(this)
  146.  
  147. class WObject;
  148.  
  149. //
  150. // WNoDefault macro -- Use to catch unexpected switch values.
  151. //
  152.  
  153. #ifdef _DEBUG
  154. #define WNoDefault() WDebug::NoDefault(__WTEXT(__FUNCTION__),\
  155.                                        __WTEXT(__FILE__),__LINE__ )
  156. #else
  157. #define WNoDefault()
  158. #endif
  159.  
  160. #define WNODEFAULT WNoDefault()
  161.  
  162. #ifdef _DEBUG
  163. #define CHECKGDI(__x)  (WGETCHECKGDIHANDLES?WASSERT(__x):(void)0)
  164. #else
  165. #define CHECKGDI(__x)
  166. #endif
  167.  
  168. //
  169. // System error checking
  170. //
  171.  
  172. #if defined( _DEBUG ) && defined( __BUILDING_WCM__ )
  173. #define WUPDATEERRORLOGFILE() if( WDebug::CheckErrors ) \
  174.                                  WDebug::CheckErrorCode( __WTEXT(__FUNCTION__),\
  175.                                  __WTEXT(__FILE__), __LINE__ )
  176. #elif defined( _DEBUG )
  177. #define WUPDATEERRORLOGFILE() WDebug::CheckErrorCode( __WTEXT(__FUNCTION__),\
  178.                                  __WTEXT(__FILE__), __LINE__ )
  179. #else
  180. #define WUPDATEERRORLOGFILE() ((void)0)
  181. #endif
  182.  
  183. #define WUPDATESYSTEMERRORLOGFILE() WUPDATEERRORLOGFILE()
  184.  
  185. //
  186. // Tracing facilities
  187. //
  188. //    Declare a WTraceMarker statically at the beginning of a function.
  189. //    Then declare a WTrace on the stack, passing in the address of the
  190. //    trace marker.
  191. //
  192.  
  193. class WCMCLASS WTraceMarker {
  194.  
  195.     public:
  196.  
  197.         struct WTraceMarkerEntry {
  198.             wllist_link  link;
  199.  
  200.             const WChar *name;
  201.             const WChar *fileName;
  202.             WULong       line;
  203.         };
  204.  
  205.     public:
  206.         WTraceMarker( const WChar *name, const WChar *fileName,
  207.                       WULong line );
  208.  
  209.         // ~WTraceMarker();   no destructor is needed....
  210.  
  211.         //
  212.         // Data
  213.         //
  214.  
  215.         WTraceMarkerEntry _markerInfo;
  216. };
  217.  
  218. class WCMCLASS WTrace {
  219.  
  220.     public:
  221.  
  222.         typedef WTraceMarker::WTraceMarkerEntry wtm_entry;
  223.  
  224.         struct WTraceEntry {
  225.             wllist_link            link;
  226.             wtm_entry             *marker;
  227.         };
  228.          
  229.     public:
  230.  
  231.         WTrace( WTraceMarker & marker );
  232.  
  233.         ~WTrace();
  234.  
  235.         WTraceEntry _traceInfo;
  236. };
  237.  
  238. //
  239. // Debugging control
  240. //
  241. //    Used to turn on/off debugging options.
  242. //
  243.  
  244. class WCMCLASS WDebug {
  245.  
  246.     public:
  247.  
  248.         struct WDebugEntry {
  249.             wllist_link link;
  250.             WULong      memoryFlags;
  251.             WBool       checkGDI;
  252.             WBool       checkStrings;
  253.         };
  254.  
  255.     public:
  256.  
  257.         WDebug();
  258.  
  259.         ~WDebug();
  260.  
  261.         /**************************************************************
  262.          * Properties
  263.          **************************************************************/
  264.  
  265.         // CheckErrors
  266.  
  267.         static WBool GetCheckErrors();
  268.         static WBool SetCheckErrors( WBool check );
  269.  
  270.         // CheckGDIHandles
  271.  
  272.         static WBool GetCheckGDIHandles();
  273.         static WBool SetCheckGDIHandles( WBool check );
  274.  
  275.         // CheckStrings
  276.  
  277.         static WBool GetCheckStrings();
  278.         static WBool SetCheckStrings( WBool check );
  279.  
  280.         // MemoryTracking
  281.  
  282.         static WULong GetMemoryTracking();
  283.         static WBool  SetMemoryTracking( WULong trackFlags );
  284.  
  285.         // ObjectFlags
  286.  
  287.         static WULong GetObjectFlags();
  288.         static WBool  SetObjectFlags( WULong flags );
  289.  
  290.         /**************************************************************
  291.          * Methods
  292.          **************************************************************/
  293.  
  294.         //
  295.         // Assert
  296.         //
  297.  
  298.         static void Assert( const WChar *line1, const WChar *line2,
  299.                             const WChar *line3, const WChar *name,
  300.                             const WChar *fileName, WULong lineNumber );
  301.  
  302.         // BreakIntoDebugger
  303.         //
  304.         //    Enter the debugger if it is present.  Returns FALSE
  305.         //    if the debugger was not entered.  The parameter
  306.         //    is a message to display (it can be NULL).  Only works
  307.         //    in debugging version.
  308.  
  309.         static WBool BreakIntoDebugger( const WAnsiChar *message,
  310.                                         WBool startWDW );
  311.  
  312.         // CheckErrorCode
  313.         //
  314.         //    Used for debugging.  Checks to see if the last error
  315.         //    code is non-zero, and if so prints a debugging message.
  316.         //    It then calls ::SetErrorCode( 0 ) to reset the error.
  317.         //    Returns the error code that was detected.  Fails without
  318.         //    checking the error code if GetCheckErrors() is FALSE.
  319.  
  320.         static WDWord CheckErrorCode( const WChar *functionName,
  321.                                       const WChar *fileName,
  322.                                       WULong line );
  323.  
  324.         //
  325.         // InternalError
  326.         //
  327.  
  328.         static void InternalError( const WChar *text, const WChar *name,
  329.                                    const WChar *fileName, WULong line );
  330.  
  331.         //
  332.         // IsDebuggerPresent
  333.         //
  334.  
  335.         static WBool IsDebuggerPresent();
  336.  
  337.         //
  338.         // NoDefault
  339.         //
  340.  
  341.         static void NoDefault( const WChar *name,
  342.                                const WChar *fileName, WULong lineNumber );
  343.  
  344.         //
  345.         // Printf
  346.         //
  347.  
  348.         static void Printf( const WChar *format, ... );
  349.  
  350.         //
  351.         // ValidateObject
  352.         //
  353.  
  354.         static void ValidateObject( WObject *ptr, WULong len,
  355.                                     const WChar *name,
  356.                                     const WChar *fileName, WULong line );
  357.                                     
  358.  
  359.         /**************************************************************
  360.          * Data
  361.          **************************************************************/
  362.  
  363.     #ifdef __BUILDING_WCM__
  364.     public:
  365.     #else
  366.     private:
  367.     #endif
  368.         WDebugEntry _debugInfo;
  369.  
  370.         static WBool                   CheckStrings;
  371.         static WBool                   CheckGDIHandles;
  372.         static WULong                  MemoryTracking;
  373.         static WULong                  ObjectFlags;
  374.         static WBool                   CheckErrors;
  375. };
  376.  
  377. #define WDEBUG_CHECK_OBJECT_FLAGS 0x01
  378. #define WDEBUG_CHECK_OBJECT_WRITE 0x02
  379. #define WDEBUG_CHECK_OBJECT_READ  0x04
  380.  
  381. #define WDEBUG_CHECK_OBJECT_NONE  0x00
  382. #define WDEBUG_CHECK_OBJECT_ALL   0xFF
  383.  
  384.  
  385. #ifndef _WNO_PRAGMA_PUSH
  386. #pragma enum pop;
  387. #pragma pack(pop);
  388. #endif
  389.  
  390. #endif // _WDEBUG_HPP_INCLUDED
  391.