2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % EEEEE X X CCCC EEEEE PPPP TTTTT IIIII OOO N N %
7 % E X X C E P P T I O O NN N %
8 % EEE X C EEE PPPP T I O O N N N %
9 % E X X C E P T I O O N NN %
10 % EEEEE X X CCCC EEEEE P T IIIII OOO N N %
13 % MagickCore Exception Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/exception.h"
46 #include "MagickCore/exception-private.h"
47 #include "MagickCore/hashmap.h"
48 #include "MagickCore/locale_.h"
49 #include "MagickCore/log.h"
50 #include "MagickCore/magick.h"
51 #include "MagickCore/memory_.h"
52 #include "MagickCore/string_.h"
53 #include "MagickCore/utility.h"
54 #include "MagickCore/utility-private.h"
59 #if defined(__cplusplus) || defined(c_plusplus)
64 DefaultErrorHandler(const ExceptionType,const char *,const char *),
65 DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
66 DefaultWarningHandler(const ExceptionType,const char *,const char *);
68 #if defined(__cplusplus) || defined(c_plusplus)
76 error_handler = DefaultErrorHandler;
78 static FatalErrorHandler
79 fatal_error_handler = DefaultFatalErrorHandler;
82 warning_handler = DefaultWarningHandler;
85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 % A c q u i r e E x c e p t i o n I n f o %
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 % AcquireExceptionInfo() allocates the ExceptionInfo structure.
97 % The format of the AcquireExceptionInfo method is:
99 % ExceptionInfo *AcquireExceptionInfo(void)
102 MagickExport ExceptionInfo *AcquireExceptionInfo(void)
107 exception=(ExceptionInfo *) AcquireMagickMemory(sizeof(*exception));
108 if (exception == (ExceptionInfo *) NULL)
109 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
110 GetExceptionInfo(exception);
111 exception->relinquish=MagickTrue;
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % C l e a r M a g i c k E x c e p t i o n %
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 % ClearMagickException() clears any exception that may not have been caught
129 % The format of the ClearMagickException method is:
131 % ClearMagickException(ExceptionInfo *exception)
133 % A description of each parameter follows:
135 % o exception: the exception info.
139 static void *DestroyExceptionElement(void *exception)
141 register ExceptionInfo
144 p=(ExceptionInfo *) exception;
145 if (p->reason != (char *) NULL)
146 p->reason=DestroyString(p->reason);
147 if (p->description != (char *) NULL)
148 p->description=DestroyString(p->description);
149 p=(ExceptionInfo *) RelinquishMagickMemory(p);
150 return((void *) NULL);
153 MagickExport void ClearMagickException(ExceptionInfo *exception)
155 register ExceptionInfo
158 assert(exception != (ExceptionInfo *) NULL);
159 assert(exception->signature == MagickSignature);
160 if (exception->exceptions == (void *) NULL)
162 LockSemaphoreInfo(exception->semaphore);
163 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
164 exception->exceptions);
165 while (p != (ExceptionInfo *) NULL)
167 (void) DestroyExceptionElement(p);
168 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
169 exception->exceptions);
171 exception->severity=UndefinedException;
172 exception->reason=(char *) NULL;
173 exception->description=(char *) NULL;
174 UnlockSemaphoreInfo(exception->semaphore);
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 % C a t c h E x c e p t i o n %
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % CatchException() returns if no exceptions is found otherwise it reports
190 % the exception as a warning, error, or fatal depending on the severity.
192 % The format of the CatchException method is:
194 % CatchException(ExceptionInfo *exception)
196 % A description of each parameter follows:
198 % o exception: the exception info.
201 MagickExport void CatchException(ExceptionInfo *exception)
203 register const ExceptionInfo
206 assert(exception != (ExceptionInfo *) NULL);
207 assert(exception->signature == MagickSignature);
208 if (exception->exceptions == (void *) NULL)
210 LockSemaphoreInfo(exception->semaphore);
211 ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
212 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
213 exception->exceptions);
214 while (p != (const ExceptionInfo *) NULL)
216 if ((p->severity >= WarningException) && (p->severity < ErrorException))
217 MagickWarning(p->severity,p->reason,p->description);
218 if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
219 MagickError(p->severity,p->reason,p->description);
220 if (p->severity >= FatalErrorException)
221 MagickFatalError(p->severity,p->reason,p->description);
222 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
223 exception->exceptions);
225 UnlockSemaphoreInfo(exception->semaphore);
226 ClearMagickException(exception);
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % C l o n e E x c e p t i o n I n f o %
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % CloneExceptionInfo() clones the ExceptionInfo structure.
242 % The format of the CloneExceptionInfo method is:
244 % ExceptionInfo *CloneException(ExceptionInfo *exception)
246 % A description of each parameter follows:
248 % o exception: the exception info.
251 MagickExport ExceptionInfo *CloneExceptionInfo(ExceptionInfo *exception)
256 clone_exception=(ExceptionInfo *) AcquireMagickMemory(sizeof(*exception));
257 if (clone_exception == (ExceptionInfo *) NULL)
258 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
259 GetExceptionInfo(clone_exception);
260 InheritException(clone_exception,exception);
261 exception->relinquish=MagickTrue;
266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270 + D e f a u l t E r r o r H a n d l e r %
274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % DefaultErrorHandler() displays an error reason.
278 % The format of the DefaultErrorHandler method is:
280 % void MagickError(const ExceptionType severity,const char *reason,
281 % const char *description)
283 % A description of each parameter follows:
285 % o severity: Specifies the numeric error category.
287 % o reason: Specifies the reason to display before terminating the
290 % o description: Specifies any description to the reason.
293 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
294 const char *reason,const char *description)
296 if (reason == (char *) NULL)
298 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
299 if (description != (char *) NULL)
300 (void) FormatLocaleFile(stderr," (%s)",description);
301 (void) FormatLocaleFile(stderr,".\n");
302 (void) fflush(stderr);
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 + D e f a u l t F a t a l E r r o r H a n d l e r %
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 % DefaultFatalErrorHandler() displays an error reason and then terminates the
319 % The format of the DefaultFatalErrorHandler method is:
321 % void MagickFatalError(const ExceptionType severity,const char *reason,
322 % const char *description)
324 % A description of each parameter follows:
326 % o severity: Specifies the numeric error category.
328 % o reason: Specifies the reason to display before terminating the
331 % o description: Specifies any description to the reason.
334 static void DefaultFatalErrorHandler(
335 const ExceptionType magick_unused(severity),
336 const char *reason,const char *description)
338 if (reason == (char *) NULL)
340 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
341 if (description != (char *) NULL)
342 (void) FormatLocaleFile(stderr," (%s)",description);
343 (void) FormatLocaleFile(stderr,".\n");
344 (void) fflush(stderr);
345 MagickCoreTerminus();
350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354 + D e f a u l t W a r n i n g H a n d l e r %
358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
360 % DefaultWarningHandler() displays a warning reason.
362 % The format of the DefaultWarningHandler method is:
364 % void DefaultWarningHandler(const ExceptionType severity,
365 % const char *reason,const char *description)
367 % A description of each parameter follows:
369 % o severity: Specifies the numeric warning category.
371 % o reason: Specifies the reason to display before terminating the
374 % o description: Specifies any description to the reason.
377 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
378 const char *reason,const char *description)
380 if (reason == (char *) NULL)
382 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
383 if (description != (char *) NULL)
384 (void) FormatLocaleFile(stderr," (%s)",description);
385 (void) FormatLocaleFile(stderr,".\n");
386 (void) fflush(stderr);
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % D e s t r o y E x c e p t i o n I n f o %
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % DestroyExceptionInfo() deallocates memory associated with an exception.
402 % The format of the DestroyExceptionInfo method is:
404 % ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
406 % A description of each parameter follows:
408 % o exception: the exception info.
411 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
416 assert(exception != (ExceptionInfo *) NULL);
417 assert(exception->signature == MagickSignature);
418 if (exception->semaphore == (SemaphoreInfo *) NULL)
419 AcquireSemaphoreInfo(&exception->semaphore);
420 LockSemaphoreInfo(exception->semaphore);
421 exception->severity=UndefinedException;
422 if (exception->exceptions != (void *) NULL)
423 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
424 exception->exceptions,DestroyExceptionElement);
425 relinquish=exception->relinquish;
426 if (exception->relinquish != MagickFalse)
427 exception->signature=(~MagickSignature);
428 UnlockSemaphoreInfo(exception->semaphore);
429 DestroySemaphoreInfo(&exception->semaphore);
430 if (relinquish != MagickFalse)
431 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 % G e t E x c e p t i o n I n f o %
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446 % GetExceptionInfo() initializes an exception to default values.
448 % The format of the GetExceptionInfo method is:
450 % GetExceptionInfo(ExceptionInfo *exception)
452 % A description of each parameter follows:
454 % o exception: the exception info.
457 MagickExport void GetExceptionInfo(ExceptionInfo *exception)
459 assert(exception != (ExceptionInfo *) NULL);
460 (void) ResetMagickMemory(exception,0,sizeof(*exception));
461 exception->severity=UndefinedException;
462 exception->exceptions=(void *) NewLinkedList(0);
463 exception->semaphore=AllocateSemaphoreInfo();
464 exception->signature=MagickSignature;
468 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472 % G e t E x c e p t i o n M e s s a g e %
476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 % GetExceptionMessage() returns the error message defined by the specified
481 % The format of the GetExceptionMessage method is:
483 % char *GetExceptionMessage(const int error)
485 % A description of each parameter follows:
487 % o error: the error code.
490 MagickExport char *GetExceptionMessage(const int error)
493 exception[MaxTextExtent];
496 #if defined(MAGICKCORE_HAVE_STRERROR_R)
497 #if !defined(MAGICKCORE_STRERROR_R_CHAR_P)
498 (void) strerror_r(error,exception,sizeof(exception));
500 (void) CopyMagickString(exception,strerror_r(error,exception,
501 sizeof(exception)),sizeof(exception));
504 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
506 return(ConstantString(exception));
510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 % 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 %
518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520 % GetLocaleExceptionMessage() converts a enumerated exception severity and tag
521 % to a message in the current locale.
523 % The format of the GetLocaleExceptionMessage method is:
525 % const char *GetLocaleExceptionMessage(const ExceptionType severity,
528 % A description of each parameter follows:
530 % o severity: the severity of the exception.
532 % o tag: the message tag.
536 static const char *ExceptionSeverityToTag(const ExceptionType severity)
540 case ResourceLimitWarning: return("Resource/Limit/Warning/");
541 case TypeWarning: return("Type/Warning/");
542 case OptionWarning: return("Option/Warning/");
543 case DelegateWarning: return("Delegate/Warning/");
544 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
545 case CorruptImageWarning: return("Corrupt/Image/Warning/");
546 case FileOpenWarning: return("File/Open/Warning/");
547 case BlobWarning: return("Blob/Warning/");
548 case StreamWarning: return("Stream/Warning/");
549 case CacheWarning: return("Cache/Warning/");
550 case CoderWarning: return("Coder/Warning/");
551 case FilterWarning: return("Filter/Warning/");
552 case ModuleWarning: return("Module/Warning/");
553 case DrawWarning: return("Draw/Warning/");
554 case ImageWarning: return("Image/Warning/");
555 case WandWarning: return("Wand/Warning/");
556 case XServerWarning: return("XServer/Warning/");
557 case MonitorWarning: return("Monitor/Warning/");
558 case RegistryWarning: return("Registry/Warning/");
559 case ConfigureWarning: return("Configure/Warning/");
560 case PolicyWarning: return("Policy/Warning/");
561 case ResourceLimitError: return("Resource/Limit/Error/");
562 case TypeError: return("Type/Error/");
563 case OptionError: return("Option/Error/");
564 case DelegateError: return("Delegate/Error/");
565 case MissingDelegateError: return("Missing/Delegate/Error/");
566 case CorruptImageError: return("Corrupt/Image/Error/");
567 case FileOpenError: return("File/Open/Error/");
568 case BlobError: return("Blob/Error/");
569 case StreamError: return("Stream/Error/");
570 case CacheError: return("Cache/Error/");
571 case CoderError: return("Coder/Error/");
572 case FilterError: return("Filter/Error/");
573 case ModuleError: return("Module/Error/");
574 case DrawError: return("Draw/Error/");
575 case ImageError: return("Image/Error/");
576 case WandError: return("Wand/Error/");
577 case XServerError: return("XServer/Error/");
578 case MonitorError: return("Monitor/Error/");
579 case RegistryError: return("Registry/Error/");
580 case ConfigureError: return("Configure/Error/");
581 case PolicyError: return("Policy/Error/");
582 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
583 case TypeFatalError: return("Type/FatalError/");
584 case OptionFatalError: return("Option/FatalError/");
585 case DelegateFatalError: return("Delegate/FatalError/");
586 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
587 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
588 case FileOpenFatalError: return("File/Open/FatalError/");
589 case BlobFatalError: return("Blob/FatalError/");
590 case StreamFatalError: return("Stream/FatalError/");
591 case CacheFatalError: return("Cache/FatalError/");
592 case CoderFatalError: return("Coder/FatalError/");
593 case FilterFatalError: return("Filter/FatalError/");
594 case ModuleFatalError: return("Module/FatalError/");
595 case DrawFatalError: return("Draw/FatalError/");
596 case ImageFatalError: return("Image/FatalError/");
597 case WandFatalError: return("Wand/FatalError/");
598 case XServerFatalError: return("XServer/FatalError/");
599 case MonitorFatalError: return("Monitor/FatalError/");
600 case RegistryFatalError: return("Registry/FatalError/");
601 case ConfigureFatalError: return("Configure/FatalError/");
602 case PolicyFatalError: return("Policy/FatalError/");
608 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
612 message[MaxTextExtent];
617 assert(tag != (const char *) NULL);
618 (void) FormatLocaleString(message,MaxTextExtent,"Exception/%s%s",
619 ExceptionSeverityToTag(severity),tag);
620 locale_message=GetLocaleMessage(message);
621 if (locale_message == (const char *) NULL)
623 if (locale_message == message)
625 return(locale_message);
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 % I n h e r i t E x c e p t i o n %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % InheritException() inherits an exception from a related exception.
641 % The format of the InheritException method is:
643 % InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
645 % A description of each parameter follows:
647 % o exception: the exception info.
649 % o relative: the related exception info.
652 MagickExport void InheritException(ExceptionInfo *exception,
653 const ExceptionInfo *relative)
655 register const ExceptionInfo
658 assert(exception != (ExceptionInfo *) NULL);
659 assert(exception->signature == MagickSignature);
660 assert(relative != (ExceptionInfo *) NULL);
661 assert(relative->signature == MagickSignature);
662 if (relative->exceptions == (void *) NULL)
664 LockSemaphoreInfo(exception->semaphore);
665 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
666 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
667 relative->exceptions);
668 while (p != (const ExceptionInfo *) NULL)
670 (void) ThrowException(exception,p->severity,p->reason,p->description);
671 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
672 relative->exceptions);
674 UnlockSemaphoreInfo(exception->semaphore);
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 % M a g i c k E r r o r %
686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688 % MagickError() calls the exception handler methods with an error reason.
690 % The format of the MagickError method is:
692 % void MagickError(const ExceptionType error,const char *reason,
693 % const char *description)
695 % A description of each parameter follows:
697 % o exception: Specifies the numeric error category.
699 % o reason: Specifies the reason to display before terminating the
702 % o description: Specifies any description to the reason.
705 MagickExport void MagickError(const ExceptionType error,const char *reason,
706 const char *description)
708 if (error_handler != (ErrorHandler) NULL)
709 (*error_handler)(error,reason,description);
713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717 % M a g i c k F a t al E r r o r %
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 % MagickFatalError() calls the fatal exception handler methods with an error
726 % The format of the MagickError method is:
728 % void MagickFatalError(const ExceptionType error,const char *reason,
729 % const char *description)
731 % A description of each parameter follows:
733 % o exception: Specifies the numeric error category.
735 % o reason: Specifies the reason to display before terminating the
738 % o description: Specifies any description to the reason.
741 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
742 const char *description)
744 if (fatal_error_handler != (ErrorHandler) NULL)
745 (*fatal_error_handler)(error,reason,description);
749 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
753 % M a g i c k W a r n i n g %
757 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 % MagickWarning() calls the warning handler methods with a warning reason.
761 % The format of the MagickWarning method is:
763 % void MagickWarning(const ExceptionType warning,const char *reason,
764 % const char *description)
766 % A description of each parameter follows:
768 % o warning: the warning severity.
770 % o reason: Define the reason for the warning.
772 % o description: Describe the warning.
775 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
776 const char *description)
778 if (warning_handler != (WarningHandler) NULL)
779 (*warning_handler)(warning,reason,description);
783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
787 % S e t E r r o r H a n d l e r %
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % SetErrorHandler() sets the exception handler to the specified method
794 % and returns the previous exception handler.
796 % The format of the SetErrorHandler method is:
798 % ErrorHandler SetErrorHandler(ErrorHandler handler)
800 % A description of each parameter follows:
802 % o handler: the method to handle errors.
805 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
810 previous_handler=error_handler;
811 error_handler=handler;
812 return(previous_handler);
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820 % S e t F a t a l E r r o r H a n d l e r %
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % SetFatalErrorHandler() sets the fatal exception handler to the specified
827 % method and returns the previous fatal exception handler.
829 % The format of the SetErrorHandler method is:
831 % ErrorHandler SetErrorHandler(ErrorHandler handler)
833 % A description of each parameter follows:
835 % o handler: the method to handle errors.
838 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
843 previous_handler=fatal_error_handler;
844 fatal_error_handler=handler;
845 return(previous_handler);
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 % S e t W a r n i n g H a n d l e r %
857 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 % SetWarningHandler() sets the warning handler to the specified method
860 % and returns the previous warning handler.
862 % The format of the SetWarningHandler method is:
864 % ErrorHandler SetWarningHandler(ErrorHandler handler)
866 % A description of each parameter follows:
868 % o handler: the method to handle warnings.
871 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
876 previous_handler=warning_handler;
877 warning_handler=handler;
878 return(previous_handler);
882 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886 % T h r o w E x c e p t i o n %
890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 % ThrowException() throws an exception with the specified severity code,
893 % reason, and optional description.
895 % The format of the ThrowException method is:
897 % MagickBooleanType ThrowException(ExceptionInfo *exception,
898 % const ExceptionType severity,const char *reason,
899 % const char *description)
901 % A description of each parameter follows:
903 % o exception: the exception info.
905 % o severity: the severity of the exception.
907 % o reason: the reason for the exception.
909 % o description: the exception description.
912 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
913 const ExceptionType severity,const char *reason,const char *description)
915 register ExceptionInfo
918 assert(exception != (ExceptionInfo *) NULL);
919 assert(exception->signature == MagickSignature);
920 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
921 exception->exceptions);
922 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
923 (LocaleCompare(exception->reason,reason) == 0) &&
924 (LocaleCompare(exception->description,description) == 0))
926 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
927 if (p == (ExceptionInfo *) NULL)
928 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
929 (void) ResetMagickMemory(p,0,sizeof(*p));
930 p->severity=severity;
931 if (reason != (const char *) NULL)
932 p->reason=ConstantString(reason);
933 if (description != (const char *) NULL)
934 p->description=ConstantString(description);
935 p->signature=MagickSignature;
936 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
937 exception->severity=p->severity;
938 exception->reason=p->reason;
939 exception->description=p->description;
944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948 % T h r o w M a g i c k E x c e p t i o n %
952 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954 % ThrowMagickException logs an exception as determined by the log
955 % configuration file. If an error occurs, MagickFalse is returned
956 % otherwise MagickTrue.
958 % The format of the ThrowMagickException method is:
960 % MagickBooleanType ThrowFileException(ExceptionInfo *exception,
961 % const char *module,const char *function,const size_t line,
962 % const ExceptionType severity,const char *tag,const char *format,...)
964 % A description of each parameter follows:
966 % o exception: the exception info.
968 % o filename: the source module filename.
970 % o function: the function name.
972 % o line: the line number of the source module.
974 % o severity: Specifies the numeric error category.
976 % o tag: the locale tag.
978 % o format: the output format.
982 MagickExport MagickBooleanType ThrowMagickExceptionList(ExceptionInfo *exception,
983 const char *module,const char *function,const size_t line,
984 const ExceptionType severity,const char *tag,const char *format,
988 message[MaxTextExtent],
990 reason[MaxTextExtent];
1005 assert(exception != (ExceptionInfo *) NULL);
1006 assert(exception->signature == MagickSignature);
1007 locale=GetLocaleExceptionMessage(severity,tag);
1008 (void) CopyMagickString(reason,locale,MaxTextExtent);
1009 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
1010 length=strlen(reason);
1011 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1012 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
1014 n=vsprintf(reason+length,format,operands);
1017 reason[MaxTextExtent-1]='\0';
1018 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
1019 GetPathComponent(module,TailPath,path);
1021 if ((severity >= WarningException) && (severity < ErrorException))
1023 if ((severity >= ErrorException) && (severity < FatalErrorException))
1025 if (severity >= FatalErrorException)
1027 (void) FormatLocaleString(message,MaxTextExtent,"%s @ %s/%s/%s/%.20g",reason,
1028 type,path,function,(double) line);
1029 (void) ThrowException(exception,severity,message,(char *) NULL);
1033 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
1034 const char *module,const char *function,const size_t line,
1035 const ExceptionType severity,const char *tag,const char *format,...)
1043 va_start(operands,format);
1044 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,