home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / mac / oop / macapp3 / 179 < prev    next >
Encoding:
Text File  |  1992-12-12  |  6.7 KB  |  264 lines

  1. Newsgroups: comp.sys.mac.oop.macapp3
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!apple!mumbo.apple.com!gallant.apple.com!wintermute.apple.com!user
  3. From: ksand@apple.com (Kent Sandvik )
  4. Subject: Re: TAppl.InstallCohandler mo
  5. Sender: news@gallant.apple.com
  6. Message-ID: <ksand-121292151926@wintermute.apple.com>
  7. Date: Sat, 12 Dec 1992 23:25:18 GMT
  8. References: <724097388.6934764@AppleLink.Apple.COM>
  9. Organization: (Evil Eye Creature from Mars, Inc.)
  10. Followup-To: comp.sys.mac.oop.macapp3
  11. Lines: 251
  12.  
  13. In article <724097388.6934764@AppleLink.Apple.COM>,
  14. DEWAELE.W@AppleLink.Apple.COM (De Waele Willy - Gent,BE,IHD) wrote:
  15. >  
  16. > Indeed, Mark, debug versions (of frameworks and our own applications - MacApp
  17. > and others) should be defensive programmed (which save a lot of time), i use
  18. > the following technique taking your example:
  19. >  
  20. > TFoo::Something()
  21. > {...
  22. > #if qDebug
  23. >    if (!IsObject(aCohandler)) {
  24. >      SysBreakStr("TFoo::Something - aCohandler is invalid!");
  25. >     return; }
  26. > #endif
  27. > ...
  28. > }
  29. > SADE must be started up, but i think this is no problem because when working
  30. > with MacApp 3.x, we need a lot of meg's (i don't like limited resources! - *The
  31. > sky's the limit!*).
  32.  
  33. SysBreakStr are kind of evil, unless SADE is running you usually get a 
  34. System crash. Here's an ASSERT package I worked a little bit on two
  35. weeks ago, it's not yet complete but it could provide material for 
  36. someone else writing similar stuff. Yes, I know of the Assertion MacApp
  37. version, but I wanted more flexibility in my ASSERT package. Note that
  38. this is not a full example, you need to add an Alert resource and a couple
  39. of other small things -- but the main thing was to stimulate the idea
  40. of writing similar flexible ASSERT packages. And this one should be ready
  41. within a month or so (and will then be available on the developer CD).
  42.  
  43. Kent
  44. ---- snippet stuff starts here ----
  45.  
  46. // TESTING, used for flagging that we use test code inside the classes and
  47. frameworks
  48. // TESTLEVEL0        - user friendly alerts as part of the application
  49. // TESTLEVEL1     - minor test level (detailed error message in alert box)
  50. // TESTLEVEL2     - semi-major test level (high level debuggers)
  51. // TESTLEVEL3     - major test level (low level debuggers)
  52. // TESTLOG1        - trigger a file log of information
  53. // TESTLOG2        - trigger a file log of information from a low level debugger
  54.  
  55. #define TESTLEVEL3
  56.  
  57. //
  58. _________________________________________________________________________________________________________
  59. //
  60. //    2. STRING FUNCTIONS
  61.  
  62. // return the len of a C string
  63. inline short clen(char *cptr)
  64. {
  65.     short    i;
  66.  
  67.     for (i = 0; cptr[i]; ++i)
  68.         ;
  69.     return(i);
  70. }
  71.  
  72.  
  73. // Convert a c-string to a pascal-string
  74.  
  75. inline void    c2p(char *cptr)
  76. {
  77.     char    len;
  78.  
  79.     BlockMove(cptr, cptr + 1, len = clen(cptr));
  80.     *cptr = len;
  81. }
  82.  
  83.  
  84. // Convert a pascal-string to a c-string
  85. inline void    p2c(StringPtr cptr)
  86. {
  87.     char    len;
  88.  
  89.     BlockMove(cptr + 1, cptr, len = *cptr);
  90.     cptr[len] = 0;
  91. }
  92.  
  93.  
  94. // Copy two Pascal strings
  95. inline void    Pstrcpy(StringPtr d, StringPtr s)
  96. {
  97.     short    i;
  98.  
  99.     i = *s;
  100.     do {
  101.         d[i] = s[i];
  102.     } while (i--);
  103. }
  104.  
  105.  
  106. // Concatenate two Pascal strings
  107. inline void ConcatPStrings(Str255 first,
  108.                            Str255 second)
  109. {
  110.     short charsToCopy;
  111.  
  112.     // Truncate if concatenated string would be longer than 255 chars
  113.     charsToCopy = Min(second[0], 255 - first[0]);
  114.     BlockMove(second + 1, first + first[0] + 1, (long)charsToCopy);
  115.     first[0] += charsToCopy;
  116. }
  117.  
  118.  
  119. //
  120. _________________________________________________________________________________________________________
  121. //
  122. // MACROS AND DEBUGGING
  123. // ASSERT Function, used for testing conditions and flagging error states.
  124. // Example: vdebugstr("The error message from Foo is %i", fError);
  125.  
  126. // We need to load in certain include files if we want to use the alert
  127. boxes for
  128. // high level ASSERT functions (we also need to include the
  129. GUIApplication.r file
  130. // into the project).
  131.  
  132. #if (defined TESTLEVEL0 || defined TESTLEVEL1)
  133. #include "ApplicationResources.h"
  134. #endif
  135.  
  136.  
  137. //  Define this flag if you want file and line information as part of the
  138. message.
  139. #define FILELINEINFO
  140.  
  141.  
  142. //     fileinfo will keep track of the current file and line in a global.
  143. static char fileNameArray[32];
  144. static char lineNumberArray[32];
  145.  
  146.  
  147. #if (defined TESTLEVEL0 || defined TESTLEVEL1)
  148. inline void AlertUser(Str255 theMessage)
  149. {
  150.     short itemHit;
  151.     
  152.     if(!(AEInteractWithUser(kNoTimeOut, nil, nil)))                // could we interact
  153. with the user?
  154.     {
  155.         ParamText(theMessage, 0, 0,0);
  156.         itemHit = Alert(kUserAlert, nil);
  157.     }
  158. }
  159. #endif
  160.  
  161. inline void fileinfo(char *file, int line)
  162. {
  163.     sprintf(fileNameArray, "File %s ", file);
  164.     sprintf(lineNumberArray, " Line %d  #", line);
  165. }
  166.  
  167.  
  168. //    vxdebugstr that also prints out by default __FILE__ and __LINE__
  169. information
  170. inline void vxdebugstr(char* format,...) 
  171. {
  172.     char buff[255];
  173.     char final[255];
  174.  
  175.     va_list arglist;
  176.     va_start(arglist,format);
  177.     vsprintf(buff,format,arglist);
  178.     va_end(arglist);
  179.  
  180. #ifdef FILELINEINFO    
  181.     strcat(final,fileNameArray);
  182. #ifdef TESTLEVEL2
  183.     strcat(final, " ;");                            // MacsBug does not like embedded ;s
  184. #endif
  185.     strcat(final, lineNumberArray);
  186. #endif
  187.  
  188.     strcat(final, buff);
  189.     c2p(final);
  190.     
  191.     // Print out the message, based on the testing level
  192. #ifdef TESTLEVEL1
  193.     AlertUser((Str255)final);
  194. #endif
  195. #ifdef TESTLEVEL2
  196.     SysBreakStr((Str255)final);                        // for high level debuggers
  197. #endif
  198. #ifdef TESTLEVEL3
  199.     DebugStr((Str255)final);
  200. #endif
  201. }
  202.  
  203.  
  204. //    ASSERT MACROS
  205.  
  206. //    ASSERT, small (and quick assert),  if assertion is false it will print
  207. out 
  208. //     a message
  209. //    Use: ASSERT(false, "Problems with TSoundRecorder");
  210.  
  211. #if (defined TESTLEVEL0 || defined TESTLEVEL1)
  212. #define ASSERT(condition,debugMsg) do {                        \
  213.     if(condition == 0)                                    \
  214.     {                                                \
  215.         AlertUser(debugMsg);                            \
  216.     }                                                \
  217.    } while (0)                                            \
  218.  
  219. #elif (defined TESTLEVEL2 || defined TESTLEVEL3)
  220. #define ASSERT(condition,debugMsg) do {                        \
  221.     if(condition == 0)                                    \
  222.     {                                                \
  223.         DebugStr(debugMsg);                            \
  224.     }                                                \
  225.    } while (0)
  226. #else
  227. #define ASSERT(condition,debugMsg) ((void) 0)
  228. #endif
  229.  
  230.  
  231. //    VASSERT, if assertion is false it will print out file, line, message and
  232. possible variable
  233. //    Use: VASSERT(false,("Problems with %d", anErr));
  234.  
  235. #if (defined TESTLEVEL0)
  236. ##define VASSERT(condition,debugMsg) do {                    \
  237.     if(condition == 0)                                    \
  238.     {                                                \
  239.         vxdebugstr  debugMsg;                            \
  240.     }                                                \
  241.    } while (0)
  242.  
  243. #elif (defined TESTLEVEL1 || defined TESTLEVEL2 ||  defined TESTLEVEL3)
  244. #define VASSERT(condition,debugMsg) do {                    \
  245.     if(condition == 0)                                    \
  246.     {                                                \
  247.         fileinfo(__FILE__, __LINE__);                        \
  248.         vxdebugstr  debugMsg;                            \
  249.     }                                                \
  250.    } while (0)
  251.    
  252. #else
  253. #define (condition,debugMsg) ((void) 0)
  254. #endif
  255.  
  256.  
  257.  
  258.  
  259. -------------------
  260. Kent Sandvik (UUCP: ....!apple!ksand; INTERNET: ksand@apple.com)
  261. DISCLAIMER: Private activities on the Net.
  262.