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-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.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   AcquireSemaphoreInfo(&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   RelinquishSemaphoreInfo(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   AcquireSemaphoreInfo(&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   RelinquishSemaphoreInfo(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 warning,
00328 %        const char *reason,const char *description)
00329 %
00330 %  A description of each parameter follows:
00331 %
00332 %    o warning: 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   AcquireSemaphoreInfo(&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   RelinquishSemaphoreInfo(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->signature=MagickSignature;
00425 }
00426 
00427 /*
00428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00429 %                                                                             %
00430 %                                                                             %
00431 %                                                                             %
00432 %   G e t E x c e p t i o n M e s s a g e                                     %
00433 %                                                                             %
00434 %                                                                             %
00435 %                                                                             %
00436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00437 %
00438 %  GetExceptionMessage() returns the error message defined by the specified
00439 %  error code.
00440 %
00441 %  The format of the GetExceptionMessage method is:
00442 %
00443 %      char *GetExceptionMessage(const int error)
00444 %
00445 %  A description of each parameter follows:
00446 %
00447 %    o error: the error code.
00448 %
00449 */
00450 MagickExport char *GetExceptionMessage(const int error)
00451 {
00452   char
00453     exception[MaxTextExtent];
00454 
00455 #if defined(MAGICKCORE_HAVE_STRERROR_R)
00456   (void) strerror_r(error,exception,sizeof(exception));
00457 #else
00458   (void) CopyMagickString(exception,strerror(error),sizeof(exception));
00459 #endif
00460   return(ConstantString(exception));
00461 }
00462 
00463 /*
00464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00465 %                                                                             %
00466 %                                                                             %
00467 %                                                                             %
00468 %   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                         %
00469 %                                                                             %
00470 %                                                                             %
00471 %                                                                             %
00472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00473 %
00474 %  GetLocaleExceptionMessage() converts a enumerated exception severity and tag
00475 %  to a message in the current locale.
00476 %
00477 %  The format of the GetLocaleExceptionMessage method is:
00478 %
00479 %      const char *GetLocaleExceptionMessage(const ExceptionType severity,
00480 %        const char *tag)
00481 %
00482 %  A description of each parameter follows:
00483 %
00484 %    o severity: the severity of the exception.
00485 %
00486 %    o tag: the message tag.
00487 %
00488 */
00489 
00490 static const char *ExceptionSeverityToTag(const ExceptionType severity)
00491 {
00492   switch (severity)
00493   {
00494     case ResourceLimitWarning: return("Resource/Limit/Warning/");
00495     case TypeWarning: return("Type/Warning/");
00496     case OptionWarning: return("Option/Warning/");
00497     case DelegateWarning: return("Delegate/Warning/");
00498     case MissingDelegateWarning: return("Missing/Delegate/Warning/");
00499     case CorruptImageWarning: return("Corrupt/Image/Warning/");
00500     case FileOpenWarning: return("File/Open/Warning/");
00501     case BlobWarning: return("Blob/Warning/");
00502     case StreamWarning: return("Stream/Warning/");
00503     case CacheWarning: return("Cache/Warning/");
00504     case CoderWarning: return("Coder/Warning/");
00505     case ModuleWarning: return("Module/Warning/");
00506     case DrawWarning: return("Draw/Warning/");
00507     case ImageWarning: return("Image/Warning/");
00508     case WandWarning: return("Wand/Warning/");
00509     case XServerWarning: return("XServer/Warning/");
00510     case MonitorWarning: return("Monitor/Warning/");
00511     case RegistryWarning: return("Registry/Warning/");
00512     case ConfigureWarning: return("Configure/Warning/");
00513     case PolicyWarning: return("Policy/Warning/");
00514     case ResourceLimitError: return("Resource/Limit/Error/");
00515     case TypeError: return("Type/Error/");
00516     case OptionError: return("Option/Error/");
00517     case DelegateError: return("Delegate/Error/");
00518     case MissingDelegateError: return("Missing/Delegate/Error/");
00519     case CorruptImageError: return("Corrupt/Image/Error/");
00520     case FileOpenError: return("File/Open/Error/");
00521     case BlobError: return("Blob/Error/");
00522     case StreamError: return("Stream/Error/");
00523     case CacheError: return("Cache/Error/");
00524     case CoderError: return("Coder/Error/");
00525     case ModuleError: return("Module/Error/");
00526     case DrawError: return("Draw/Error/");
00527     case ImageError: return("Image/Error/");
00528     case WandError: return("Wand/Error/");
00529     case XServerError: return("XServer/Error/");
00530     case MonitorError: return("Monitor/Error/");
00531     case RegistryError: return("Registry/Error/");
00532     case ConfigureError: return("Configure/Error/");
00533     case PolicyError: return("Policy/Error/");
00534     case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
00535     case TypeFatalError: return("Type/FatalError/");
00536     case OptionFatalError: return("Option/FatalError/");
00537     case DelegateFatalError: return("Delegate/FatalError/");
00538     case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
00539     case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
00540     case FileOpenFatalError: return("File/Open/FatalError/");
00541     case BlobFatalError: return("Blob/FatalError/");
00542     case StreamFatalError: return("Stream/FatalError/");
00543     case CacheFatalError: return("Cache/FatalError/");
00544     case CoderFatalError: return("Coder/FatalError/");
00545     case ModuleFatalError: return("Module/FatalError/");
00546     case DrawFatalError: return("Draw/FatalError/");
00547     case ImageFatalError: return("Image/FatalError/");
00548     case WandFatalError: return("Wand/FatalError/");
00549     case XServerFatalError: return("XServer/FatalError/");
00550     case MonitorFatalError: return("Monitor/FatalError/");
00551     case RegistryFatalError: return("Registry/FatalError/");
00552     case ConfigureFatalError: return("Configure/FatalError/");
00553     case PolicyFatalError: return("Policy/FatalError/");
00554     default: break;
00555   }
00556   return("");
00557 }
00558 
00559 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
00560   const char *tag)
00561 {
00562   char
00563     message[MaxTextExtent];
00564 
00565   const char
00566     *locale_message;
00567 
00568   assert(tag != (const char *) NULL);
00569   (void) FormatMagickString(message,MaxTextExtent,"Exception/%s%s",
00570     ExceptionSeverityToTag(severity),tag);
00571   locale_message=GetLocaleMessage(message);
00572   if (locale_message == (const char *) NULL)
00573     return(tag);
00574   if (locale_message == message)
00575     return(tag);
00576   return(locale_message);
00577 }
00578 
00579 /*
00580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00581 %                                                                             %
00582 %                                                                             %
00583 %                                                                             %
00584 %   I n h e r i t E x c e p t i o n                                           %
00585 %                                                                             %
00586 %                                                                             %
00587 %                                                                             %
00588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00589 %
00590 %  InheritException() inherits an exception from a related exception.
00591 %
00592 %  The format of the InheritException method is:
00593 %
00594 %      InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
00595 %
00596 %  A description of each parameter follows:
00597 %
00598 %    o exception: the exception info.
00599 %
00600 %    o relative: the related exception info.
00601 %
00602 */
00603 MagickExport void InheritException(ExceptionInfo *exception,
00604   const ExceptionInfo *relative)
00605 {
00606   register const ExceptionInfo
00607     *p;
00608 
00609   assert(exception != (ExceptionInfo *) NULL);
00610   assert(exception->signature == MagickSignature);
00611   assert(relative != (ExceptionInfo *) NULL);
00612   assert(relative->signature == MagickSignature);
00613   if (relative->exceptions == (void *) NULL)
00614     return;
00615   AcquireSemaphoreInfo(&exception->semaphore);
00616   ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
00617   p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00618     relative->exceptions);
00619   while (p != (const ExceptionInfo *) NULL)
00620   {
00621     (void) ThrowException(exception,p->severity,p->reason,p->description);
00622     p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
00623       relative->exceptions);
00624   }
00625   RelinquishSemaphoreInfo(exception->semaphore);
00626 }
00627 
00628 /*
00629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00630 %                                                                             %
00631 %                                                                             %
00632 %                                                                             %
00633 %   M a g i c k E r r o r                                                     %
00634 %                                                                             %
00635 %                                                                             %
00636 %                                                                             %
00637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00638 %
00639 %  MagickError() calls the exception handler methods with an error reason.
00640 %
00641 %  The format of the MagickError method is:
00642 %
00643 %      void MagickError(const ExceptionType error,const char *reason,
00644 %        const char *description)
00645 %
00646 %  A description of each parameter follows:
00647 %
00648 %    o exception: Specifies the numeric error category.
00649 %
00650 %    o reason: Specifies the reason to display before terminating the
00651 %      program.
00652 %
00653 %    o description: Specifies any description to the reason.
00654 %
00655 */
00656 MagickExport void MagickError(const ExceptionType error,const char *reason,
00657   const char *description)
00658 {
00659   if (error_handler != (ErrorHandler) NULL)
00660     (*error_handler)(error,reason,description);
00661 }
00662 
00663 /*
00664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00665 %                                                                             %
00666 %                                                                             %
00667 %                                                                             %
00668 %   M a g i c k F a t al E r r o r                                            %
00669 %                                                                             %
00670 %                                                                             %
00671 %                                                                             %
00672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00673 %
00674 %  MagickFatalError() calls the fatal exception handler methods with an error
00675 %  reason.
00676 %
00677 %  The format of the MagickError method is:
00678 %
00679 %      void MagickFatalError(const ExceptionType error,const char *reason,
00680 %        const char *description)
00681 %
00682 %  A description of each parameter follows:
00683 %
00684 %    o exception: Specifies the numeric error category.
00685 %
00686 %    o reason: Specifies the reason to display before terminating the
00687 %      program.
00688 %
00689 %    o description: Specifies any description to the reason.
00690 %
00691 */
00692 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
00693   const char *description)
00694 {
00695   if (fatal_error_handler != (ErrorHandler) NULL)
00696     (*fatal_error_handler)(error,reason,description);
00697 }
00698 
00699 /*
00700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00701 %                                                                             %
00702 %                                                                             %
00703 %                                                                             %
00704 %   M a g i c k W a r n i n g                                                 %
00705 %                                                                             %
00706 %                                                                             %
00707 %                                                                             %
00708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00709 %
00710 %  MagickWarning() calls the warning handler methods with a warning reason.
00711 %
00712 %  The format of the MagickWarning method is:
00713 %
00714 %      void MagickWarning(const ExceptionType warning,const char *reason,
00715 %        const char *description)
00716 %
00717 %  A description of each parameter follows:
00718 %
00719 %    o warning: the warning severity.
00720 %
00721 %    o reason: Define the reason for the warning.
00722 %
00723 %    o description: Describe the warning.
00724 %
00725 */
00726 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
00727   const char *description)
00728 {
00729   if (warning_handler != (WarningHandler) NULL)
00730     (*warning_handler)(warning,reason,description);
00731 }
00732 
00733 /*
00734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00735 %                                                                             %
00736 %                                                                             %
00737 %                                                                             %
00738 %   S e t E r r o r H a n d l e r                                             %
00739 %                                                                             %
00740 %                                                                             %
00741 %                                                                             %
00742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00743 %
00744 %  SetErrorHandler() sets the exception handler to the specified method
00745 %  and returns the previous exception handler.
00746 %
00747 %  The format of the SetErrorHandler method is:
00748 %
00749 %      ErrorHandler SetErrorHandler(ErrorHandler handler)
00750 %
00751 %  A description of each parameter follows:
00752 %
00753 %    o handler: the method to handle errors.
00754 %
00755 */
00756 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
00757 {
00758   ErrorHandler
00759     previous_handler;
00760 
00761   previous_handler=error_handler;
00762   error_handler=handler;
00763   return(previous_handler);
00764 }
00765 
00766 /*
00767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00768 %                                                                             %
00769 %                                                                             %
00770 %                                                                             %
00771 %   S e t F a t a l E r r o r H a n d l e r                                   %
00772 %                                                                             %
00773 %                                                                             %
00774 %                                                                             %
00775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00776 %
00777 %  SetFatalErrorHandler() sets the fatal exception handler to the specified
00778 %  method and returns the previous fatal exception handler.
00779 %
00780 %  The format of the SetErrorHandler method is:
00781 %
00782 %      ErrorHandler SetErrorHandler(ErrorHandler handler)
00783 %
00784 %  A description of each parameter follows:
00785 %
00786 %    o handler: the method to handle errors.
00787 %
00788 */
00789 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
00790 {
00791   FatalErrorHandler
00792     previous_handler;
00793 
00794   previous_handler=fatal_error_handler;
00795   fatal_error_handler=handler;
00796   return(previous_handler);
00797 }
00798 
00799 /*
00800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00801 %                                                                             %
00802 %                                                                             %
00803 %                                                                             %
00804 %   S e t W a r n i n g H a n d l e r                                         %
00805 %                                                                             %
00806 %                                                                             %
00807 %                                                                             %
00808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00809 %
00810 %  SetWarningHandler() sets the warning handler to the specified method
00811 %  and returns the previous warning handler.
00812 %
00813 %  The format of the SetWarningHandler method is:
00814 %
00815 %      ErrorHandler SetWarningHandler(ErrorHandler handler)
00816 %
00817 %  A description of each parameter follows:
00818 %
00819 %    o handler: the method to handle warnings.
00820 %
00821 */
00822 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
00823 {
00824   WarningHandler
00825     previous_handler;
00826 
00827   previous_handler=warning_handler;
00828   warning_handler=handler;
00829   return(previous_handler);
00830 }
00831 
00832 /*
00833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00834 %                                                                             %
00835 %                                                                             %
00836 %                                                                             %
00837 %   T h r o w E x c e p t i o n                                               %
00838 %                                                                             %
00839 %                                                                             %
00840 %                                                                             %
00841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00842 %
00843 %  ThrowException() throws an exception with the specified severity code,
00844 %  reason, and optional description.
00845 %
00846 %  The format of the ThrowException method is:
00847 %
00848 %      MagickBooleanType ThrowException(ExceptionInfo *exception,
00849 %        const ExceptionType severity,const char *reason,
00850 %        const char *description)
00851 %
00852 %  A description of each parameter follows:
00853 %
00854 %    o exception: the exception info.
00855 %
00856 %    o severity: the severity of the exception.
00857 %
00858 %    o reason: the reason for the exception.
00859 %
00860 %    o description: the exception description.
00861 %
00862 */
00863 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
00864   const ExceptionType severity,const char *reason,const char *description)
00865 {
00866   register ExceptionInfo
00867     *p;
00868 
00869   assert(exception != (ExceptionInfo *) NULL);
00870   assert(exception->signature == MagickSignature);
00871   p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
00872     exception->exceptions);
00873   if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
00874       (LocaleCompare(exception->reason,reason) == 0) &&
00875       (LocaleCompare(exception->description,description) == 0))
00876     return(MagickTrue);
00877   p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
00878   if (p == (ExceptionInfo *) NULL)
00879     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00880   (void) ResetMagickMemory(p,0,sizeof(*p));
00881   p->severity=severity;
00882   if (reason != (const char *) NULL)
00883     p->reason=ConstantString(reason);
00884   if (description != (const char *) NULL)
00885     p->description=ConstantString(description);
00886   p->signature=MagickSignature;
00887   (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
00888   exception->severity=p->severity;
00889   exception->reason=p->reason;
00890   exception->description=p->description;
00891   return(MagickTrue);
00892 }
00893 
00894 /*
00895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00896 %                                                                             %
00897 %                                                                             %
00898 %                                                                             %
00899 %   T h r o w M a g i c k E x c e p t i o n                                   %
00900 %                                                                             %
00901 %                                                                             %
00902 %                                                                             %
00903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00904 %
00905 %  ThrowMagickException logs an exception as determined by the log configuration
00906 %  file.  If an error occurs, MagickFalse is returned otherwise MagickTrue.
00907 %
00908 %  The format of the ThrowMagickException method is:
00909 %
00910 %      MagickBooleanType ThrowFileException(ExceptionInfo *exception,
00911 %        const char *module,const char *function,const unsigned long line,
00912 %        const ExceptionType severity,const char *tag,const char *format,...)
00913 %
00914 %  A description of each parameter follows:
00915 %
00916 %    o exception: the exception info.
00917 %
00918 %    o filename: the source module filename.
00919 %
00920 %    o function: the function name.
00921 %
00922 %    o line: the line number of the source module.
00923 %
00924 %    o severity: Specifies the numeric error category.
00925 %
00926 %    o tag: the locale tag.
00927 %
00928 %    o format: the output format.
00929 %
00930 */
00931 
00932 MagickExport MagickBooleanType ThrowMagickExceptionList(
00933   ExceptionInfo *exception,const char *module,const char *function,
00934   const unsigned long line,const ExceptionType severity,const char *tag,
00935   const char *format,va_list operands)
00936 {
00937   char
00938     message[MaxTextExtent],
00939     path[MaxTextExtent],
00940     reason[MaxTextExtent];
00941 
00942   const char
00943     *locale;
00944 
00945   int
00946     n;
00947 
00948   MagickBooleanType
00949     status;
00950 
00951   size_t
00952     length;
00953 
00954   assert(exception != (ExceptionInfo *) NULL);
00955   assert(exception->signature == MagickSignature);
00956   locale=GetLocaleExceptionMessage(severity,tag);
00957   (void) CopyMagickString(reason,locale,MaxTextExtent);
00958   (void) ConcatenateMagickString(reason," ",MaxTextExtent);
00959   length=strlen(reason);
00960 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
00961   n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
00962 #else
00963   n=vsprintf(reason+length,format,operands);
00964 #endif
00965   if (n < 0)
00966     reason[MaxTextExtent-1]='\0';
00967   status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
00968   GetPathComponent(module,TailPath,path);
00969   (void) FormatMagickString(message,MaxTextExtent,"%s @ %s/%s/%ld",reason,path,
00970     function,line);
00971   (void) ThrowException(exception,severity,message,(char *) NULL);
00972   return(status);
00973 }
00974 
00975 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
00976   const char *module,const char *function,const unsigned long line,
00977   const ExceptionType severity,const char *tag,const char *format,...)
00978 {
00979   MagickBooleanType
00980     status;
00981 
00982   va_list
00983     operands;
00984 
00985   va_start(operands,format);
00986   status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
00987     format,operands);
00988   va_end(operands);
00989   return(status);
00990 }

Generated on Thu Jul 2 12:03:19 2009 for MagickCore by  doxygen 1.5.8