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

Generated on 19 Nov 2009 for MagickCore by  doxygen 1.6.1