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-2014 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 clone_exception->relinquish=MagickTrue;
262 return(clone_exception);
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 program.
330 % o description: Specifies any description to the reason.
333 static void DefaultFatalErrorHandler(const ExceptionType severity,
334 const char *reason,const char *description)
336 if (reason == (char *) NULL)
338 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
339 if (description != (char *) NULL)
340 (void) FormatLocaleFile(stderr," (%s)",description);
341 (void) FormatLocaleFile(stderr,".\n");
342 (void) fflush(stderr);
343 MagickCoreTerminus();
344 exit((int) (severity-FatalErrorException)+1);
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
352 + D e f a u l t W a r n i n g H a n d l e r %
356 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 % DefaultWarningHandler() displays a warning reason.
360 % The format of the DefaultWarningHandler method is:
362 % void DefaultWarningHandler(const ExceptionType severity,
363 % const char *reason,const char *description)
365 % A description of each parameter follows:
367 % o severity: Specifies the numeric warning category.
369 % o reason: Specifies the reason to display before terminating the
372 % o description: Specifies any description to the reason.
375 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
376 const char *reason,const char *description)
378 if (reason == (char *) NULL)
380 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
381 if (description != (char *) NULL)
382 (void) FormatLocaleFile(stderr," (%s)",description);
383 (void) FormatLocaleFile(stderr,".\n");
384 (void) fflush(stderr);
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392 % D e s t r o y E x c e p t i o n I n f o %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 % DestroyExceptionInfo() deallocates memory associated with an exception.
400 % The format of the DestroyExceptionInfo method is:
402 % ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
404 % A description of each parameter follows:
406 % o exception: the exception info.
409 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
414 assert(exception != (ExceptionInfo *) NULL);
415 assert(exception->signature == MagickSignature);
416 if (exception->semaphore == (SemaphoreInfo *) NULL)
417 ActivateSemaphoreInfo(&exception->semaphore);
418 LockSemaphoreInfo(exception->semaphore);
419 exception->severity=UndefinedException;
420 if (exception->exceptions != (void *) NULL)
421 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
422 exception->exceptions,DestroyExceptionElement);
423 relinquish=exception->relinquish;
424 if (exception->relinquish != MagickFalse)
425 exception->signature=(~MagickSignature);
426 UnlockSemaphoreInfo(exception->semaphore);
427 RelinquishSemaphoreInfo(&exception->semaphore);
428 if (relinquish != MagickFalse)
429 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
434 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 % G e t E x c e p t i o n I n f o %
442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % GetExceptionInfo() initializes an exception to default values.
446 % The format of the GetExceptionInfo method is:
448 % GetExceptionInfo(ExceptionInfo *exception)
450 % A description of each parameter follows:
452 % o exception: the exception info.
455 MagickExport void GetExceptionInfo(ExceptionInfo *exception)
457 assert(exception != (ExceptionInfo *) NULL);
458 (void) ResetMagickMemory(exception,0,sizeof(*exception));
459 exception->severity=UndefinedException;
460 exception->exceptions=(void *) NewLinkedList(0);
461 exception->semaphore=AcquireSemaphoreInfo();
462 exception->signature=MagickSignature;
466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
470 % G e t E x c e p t i o n M e s s a g e %
474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
476 % GetExceptionMessage() returns the error message defined by the specified
479 % The format of the GetExceptionMessage method is:
481 % char *GetExceptionMessage(const int error)
483 % A description of each parameter follows:
485 % o error: the error code.
488 MagickExport char *GetExceptionMessage(const int error)
491 exception[MaxTextExtent];
494 #if defined(MAGICKCORE_HAVE_STRERROR_R)
495 #if !defined(MAGICKCORE_STRERROR_R_CHAR_P)
496 (void) strerror_r(error,exception,sizeof(exception));
498 (void) CopyMagickString(exception,strerror_r(error,exception,
499 sizeof(exception)),sizeof(exception));
502 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
504 return(ConstantString(exception));
508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 % 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 %
516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 % GetLocaleExceptionMessage() converts a enumerated exception severity and tag
519 % to a message in the current locale.
521 % The format of the GetLocaleExceptionMessage method is:
523 % const char *GetLocaleExceptionMessage(const ExceptionType severity,
526 % A description of each parameter follows:
528 % o severity: the severity of the exception.
530 % o tag: the message tag.
534 static const char *ExceptionSeverityToTag(const ExceptionType severity)
538 case ResourceLimitWarning: return("Resource/Limit/Warning/");
539 case TypeWarning: return("Type/Warning/");
540 case OptionWarning: return("Option/Warning/");
541 case DelegateWarning: return("Delegate/Warning/");
542 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
543 case CorruptImageWarning: return("Corrupt/Image/Warning/");
544 case FileOpenWarning: return("File/Open/Warning/");
545 case BlobWarning: return("Blob/Warning/");
546 case StreamWarning: return("Stream/Warning/");
547 case CacheWarning: return("Cache/Warning/");
548 case CoderWarning: return("Coder/Warning/");
549 case FilterWarning: return("Filter/Warning/");
550 case ModuleWarning: return("Module/Warning/");
551 case DrawWarning: return("Draw/Warning/");
552 case ImageWarning: return("Image/Warning/");
553 case WandWarning: return("Wand/Warning/");
554 case XServerWarning: return("XServer/Warning/");
555 case MonitorWarning: return("Monitor/Warning/");
556 case RegistryWarning: return("Registry/Warning/");
557 case ConfigureWarning: return("Configure/Warning/");
558 case PolicyWarning: return("Policy/Warning/");
559 case ResourceLimitError: return("Resource/Limit/Error/");
560 case TypeError: return("Type/Error/");
561 case OptionError: return("Option/Error/");
562 case DelegateError: return("Delegate/Error/");
563 case MissingDelegateError: return("Missing/Delegate/Error/");
564 case CorruptImageError: return("Corrupt/Image/Error/");
565 case FileOpenError: return("File/Open/Error/");
566 case BlobError: return("Blob/Error/");
567 case StreamError: return("Stream/Error/");
568 case CacheError: return("Cache/Error/");
569 case CoderError: return("Coder/Error/");
570 case FilterError: return("Filter/Error/");
571 case ModuleError: return("Module/Error/");
572 case DrawError: return("Draw/Error/");
573 case ImageError: return("Image/Error/");
574 case WandError: return("Wand/Error/");
575 case XServerError: return("XServer/Error/");
576 case MonitorError: return("Monitor/Error/");
577 case RegistryError: return("Registry/Error/");
578 case ConfigureError: return("Configure/Error/");
579 case PolicyError: return("Policy/Error/");
580 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
581 case TypeFatalError: return("Type/FatalError/");
582 case OptionFatalError: return("Option/FatalError/");
583 case DelegateFatalError: return("Delegate/FatalError/");
584 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
585 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
586 case FileOpenFatalError: return("File/Open/FatalError/");
587 case BlobFatalError: return("Blob/FatalError/");
588 case StreamFatalError: return("Stream/FatalError/");
589 case CacheFatalError: return("Cache/FatalError/");
590 case CoderFatalError: return("Coder/FatalError/");
591 case FilterFatalError: return("Filter/FatalError/");
592 case ModuleFatalError: return("Module/FatalError/");
593 case DrawFatalError: return("Draw/FatalError/");
594 case ImageFatalError: return("Image/FatalError/");
595 case WandFatalError: return("Wand/FatalError/");
596 case XServerFatalError: return("XServer/FatalError/");
597 case MonitorFatalError: return("Monitor/FatalError/");
598 case RegistryFatalError: return("Registry/FatalError/");
599 case ConfigureFatalError: return("Configure/FatalError/");
600 case PolicyFatalError: return("Policy/FatalError/");
606 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
610 message[MaxTextExtent];
615 assert(tag != (const char *) NULL);
616 (void) FormatLocaleString(message,MaxTextExtent,"Exception/%s%s",
617 ExceptionSeverityToTag(severity),tag);
618 locale_message=GetLocaleMessage(message);
619 if (locale_message == (const char *) NULL)
621 if (locale_message == message)
623 return(locale_message);
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 % I n h e r i t E x c e p t i o n %
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 % InheritException() inherits an exception from a related exception.
639 % The format of the InheritException method is:
641 % InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
643 % A description of each parameter follows:
645 % o exception: the exception info.
647 % o relative: the related exception info.
650 MagickExport void InheritException(ExceptionInfo *exception,
651 const ExceptionInfo *relative)
653 register const ExceptionInfo
656 assert(exception != (ExceptionInfo *) NULL);
657 assert(exception->signature == MagickSignature);
658 assert(relative != (ExceptionInfo *) NULL);
659 assert(relative->signature == MagickSignature);
660 if (relative->exceptions == (void *) NULL)
662 LockSemaphoreInfo(relative->semaphore);
663 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
664 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
665 relative->exceptions);
666 while (p != (const ExceptionInfo *) NULL)
668 (void) ThrowException(exception,p->severity,p->reason,p->description);
669 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
670 relative->exceptions);
672 UnlockSemaphoreInfo(relative->semaphore);
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % M a g i c k E r r o r %
684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686 % MagickError() calls the exception handler methods with an error reason.
688 % The format of the MagickError method is:
690 % void MagickError(const ExceptionType error,const char *reason,
691 % const char *description)
693 % A description of each parameter follows:
695 % o exception: Specifies the numeric error category.
697 % o reason: Specifies the reason to display before terminating the
700 % o description: Specifies any description to the reason.
703 MagickExport void MagickError(const ExceptionType error,const char *reason,
704 const char *description)
706 if (error_handler != (ErrorHandler) NULL)
707 (*error_handler)(error,reason,description);
711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
715 % M a g i c k F a t al E r r o r %
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
721 % MagickFatalError() calls the fatal exception handler methods with an error
724 % The format of the MagickError method is:
726 % void MagickFatalError(const ExceptionType error,const char *reason,
727 % const char *description)
729 % A description of each parameter follows:
731 % o exception: Specifies the numeric error category.
733 % o reason: Specifies the reason to display before terminating the
736 % o description: Specifies any description to the reason.
739 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
740 const char *description)
742 if (fatal_error_handler != (ErrorHandler) NULL)
743 (*fatal_error_handler)(error,reason,description);
747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 % M a g i c k W a r n i n g %
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % MagickWarning() calls the warning handler methods with a warning reason.
759 % The format of the MagickWarning method is:
761 % void MagickWarning(const ExceptionType warning,const char *reason,
762 % const char *description)
764 % A description of each parameter follows:
766 % o warning: the warning severity.
768 % o reason: Define the reason for the warning.
770 % o description: Describe the warning.
773 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
774 const char *description)
776 if (warning_handler != (WarningHandler) NULL)
777 (*warning_handler)(warning,reason,description);
781 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 % S e t E r r o r H a n d l e r %
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 % SetErrorHandler() sets the exception handler to the specified method
792 % and returns the previous exception handler.
794 % The format of the SetErrorHandler method is:
796 % ErrorHandler SetErrorHandler(ErrorHandler handler)
798 % A description of each parameter follows:
800 % o handler: the method to handle errors.
803 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
808 previous_handler=error_handler;
809 error_handler=handler;
810 return(previous_handler);
814 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818 % S e t F a t a l E r r o r H a n d l e r %
822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 % SetFatalErrorHandler() sets the fatal exception handler to the specified
825 % method and returns the previous fatal exception handler.
827 % The format of the SetErrorHandler method is:
829 % ErrorHandler SetErrorHandler(ErrorHandler handler)
831 % A description of each parameter follows:
833 % o handler: the method to handle errors.
836 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
841 previous_handler=fatal_error_handler;
842 fatal_error_handler=handler;
843 return(previous_handler);
847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851 % S e t W a r n i n g H a n d l e r %
855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857 % SetWarningHandler() sets the warning handler to the specified method
858 % and returns the previous warning handler.
860 % The format of the SetWarningHandler method is:
862 % ErrorHandler SetWarningHandler(ErrorHandler handler)
864 % A description of each parameter follows:
866 % o handler: the method to handle warnings.
869 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
874 previous_handler=warning_handler;
875 warning_handler=handler;
876 return(previous_handler);
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % T h r o w E x c e p t i o n %
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 % ThrowException() throws an exception with the specified severity code,
891 % reason, and optional description.
893 % The format of the ThrowException method is:
895 % MagickBooleanType ThrowException(ExceptionInfo *exception,
896 % const ExceptionType severity,const char *reason,
897 % const char *description)
899 % A description of each parameter follows:
901 % o exception: the exception info.
903 % o severity: the severity of the exception.
905 % o reason: the reason for the exception.
907 % o description: the exception description.
910 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
911 const ExceptionType severity,const char *reason,const char *description)
913 register ExceptionInfo
916 assert(exception != (ExceptionInfo *) NULL);
917 assert(exception->signature == MagickSignature);
918 LockSemaphoreInfo(exception->semaphore);
919 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
920 exception->exceptions);
921 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
922 (LocaleCompare(exception->reason,reason) == 0) &&
923 (LocaleCompare(exception->description,description) == 0))
925 UnlockSemaphoreInfo(exception->semaphore);
928 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
929 if (p == (ExceptionInfo *) NULL)
931 UnlockSemaphoreInfo(exception->semaphore);
932 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
934 (void) ResetMagickMemory(p,0,sizeof(*p));
935 p->severity=severity;
936 if (reason != (const char *) NULL)
937 p->reason=ConstantString(reason);
938 if (description != (const char *) NULL)
939 p->description=ConstantString(description);
940 p->signature=MagickSignature;
941 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
942 if (p->severity >= exception->severity)
944 exception->severity=p->severity;
945 exception->reason=p->reason;
946 exception->description=p->description;
948 UnlockSemaphoreInfo(exception->semaphore);
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957 % T h r o w M a g i c k E x c e p t i o n %
961 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963 % ThrowMagickException logs an exception as determined by the log
964 % configuration file. If an error occurs, MagickFalse is returned
965 % otherwise MagickTrue.
967 % The format of the ThrowMagickException method is:
969 % MagickBooleanType ThrowFileException(ExceptionInfo *exception,
970 % const char *module,const char *function,const size_t line,
971 % const ExceptionType severity,const char *tag,const char *format,...)
973 % A description of each parameter follows:
975 % o exception: the exception info.
977 % o filename: the source module filename.
979 % o function: the function name.
981 % o line: the line number of the source module.
983 % o severity: Specifies the numeric error category.
985 % o tag: the locale tag.
987 % o format: the output format.
991 MagickExport MagickBooleanType ThrowMagickExceptionList(
992 ExceptionInfo *exception,const char *module,const char *function,
993 const size_t line,const ExceptionType severity,const char *tag,
994 const char *format,va_list operands)
997 message[MaxTextExtent],
999 reason[MaxTextExtent];
1014 assert(exception != (ExceptionInfo *) NULL);
1015 assert(exception->signature == MagickSignature);
1016 locale=GetLocaleExceptionMessage(severity,tag);
1017 (void) CopyMagickString(reason,locale,MaxTextExtent);
1018 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
1019 length=strlen(reason);
1020 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1021 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
1023 n=vsprintf(reason+length,format,operands);
1026 reason[MaxTextExtent-1]='\0';
1027 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
1028 GetPathComponent(module,TailPath,path);
1030 if ((severity >= WarningException) && (severity < ErrorException))
1032 if ((severity >= ErrorException) && (severity < FatalErrorException))
1034 if (severity >= FatalErrorException)
1036 (void) FormatLocaleString(message,MaxTextExtent,"%s @ %s/%s/%s/%.20g",reason,
1037 type,path,function,(double) line);
1038 (void) ThrowException(exception,severity,message,(char *) NULL);
1042 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
1043 const char *module,const char *function,const size_t line,
1044 const ExceptionType severity,const char *tag,const char *format,...)
1052 va_start(operands,format);
1053 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,