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-2017 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 % https://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/linked-list.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)
75 #define MaxExceptions 128
81 error_handler = DefaultErrorHandler;
83 static FatalErrorHandler
84 fatal_error_handler = DefaultFatalErrorHandler;
87 warning_handler = DefaultWarningHandler;
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % A c q u i r e E x c e p t i o n I n f o %
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % AcquireExceptionInfo() allocates the ExceptionInfo structure.
102 % The format of the AcquireExceptionInfo method is:
104 % ExceptionInfo *AcquireExceptionInfo(void)
107 MagickExport ExceptionInfo *AcquireExceptionInfo(void)
112 exception=(ExceptionInfo *) AcquireMagickMemory(sizeof(*exception));
113 if (exception == (ExceptionInfo *) NULL)
114 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
115 InitializeExceptionInfo(exception);
116 exception->relinquish=MagickTrue;
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % C l e a r M a g i c k E x c e p t i o n %
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 % ClearMagickException() clears any exception that may not have been caught
134 % The format of the ClearMagickException method is:
136 % ClearMagickException(ExceptionInfo *exception)
138 % A description of each parameter follows:
140 % o exception: the exception info.
144 static void *DestroyExceptionElement(void *exception)
146 register ExceptionInfo
149 p=(ExceptionInfo *) exception;
150 if (p->reason != (char *) NULL)
151 p->reason=DestroyString(p->reason);
152 if (p->description != (char *) NULL)
153 p->description=DestroyString(p->description);
154 p=(ExceptionInfo *) RelinquishMagickMemory(p);
155 return((void *) NULL);
158 MagickExport void ClearMagickException(ExceptionInfo *exception)
160 assert(exception != (ExceptionInfo *) NULL);
161 assert(exception->signature == MagickCoreSignature);
162 if (exception->exceptions == (void *) NULL)
164 LockSemaphoreInfo(exception->semaphore);
165 ClearLinkedList((LinkedListInfo *) exception->exceptions,
166 DestroyExceptionElement);
167 exception->severity=UndefinedException;
168 exception->reason=(char *) NULL;
169 exception->description=(char *) NULL;
170 UnlockSemaphoreInfo(exception->semaphore);
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % C a t c h E x c e p t i o n %
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 % CatchException() returns if no exceptions is found otherwise it reports
186 % the exception as a warning, error, or fatal depending on the severity.
188 % The format of the CatchException method is:
190 % CatchException(ExceptionInfo *exception)
192 % A description of each parameter follows:
194 % o exception: the exception info.
197 MagickExport void CatchException(ExceptionInfo *exception)
199 register const ExceptionInfo
205 assert(exception != (ExceptionInfo *) NULL);
206 assert(exception->signature == MagickCoreSignature);
207 if (exception->exceptions == (void *) NULL)
209 LockSemaphoreInfo(exception->semaphore);
210 ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
211 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
212 exception->exceptions);
213 for (i=0; p != (const ExceptionInfo *) NULL; i++)
215 if (i < MaxExceptions)
217 if ((p->severity >= WarningException) && (p->severity < ErrorException))
218 MagickWarning(p->severity,p->reason,p->description);
219 if ((p->severity >= ErrorException) &&
220 (p->severity < FatalErrorException))
221 MagickError(p->severity,p->reason,p->description);
224 if (i == MaxExceptions)
225 MagickError(ResourceLimitError,"too many exceptions",
226 "exception processing is suspended");
227 if (p->severity >= FatalErrorException)
228 MagickFatalError(p->severity,p->reason,p->description);
229 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
230 exception->exceptions);
232 UnlockSemaphoreInfo(exception->semaphore);
233 ClearMagickException(exception);
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % C l o n e E x c e p t i o n I n f o %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % CloneExceptionInfo() clones the ExceptionInfo structure.
249 % The format of the CloneExceptionInfo method is:
251 % ExceptionInfo *CloneException(ExceptionInfo *exception)
253 % A description of each parameter follows:
255 % o exception: the exception info.
258 MagickExport ExceptionInfo *CloneExceptionInfo(ExceptionInfo *exception)
263 clone_exception=(ExceptionInfo *) AcquireMagickMemory(sizeof(*exception));
264 if (clone_exception == (ExceptionInfo *) NULL)
265 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
266 InitializeExceptionInfo(clone_exception);
267 InheritException(clone_exception,exception);
268 clone_exception->relinquish=MagickTrue;
269 return(clone_exception);
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 + D e f a u l t E r r o r H a n d l e r %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % DefaultErrorHandler() displays an error reason.
285 % The format of the DefaultErrorHandler method is:
287 % void MagickError(const ExceptionType severity,const char *reason,
288 % const char *description)
290 % A description of each parameter follows:
292 % o severity: Specifies the numeric error category.
294 % o reason: Specifies the reason to display before terminating the
297 % o description: Specifies any description to the reason.
300 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
301 const char *reason,const char *description)
303 magick_unreferenced(severity);
305 if (reason == (char *) NULL)
307 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
308 if (description != (char *) NULL)
309 (void) FormatLocaleFile(stderr," (%s)",description);
310 (void) FormatLocaleFile(stderr,".\n");
311 (void) fflush(stderr);
315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 + D e f a u l t F a t a l E r r o r H a n d l e r %
323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 % DefaultFatalErrorHandler() displays an error reason and then terminates the
328 % The format of the DefaultFatalErrorHandler method is:
330 % void MagickFatalError(const ExceptionType severity,const char *reason,
331 % const char *description)
333 % A description of each parameter follows:
335 % o severity: Specifies the numeric error category.
337 % o reason: Specifies the reason to display before terminating the program.
339 % o description: Specifies any description to the reason.
342 static void DefaultFatalErrorHandler(const ExceptionType severity,
343 const char *reason,const char *description)
345 if (reason == (char *) NULL)
347 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
348 if (description != (char *) NULL)
349 (void) FormatLocaleFile(stderr," (%s)",description);
350 (void) FormatLocaleFile(stderr,".\n");
351 (void) fflush(stderr);
352 MagickCoreTerminus();
353 exit((int) (severity-FatalErrorException)+1);
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 + D e f a u l t W a r n i n g H a n d l e r %
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 % DefaultWarningHandler() displays a warning reason.
369 % The format of the DefaultWarningHandler method is:
371 % void DefaultWarningHandler(const ExceptionType severity,
372 % const char *reason,const char *description)
374 % A description of each parameter follows:
376 % o severity: Specifies the numeric warning category.
378 % o reason: Specifies the reason to display before terminating the
381 % o description: Specifies any description to the reason.
384 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
385 const char *reason,const char *description)
387 magick_unreferenced(severity);
389 if (reason == (char *) NULL)
391 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
392 if (description != (char *) NULL)
393 (void) FormatLocaleFile(stderr," (%s)",description);
394 (void) FormatLocaleFile(stderr,".\n");
395 (void) fflush(stderr);
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 % D e s t r o y E x c e p t i o n I n f o %
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 % DestroyExceptionInfo() deallocates memory associated with an exception.
411 % The format of the DestroyExceptionInfo method is:
413 % ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
415 % A description of each parameter follows:
417 % o exception: the exception info.
420 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
425 assert(exception != (ExceptionInfo *) NULL);
426 assert(exception->signature == MagickCoreSignature);
427 if (exception->semaphore == (SemaphoreInfo *) NULL)
428 ActivateSemaphoreInfo(&exception->semaphore);
429 LockSemaphoreInfo(exception->semaphore);
430 exception->severity=UndefinedException;
431 if (exception->relinquish != MagickFalse)
433 exception->signature=(~MagickCoreSignature);
434 if (exception->exceptions != (void *) NULL)
435 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
436 exception->exceptions,DestroyExceptionElement);
438 else if (exception->exceptions != (void *) NULL)
439 ClearLinkedList((LinkedListInfo *) exception->exceptions,
440 DestroyExceptionElement);
441 relinquish=exception->relinquish;
442 UnlockSemaphoreInfo(exception->semaphore);
443 if (relinquish != MagickFalse)
445 RelinquishSemaphoreInfo(&exception->semaphore);
446 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
456 % G e t E x c e p t i o n M e s s a g e %
460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
462 % GetExceptionMessage() returns the error message defined by the specified
465 % The format of the GetExceptionMessage method is:
467 % char *GetExceptionMessage(const int error)
469 % A description of each parameter follows:
471 % o error: the error code.
474 MagickExport char *GetExceptionMessage(const int error)
477 exception[MagickPathExtent];
480 #if defined(MAGICKCORE_HAVE_STRERROR_R)
481 #if !defined(MAGICKCORE_STRERROR_R_CHAR_P)
482 (void) strerror_r(error,exception,sizeof(exception));
484 (void) CopyMagickString(exception,strerror_r(error,exception,
485 sizeof(exception)),sizeof(exception));
488 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
490 return(ConstantString(exception));
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 % 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 %
502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 % GetLocaleExceptionMessage() converts a enumerated exception severity and tag
505 % to a message in the current locale.
507 % The format of the GetLocaleExceptionMessage method is:
509 % const char *GetLocaleExceptionMessage(const ExceptionType severity,
512 % A description of each parameter follows:
514 % o severity: the severity of the exception.
516 % o tag: the message tag.
520 static const char *ExceptionSeverityToTag(const ExceptionType severity)
524 case ResourceLimitWarning: return("Resource/Limit/Warning/");
525 case TypeWarning: return("Type/Warning/");
526 case OptionWarning: return("Option/Warning/");
527 case DelegateWarning: return("Delegate/Warning/");
528 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
529 case CorruptImageWarning: return("Corrupt/Image/Warning/");
530 case FileOpenWarning: return("File/Open/Warning/");
531 case BlobWarning: return("Blob/Warning/");
532 case StreamWarning: return("Stream/Warning/");
533 case CacheWarning: return("Cache/Warning/");
534 case CoderWarning: return("Coder/Warning/");
535 case FilterWarning: return("Filter/Warning/");
536 case ModuleWarning: return("Module/Warning/");
537 case DrawWarning: return("Draw/Warning/");
538 case ImageWarning: return("Image/Warning/");
539 case WandWarning: return("Wand/Warning/");
540 case XServerWarning: return("XServer/Warning/");
541 case MonitorWarning: return("Monitor/Warning/");
542 case RegistryWarning: return("Registry/Warning/");
543 case ConfigureWarning: return("Configure/Warning/");
544 case PolicyWarning: return("Policy/Warning/");
545 case ResourceLimitError: return("Resource/Limit/Error/");
546 case TypeError: return("Type/Error/");
547 case OptionError: return("Option/Error/");
548 case DelegateError: return("Delegate/Error/");
549 case MissingDelegateError: return("Missing/Delegate/Error/");
550 case CorruptImageError: return("Corrupt/Image/Error/");
551 case FileOpenError: return("File/Open/Error/");
552 case BlobError: return("Blob/Error/");
553 case StreamError: return("Stream/Error/");
554 case CacheError: return("Cache/Error/");
555 case CoderError: return("Coder/Error/");
556 case FilterError: return("Filter/Error/");
557 case ModuleError: return("Module/Error/");
558 case DrawError: return("Draw/Error/");
559 case ImageError: return("Image/Error/");
560 case WandError: return("Wand/Error/");
561 case XServerError: return("XServer/Error/");
562 case MonitorError: return("Monitor/Error/");
563 case RegistryError: return("Registry/Error/");
564 case ConfigureError: return("Configure/Error/");
565 case PolicyError: return("Policy/Error/");
566 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
567 case TypeFatalError: return("Type/FatalError/");
568 case OptionFatalError: return("Option/FatalError/");
569 case DelegateFatalError: return("Delegate/FatalError/");
570 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
571 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
572 case FileOpenFatalError: return("File/Open/FatalError/");
573 case BlobFatalError: return("Blob/FatalError/");
574 case StreamFatalError: return("Stream/FatalError/");
575 case CacheFatalError: return("Cache/FatalError/");
576 case CoderFatalError: return("Coder/FatalError/");
577 case FilterFatalError: return("Filter/FatalError/");
578 case ModuleFatalError: return("Module/FatalError/");
579 case DrawFatalError: return("Draw/FatalError/");
580 case ImageFatalError: return("Image/FatalError/");
581 case WandFatalError: return("Wand/FatalError/");
582 case XServerFatalError: return("XServer/FatalError/");
583 case MonitorFatalError: return("Monitor/FatalError/");
584 case RegistryFatalError: return("Registry/FatalError/");
585 case ConfigureFatalError: return("Configure/FatalError/");
586 case PolicyFatalError: return("Policy/FatalError/");
592 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
596 message[MagickPathExtent];
601 assert(tag != (const char *) NULL);
602 (void) FormatLocaleString(message,MagickPathExtent,"Exception/%s%s",
603 ExceptionSeverityToTag(severity),tag);
604 locale_message=GetLocaleMessage(message);
605 if (locale_message == (const char *) NULL)
607 if (locale_message == message)
609 return(locale_message);
613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617 % I n h e r i t E x c e p t i o n %
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 % InheritException() inherits an exception from a related exception.
625 % The format of the InheritException method is:
627 % InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
629 % A description of each parameter follows:
631 % o exception: the exception info.
633 % o relative: the related exception info.
636 MagickExport void InheritException(ExceptionInfo *exception,
637 const ExceptionInfo *relative)
639 register const ExceptionInfo
642 assert(exception != (ExceptionInfo *) NULL);
643 assert(exception->signature == MagickCoreSignature);
644 assert(relative != (ExceptionInfo *) NULL);
645 assert(relative->signature == MagickCoreSignature);
646 assert(exception != relative);
647 if (relative->exceptions == (void *) NULL)
649 LockSemaphoreInfo(relative->semaphore);
650 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
651 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
652 relative->exceptions);
653 while (p != (const ExceptionInfo *) NULL)
655 (void) ThrowException(exception,p->severity,p->reason,p->description);
656 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
657 relative->exceptions);
659 UnlockSemaphoreInfo(relative->semaphore);
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
667 % I n i t i a l i z e t E x c e p t i o n I n f o %
671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673 % InitializeExceptionInfo() initializes an exception to default values.
675 % The format of the InitializeExceptionInfo method is:
677 % InitializeExceptionInfo(ExceptionInfo *exception)
679 % A description of each parameter follows:
681 % o exception: the exception info.
684 MagickPrivate void InitializeExceptionInfo(ExceptionInfo *exception)
686 assert(exception != (ExceptionInfo *) NULL);
687 (void) ResetMagickMemory(exception,0,sizeof(*exception));
688 exception->severity=UndefinedException;
689 exception->exceptions=(void *) NewLinkedList(0);
690 exception->semaphore=AcquireSemaphoreInfo();
691 exception->signature=MagickCoreSignature;
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699 % M a g i c k E r r o r %
703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
705 % MagickError() calls the exception handler methods with an error reason.
707 % The format of the MagickError method is:
709 % void MagickError(const ExceptionType error,const char *reason,
710 % const char *description)
712 % A description of each parameter follows:
714 % o exception: Specifies the numeric error category.
716 % o reason: Specifies the reason to display before terminating the
719 % o description: Specifies any description to the reason.
722 MagickExport void MagickError(const ExceptionType error,const char *reason,
723 const char *description)
725 if (error_handler != (ErrorHandler) NULL)
726 (*error_handler)(error,reason,description);
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % M a g i c k F a t al E r r o r %
738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740 % MagickFatalError() calls the fatal exception handler methods with an error
743 % The format of the MagickError method is:
745 % void MagickFatalError(const ExceptionType error,const char *reason,
746 % const char *description)
748 % A description of each parameter follows:
750 % o exception: Specifies the numeric error category.
752 % o reason: Specifies the reason to display before terminating the
755 % o description: Specifies any description to the reason.
758 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
759 const char *description)
761 if (fatal_error_handler != (ErrorHandler) NULL)
762 (*fatal_error_handler)(error,reason,description);
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770 % M a g i c k W a r n i n g %
774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 % MagickWarning() calls the warning handler methods with a warning reason.
778 % The format of the MagickWarning method is:
780 % void MagickWarning(const ExceptionType warning,const char *reason,
781 % const char *description)
783 % A description of each parameter follows:
785 % o warning: the warning severity.
787 % o reason: Define the reason for the warning.
789 % o description: Describe the warning.
792 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
793 const char *description)
795 if (warning_handler != (WarningHandler) NULL)
796 (*warning_handler)(warning,reason,description);
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804 % S e t E r r o r H a n d l e r %
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 % SetErrorHandler() sets the exception handler to the specified method
811 % and returns the previous exception handler.
813 % The format of the SetErrorHandler method is:
815 % ErrorHandler SetErrorHandler(ErrorHandler handler)
817 % A description of each parameter follows:
819 % o handler: the method to handle errors.
822 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
827 previous_handler=error_handler;
828 error_handler=handler;
829 return(previous_handler);
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 % S e t F a t a l E r r o r H a n d l e r %
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
843 % SetFatalErrorHandler() sets the fatal exception handler to the specified
844 % method and returns the previous fatal exception handler.
846 % The format of the SetErrorHandler method is:
848 % ErrorHandler SetErrorHandler(ErrorHandler handler)
850 % A description of each parameter follows:
852 % o handler: the method to handle errors.
855 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
860 previous_handler=fatal_error_handler;
861 fatal_error_handler=handler;
862 return(previous_handler);
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 % S e t W a r n i n g H a n d l e r %
874 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
876 % SetWarningHandler() sets the warning handler to the specified method
877 % and returns the previous warning handler.
879 % The format of the SetWarningHandler method is:
881 % ErrorHandler SetWarningHandler(ErrorHandler handler)
883 % A description of each parameter follows:
885 % o handler: the method to handle warnings.
888 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
893 previous_handler=warning_handler;
894 warning_handler=handler;
895 return(previous_handler);
899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903 % T h r o w E x c e p t i o n %
907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909 % ThrowException() throws an exception with the specified severity code,
910 % reason, and optional description.
912 % The format of the ThrowException method is:
914 % MagickBooleanType ThrowException(ExceptionInfo *exception,
915 % const ExceptionType severity,const char *reason,
916 % const char *description)
918 % A description of each parameter follows:
920 % o exception: the exception info.
922 % o severity: the severity of the exception.
924 % o reason: the reason for the exception.
926 % o description: the exception description.
929 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
930 const ExceptionType severity,const char *reason,const char *description)
932 register ExceptionInfo
935 assert(exception != (ExceptionInfo *) NULL);
936 assert(exception->signature == MagickCoreSignature);
937 LockSemaphoreInfo(exception->semaphore);
938 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
939 exception->exceptions);
940 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
941 (LocaleCompare(exception->reason,reason) == 0) &&
942 (LocaleCompare(exception->description,description) == 0))
944 UnlockSemaphoreInfo(exception->semaphore);
947 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
948 if (p == (ExceptionInfo *) NULL)
950 UnlockSemaphoreInfo(exception->semaphore);
951 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
953 (void) ResetMagickMemory(p,0,sizeof(*p));
954 p->severity=severity;
955 if (reason != (const char *) NULL)
956 p->reason=ConstantString(reason);
957 if (description != (const char *) NULL)
958 p->description=ConstantString(description);
959 p->signature=MagickCoreSignature;
960 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
961 if (p->severity >= exception->severity)
963 exception->severity=p->severity;
964 exception->reason=p->reason;
965 exception->description=p->description;
967 UnlockSemaphoreInfo(exception->semaphore);
972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 % T h r o w M a g i c k E x c e p t i o n %
980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982 % ThrowMagickException logs an exception as determined by the log
983 % configuration file. If an error occurs, MagickFalse is returned
984 % otherwise MagickTrue.
986 % The format of the ThrowMagickException method is:
988 % MagickBooleanType ThrowFileException(ExceptionInfo *exception,
989 % const char *module,const char *function,const size_t line,
990 % const ExceptionType severity,const char *tag,const char *format,...)
992 % A description of each parameter follows:
994 % o exception: the exception info.
996 % o filename: the source module filename.
998 % o function: the function name.
1000 % o line: the line number of the source module.
1002 % o severity: Specifies the numeric error category.
1004 % o tag: the locale tag.
1006 % o format: the output format.
1010 MagickExport MagickBooleanType ThrowMagickExceptionList(
1011 ExceptionInfo *exception,const char *module,const char *function,
1012 const size_t line,const ExceptionType severity,const char *tag,
1013 const char *format,va_list operands)
1016 message[MagickPathExtent],
1017 path[MagickPathExtent],
1018 reason[MagickPathExtent];
1033 assert(exception != (ExceptionInfo *) NULL);
1034 assert(exception->signature == MagickCoreSignature);
1035 locale=GetLocaleExceptionMessage(severity,tag);
1036 (void) CopyMagickString(reason,locale,MagickPathExtent);
1037 (void) ConcatenateMagickString(reason," ",MagickPathExtent);
1038 length=strlen(reason);
1039 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1040 n=vsnprintf(reason+length,MagickPathExtent-length,format,operands);
1042 n=vsprintf(reason+length,format,operands);
1045 reason[MagickPathExtent-1]='\0';
1046 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
1047 GetPathComponent(module,TailPath,path);
1049 if ((severity >= WarningException) && (severity < ErrorException))
1051 if ((severity >= ErrorException) && (severity < FatalErrorException))
1053 if (severity >= FatalErrorException)
1055 (void) FormatLocaleString(message,MagickPathExtent,"%s @ %s/%s/%s/%.20g",reason,
1056 type,path,function,(double) line);
1057 (void) ThrowException(exception,severity,message,(char *) NULL);
1061 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
1062 const char *module,const char *function,const size_t line,
1063 const ExceptionType severity,const char *tag,const char *format,...)
1071 va_start(operands,format);
1072 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,