| 1 |
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. |
| 2 |
This software is licensed as OpenSource, under the Apache License, Version 2.0. |
| 3 |
This license is available at: http://opensource.org/licenses/Apache-2.0. */ |
| 4 |
|
| 5 |
/* |
| 6 |
canthappen.h |
| 7 |
*/ |
| 8 |
|
| 9 |
/* |
| 10 |
Charter -- Provide an abnormal termination facility, which can identify the |
| 11 |
source of the fatal error location by source file name and line number, and |
| 12 |
which supports a programmer-generated termination message, incorporating |
| 13 |
formatted values of execution data. |
| 14 |
*/ |
| 15 |
#ifndef CANTHAPPEN_H |
| 16 |
#define CANTHAPPEN_H |
| 17 |
|
| 18 |
#include "supportpublictypes.h" |
| 19 |
#include "supportenvironment.h" |
| 20 |
|
| 21 |
#ifndef WINATM |
| 22 |
#define WINATM 0 |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifndef CDECL |
| 26 |
#define CDECL |
| 27 |
#endif |
| 28 |
|
| 29 |
#ifndef os_abort |
| 30 |
#if OS == os_mach |
| 31 |
#define os_abort abort |
| 32 |
#include "supportossyslib.h" |
| 33 |
#elif OS == os_mac |
| 34 |
#define os_abort() DebugStr("\pcanthappen") |
| 35 |
#elif WINATM |
| 36 |
#ifdef CODE32 |
| 37 |
#define os_abort() \ |
| 38 |
{ \ |
| 39 |
char *n = NULL, m; \ |
| 40 |
m = *n; \ |
| 41 |
} |
| 42 |
#else |
| 43 |
#define os_abort() \ |
| 44 |
{ \ |
| 45 |
char *n = NULL, m; \ |
| 46 |
_asm { int 3} \ |
| 47 |
; \ |
| 48 |
m = *n; \ |
| 49 |
} |
| 50 |
#endif |
| 51 |
#else |
| 52 |
#include <stdlib.h> |
| 53 |
#define os_abort abort |
| 54 |
#endif |
| 55 |
#endif |
| 56 |
|
| 57 |
/*----------------------------------------------------------------------*/ |
| 58 |
/* USER INTERFACE -- Summary of Calls */ |
| 59 |
/*----------------------------------------------------------------------*/ |
| 60 |
/* CantHappen and Assert MACROS |
| 61 |
---------------------------- |
| 62 |
(For new development) |
| 63 |
|
| 64 |
AssertMsg(cond,errID,msg); Assert condition; if cond fail, quit w. msg. |
| 65 |
|
| 66 |
DebugAssertMsg(cond,errID,msg); Assert condition; if cond fail, quit w. msg. |
| 67 |
Note: DebugAssertMsg is compiled only for |
| 68 |
DEVELOP stage. |
| 69 |
|
| 70 |
CantHappenMsg(errID,msg); Quit with msg. |
| 71 |
CantHappenMsg1(errID,format,v1) Quit with msg and 1 formatted value. |
| 72 |
CantHappenMsg2(errID,format,v1,v2) Quit with msg and 2 formatted values. |
| 73 |
CantHappenMsg3(errID,format,v1,v2,v3) Quit with msg and 3 formatted values. |
| 74 |
|
| 75 |
CantHappen and Assert MACROS |
| 76 |
---------------------------- |
| 77 |
(For Backward Compatibility) |
| 78 |
|
| 79 |
Assert(cond); Assert condition; if cond fails, quit. |
| 80 |
|
| 81 |
DebugAssert(cond); Assert condition; if cond fails, quit. |
| 82 |
Note: DebugAssert is compiled only for |
| 83 |
DEVELOP stage. |
| 84 |
CantHappen(); Quit |
| 85 |
|
| 86 |
|
| 87 |
examples: |
| 88 |
--------- |
| 89 |
*/ |
| 90 |
|
| 91 |
/* |
| 92 |
|
| 93 |
Notes: |
| 94 |
------ |
| 95 |
1) CantHappen(), Assert(), DebugAssert() should not be used for new |
| 96 |
development. Included only for backward compatibility. |
| 97 |
2) errID is a 2-part error ID: |
| 98 |
2a) the most significant 8 bits forms the package ID number |
| 99 |
(see pkgid.h in util pkg for definitions) |
| 100 |
2b) for a package listed in pkgid.h - the lower 24 bits forms the |
| 101 |
error ID within the package. For example, for the pslib pkg, |
| 102 |
see the psliberrid.h file in the pslib pkg sources. |
| 103 |
2c) for a package group listed in pkgid.h - the lower 24 bits forms the |
| 104 |
error ID within the package group. For example, for the PS Server |
| 105 |
package group, see the (for example) external interface |
| 106 |
PSSERVERERRID. |
| 107 |
2d) for a product - the lower 24 bits forms the error ID within the |
| 108 |
product. See the MUMBLEerrid.h file in the product sources, |
| 109 |
for example the refsyserrid.h file in the adobe/rxrefsys/sources. |
| 110 |
3) msg is a C-language string, e.g. "Abort Message" |
| 111 |
4) format is a format string legal for the C function printf, excluding |
| 112 |
floating point values. |
| 113 |
5) string is buffer of sufficient size to hold formatted DebugSprintf value. |
| 114 |
6) v1,v2,v3 are values to match format. |
| 115 |
7) length of expanded formatted data must be less than 256 bytes. |
| 116 |
8) For EXPORT stage, msg or formatting strings and formatted values are not |
| 117 |
compiled; they are stripped by the macro, to avoid taking up ROM space. |
| 118 |
Only the errID is compiled and used in the termination message. |
| 119 |
*/ |
| 120 |
|
| 121 |
/*----------------------------------------------------------------------*/ |
| 122 |
/* EXPORTED PROCEDURES -- Procedure called primarily by macros in this file */ |
| 123 |
/*----------------------------------------------------------------------*/ |
| 124 |
extern procedure CDECL CantHappenPrintf( |
| 125 |
readonly char* pPgmFileName, |
| 126 |
IntX pgmLineNum, |
| 127 |
Card32 errID, |
| 128 |
readonly char* formatString, ... |
| 129 |
/* 0-n vars, as per formatString .... */ |
| 130 |
); |
| 131 |
/* |
| 132 |
This is called by the CantHappenMsg1, CantHappenMsg2, CantHappenMsg3 macros, |
| 133 |
when STAGE == DEVELOP. It has the same syntax as the ANSI C printf |
| 134 |
statement, except that it always expects the filename and line number, and the |
| 135 |
errID as the first arguments. The formatString describes Only the |
| 136 |
programmer-generated message and the formatting of the program |
| 137 |
variables which follow the formatString; it does Not deal with the |
| 138 |
filename and line number and errID, which are dealt with internal to the |
| 139 |
procedure. |
| 140 |
Note: |
| 141 |
See "USER INTERFACE -- Summary of Calls" at start of file for errID descr. |
| 142 |
Note: |
| 143 |
No \n must appear in the message you code; this is added, together with other |
| 144 |
information, in the message generated by CantHappenPrintf. |
| 145 |
Example: |
| 146 |
CantHappenPrintf(__FILE__, __LINE__, PSLIB_MUMBLE_ERR, |
| 147 |
"MyProc: Too many gorfs(%d)", numGorfs); |
| 148 |
*/ |
| 149 |
|
| 150 |
/*----------------------------------------------------------------------*/ |
| 151 |
/* EXPORTED PROCEDURES -- Procedures called only by macros in this file */ |
| 152 |
/*----------------------------------------------------------------------*/ |
| 153 |
extern procedure CantHappenForDevelop( |
| 154 |
Card32 errID, |
| 155 |
readonly char* pMessage, |
| 156 |
readonly char* pPgmFileName, |
| 157 |
IntX lineNum); |
| 158 |
/* |
| 159 |
This is called by the CantHappenMsg macro when STAGE == DEVELOP. |
| 160 |
It is passed a programmer-generated errID and termination message, plus the |
| 161 |
filename and line number generated by the CantHappenMsg macro. |
| 162 |
Note: |
| 163 |
See "USER INTERFACE -- Summary of Calls" at start of file for errID descr. |
| 164 |
Note: |
| 165 |
No \n need appear in the message you code; this is added, together with other |
| 166 |
information, in the message generated by CantHappenForDevelop. |
| 167 |
Example: |
| 168 |
CantHappenForDevelop(PSLIB_MUMBLE_ERR, |
| 169 |
"MyProc: My abort msg", __FILE__,__LINE__); |
| 170 |
*/ |
| 171 |
|
| 172 |
extern procedure CantHappenForExport( |
| 173 |
Card32 errID); |
| 174 |
|
| 175 |
/* |
| 176 |
This is called by the CantHappenMsg, CantHappenMsg1, CantHappenMsg2 and |
| 177 |
CantHappenMsg3 macros when STAGE == EXPORT. It is passed the programmer- |
| 178 |
generated errID. |
| 179 |
p Note: |
| 180 |
See "USER INTERFACE -- Summary of Calls" at start of file for errID descr. |
| 181 |
Example: |
| 182 |
CantHappenForExport(PSLIB_MUMBLE_ERR); |
| 183 |
*/ |
| 184 |
|
| 185 |
#if (ANSI_C) |
| 186 |
extern procedure AssertForANSIDevelop( |
| 187 |
Card32 errID, |
| 188 |
readonly char* pMessage, |
| 189 |
readonly char* pPgmFileName, |
| 190 |
int pgmLineNum, |
| 191 |
readonly char* pAssertCond); |
| 192 |
/* |
| 193 |
This is called by the Assert and DebugAssert macros, when STAGE == DEVELOP |
| 194 |
and an ANSI compiler is used. It is passed a programmer-generated errID and |
| 195 |
termination message and Assert condition, plus the filename & line number |
| 196 |
generated by the Assert macro. |
| 197 |
Note: |
| 198 |
See "USER INTERFACE -- Summary of Calls" at start of file for errID descr. |
| 199 |
Note: |
| 200 |
No \n must appear in the message you code; this is added, together with other |
| 201 |
information, in the message generated by AssertForANSIDevelop. |
| 202 |
*/ |
| 203 |
#else /* (ANSI_C) */ |
| 204 |
|
| 205 |
extern procedure AssertForNonANSIDevelop( |
| 206 |
Card32 errID, |
| 207 |
readonly char* pMessage, |
| 208 |
readonly char* pPgmFileName, |
| 209 |
int pgmLineNum); |
| 210 |
|
| 211 |
/* |
| 212 |
This is called by the Assert and DebugAssert macros, when STAGE == DEVELOP |
| 213 |
and a non-ANSI compiler is used. It is passed a programmer-generated errID and |
| 214 |
termination message, plus the filename & line number generated by the |
| 215 |
Assert macro. |
| 216 |
Note: |
| 217 |
See "USER INTERFACE -- Summary of Calls" at start of file for errID descr. |
| 218 |
Note: |
| 219 |
No \n must appear in the message you code; this is added, together with other |
| 220 |
information, in the message generated by AssertForNonANSIDevelop. |
| 221 |
*/ |
| 222 |
#endif /* (ANSI_C) */ |
| 223 |
|
| 224 |
extern procedure AssertForExport( |
| 225 |
Card32 errID); |
| 226 |
|
| 227 |
/* |
| 228 |
This is called by the Assert macro when STAGE == EXPORT, passing the |
| 229 |
programmer-generated errID. |
| 230 |
*/ |
| 231 |
|
| 232 |
/*----------------------------------------------------------------------*/ |
| 233 |
/* IN-LINE PROCEDURES -- for new CantHappenMsg, AssertMsg, DebugAssertMsg */ |
| 234 |
/*----------------------------------------------------------------------*/ |
| 235 |
/* Use these for New Development */ |
| 236 |
/*-------------------------------------------------------------------------*/ |
| 237 |
/* AssertMsg Macro */ |
| 238 |
#if (STAGE == DEVELOP) |
| 239 |
#if (ANSI_C) |
| 240 |
#define AssertMsg(cond, errID, msg) \ |
| 241 |
((void)((cond) || \ |
| 242 |
(AssertForANSIDevelop(errID, msg, __FILE__, __LINE__, #cond), 0))) |
| 243 |
/* ANSI DEVELOP AssertMsg */ |
| 244 |
#else /* not ANSI */ |
| 245 |
#define AssertMsg(cond, errID, msg) \ |
| 246 |
((void)((cond) || \ |
| 247 |
(AssertForNonANSIDevelop(errID, msg, __FILE__, __LINE__), 0))) |
| 248 |
/* non_ANSI DEVELOP AssertMsg */ |
| 249 |
#endif /* (ANSI_C) */ |
| 250 |
#else /* (STAGE == DEVELOP) */ |
| 251 |
#define AssertMsg(cond, errID, msg) \ |
| 252 |
((void)((cond) || \ |
| 253 |
(AssertForExport(errID), 0))) /* EXPORT AssertMsg */ |
| 254 |
#endif /* (STAGE == DEVELOP) */ |
| 255 |
|
| 256 |
/*-------------------------------------------------------------------------*/ |
| 257 |
/* DebugAssertMsg Macro */ |
| 258 |
#if (STAGE == DEVELOP) |
| 259 |
#if (ANSI_C) |
| 260 |
#define DebugAssertMsg(cond, errID, msg) \ |
| 261 |
((void)((cond) || \ |
| 262 |
(AssertForANSIDevelop(errID, msg, __FILE__, __LINE__, #cond), 0))) |
| 263 |
/* ANSI DEVELOP DebugAssertMsg */ |
| 264 |
#else /* not ANSI */ |
| 265 |
#define DebugAssertMsg(cond, errID, msg) \ |
| 266 |
((void)((cond) || \ |
| 267 |
(AssertForNonANSIDevelop(errID, msg, __FILE__, __LINE__), 0))) |
| 268 |
/* non_ANSI DEVELOP DebugAssertMsg */ |
| 269 |
#endif /* (ANSI_C) */ |
| 270 |
#else /* (STAGE == DEVELOP) */ |
| 271 |
#define DebugAssertMsg(cond, errID, msg) /* No-op if not DEVELOP stage */ |
| 272 |
#endif /* (STAGE == DEVELOP) */ |
| 273 |
/*-------------------------------------------------------------------------*/ |
| 274 |
/* CantHappenMsg Macro */ |
| 275 |
|
| 276 |
#if (STAGE == DEVELOP) |
| 277 |
#define CantHappenMsg(errID, msg) \ |
| 278 |
CantHappenForDevelop(errID, msg, __FILE__, __LINE__) |
| 279 |
/* DEVELOP Cant Happen */ |
| 280 |
#else /* (STAGE == DEVELOP) */ |
| 281 |
#define CantHappenMsg(errID, msg) \ |
| 282 |
CantHappenForExport(errID) /* EXPORT Cant Happen */ |
| 283 |
#endif /* (STAGE == DEVELOP) */ |
| 284 |
|
| 285 |
/*-------------------------------------------------------------------------*/ |
| 286 |
/* CantHappenMsg1 Macro */ |
| 287 |
#if (STAGE == DEVELOP) |
| 288 |
#define CantHappenMsg1(errID, format, v1) \ |
| 289 |
CantHappenPrintf(__FILE__, __LINE__, errID, format, v1) |
| 290 |
/* DEVELOP Cant Happen */ |
| 291 |
#else /* (STAGE == DEVELOP) */ |
| 292 |
#define CantHappenMsg1(errID, format, v1) \ |
| 293 |
CantHappenForExport(errID) /* EXPORT Cant Happen */ |
| 294 |
#endif /* (STAGE == DEVELOP) */ |
| 295 |
/*-------------------------------------------------------------------------*/ |
| 296 |
/* CantHappenMsg2 Macro */ |
| 297 |
#if (STAGE == DEVELOP) |
| 298 |
#define CantHappenMsg2(errID, format, v1, v2) \ |
| 299 |
CantHappenPrintf(__FILE__, __LINE__, errID, format, v1, v2) |
| 300 |
/* DEVELOP Cant Happen */ |
| 301 |
#else /* (STAGE == DEVELOP) */ |
| 302 |
#define CantHappenMsg2(errID, format, v1, v2) \ |
| 303 |
CantHappenForExport(errID) /* EXPORT Cant Happen */ |
| 304 |
#endif /* (STAGE == DEVELOP) */ |
| 305 |
/*-------------------------------------------------------------------------*/ |
| 306 |
/* CantHappenMsg3 Macro */ |
| 307 |
#if (STAGE == DEVELOP) |
| 308 |
#define CantHappenMsg3(errID, format, v1, v2, v3) \ |
| 309 |
CantHappenPrintf(__FILE__, __LINE__, errID, format, v1, v2, v3) |
| 310 |
/* DEVELOP Cant Happen */ |
| 311 |
#else /* (STAGE == DEVELOP) */ |
| 312 |
#define CantHappenMsg3(errID, format, v1, v2, v3) \ |
| 313 |
CantHappenForExport(errID) /* EXPORT Cant Happen */ |
| 314 |
#endif /* (STAGE == DEVELOP) */ |
| 315 |
/*-------------------------------------------------------------------------*/ |
| 316 |
|
| 317 |
/*----------------------------------------------------------------------*/ |
| 318 |
/* IN-LINE PROCEDURES -- for old CantHappen, Assert, DebugAssert */ |
| 319 |
/*----------------------------------------------------------------------*/ |
| 320 |
/* For Backward Compatibility; |
| 321 |
These calls are planned to be eliminated, |
| 322 |
after the conversion to the new facilities |
| 323 |
(which support messages, error numbers and |
| 324 |
which provide source filename and line |
| 325 |
numbers) are in full use. */ |
| 326 |
/*-------------------------------------------------------------------------*/ |
| 327 |
/* CantHappen() */ |
| 328 |
#if (STAGE == DEVELOP) |
| 329 |
#define CantHappen() \ |
| 330 |
CantHappenForDevelop((Card32)0, NULL, __FILE__, __LINE__) |
| 331 |
/* DEVELOP Cant Happen */ |
| 332 |
#else /* (STAGE == DEVELOP) */ |
| 333 |
#define CantHappen() \ |
| 334 |
CantHappenForExport((Card32)0) /* EXPORT Cant Happen */ |
| 335 |
#endif /* (STAGE == DEVELOP) */ |
| 336 |
/*-------------------------------------------------------------------------*/ |
| 337 |
/* Assert(cond) */ |
| 338 |
#if (STAGE == DEVELOP) |
| 339 |
#if (ANSI_C) |
| 340 |
#define Assert(cond) \ |
| 341 |
((void)((cond) || \ |
| 342 |
(AssertForANSIDevelop((Card32)0, NULL, __FILE__, __LINE__, #cond), 0))) |
| 343 |
/* ANSI DEVELOP Assert */ |
| 344 |
#else /* not ANSI */ |
| 345 |
#define Assert(cond) \ |
| 346 |
((void)((cond) || \ |
| 347 |
(AssertForNonANSIDevelop((Card32)0, NULL, __FILE__, __LINE__), 0))) |
| 348 |
/* non_ANSI DEVELOP Assert */ |
| 349 |
#endif /* (ANSI_C) */ |
| 350 |
#else /* (STAGE == DEVELOP) */ |
| 351 |
#define Assert(cond) \ |
| 352 |
((void)((cond) || \ |
| 353 |
(CantHappenForExport((Card32)0), 0))) /* EXPORT Assert */ |
| 354 |
#endif /* (STAGE == DEVELOP) */ |
| 355 |
/*-------------------------------------------------------------------------*/ |
| 356 |
/* DebugAssert(cond) */ |
| 357 |
#if (STAGE == DEVELOP) |
| 358 |
#if (ANSI_C) |
| 359 |
#define DebugAssert(cond) \ |
| 360 |
((void)((cond) || \ |
| 361 |
(AssertForANSIDevelop((Card32)0, NULL, __FILE__, __LINE__, #cond), 0))) |
| 362 |
/* ANSI DEVELOP DebugAssert */ |
| 363 |
#else /* not ANSI */ |
| 364 |
#define DebugAssert(cond) \ |
| 365 |
((void)((cond) || \ |
| 366 |
(AssertForNonANSIDevelop((Card32)0, NULL, __FILE__, __LINE__), 0))) |
| 367 |
/* non_ANSI DEVELOP DebugAssert */ |
| 368 |
#endif /* (ANSI_C) */ |
| 369 |
#else /* (STAGE == DEVELOP) */ |
| 370 |
#define DebugAssert(cond) /* No-op if not DEVELOP stage */ |
| 371 |
#endif /* (STAGE == DEVELOP) */ |
| 372 |
/*-------------------------------------------------------------------------*/ |
| 373 |
|
| 374 |
#endif /* CANTHAPPEN_H */ |