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 %                        MagickCore Exception Methods                         %
00014 %                                                                             %
00015 %                             Software Design                                 %
00016 %                               John Cristy                                   %
00017 %                                July 1993                                    %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2008 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.imagemagick.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 "magick/studio.h"
00044 #include "magick/client.h"
00045 #include "magick/exception.h"
00046 #include "magick/exception-private.h"
00047 #include "magick/hashmap.h"
00048 #include "magick/locale_.h"
00049 #include "magick/log.h"
00050 #include "magick/magick.h"
00051 #include "magick/memory_.h"
00052 #include "magick/string_.h"
00053 #include "magick/utility.h"
00054 
00055 /*
00056   Forward declarations.
00057 */
00058 #if defined(__cplusplus) || defined(c_plusplus)
00059 extern "C" {
00060 #endif
00061 
00062 static void
00063   DefaultErrorHandler(const ExceptionType,const char *,const char *),
00064   DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
00065   DefaultWarningHandler(const ExceptionType,const char *,const char *);
00066 
00067 #if defined(__cplusplus) || defined(c_plusplus)
00068 }
00069 #endif
00070 
00071 /*
00072   Global declarations.
00073 */
00074 static ErrorHandler
00075   error_handler = DefaultErrorHandler;
00076 
00077 static FatalErrorHandler
00078   fatal_error_handler = DefaultFatalErrorHandler;
00079 
00080 static WarningHandler
00081   warning_handler = DefaultWarningHandler;
00082 
00083 /*
00084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00085 %                                                                             %
00086 %                                                                             %
00087 %                                                                             %
00088 %   A c q u i r e E x c e p t i o n I n f o                                   %
00089 %                                                                             %
00090 %                                                                             %
00091 %                                                                             %
00092 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00093 %
00094 %  AcquireExceptionInfo() allocates the ExceptionInfo structure.
00095 %
00096 %  The format of the AcquireExceptionInfo method is:
00097 %
00098 %      ExceptionInfo *AcquireExceptionInfo(void)
00099 %
00100 */
00101 MagickExport ExceptionInfo *AcquireExceptionInfo(void)
00102 {
00103   ExceptionInfo
00104     *exception;
00105 
00106   exception=(ExceptionInfo *) AcquireMagickMemory(sizeof(*exception));
00107   if (exception == (ExceptionInfo *) NULL)
00108     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00109   GetExceptionInfo(exception);
00110   exception->relinquish=MagickTrue;
00111   return(exception);
00112 }
00113 
00114 /*
00115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00116 %                                                                             %
00117 %                                                                             %
00118 %                                                                             %
00119 %   C l e a r M a g i c k E x c e p t i o n                                   %
00120 %                                                                             %
00121 %                                                                             %
00122 %                                                                             %
00123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00124 %
00125 %  ClearMagickException() clears any exception that may not have been caught
00126 %  yet.
00127 %
00128 %  The format of the ClearMagickException method is:
00129 %
00130 %      ClearMagickException(ExceptionInfo *exception)
00131 %
00132 %  A description of each parameter follows:
00133 %
00134 %    o exception: the exception info.
00135 %
00136 */
00137 
00138 static void *DestroyExceptionElement(void *exception)
00139 {
00140   register ExceptionInfo
00141     *p;
00142 
00143   p=(ExceptionInfo *) exception;
00144   if (p->reason != (char *) NULL)
00145     p->reason=DestroyString(p->reason);
00146   if (p->description != (char *) NULL)
00147     p->description=DestroyString(p->description);
00148   p=(ExceptionInfo *) RelinquishMagickMemory(p);
00149   return((void *) NULL);
00150 }
00151 
00152 MagickExport void ClearMagickException(ExceptionInfo *exception)
00153 {
00154   register ExceptionInfo
00155     *p;
00156 
00157   assert(exception != (ExceptionInfo *) NULL);
00158   assert(exception->signature == MagickSignature);
00159   AcquireSemaphoreInfo(&exception->semaphore);
00160   p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00161     exception->exceptions);
00162   while (p != (ExceptionInfo *) NULL)
00163   {
00164     (void) DestroyExceptionElement(p);
00165     p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
00166       exception->exceptions);
00167   }
00168   exception->severity=UndefinedException;
00169   exception->reason=(char *) NULL;
00170   exception->description=(char *) NULL;
00171   RelinquishSemaphoreInfo(exception->semaphore);
00172   errno=0;
00173 }
00174 
00175 /*
00176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00177 %                                                                             %
00178 %                                                                             %
00179 %                                                                             %
00180 %   C a t c h E x c e p t i o n                                               %
00181 %                                                                             %
00182 %                                                                             %
00183 %                                                                             %
00184 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00185 %
00186 %  CatchException() returns if no exceptions is found otherwise it reports
00187 %  the exception as a warning, error, or fatal depending on the severity.
00188 %
00189 %  The format of the CatchException method is:
00190 %
00191 %      CatchException(ExceptionInfo *exception)
00192 %
00193 %  A description of each parameter follows:
00194 %
00195 %    o exception: the exception info.
00196 %
00197 */
00198 MagickExport void CatchException(ExceptionInfo *exception)
00199 {
00200   register const ExceptionInfo
00201     *p;
00202 
00203   assert(exception != (ExceptionInfo *) NULL);
00204   assert(exception->signature == MagickSignature);
00205   AcquireSemaphoreInfo(&exception->semaphore);
00206   ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
00207   p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00208     exception->exceptions);
00209   while (p != (const ExceptionInfo *) NULL)
00210   {
00211     if ((p->severity >= WarningException) && (p->severity < ErrorException))
00212       MagickWarning(p->severity,p->reason,p->description);
00213     if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
00214       MagickError(p->severity,p->reason,p->description);
00215     if (exception->severity >= FatalErrorException)
00216       MagickFatalError(p->severity,p->reason,p->description);
00217     p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00218       exception->exceptions);
00219   }
00220   RelinquishSemaphoreInfo(exception->semaphore);
00221   ClearMagickException(exception);
00222 }
00223 
00224 /*
00225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00226 %                                                                             %
00227 %                                                                             %
00228 %                                                                             %
00229 +   D e f a u l t E r r o r H a n d l e r                                     %
00230 %                                                                             %
00231 %                                                                             %
00232 %                                                                             %
00233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00234 %
00235 %  DefaultErrorHandler() displays an error reason.
00236 %
00237 %  The format of the DefaultErrorHandler method is:
00238 %
00239 %      void MagickError(const ExceptionType severity,const char *reason,
00240 %        const char *description)
00241 %
00242 %  A description of each parameter follows:
00243 %
00244 %    o severity: Specifies the numeric error category.
00245 %
00246 %    o reason: Specifies the reason to display before terminating the
00247 %      program.
00248 %
00249 %    o description: Specifies any description to the reason.
00250 %
00251 */
00252 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
00253   const char *reason,const char *description)
00254 {
00255   if (reason == (char *) NULL)
00256     return;
00257   (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00258   if (description != (char *) NULL)
00259     (void) fprintf(stderr," (%s)",description);
00260   (void) fprintf(stderr,".\n");
00261   (void) fflush(stderr);
00262 }
00263 
00264 /*
00265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00266 %                                                                             %
00267 %                                                                             %
00268 %                                                                             %
00269 +   D e f a u l t F a t a l E r r o r H a n d l e r                           %
00270 %                                                                             %
00271 %                                                                             %
00272 %                                                                             %
00273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00274 %
00275 %  DefaultFatalErrorHandler() displays an error reason and then terminates the
00276 %  program.
00277 %
00278 %  The format of the DefaultFatalErrorHandler method is:
00279 %
00280 %      void MagickFatalError(const ExceptionType severity,const char *reason,
00281 %        const char *description)
00282 %
00283 %  A description of each parameter follows:
00284 %
00285 %    o severity: Specifies the numeric error category.
00286 %
00287 %    o reason: Specifies the reason to display before terminating the
00288 %      program.
00289 %
00290 %    o description: Specifies any description to the reason.
00291 %
00292 */
00293 static void DefaultFatalErrorHandler(const ExceptionType magick_unused(severity),
00294   const char *reason,const char *description)
00295 {
00296   if (reason == (char *) NULL)
00297     return;
00298   (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00299   if (description != (char *) NULL)
00300     (void) fprintf(stderr," (%s)",description);
00301   (void) fprintf(stderr,".\n");
00302   (void) fflush(stderr);
00303   MagickCoreTerminus();
00304   exit(1);
00305 }
00306 
00307 /*
00308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00309 %                                                                             %
00310 %                                                                             %
00311 %                                                                             %
00312 +   D e f a u l t W a r n i n g H a n d l e r                                 %
00313 %                                                                             %
00314 %                                                                             %
00315 %                                                                             %
00316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00317 %
00318 %  DefaultWarningHandler() displays a warning reason.
00319 %
00320 %  The format of the DefaultWarningHandler method is:
00321 %
00322 %      void DefaultWarningHandler(const ExceptionType warning,
00323 %        const char *reason,const char *description)
00324 %
00325 %  A description of each parameter follows:
00326 %
00327 %    o warning: Specifies the numeric warning category.
00328 %
00329 %    o reason: Specifies the reason to display before terminating the
00330 %      program.
00331 %
00332 %    o description: Specifies any description to the reason.
00333 %
00334 */
00335 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
00336   const char *reason,const char *description)
00337 {
00338   if (reason == (char *) NULL)
00339     return;
00340   (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
00341   if (description != (char *) NULL)
00342     (void) fprintf(stderr," (%s)",description);
00343   (void) fprintf(stderr,".\n");
00344   (void) fflush(stderr);
00345 }
00346 
00347 /*
00348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00349 %                                                                             %
00350 %                                                                             %
00351 %                                                                             %
00352 %   D e s t r o y E x c e p t i o n I n f o                                   %
00353 %                                                                             %
00354 %                                                                             %
00355 %                                                                             %
00356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00357 %
00358 %  DestroyExceptionInfo() deallocates memory associated with an exception.
00359 %
00360 %  The format of the DestroyExceptionInfo method is:
00361 %
00362 %      ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
00363 %
00364 %  A description of each parameter follows:
00365 %
00366 %    o exception: the exception info.
00367 %
00368 */
00369 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
00370 {
00371   MagickBooleanType
00372     relinquish;
00373 
00374   assert(exception != (ExceptionInfo *) NULL);
00375   assert(exception->signature == MagickSignature);
00376   AcquireSemaphoreInfo(&exception->semaphore);
00377   exception->severity=UndefinedException;
00378   if (exception->exceptions != (void *) NULL)
00379     exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
00380       exception->exceptions,DestroyExceptionElement);
00381   relinquish=exception->relinquish;
00382   if (exception->relinquish != MagickFalse)
00383     exception->signature=(~MagickSignature);
00384   RelinquishSemaphoreInfo(exception->semaphore);
00385   DestroySemaphoreInfo(&exception->semaphore);
00386   if (relinquish != MagickFalse)
00387     exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
00388   return(exception);
00389 }
00390 
00391 /*
00392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00393 %                                                                             %
00394 %                                                                             %
00395 %                                                                             %
00396 %   G e t E x c e p t i o n I n f o                                           %
00397 %                                                                             %
00398 %                                                                             %
00399 %                                                                             %
00400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00401 %
00402 %  GetExceptionInfo() initializes an exception to default values.
00403 %
00404 %  The format of the GetExceptionInfo method is:
00405 %
00406 %      GetExceptionInfo(ExceptionInfo *exception)
00407 %
00408 %  A description of each parameter follows:
00409 %
00410 %    o exception: the exception info.
00411 %
00412 */
00413 MagickExport void GetExceptionInfo(ExceptionInfo *exception)
00414 {
00415   assert(exception != (ExceptionInfo *) NULL);
00416   (void) ResetMagickMemory(exception,0,sizeof(*exception));
00417   exception->severity=UndefinedException;
00418   exception->exceptions=(void *) NewLinkedList(0);
00419   exception->signature=MagickSignature;
00420 }
00421 
00422 /*
00423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00424 %                                                                             %
00425 %                                                                             %
00426 %                                                                             %
00427 %   G e t E x c e p t i o n M e s s a g e                                     %
00428 %                                                                             %
00429 %                                                                             %
00430 %                                                                             %
00431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00432 %
00433 %  GetExceptionMessage() returns the error message defined by the specified
00434 %  error code.
00435 %
00436 %  The format of the GetExceptionMessage method is:
00437 %
00438 %      char *GetExceptionMessage(const int error_code)
00439 %
00440 %  A description of each parameter follows:
00441 %
00442 %    o error_code: the error code.
00443 %
00444 */
00445 MagickExport char *GetExceptionMessage(const int error_code)
00446 {
00447   return(ConstantString(strerror(error_code)));
00448 }
00449 
00450 /*
00451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00452 %                                                                             %
00453 %                                                                             %
00454 %                                                                             %
00455 %   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                         %
00456 %                                                                             %
00457 %                                                                             %
00458 %                                                                             %
00459 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00460 %
00461 %  GetLocaleExceptionMessage() converts a enumerated exception severity and tag
00462 %  to a message in the current locale.
00463 %
00464 %  The format of the GetLocaleExceptionMessage method is:
00465 %
00466 %      const char *GetLocaleExceptionMessage(const ExceptionType severity,
00467 %        const char *tag)
00468 %
00469 %  A description of each parameter follows:
00470 %
00471 %    o severity: the severity of the exception.
00472 %
00473 %    o tag: the message tag.
00474 %
00475 */
00476 
00477 static const char *ExceptionSeverityToTag(const ExceptionType severity)
00478 {
00479   switch (severity)
00480   {
00481     case ResourceLimitWarning: return("Resource/Limit/Warning/");
00482     case TypeWarning: return("Type/Warning/");
00483     case OptionWarning: return("Option/Warning/");
00484     case DelegateWarning: return("Delegate/Warning/");
00485     case MissingDelegateWarning: return("Missing/Delegate/Warning/");
00486     case CorruptImageWarning: return("Corrupt/Image/Warning/");
00487     case FileOpenWarning: return("File/Open/Warning/");
00488     case BlobWarning: return("Blob/Warning/");
00489     case StreamWarning: return("Stream/Warning/");
00490     case CacheWarning: return("Cache/Warning/");
00491     case CoderWarning: return("Coder/Warning/");
00492     case ModuleWarning: return("Module/Warning/");
00493     case DrawWarning: return("Draw/Warning/");
00494     case ImageWarning: return("Image/Warning/");
00495     case WandWarning: return("Wand/Warning/");
00496     case XServerWarning: return("XServer/Warning/");
00497     case MonitorWarning: return("Monitor/Warning/");
00498     case RegistryWarning: return("Registry/Warning/");
00499     case ConfigureWarning: return("Configure/Warning/");
00500     case ResourceLimitError: return("Resource/Limit/Error/");
00501     case TypeError: return("Type/Error/");
00502     case OptionError: return("Option/Error/");
00503     case DelegateError: return("Delegate/Error/");
00504     case MissingDelegateError: return("Missing/Delegate/Error/");
00505     case CorruptImageError: return("Corrupt/Image/Error/");
00506     case FileOpenError: return("File/Open/Error/");
00507     case BlobError: return("Blob/Error/");
00508     case StreamError: return("Stream/Error/");
00509     case CacheError: return("Cache/Error/");
00510     case CoderError: return("Coder/Error/");
00511     case ModuleError: return("Module/Error/");
00512     case DrawError: return("Draw/Error/");
00513     case ImageError: return("Image/Error/");
00514     case WandError: return("Wand/Error/");
00515     case XServerError: return("XServer/Error/");
00516     case MonitorError: return("Monitor/Error/");
00517     case RegistryError: return("Registry/Error/");
00518     case ConfigureError: return("Configure/Error/");
00519     case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
00520     case TypeFatalError: return("Type/FatalError/");
00521     case OptionFatalError: return("Option/FatalError/");
00522     case DelegateFatalError: return("Delegate/FatalError/");
00523     case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
00524     case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
00525     case FileOpenFatalError: return("File/Open/FatalError/");
00526     case BlobFatalError: return("Blob/FatalError/");
00527     case StreamFatalError: return("Stream/FatalError/");
00528     case CacheFatalError: return("Cache/FatalError/");
00529     case CoderFatalError: return("Coder/FatalError/");
00530     case ModuleFatalError: return("Module/FatalError/");
00531     case DrawFatalError: return("Draw/FatalError/");
00532     case ImageFatalError: return("Image/FatalError/");
00533     case WandFatalError: return("Wand/FatalError/");
00534     case XServerFatalError: return("XServer/FatalError/");
00535     case MonitorFatalError: return("Monitor/FatalError/");
00536     case RegistryFatalError: return("Registry/FatalError/");
00537     case ConfigureFatalError: return("Configure/FatalError/");
00538     default: break;
00539   }
00540   return("");
00541 }
00542 
00543 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
00544   const char *tag)
00545 {
00546   char
00547     message[MaxTextExtent];
00548 
00549   const char
00550     *locale_message;
00551 
00552   assert(tag != (const char *) NULL);
00553   (void) FormatMagickString(message,MaxTextExtent,"Exception/%s%s",
00554     ExceptionSeverityToTag(severity),tag);
00555   locale_message=GetLocaleMessage(message);
00556   if (locale_message == (const char *) NULL)
00557     return(tag);
00558   if (locale_message == message)
00559     return(tag);
00560   return(locale_message);
00561 }
00562 
00563 /*
00564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00565 %                                                                             %
00566 %                                                                             %
00567 %                                                                             %
00568 %   I n h e r i t E x c e p t i o n                                           %
00569 %                                                                             %
00570 %                                                                             %
00571 %                                                                             %
00572 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00573 %
00574 %  InheritException() inherits an exception from a related exception.
00575 %
00576 %  The format of the InheritException method is:
00577 %
00578 %      InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
00579 %
00580 %  A description of each parameter follows:
00581 %
00582 %    o exception: the exception info.
00583 %
00584 %    o relative: the related exception info.
00585 %
00586 */
00587 MagickExport void InheritException(ExceptionInfo *exception,
00588   const ExceptionInfo *relative)
00589 {
00590   register const ExceptionInfo
00591     *p;
00592 
00593   assert(exception != (ExceptionInfo *) NULL);
00594   assert(exception->signature == MagickSignature);
00595   assert(relative != (ExceptionInfo *) NULL);
00596   assert(relative->signature == MagickSignature);
00597   AcquireSemaphoreInfo(&exception->semaphore);
00598   ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
00599   p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00600     relative->exceptions);
00601   while (p != (const ExceptionInfo *) NULL)
00602   {
00603     (void) ThrowException(exception,p->severity,p->reason,p->description);
00604     p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00605       relative->exceptions);
00606   }
00607   RelinquishSemaphoreInfo(exception->semaphore);
00608 }
00609 
00610 /*
00611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00612 %                                                                             %
00613 %                                                                             %
00614 %                                                                             %
00615 %   M a g i c k E r r o r                                                     %
00616 %                                                                             %
00617 %                                                                             %
00618 %                                                                             %
00619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00620 %
00621 %  MagickError() calls the exception handler methods with an error reason.
00622 %
00623 %  The format of the MagickError method is:
00624 %
00625 %      void MagickError(const ExceptionType error,const char *reason,
00626 %        const char *description)
00627 %
00628 %  A description of each parameter follows:
00629 %
00630 %    o exception: Specifies the numeric error category.
00631 %
00632 %    o reason: Specifies the reason to display before terminating the
00633 %      program.
00634 %
00635 %    o description: Specifies any description to the reason.
00636 %
00637 */
00638 MagickExport void MagickError(const ExceptionType error,const char *reason,
00639   const char *description)
00640 {
00641   if (error_handler != (ErrorHandler) NULL)
00642     (*error_handler)(error,reason,description);
00643 }
00644 
00645 /*
00646 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00647 %                                                                             %
00648 %                                                                             %
00649 %                                                                             %
00650 %   M a g i c k F a t al E r r o r                                            %
00651 %                                                                             %
00652 %                                                                             %
00653 %                                                                             %
00654 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00655 %
00656 %  MagickFatalError() calls the fatal exception handler methods with an error
00657 %  reason.
00658 %
00659 %  The format of the MagickError method is:
00660 %
00661 %      void MagickFatalError(const ExceptionType error,const char *reason,
00662 %        const char *description)
00663 %
00664 %  A description of each parameter follows:
00665 %
00666 %    o exception: Specifies the numeric error category.
00667 %
00668 %    o reason: Specifies the reason to display before terminating the
00669 %      program.
00670 %
00671 %    o description: Specifies any description to the reason.
00672 %
00673 */
00674 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
00675   const char *description)
00676 {
00677   if (fatal_error_handler != (ErrorHandler) NULL)
00678     (*fatal_error_handler)(error,reason,description);
00679 }
00680 
00681 /*
00682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00683 %                                                                             %
00684 %                                                                             %
00685 %                                                                             %
00686 %   M a g i c k W a r n i n g                                                 %
00687 %                                                                             %
00688 %                                                                             %
00689 %                                                                             %
00690 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00691 %
00692 %  MagickWarning() calls the warning handler methods with a warning reason.
00693 %
00694 %  The format of the MagickWarning method is:
00695 %
00696 %      void MagickWarning(const ExceptionType warning,const char *reason,
00697 %        const char *description)
00698 %
00699 %  A description of each parameter follows:
00700 %
00701 %    o warning: the warning severity.
00702 %
00703 %    o reason: Define the reason for the warning.
00704 %
00705 %    o description: Describe the warning.
00706 %
00707 */
00708 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
00709   const char *description)
00710 {
00711   if (warning_handler != (WarningHandler) NULL)
00712     (*warning_handler)(warning,reason,description);
00713 }
00714 
00715 /*
00716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00717 %                                                                             %
00718 %                                                                             %
00719 %                                                                             %
00720 %   S e t E r r o r H a n d l e r                                             %
00721 %                                                                             %
00722 %                                                                             %
00723 %                                                                             %
00724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00725 %
00726 %  SetErrorHandler() sets the exception handler to the specified method
00727 %  and returns the previous exception handler.
00728 %
00729 %  The format of the SetErrorHandler method is:
00730 %
00731 %      ErrorHandler SetErrorHandler(ErrorHandler handler)
00732 %
00733 %  A description of each parameter follows:
00734 %
00735 %    o handler: the method to handle errors.
00736 %
00737 */
00738 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
00739 {
00740   ErrorHandler
00741     previous_handler;
00742 
00743   previous_handler=error_handler;
00744   error_handler=handler;
00745   return(previous_handler);
00746 }
00747 
00748 /*
00749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00750 %                                                                             %
00751 %                                                                             %
00752 %                                                                             %
00753 %   S e t F a t a l E r r o r H a n d l e r                                   %
00754 %                                                                             %
00755 %                                                                             %
00756 %                                                                             %
00757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00758 %
00759 %  SetFatalErrorHandler() sets the fatal exception handler to the specified
00760 %  method and returns the previous fatal exception handler.
00761 %
00762 %  The format of the SetErrorHandler method is:
00763 %
00764 %      ErrorHandler SetErrorHandler(ErrorHandler handler)
00765 %
00766 %  A description of each parameter follows:
00767 %
00768 %    o handler: the method to handle errors.
00769 %
00770 */
00771 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
00772 {
00773   FatalErrorHandler
00774     previous_handler;
00775 
00776   previous_handler=fatal_error_handler;
00777   fatal_error_handler=handler;
00778   return(previous_handler);
00779 }
00780 
00781 /*
00782 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00783 %                                                                             %
00784 %                                                                             %
00785 %                                                                             %
00786 %   S e t W a r n i n g H a n d l e r                                         %
00787 %                                                                             %
00788 %                                                                             %
00789 %                                                                             %
00790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00791 %
00792 %  SetWarningHandler() sets the warning handler to the specified method
00793 %  and returns the previous warning handler.
00794 %
00795 %  The format of the SetWarningHandler method is:
00796 %
00797 %      ErrorHandler SetWarningHandler(ErrorHandler handler)
00798 %
00799 %  A description of each parameter follows:
00800 %
00801 %    o handler: the method to handle warnings.
00802 %
00803 */
00804 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
00805 {
00806   WarningHandler
00807     previous_handler;
00808 
00809   previous_handler=warning_handler;
00810   warning_handler=handler;
00811   return(previous_handler);
00812 }
00813 
00814 /*
00815 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00816 %                                                                             %
00817 %                                                                             %
00818 %                                                                             %
00819 %   T h r o w E x c e p t i o n                                               %
00820 %                                                                             %
00821 %                                                                             %
00822 %                                                                             %
00823 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00824 %
00825 %  ThrowException() throws an exception with the specified severity code,
00826 %  reason, and optional description.
00827 %
00828 %  The format of the ThrowException method is:
00829 %
00830 %      MagickBooleanType ThrowException(ExceptionInfo *exception,
00831 %        const ExceptionType severity,const char *reason,
00832 %        const char *description)
00833 %
00834 %  A description of each parameter follows:
00835 %
00836 %    o exception: the exception info.
00837 %
00838 %    o severity: the severity of the exception.
00839 %
00840 %    o tag: the message tag.
00841 %
00842 %    o description: the exception description.
00843 %
00844 */
00845 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
00846   const ExceptionType severity,const char *reason,const char *description)
00847 {
00848   register ExceptionInfo
00849     *p;
00850 
00851   assert(exception != (ExceptionInfo *) NULL);
00852   assert(exception->signature == MagickSignature);
00853   p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
00854     exception->exceptions);
00855   if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
00856       (LocaleCompare(exception->reason,reason) == 0) &&
00857       (LocaleCompare(exception->description,description) == 0))
00858     return(MagickTrue);
00859   p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
00860   if (p == (ExceptionInfo *) NULL)
00861     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00862   (void) ResetMagickMemory(p,0,sizeof(*p));
00863   p->severity=severity;
00864   if (reason != (const char *) NULL)
00865     p->reason=ConstantString(reason);
00866   if (description != (const char *) NULL)
00867     p->description=ConstantString(description);
00868   p->signature=MagickSignature;
00869   (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
00870   exception->severity=p->severity;
00871   exception->reason=p->reason;
00872   exception->description=p->description;
00873   return(MagickTrue);
00874 }
00875 
00876 /*
00877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00878 %                                                                             %
00879 %                                                                             %
00880 %                                                                             %
00881 %   T h r o w M a g i c k E x c e p t i o n                                   %
00882 %                                                                             %
00883 %                                                                             %
00884 %                                                                             %
00885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00886 %
00887 %  ThrowMagickException logs an exception as determined by the log configuration
00888 %  file.  If an error occurs, MagickFalse is returned otherwise MagickTrue.
00889 %
00890 %  The format of the ThrowMagickException method is:
00891 %
00892 %      MagickBooleanType ThrowFileException(ExceptionInfo *exception,
00893 %        const char *module,const char *function,const unsigned long line,
00894 %        const ExceptionType severity,const char *tag,const char *format,...)
00895 %
00896 %  A description of each parameter follows:
00897 %
00898 %    o exception: the exception info.
00899 %
00900 %    o filename: the source module filename.
00901 %
00902 %    o function: the function name.
00903 %
00904 %    o line: the line number of the source module.
00905 %
00906 %    o severity: Specifies the numeric error category.
00907 %
00908 %    o tag: the locale tag.
00909 %
00910 %    o format: the output format.
00911 %
00912 */
00913 
00914 MagickExport MagickBooleanType ThrowMagickExceptionList(
00915   ExceptionInfo *exception,const char *module,const char *function,
00916   const unsigned long line,const ExceptionType severity,const char *tag,
00917   const char *format,va_list operands)
00918 {
00919   char
00920     message[MaxTextExtent],
00921     reason[MaxTextExtent];
00922 
00923   const char
00924     *locale;
00925 
00926   int
00927     n;
00928 
00929   MagickBooleanType
00930     status;
00931 
00932   size_t
00933     length;
00934 
00935   assert(exception != (ExceptionInfo *) NULL);
00936   assert(exception->signature == MagickSignature);
00937   locale=GetLocaleExceptionMessage(severity,tag);
00938   (void) CopyMagickString(reason,locale,MaxTextExtent);
00939   (void) ConcatenateMagickString(reason," ",MaxTextExtent);
00940   length=strlen(reason);
00941 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
00942   n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
00943 #else
00944   n=vsprintf(reason+length,format,operands);
00945 #endif
00946   if (n < 0)
00947     reason[MaxTextExtent-1]='\0';
00948   status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
00949   (void) FormatMagickString(message,MaxTextExtent,"%s @ %s/%s/%ld",reason,
00950     module,function,line);
00951   (void) ThrowException(exception,severity,message,(char *) NULL);
00952   return(status);
00953 }
00954 
00955 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
00956   const char *module,const char *function,const unsigned long line,
00957   const ExceptionType severity,const char *tag,const char *format,...)
00958 {
00959   MagickBooleanType
00960     status;
00961 
00962   va_list
00963     operands;
00964 
00965   va_start(operands,format);
00966   status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
00967     format,operands);
00968   va_end(operands);
00969   return(status);
00970 }

Generated on Thu Nov 20 21:54:46 2008 for MagickCore by  doxygen 1.5.7.1