exception.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %        EEEEE  X   X   CCCC  EEEEE  PPPP  TTTTT  IIIII   OOO   N   N         %
00007 %        E       X X   C      E      P   P   T      I    O   O  NN  N         %
00008 %        EEE      X    C      EEE    PPPP    T      I    O   O  N N N         %
00009 %        E       X X   C      E      P       T      I    O   O  N  NN         %
00010 %        EEEEE   X  X   CCCC  EEEEE  P       T    IIIII   OOO   N   N         %
00011 %                                                                             %
00012 %                                                                             %
00013 %                    Wizards Toolkit Exception Methods                        %
00014 %                                                                             %
00015 %                             Software Design                                 %
00016 %                               John Cristy                                   %
00017 %                                July 1993                                    %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.wizards-toolkit.org/script/license.php                        %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %
00037 %
00038 */
00039 
00040 /*
00041   Include declarations.
00042 */
00043 #include "wizard/studio.h"
00044 #include "wizard/client.h"
00045 #include "wizard/exception.h"
00046 #include "wizard/exception-private.h"
00047 #include "wizard/hashmap.h"
00048 #include "wizard/log.h"
00049 #include "wizard/memory_.h"
00050 #include "wizard/string_.h"
00051 #include "wizard/utility.h"
00052 #include "wizard/wizard.h"
00053 
00054 /*
00055   Typedef declarations.
00056 */
00057 struct _ExceptionInfo
00058 {
00059   ExceptionType
00060     severity;
00061 
00062   char
00063     *reason,
00064     *description;
00065 
00066   void
00067     *exceptions;
00068 
00069   WizardBooleanType
00070     relinquish;
00071 
00072   SemaphoreInfo
00073     *semaphore;
00074 
00075   size_t
00076     signature;
00077 };
00078 
00079 /*
00080   Forward declarations.
00081 */
00082 #if defined(__cplusplus) || defined(c_plusplus)
00083 extern "C" {
00084 #endif
00085 
00086 static void
00087   DefaultErrorHandler(const ExceptionType,const char *,const char *),
00088   DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
00089   DefaultWarningHandler(const ExceptionType,const char *,const char *);
00090 
00091 #if defined(__cplusplus) || defined(c_plusplus)
00092 }
00093 #endif
00094 
00095 /*
00096   Global declarations.
00097 */
00098 static ErrorHandler
00099   error_handler = DefaultErrorHandler;
00100 
00101 static FatalErrorHandler
00102   fatal_error_handler = DefaultFatalErrorHandler;
00103 
00104 static WarningHandler
00105   warning_handler = DefaultWarningHandler;
00106 
00107 /*
00108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00109 %                                                                             %
00110 %                                                                             %
00111 %                                                                             %
00112 %   A c q u i r e E x c e p t i o n I n f o                                   %
00113 %                                                                             %
00114 %                                                                             %
00115 %                                                                             %
00116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00117 %
00118 %  AcquireExceptionInfo() allocates the ExceptionInfo structure.
00119 %
00120 %  The format of the AcquireExceptionInfo method is:
00121 %
00122 %      ExceptionInfo *AcquireExceptionInfo(void)
00123 %
00124 */
00125 WizardExport ExceptionInfo *AcquireExceptionInfo(void)
00126 {
00127   ExceptionInfo
00128     *exception;
00129 
00130   exception=(ExceptionInfo *) AcquireAlignedMemory(1,sizeof(*exception));
00131   if (exception == (ExceptionInfo *) NULL)
00132     ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
00133   (void) ResetWizardMemory(exception,0,sizeof(*exception));
00134   GetExceptionInfo(exception);
00135   exception->relinquish=WizardTrue;
00136   exception->signature=WizardSignature;
00137   return(exception);
00138 }
00139 
00140 /*
00141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00142 %                                                                             %
00143 %                                                                             %
00144 %                                                                             %
00145 %   C l e a r W i z a r d E x c e p t i o n                                   %
00146 %                                                                             %
00147 %                                                                             %
00148 %                                                                             %
00149 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00150 %
00151 %  ClearWizardException() clears any exception that may not have been caught
00152 %  yet.
00153 %
00154 %  The format of the ClearWizardException method is:
00155 %
00156 %      ClearWizardException(ExceptionInfo *exception)
00157 %
00158 %  A description of each parameter follows:
00159 %
00160 %    o exception: The exception info.
00161 %
00162 */
00163 
00164 static void *DestroyExceptionElement(void *exception)
00165 {
00166   register ExceptionInfo
00167     *p;
00168 
00169   p=(ExceptionInfo *) exception;
00170   if (p->reason != (char *) NULL)
00171     p->reason=(char *) RelinquishWizardMemory(p->reason);
00172   if (p->description != (char *) NULL)
00173     p->description=(char *) RelinquishWizardMemory(p->description);
00174   p=(ExceptionInfo *) RelinquishWizardMemory(p);
00175   return((void *) NULL);
00176 }
00177 
00178 WizardExport void ClearWizardException(ExceptionInfo *exception)
00179 {
00180   register ExceptionInfo
00181     *p;
00182 
00183   assert(exception != (ExceptionInfo *) NULL);
00184   assert(exception->signature == WizardSignature);
00185   if (exception->exceptions == (void *) NULL)
00186     return;
00187   LockSemaphoreInfo(exception->semaphore);
00188   p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00189     exception->exceptions);
00190   while (p != (ExceptionInfo *) NULL)
00191   {
00192     p=(ExceptionInfo *) DestroyExceptionElement(p);
00193     p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00194       exception->exceptions);
00195   }
00196   exception->severity=UndefinedException;
00197   exception->reason=(char *) NULL;
00198   exception->description=(char *) NULL;
00199   UnlockSemaphoreInfo(exception->semaphore);
00200   errno=0;
00201 }
00202 
00203 /*
00204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00205 %                                                                             %
00206 %                                                                             %
00207 %                                                                             %
00208 %   C a t c h E x c e p t i o n                                               %
00209 %                                                                             %
00210 %                                                                             %
00211 %                                                                             %
00212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00213 %
00214 %  CatchException() returns if no exceptions is found otherwise it reports
00215 %  the exception as a warning, error, or fatal depending on the severity.
00216 %
00217 %  The format of the CatchException method is:
00218 %
00219 %      CatchException(ExceptionInfo *exception)
00220 %
00221 %  A description of each parameter follows:
00222 %
00223 %    o exception: The exception info.
00224 %
00225 */
00226 WizardExport void CatchException(ExceptionInfo *exception)
00227 {
00228   register const ExceptionInfo
00229     *p;
00230 
00231   assert(exception != (ExceptionInfo *) NULL);
00232   assert(exception->signature == WizardSignature);
00233   if (exception->exceptions == (void *) NULL)
00234     return;
00235   LockSemaphoreInfo(exception->semaphore);
00236   ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
00237   p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00238     exception->exceptions);
00239   while (p != (const ExceptionInfo *) NULL)
00240   {
00241     if ((p->severity >= WarningException) && (p->severity < ErrorException))
00242       WizardWarning(p->severity,p->reason,p->description);
00243     if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
00244       WizardError(p->severity,p->reason,p->description);
00245     if (exception->severity >= FatalErrorException)
00246       WizardFatalError(p->severity,p->reason,p->description);
00247     p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00248       exception->exceptions);
00249   }
00250   UnlockSemaphoreInfo(exception->semaphore);
00251   ClearWizardException(exception);
00252 }
00253 
00254 /*
00255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00256 %                                                                             %
00257 %                                                                             %
00258 %                                                                             %
00259 +   D e f a u l t E r r o r H a n d l e r                                     %
00260 %                                                                             %
00261 %                                                                             %
00262 %                                                                             %
00263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00264 %
00265 %  DefaultErrorHandler() displays an error reason.
00266 %
00267 %  The format of the DefaultErrorHandler method is:
00268 %
00269 %      void WizardError(const ExceptionType severity,const char *reason,
00270 %        const char *description)
00271 %
00272 %  A description of each parameter follows:
00273 %
00274 %    o severity: Specifies the numeric error category.
00275 %
00276 %    o reason: Specifies the reason to display before terminating the
00277 %      program.
00278 %
00279 %    o description: Specifies any description to the reason.
00280 %
00281 */
00282 static void DefaultErrorHandler(const ExceptionType wizard_unused(severity),
00283   const char *reason,const char *description)
00284 {
00285   if (reason == (char *) NULL)
00286     return;
00287   (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00288   if (description != (char *) NULL)
00289     (void) fprintf(stderr," (%s)",description);
00290   (void) fprintf(stderr,".\n");
00291   (void) fflush(stderr);
00292 }
00293 
00294 /*
00295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00296 %                                                                             %
00297 %                                                                             %
00298 %                                                                             %
00299 +   D e f a u l t F a t a l E r r o r H a n d l e r                           %
00300 %                                                                             %
00301 %                                                                             %
00302 %                                                                             %
00303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00304 %
00305 %  DefaultFatalErrorHandler() displays an error reason and then terminates the
00306 %  program.
00307 %
00308 %  The format of the DefaultFatalErrorHandler method is:
00309 %
00310 %      void WizardFatalError(const ExceptionType severity,const char *reason,
00311 %        const char *description)
00312 %
00313 %  A description of each parameter follows:
00314 %
00315 %    o severity: Specifies the numeric error category.
00316 %
00317 %    o reason: Specifies the reason to display before terminating the
00318 %      program.
00319 %
00320 %    o description: Specifies any description to the reason.
00321 %
00322 */
00323 static void DefaultFatalErrorHandler(const ExceptionType severity,
00324   const char *reason,const char *description)
00325 {
00326   if (reason == (char *) NULL)
00327     return;
00328   (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00329   if (description != (char *) NULL)
00330     (void) fprintf(stderr," (%s)",description);
00331   (void) fprintf(stderr,".\n");
00332   (void) fflush(stderr);
00333   WizardsToolkitTerminus();
00334   exit(1);
00335 }
00336 
00337 /*
00338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00339 %                                                                             %
00340 %                                                                             %
00341 %                                                                             %
00342 +   D e f a u l t W a r n i n g H a n d l e r                                 %
00343 %                                                                             %
00344 %                                                                             %
00345 %                                                                             %
00346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00347 %
00348 %  DefaultWarningHandler() displays a warning reason.
00349 %
00350 %  The format of the DefaultWarningHandler method is:
00351 %
00352 %      void DefaultWarningHandler(const ExceptionType warning,
00353 %        const char *reason,const char *description)
00354 %
00355 %  A description of each parameter follows:
00356 %
00357 %    o warning: Specifies the numeric warning category.
00358 %
00359 %    o reason: Specifies the reason to display before terminating the
00360 %      program.
00361 %
00362 %    o description: Specifies any description to the reason.
00363 %
00364 */
00365 static void DefaultWarningHandler(const ExceptionType wizard_unused(severity),
00366   const char *reason,const char *description)
00367 {
00368   if (reason == (char *) NULL)
00369     return;
00370   (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00371   if (description != (char *) NULL)
00372     (void) fprintf(stderr," (%s)",description);
00373   (void) fprintf(stderr,".\n");
00374   (void) fflush(stderr);
00375 }
00376 
00377 /*
00378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00379 %                                                                             %
00380 %                                                                             %
00381 %                                                                             %
00382 %   D e s t r o y E x c e p t i o n I n f o                                   %
00383 %                                                                             %
00384 %                                                                             %
00385 %                                                                             %
00386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00387 %
00388 %  DestroyExceptionInfo() deallocates memory associated with an exception.
00389 %
00390 %  The format of the DestroyExceptionInfo method is:
00391 %
00392 %      ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
00393 %
00394 %  A description of each parameter follows:
00395 %
00396 %    o exception: The exception info.
00397 %
00398 */
00399 WizardExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
00400 {
00401   assert(exception != (ExceptionInfo *) NULL);
00402   assert(exception->signature == WizardSignature);
00403   LockSemaphoreInfo(exception->semaphore);
00404   exception->severity=UndefinedException;
00405   if (exception->exceptions != (void *) NULL)
00406     exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
00407       exception->exceptions,DestroyExceptionElement);
00408   exception->signature=(~WizardSignature);
00409   UnlockSemaphoreInfo(exception->semaphore);
00410   DestroySemaphoreInfo(&exception->semaphore);
00411   if (exception->relinquish != WizardFalse)
00412     exception=(ExceptionInfo *) RelinquishWizardMemory(exception);
00413   return(exception);
00414 }
00415 
00416 /*
00417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00418 %                                                                             %
00419 %                                                                             %
00420 %                                                                             %
00421 %   G e t E x c e p t i o n I n f o                                           %
00422 %                                                                             %
00423 %                                                                             %
00424 %                                                                             %
00425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00426 %
00427 %  GetExceptionInfo() initializes an exception to default values.
00428 %
00429 %  The format of the GetExceptionInfo method is:
00430 %
00431 %      GetExceptionInfo(ExceptionInfo *exception)
00432 %
00433 %  A description of each parameter follows:
00434 %
00435 %    o exception: The exception info.
00436 %
00437 */
00438 WizardExport void GetExceptionInfo(ExceptionInfo *exception)
00439 {
00440   assert(exception != (ExceptionInfo *) NULL);
00441   (void) ResetWizardMemory(exception,0,sizeof(*exception));
00442   exception->severity=UndefinedException;
00443   exception->exceptions=(void *) NewLinkedList(0);
00444   exception->semaphore=AllocateSemaphoreInfo();
00445   exception->signature=WizardSignature;
00446 }
00447 
00448 /*
00449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00450 %                                                                             %
00451 %                                                                             %
00452 %                                                                             %
00453 %   G e t E x c e p t i o n M e s s a g e                                     %
00454 %                                                                             %
00455 %                                                                             %
00456 %                                                                             %
00457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00458 %
00459 %  GetExceptionMessage() returns the error message defined by the specified
00460 %  error code.
00461 %
00462 %  The format of the GetExceptionMessage method is:
00463 %
00464 %      char *GetExceptionMessage(const int error)
00465 %
00466 %  A description of each parameter follows:
00467 %
00468 %    o error: the error code.
00469 %
00470 */
00471 WizardExport char *GetExceptionMessage(const int error)
00472 {
00473  char
00474    exception[MaxTextExtent];
00475 
00476   *exception='\0';
00477 #if defined(WIZARDSTOOLKIT_HAVE_STRERROR_R)
00478   (void) strerror_r(error,exception,sizeof(exception));
00479 #else
00480   (void) CopyWizardString(exception,strerror(error),sizeof(exception));
00481 #endif
00482   return(ConstantString(exception));
00483 }
00484 
00485 /*
00486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00487 %                                                                             %
00488 %                                                                             %
00489 %                                                                             %
00490 %   G e t L o c a l e E x c e p t i o n M e s s a g e                         %
00491 %                                                                             %
00492 %                                                                             %
00493 %                                                                             %
00494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00495 %
00496 %  GetLocaleExceptionMessage() converts a enumerated exception severity and tag
00497 %  to a message in the current locale.
00498 %
00499 %  The format of the GetLocaleExceptionMessage method is:
00500 %
00501 %      const char *GetLocaleExceptionMessage(const ExceptionType severity,
00502 %        const char *tag)
00503 %
00504 %  A description of each parameter follows:
00505 %
00506 %    o severity: the severity of the exception.
00507 %
00508 %    o tag: the message tag.
00509 %
00510 */
00511 
00512 static const char *ExceptionSeverityToTag(const ExceptionType severity)
00513 {
00514   switch (severity)
00515   {
00516     case OptionWarning: return("Option/Warning/");
00517     case RandomWarning: return("Random/Warning/");
00518     case HashWarning: return("Hash/Warning/");
00519     case MACWarning: return("MAC/Warning/");
00520     case EntropyWarning: return("Entropy/Warning/");
00521     case ConfigureWarning: return("Configure/Warning/");
00522     case CipherWarning: return("Cipher/Warning/");
00523     case KeymapWarning: return("Keymap/Warning/");
00524     case AuthenticateWarning: return("Authenticate/Warning/");
00525     case KeyringWarning: return("Keyring/Warning/");
00526     case ParseWarning: return("Parse/Warning/");
00527     case UserWarning: return("User/Warning/");
00528     case SplayTreeWarning: return("SplayTree/Warning/");
00529     case HashmapWarning: return("Hashmap/Warning/");
00530     case LogWarning: return("Log/Warning/");
00531     case StringWarning: return("String/Warning/");
00532     case FileWarning: return("File/Warning/");
00533     case BlobWarning: return("Blob/Warning/");
00534     case ResourceWarning: return("ResourceLimit/Warning/");
00535     case OptionError: return("Option/Error/");
00536     case RandomError: return("Random/Error/");
00537     case HashError: return("Hash/Error/");
00538     case MACError: return("MAC/Error/");
00539     case EntropyError: return("Entropy/Error/");
00540     case ConfigureError: return("Configure/Error/");
00541     case CipherError: return("Cipher/Error/");
00542     case KeymapError: return("Keymap/Error/");
00543     case AuthenticateError: return("Authenticate/Error/");
00544     case KeyringError: return("Keyring/Error/");
00545     case ParseError: return("Parse/Error/");
00546     case UserError: return("User/Error/");
00547     case SplayTreeError: return("SplayTree/Error/");
00548     case StringError: return("String/Error/");
00549     case FileError: return("File/Error/");
00550     case BlobError: return("Blob/Error/");
00551     case ResourceError: return("ResourceLimit/Error/");
00552     case OptionFatalError: return("Option/FatalError/");
00553     case RandomFatalError: return("Random/FatalError/");
00554     case HashFatalError: return("Hash/FatalError/");
00555     case MACFatalError: return("MAC/FatalError/");
00556     case EntropyFatalError: return("Entropy/FatalError/");
00557     case ConfigureFatalError: return("Configure/FatalError/");
00558     case CipherFatalError: return("Cipher/FatalError/");
00559     case KeymapFatalError: return("Keymap/FatalError/");
00560     case AuthenticateFatalError: return("Authenticate/FatalError/");
00561     case KeyringFatalError: return("Keyring/FatalError/");
00562     case ParseFatalError: return("Parse/FatalError/");
00563     case UserFatalError: return("User/FatalError/");
00564     case SplayTreeFatalError: return("SplayTree/FatalError/");
00565     case HashmapFatalError: return("Hashmap/FatalError/");
00566     case LogFatalError: return("Log/FatalError/");
00567     case StringFatalError: return("String/FatalError/");
00568     case FileFatalError: return("File/FatalError/");
00569     case BlobFatalError: return("Blob/FatalError/");
00570     case ResourceFatalError: return("ResourceLimit/FatalError/");
00571     default: break;
00572   }
00573   return("");
00574 }
00575 
00576 WizardExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
00577   const char *tag)
00578 {
00579 #if defined(WIZARDSTOOLKIT_LOCALE)
00580   char
00581     message[MaxTextExtent];
00582 
00583   const char
00584     *locale_message;
00585 
00586   assert(tag != (const char *) NULL);
00587   (void) FormatWizardString(message,MaxTextExtent,"Exception/%s%s",
00588     ExceptionSeverityToTag(severity),tag);
00589   locale_message=GetLocaleMessage(message);
00590   if (locale_message == (const char *) NULL)
00591     return(tag);
00592   if (locale_message == message)
00593     return(tag);
00594   return(locale_message);
00595 #else
00596   return(tag);
00597 #endif
00598 }
00599 
00600 /*
00601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00602 %                                                                             %
00603 %                                                                             %
00604 %                                                                             %
00605 %   G e t E x c e p t i o n S e v e r i t y                                   %
00606 %                                                                             %
00607 %                                                                             %
00608 %                                                                             %
00609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00610 %
00611 %  GetExceptionSeverity() returns the exception severity.
00612 %
00613 %  The format of the GetExceptionSeverity method is:
00614 %
00615 %      ExceptionType GetExceptionSeverity(const ExceptionInfo *exception)
00616 %
00617 %  A description of each parameter follows:
00618 %
00619 %    o exception: The exception.
00620 %
00621 */
00622 WizardExport ExceptionType GetExceptionSeverity(const ExceptionInfo *exception)
00623 {
00624   assert(exception != (ExceptionInfo *) NULL);
00625   assert(exception->signature == WizardSignature);
00626   return(exception->severity);
00627 }
00628 
00629 /*
00630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00631 %                                                                             %
00632 %                                                                             %
00633 %                                                                             %
00634 %   I n h e r i t E x c e p t i o n                                           %
00635 %                                                                             %
00636 %                                                                             %
00637 %                                                                             %
00638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00639 %
00640 %  InheritException() inherits an exception from a related exception.
00641 %
00642 %  The format of the InheritException method is:
00643 %
00644 %      InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
00645 %
00646 %  A description of each parameter follows:
00647 %
00648 %    o exception: The exception info.
00649 %
00650 %    o relative: The related exception info.
00651 %
00652 %
00653 */
00654 WizardExport void InheritException(ExceptionInfo *exception,
00655   const ExceptionInfo *relative)
00656 {
00657   register const ExceptionInfo
00658     *p;
00659 
00660   assert(exception != (ExceptionInfo *) NULL);
00661   assert(exception->signature == WizardSignature);
00662   assert(relative != (ExceptionInfo *) NULL);
00663   assert(relative->signature == WizardSignature);
00664   if (relative->exceptions == (void *) NULL)
00665     return;
00666   LockSemaphoreInfo(exception->semaphore);
00667   ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
00668   p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00669     relative->exceptions);
00670   while (p != (const ExceptionInfo *) NULL)
00671   {
00672     (void) ThrowException(exception,p->severity,p->reason,p->description);
00673     p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00674       relative->exceptions);
00675   }
00676   UnlockSemaphoreInfo(exception->semaphore);
00677 }
00678 
00679 /*
00680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00681 %                                                                             %
00682 %                                                                             %
00683 %                                                                             %
00684 %   W i z a r d E r r o r                                                     %
00685 %                                                                             %
00686 %                                                                             %
00687 %                                                                             %
00688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00689 %
00690 %  WizardError() calls the exception handler methods with an error reason.
00691 %
00692 %  The format of the WizardError method is:
00693 %
00694 %      void WizardError(const ExceptionType error,const char *reason,
00695 %        const char *description)
00696 %
00697 %  A description of each parameter follows:
00698 %
00699 %    o exception: Specifies the numeric error category.
00700 %
00701 %    o reason: Specifies the reason to display before terminating the
00702 %      program.
00703 %
00704 %    o description: Specifies any description to the reason.
00705 %
00706 %
00707 */
00708 WizardExport void WizardError(const ExceptionType error,const char *reason,
00709   const char *description)
00710 {
00711   if (error_handler != (ErrorHandler) NULL)
00712     (*error_handler)(error,reason,description);
00713 }
00714 
00715 /*
00716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00717 %                                                                             %
00718 %                                                                             %
00719 %                                                                             %
00720 %   W i z a r d F a t al E r r o r                                            %
00721 %                                                                             %
00722 %                                                                             %
00723 %                                                                             %
00724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00725 %
00726 %  WizardFatalError() calls the fatal exception handler methods with an error
00727 %  reason.
00728 %
00729 %  The format of the WizardError method is:
00730 %
00731 %      void WizardFatalError(const ExceptionType error,const char *reason,
00732 %        const char *description)
00733 %
00734 %  A description of each parameter follows:
00735 %
00736 %    o exception: Specifies the numeric error category.
00737 %
00738 %    o reason: Specifies the reason to display before terminating the
00739 %      program.
00740 %
00741 %    o description: Specifies any description to the reason.
00742 %
00743 */
00744 WizardExport void WizardFatalError(const ExceptionType error,const char *reason,
00745   const char *description)
00746 {
00747   if (fatal_error_handler != (ErrorHandler) NULL)
00748     (*fatal_error_handler)(error,reason,description);
00749 }
00750 
00751 /*
00752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00753 %                                                                             %
00754 %                                                                             %
00755 %                                                                             %
00756 %   W i z a r d W a r n i n g                                                 %
00757 %                                                                             %
00758 %                                                                             %
00759 %                                                                             %
00760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00761 %
00762 %  WizardWarning() calls the warning handler methods with a warning reason.
00763 %
00764 %  The format of the WizardWarning method is:
00765 %
00766 %      void WizardWarning(const ExceptionType warning,const char *reason,
00767 %        const char *description)
00768 %
00769 %  A description of each parameter follows:
00770 %
00771 %    o warning: The warning severity.
00772 %
00773 %    o reason: Define the reason for the warning.
00774 %
00775 %    o description: Describe the warning.
00776 %
00777 */
00778 WizardExport void WizardWarning(const ExceptionType warning,const char *reason,
00779   const char *description)
00780 {
00781   if (warning_handler != (WarningHandler) NULL)
00782     (*warning_handler)(warning,reason,description);
00783 }
00784 
00785 /*
00786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00787 %                                                                             %
00788 %                                                                             %
00789 %                                                                             %
00790 %   S e t E r r o r H a n d l e r                                             %
00791 %                                                                             %
00792 %                                                                             %
00793 %                                                                             %
00794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00795 %
00796 %  SetErrorHandler() sets the exception handler to the specified method
00797 %  and returns the previous exception handler.
00798 %
00799 %  The format of the SetErrorHandler method is:
00800 %
00801 %      ErrorHandler SetErrorHandler(ErrorHandler handler)
00802 %
00803 %  A description of each parameter follows:
00804 %
00805 %    o handler: The method to handle errors.
00806 %
00807 */
00808 WizardExport ErrorHandler SetErrorHandler(ErrorHandler handler)
00809 {
00810   ErrorHandler
00811     previous_handler;
00812 
00813   previous_handler=error_handler;
00814   error_handler=handler;
00815   return(previous_handler);
00816 }
00817 
00818 /*
00819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00820 %                                                                             %
00821 %                                                                             %
00822 %                                                                             %
00823 %   S e t F a t a l E r r o r H a n d l e r                                   %
00824 %                                                                             %
00825 %                                                                             %
00826 %                                                                             %
00827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00828 %
00829 %  SetFatalErrorHandler() sets the fatal exception handler to the specified
00830 %  method and returns the previous fatal exception handler.
00831 %
00832 %  The format of the SetErrorHandler method is:
00833 %
00834 %      ErrorHandler SetErrorHandler(ErrorHandler handler)
00835 %
00836 %  A description of each parameter follows:
00837 %
00838 %    o handler: The method to handle errors.
00839 %
00840 */
00841 WizardExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
00842 {
00843   FatalErrorHandler
00844     previous_handler;
00845 
00846   previous_handler=fatal_error_handler;
00847   fatal_error_handler=handler;
00848   return(previous_handler);
00849 }
00850 
00851 /*
00852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00853 %                                                                             %
00854 %                                                                             %
00855 %                                                                             %
00856 %   S e t W a r n i n g H a n d l e r                                         %
00857 %                                                                             %
00858 %                                                                             %
00859 %                                                                             %
00860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00861 %
00862 %  SetWarningHandler() sets the warning handler to the specified method
00863 %  and returns the previous warning handler.
00864 %
00865 %  The format of the SetWarningHandler method is:
00866 %
00867 %      ErrorHandler SetWarningHandler(ErrorHandler handler)
00868 %
00869 %  A description of each parameter follows:
00870 %
00871 %    o handler: The method to handle warnings.
00872 %
00873 */
00874 WizardExport WarningHandler SetWarningHandler(WarningHandler handler)
00875 {
00876   WarningHandler
00877     previous_handler;
00878 
00879   previous_handler=warning_handler;
00880   warning_handler=handler;
00881   return(previous_handler);
00882 }
00883 
00884 /*
00885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00886 %                                                                             %
00887 %                                                                             %
00888 %                                                                             %
00889 %   T h r o w E x c e p t i o n                                               %
00890 %                                                                             %
00891 %                                                                             %
00892 %                                                                             %
00893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00894 %
00895 %  ThrowException() throws an exception with the specified severity code,
00896 %  reason, and optional description.
00897 %
00898 %  The format of the ThrowException method is:
00899 %
00900 %      WizardBooleanType ThrowException(ExceptionInfo *exception,
00901 %        const ExceptionType severity,const char *reason,
00902 %        const char *description)
00903 %
00904 %  A description of each parameter follows:
00905 %
00906 %    o exception: The exception info.
00907 %
00908 %    o severity: The severity of the exception.
00909 %
00910 %    o reason: The reason for the exception.
00911 %
00912 %    o description: The exception description.
00913 %
00914 */
00915 WizardExport WizardBooleanType ThrowException(ExceptionInfo *exception,
00916   const ExceptionType severity,const char *reason,const char *description)
00917 {
00918   register ExceptionInfo
00919     *p;
00920 
00921   assert(exception != (ExceptionInfo *) NULL);
00922   assert(exception->signature == WizardSignature);
00923   if (exception->exceptions == (void *) NULL)
00924     return(WizardTrue);
00925   p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
00926     exception->exceptions);
00927   if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
00928       (LocaleCompare(exception->reason,reason) == 0) &&
00929       (LocaleCompare(exception->description,description) == 0))
00930     return(WizardTrue);
00931   p=(ExceptionInfo *) AcquireAlignedMemory(1,sizeof(*p));
00932   if (p == (ExceptionInfo *) NULL)
00933     ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
00934   (void) ResetWizardMemory(p,0,sizeof(*p));
00935   p->severity=severity;
00936   if (reason != (const char *) NULL)
00937     p->reason=ConstantString(reason);
00938   if (description != (const char *) NULL)
00939     p->description=ConstantString(description);
00940   p->signature=WizardSignature;
00941   (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
00942   exception->severity=p->severity;
00943   exception->reason=p->reason;
00944   exception->description=p->description;
00945   return(WizardTrue);
00946 }
00947 
00948 /*
00949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00950 %                                                                             %
00951 %                                                                             %
00952 %                                                                             %
00953 %   T h r o w W i z a r d E x c e p t i o n                                   %
00954 %                                                                             %
00955 %                                                                             %
00956 %                                                                             %
00957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00958 %
00959 %  ThrowWizardException logs an exception as determined by the log configuration
00960 %  file.  If an error occurs, WizardFalse is returned otherwise WizardTrue.
00961 %
00962 %  The format of the ThrowWizardException method is:
00963 %
00964 %      WizardBooleanType ThrowWizardException(ExceptionInfo *exception,
00965 %        const char *module,const char *function,const size_t line,
00966 %        const ExceptionType severity,const char *format,...)
00967 %
00968 %  A description of each parameter follows:
00969 %
00970 %    o exception: The exception info.
00971 %
00972 %    o filename: The source module filename.
00973 %
00974 %    o function: The function name.
00975 %
00976 %    o line: The line number of the source module.
00977 %
00978 %    o severity: Specifies the numeric error category.
00979 %
00980 %    o format: The output format.
00981 %
00982 */
00983 
00984 WizardExport WizardBooleanType ThrowWizardExceptionList(
00985   ExceptionInfo *exception,const char *module,const char *function,
00986   const size_t line,const ExceptionType severity,const char *format,
00987   va_list operands)
00988 {
00989   char
00990     message[MaxTextExtent],
00991     reason[MaxTextExtent];
00992 
00993   int
00994     n;
00995 
00996   WizardBooleanType
00997     status;
00998 
00999   assert(exception != (ExceptionInfo *) NULL);
01000   assert(exception->signature == WizardSignature);
01001 #if defined(WIZARDSTOOLKIT_HAVE_VSNPRINTF)
01002   n=vsnprintf(reason,MaxTextExtent,format,operands);
01003 #else
01004   n=vsprintf(reason,format,operands);
01005 #endif
01006   if (n < 0)
01007     reason[MaxTextExtent-1]='\0';
01008   status=LogWizardEvent(exception->severity >= ErrorException ?
01009     ExceptionEvent : WarningEvent,module,function,line,"%s",reason);
01010   (void) FormatWizardString(message,MaxTextExtent,"%s @ %s/%s/%.20g",reason,
01011     module,function,(double) line);
01012   (void) ThrowException(exception,severity,message,(char *) NULL);
01013   return(status);
01014 }
01015 
01016 WizardExport WizardBooleanType ThrowWizardException(ExceptionInfo *exception,
01017   const char *module,const char *function,const size_t line,
01018   const ExceptionType severity,const char *format,...)
01019 {
01020   WizardBooleanType
01021     status;
01022 
01023   va_list
01024     operands;
01025 
01026   va_start(operands,format);
01027   status=ThrowWizardExceptionList(exception,module,function,line,severity,
01028     format,operands);
01029   va_end(operands);
01030   return(status);
01031 }
Generated by  doxygen 1.6.2-20100208