home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osp1.exe / src / utils / ODDebug.h < prev    next >
C/C++ Source or Header  |  1997-04-02  |  11KB  |  316 lines

  1. //====START_GENERATED_PROLOG======================================
  2. //
  3. //
  4. //   COMPONENT_NAME: odutils
  5. //
  6. //   CLASSES: none
  7. //
  8. //   ORIGINS: 82,27
  9. //
  10. //
  11. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  12. //   All Rights Reserved
  13. //   Licensed Materials - Property of IBM
  14. //   US Government Users Restricted Rights - Use, duplication or
  15. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  16. //       
  17. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  23. //   OR PERFORMANCE OF THIS SOFTWARE.
  24. //
  25. //====END_GENERATED_PROLOG========================================
  26. //
  27. // @(#) 1.23 com/src/utils/include/ODDebug.h, odutils, od96os2, odos29712d 3/14/97 16:51:49 [ 3/21/97 17:21:07 ]
  28. /*
  29.     File:        ODDebug.h
  30.  
  31.     Contains:    Useful debugging macros and functions.
  32.  
  33.     Owned by:    Jens Alfke
  34.  
  35.     Copyright:    (C) 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  36.  
  37. */
  38.  
  39.  
  40. #ifndef _ODDEBUG_
  41. #define _ODDEBUG_
  42.  
  43. #ifndef _ODTYPES_
  44. #include "ODTypes.h"
  45. #endif
  46.  
  47. #ifndef _ODMVCLNK_
  48. #include <ODMvcLnk.h>
  49. #endif
  50.  
  51.  
  52. #ifdef _OD_IMPL_SHARE_UTILS_
  53. #pragma import on
  54. #endif
  55.  
  56. #include <ODMessag.h>
  57.  
  58. // Keep the handle to OpenDoc's message catalog here, so all OpenDoc
  59. // Runtime code has access to it.
  60. #if defined(_IBMR2)
  61.     // If using xlC on AIX, use the built-in message catalog functions
  62.     #include <nl_types.h>
  63.     extern nl_catd ODcat_handle;
  64. #endif
  65. #ifdef __IBMCPP__
  66.     // If using the VisualAge compiler, use our local message catalog
  67.     // functions
  68.     #include <odnltyps.h>
  69.     extern nl_catd _Import ODcat_handle;
  70. #endif
  71.  
  72. #ifdef __cplusplus
  73. extern "C" {
  74. #endif
  75.  
  76.  
  77. //=====================================================================================
  78. // ODInitExceptions
  79. //=====================================================================================
  80.  
  81. _DLLIMPORTEXPORT_ void ODInitExceptions( );
  82.  
  83. #ifndef _PLATFORM_MACINTOSH_
  84.  
  85. // This really doesn't do anything for the exceptions, it just closes
  86. // the message catalog and the Opendoc log.  It's named the way it is
  87. // because the original Initialization function was named "ODInitExceptions"
  88. _DLLIMPORTEXPORT_ void ODTermExceptions( );
  89.  
  90. #endif
  91.  
  92.  
  93. //=====================================================================================
  94. // Debug Output
  95. //=====================================================================================
  96.  
  97. enum DebugOutputMode {
  98.     kNoOutput,
  99.     kWriteToFile,
  100. #ifndef _PLATFORM_MACINTOSH_
  101.     kWriteToDebugWindow
  102. #else
  103.     kWriteToDebugWindow,
  104.     kGenerateDebugStrs
  105. #endif
  106. };
  107.  
  108. _DLLIMPORTEXPORT_ DebugOutputMode    GetOutputMode( );
  109. _DLLIMPORTEXPORT_ void            SetOutputMode( DebugOutputMode );
  110.  
  111.  
  112. //=====================================================================================
  113. // Warnings
  114. //=====================================================================================
  115.  
  116. #ifdef _PLATFORM_MACINTOSH_
  117. // WARN has the same syntax as printf but produces a SysBreakStr.
  118. // Warnings are disabled (and generate no code) when ODDebug is off.
  119.  
  120. #define WARN    if(!ODDebug) ; else _Warn
  121.  
  122. #else
  123.  
  124. // WARNMSG prints a Warning.
  125. // WARN_INDEX and WARN_CATALOG are used as the first parameter of WARNMSG.
  126. // WARN_INDEX is used internally by OpenDoc, and assumes the use of the
  127. //            ODMessage catalog, and the AMSG set.
  128. // WARN_CATALOG can be used by anyone.  You pass it a handle to an open
  129. //            message catalog, the set number, and the message index.
  130. // Two examples of valid WARNMSG calls are as follows:
  131. //   WARNMSG(WARN_INDEX(0),"Here is a message without parameters.");
  132. //   WARNMSG(WARN_INDEX(0),"Message %d has a parameter.",2);
  133. #define WARN_INDEX(A)    NULL, NULL, A, __FILE__, __LINE__
  134. #define WARN_CATALOG(A,B,C)    (void*) A, B, C, __FILE__, __LINE__
  135. #define WARNMSG        _Warn
  136.  
  137. // If you have a DEBUG define set, you can use the ???_DEBUG macros.
  138. // They equate to real calls when DEBUG is set, and comments when
  139. // DEBUG is not set.  This way you can have warnings and asserts that
  140. // are only active when you have compiled with DEBUG set.
  141. #ifdef DEBUG
  142.     #define WARNMSG_DEBUG    _Warn
  143. #else
  144.     #define WARNMSG_DEBUG    //
  145. #endif
  146.  
  147. #endif // _PLATFORM_MACINTOSH_
  148.  
  149.  
  150. //=====================================================================================
  151. // Assertions
  152. //=====================================================================================
  153.  
  154. // These all cause a debugger break if the condition evaluates to false or 0.
  155. // Leading "W" is a Warning: it doesn't raise an exception.
  156. // Trailing "M" means special Message displayed instead of condition.
  157.  
  158. #ifndef __MWERKS__
  159.     #define _FIL_ ""            /* MPW puts entire pathnames in; yuk! */
  160. #else
  161.     #define _FIL_ __FILE__
  162. #endif
  163.  
  164. #ifdef _PLATFORM_MACINTOSH_
  165. #define ASSERT( COND, ERR )    \
  166.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, ERR )
  167. #define ASSERTM( COND, ERR, MSG )    \
  168.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, ERR, MSG )
  169. #define WASSERT( COND )    \
  170.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, 0)
  171. #define WASSERTM( COND, MSG )    \
  172.     if(!ODDebug || (COND)) ; else _AssertionFailed( #COND, _FIL_, 0, MSG)
  173. #else // not _PLATFORM_MACINTOSH_
  174.  
  175. #define ASSERT( COND, ERR )    \
  176.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, ERR )
  177.  
  178. #define ASSERTMSG( COND, ERR, MSG, CATALOGINDEX )    \
  179.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, ERR, MSG, CATALOGINDEX )
  180.  
  181. #define ASSERTCATALOG( COND, ERR, MSG, CATALOGINDEX, CATALOGHANDLE, SETINDEX ) \
  182.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, ERR, MSG, CATALOGINDEX, (void*) CATALOGHANDLE, SETINDEX )
  183.  
  184. #define WASSERT( COND )    \
  185.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, 0)
  186.  
  187. #define WASSERTMSG( COND, MSG, CATALOGINDEX )    \
  188.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, 0, MSG, CATALOGINDEX)
  189.  
  190. #define WASSERTCATALOG( COND, MSG, CATALOGINDEX, CATALOGHANDLE, SETINDEX )    \
  191.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, 0, MSG, CATALOGINDEX, (void*) CATALOGHANDLE, SETINDEX )
  192.  
  193. // If you have a DEBUG define set, you can use the ???_DEBUG macros.
  194. // They equate to real calls when DEBUG is set, and comments when
  195. // DEBUG is not set.  This way you can have warnings and asserts that
  196. // are only active when you have compiled with DEBUG set.
  197. #ifdef DEBUG
  198.     #define ASSERT_DEBUG( COND, ERR )    \
  199.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, ERR )
  200.     #define ASSERTMSG_DEBUG( COND, ERR, MSG )    \
  201.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, ERR, MSG, -1 )
  202.     #define WASSERT_DEBUG( COND )    \
  203.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, 0)
  204.     #define WASSERTMSG_DEBUG( COND, MSG )    \
  205.     if(COND) ; else _AssertionFailed( #COND, __FILE__, __LINE__, 0, MSG, -1)
  206. #else
  207.     #define ASSERT_DEBUG( COND, ERR )    //
  208.     #define ASSERTMSG_DEBUG( COND, ERR, MSG )    //
  209.     #define WASSERT_DEBUG( COND )    //
  210.     #define WASSERTMSG_DEBUG( COND, MSG )    //
  211. #endif
  212. #endif // _PLATFORM_MACINTOSH_
  213.  
  214. // ASSERT_NOT_NULL causes a debugger break and an exception if the parameter
  215. // is NULL. Use this in functions that take a pointer as a parameter but do
  216. // not allow the parameter to be NULL.
  217. // Do **NOT** use this macro to make sure memory allocation succeeded! It
  218. // has no effect in non-debug builds. Use THROW_IF_NULL instead.
  219.  
  220. #define ASSERT_NOT_NULL(PARAM) \
  221.     ASSERT((PARAM)!=kODNULL, kODErrIllegalNullInput)
  222.  
  223.  
  224. //=====================================================================================
  225. // Logging
  226. //=====================================================================================
  227.  
  228. // PRINT writes to the standard output via somPrintf.
  229.  
  230. #ifdef _PLATFORM_MACINTOSH_
  231. #define PRINT   if(!ODDebug) ; else somPrintf
  232. #else
  233. #define PRINT    somPrintf
  234. #endif
  235.  
  236. // LOG is like PRINT but can easily be turned on or off on a per-file basis.
  237. // To enable logging in a source file, you must redefine the symbol LOGGING
  238. // as "1" in that file, otherwise LOG statements will not be compiled. Make
  239. // sure to #undef the symbol before you re-#define it, as some compilers
  240. // won't redefine an already-defined symbol.
  241.  
  242. // PRINT and LOG statements do not generate any code if logging is off.
  243.  
  244. #define LOGGING 0        // Redefine as 1 in source file to enable logging
  245.  
  246. #ifdef _PLATFORM_MACINTOSH_
  247. #define LOG        if(!ODDebug || !LOGGING) ; else somPrintf
  248. #else
  249. #define LOG        if(!LOGGING) ; else somPrintf
  250. #endif
  251.  
  252.  
  253. //==============================================================================
  254. // Safe Type-Casting
  255. //==============================================================================
  256.  
  257. /*    Use CAST as a safe way to cast a SOM object from one class to another.
  258.     For instance, if "o" is declared as an ODObject*, but your code knows
  259.     it's an ODPart*, you can say:
  260.             ODPart *part = CAST(o,ODPart);
  261.     If ODDebug is turned on, this will do a runtime check and cause an assertion
  262.     failure if o does not point to an ODPart (or subclass). Without ODDebug,
  263.     it degenerates into a simple C-style cast that generates no code.
  264.     
  265.     ASSERT_IS_A is similar to CAST but is used when you just want to assert
  266.     that the pointer points to the right kind of object.
  267. */
  268.  
  269. #if ODDebug
  270. #ifdef _PLATFORM_MACINTOSH_
  271.     #define CAST(OBJ, CLASS)    ( (CLASS*)_Cast((OBJ), (somClassDataStructure*) &CLASS##ClassData,        \
  272.                                                         CLASS##_MajorVersion, CLASS##_MinorVersion) )
  273. #else
  274.     #define CAST(OBJ, CLASS)    ( (CLASS*)_Cast((OBJ), CLASS##ClassData.classObject,        \
  275.                                                         CLASS##_MajorVersion, CLASS##_MinorVersion) )
  276. #endif // _PLATFORM_MACINTOSH_
  277.     #define ASSERT_IS_A(OBJ,CLASS)    ( (void) CAST(OBJ,CLASS) )
  278. #else
  279.     #define CAST(OBJ, CLASS)    ( (CLASS*) (OBJ) )
  280.     #define ASSERT_IS_A(OBJ,CLASS)    /* */
  281. #endif
  282.  
  283.  
  284. //=====================================================================================
  285. // Internal goop...
  286. //=====================================================================================
  287.  
  288. #ifdef _PLATFORM_MACINTOSH_
  289. void _Warn                ( char *fmt, ... );
  290. void _AssertionFailed    ( char *cond,  char *fileName,
  291.                             ODError, char *msg = kODNULL );
  292. #else
  293. _DLLIMPORTEXPORT_ void _Warn( void* catalogHandle, int setIndex, int catalogIndex, const char* fileName, const int lineNbr, char *fmt, ... );
  294. _DLLIMPORTEXPORT_ void _AssertionFailed    ( char *cond,  const char *fileName, const int lineNbr,
  295.             ODError, char *msg = kODNULL, int catalogIndex=NULL, void* catalogHandle=NULL, int setIndex=NULL );
  296. #endif
  297. #if ODDebug
  298. #ifdef _PLATFORM_MACINTOSH_
  299. SOMObject* _Cast( SOMObject *obj, somClassDataStructure *cls, long major, long minor );
  300. #else
  301. SOMObject* _Cast( SOMObject *obj, SOMClass *cls, long major, long minor );
  302. #endif
  303. #endif
  304.  
  305.  
  306. #ifdef __cplusplus
  307. }
  308. #endif
  309.  
  310. #ifdef _OD_IMPL_SHARE_UTILS_
  311. #pragma import off
  312. #endif
  313.  
  314.  
  315. #endif /*_ODDEBUG_*/
  316.