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-2015 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 InitializeExceptionInfo(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 assert(exception != (ExceptionInfo *) NULL);
156 assert(exception->signature == MagickCoreSignature);
157 if (exception->exceptions == (void *) NULL)
159 LockSemaphoreInfo(exception->semaphore);
160 ClearLinkedList((LinkedListInfo *) exception->exceptions,
161 DestroyExceptionElement);
162 exception->severity=UndefinedException;
163 exception->reason=(char *) NULL;
164 exception->description=(char *) NULL;
165 UnlockSemaphoreInfo(exception->semaphore);
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174 % C a t c h E x c e p t i o n %
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 % CatchException() returns if no exceptions is found otherwise it reports
181 % the exception as a warning, error, or fatal depending on the severity.
183 % The format of the CatchException method is:
185 % CatchException(ExceptionInfo *exception)
187 % A description of each parameter follows:
189 % o exception: the exception info.
192 MagickExport void CatchException(ExceptionInfo *exception)
194 register const ExceptionInfo
197 assert(exception != (ExceptionInfo *) NULL);
198 assert(exception->signature == MagickCoreSignature);
199 if (exception->exceptions == (void *) NULL)
201 LockSemaphoreInfo(exception->semaphore);
202 ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
203 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
204 exception->exceptions);
205 while (p != (const ExceptionInfo *) NULL)
207 if ((p->severity >= WarningException) && (p->severity < ErrorException))
208 MagickWarning(p->severity,p->reason,p->description);
209 if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
210 MagickError(p->severity,p->reason,p->description);
211 if (p->severity >= FatalErrorException)
212 MagickFatalError(p->severity,p->reason,p->description);
213 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
214 exception->exceptions);
216 UnlockSemaphoreInfo(exception->semaphore);
217 ClearMagickException(exception);
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 % C l o n e E x c e p t i o n I n f o %
229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 % CloneExceptionInfo() clones the ExceptionInfo structure.
233 % The format of the CloneExceptionInfo method is:
235 % ExceptionInfo *CloneException(ExceptionInfo *exception)
237 % A description of each parameter follows:
239 % o exception: the exception info.
242 MagickExport ExceptionInfo *CloneExceptionInfo(ExceptionInfo *exception)
247 clone_exception=(ExceptionInfo *) AcquireMagickMemory(sizeof(*exception));
248 if (clone_exception == (ExceptionInfo *) NULL)
249 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
250 InitializeExceptionInfo(clone_exception);
251 InheritException(clone_exception,exception);
252 clone_exception->relinquish=MagickTrue;
253 return(clone_exception);
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 + D e f a u l t E r r o r H a n d l e r %
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 % DefaultErrorHandler() displays an error reason.
269 % The format of the DefaultErrorHandler method is:
271 % void MagickError(const ExceptionType severity,const char *reason,
272 % const char *description)
274 % A description of each parameter follows:
276 % o severity: Specifies the numeric error category.
278 % o reason: Specifies the reason to display before terminating the
281 % o description: Specifies any description to the reason.
284 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
285 const char *reason,const char *description)
287 if (reason == (char *) NULL)
289 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
290 if (description != (char *) NULL)
291 (void) FormatLocaleFile(stderr," (%s)",description);
292 (void) FormatLocaleFile(stderr,".\n");
293 (void) fflush(stderr);
297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 + D e f a u l t F a t a l E r r o r H a n d l e r %
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 % DefaultFatalErrorHandler() displays an error reason and then terminates the
310 % The format of the DefaultFatalErrorHandler method is:
312 % void MagickFatalError(const ExceptionType severity,const char *reason,
313 % const char *description)
315 % A description of each parameter follows:
317 % o severity: Specifies the numeric error category.
319 % o reason: Specifies the reason to display before terminating the program.
321 % o description: Specifies any description to the reason.
324 static void DefaultFatalErrorHandler(const ExceptionType severity,
325 const char *reason,const char *description)
327 if (reason == (char *) NULL)
329 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
330 if (description != (char *) NULL)
331 (void) FormatLocaleFile(stderr," (%s)",description);
332 (void) FormatLocaleFile(stderr,".\n");
333 (void) fflush(stderr);
334 MagickCoreTerminus();
335 exit((int) (severity-FatalErrorException)+1);
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 + D e f a u l t W a r n i n g H a n d l e r %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % DefaultWarningHandler() displays a warning reason.
351 % The format of the DefaultWarningHandler method is:
353 % void DefaultWarningHandler(const ExceptionType severity,
354 % const char *reason,const char *description)
356 % A description of each parameter follows:
358 % o severity: Specifies the numeric warning category.
360 % o reason: Specifies the reason to display before terminating the
363 % o description: Specifies any description to the reason.
366 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
367 const char *reason,const char *description)
369 if (reason == (char *) NULL)
371 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
372 if (description != (char *) NULL)
373 (void) FormatLocaleFile(stderr," (%s)",description);
374 (void) FormatLocaleFile(stderr,".\n");
375 (void) fflush(stderr);
379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 % D e s t r o y E x c e p t i o n I n f o %
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 % DestroyExceptionInfo() deallocates memory associated with an exception.
391 % The format of the DestroyExceptionInfo method is:
393 % ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
395 % A description of each parameter follows:
397 % o exception: the exception info.
400 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
405 assert(exception != (ExceptionInfo *) NULL);
406 assert(exception->signature == MagickCoreSignature);
407 if (exception->semaphore == (SemaphoreInfo *) NULL)
408 ActivateSemaphoreInfo(&exception->semaphore);
409 LockSemaphoreInfo(exception->semaphore);
410 exception->severity=UndefinedException;
411 if (exception->relinquish != MagickFalse)
413 exception->signature=(~MagickCoreSignature);
414 if (exception->exceptions != (void *) NULL)
415 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
416 exception->exceptions,DestroyExceptionElement);
418 else if (exception->exceptions != (void *) NULL)
419 ClearLinkedList((LinkedListInfo *) exception->exceptions,
420 DestroyExceptionElement);
421 relinquish=exception->relinquish;
422 UnlockSemaphoreInfo(exception->semaphore);
423 if (relinquish != MagickFalse)
425 RelinquishSemaphoreInfo(&exception->semaphore);
426 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % G e t E x c e p t i o n M e s s a g e %
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 % GetExceptionMessage() returns the error message defined by the specified
445 % The format of the GetExceptionMessage method is:
447 % char *GetExceptionMessage(const int error)
449 % A description of each parameter follows:
451 % o error: the error code.
454 MagickExport char *GetExceptionMessage(const int error)
457 exception[MagickPathExtent];
460 #if defined(MAGICKCORE_HAVE_STRERROR_R)
461 #if !defined(MAGICKCORE_STRERROR_R_CHAR_P)
462 (void) strerror_r(error,exception,sizeof(exception));
464 (void) CopyMagickString(exception,strerror_r(error,exception,
465 sizeof(exception)),sizeof(exception));
468 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
470 return(ConstantString(exception));
474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 % 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 %
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % GetLocaleExceptionMessage() converts a enumerated exception severity and tag
485 % to a message in the current locale.
487 % The format of the GetLocaleExceptionMessage method is:
489 % const char *GetLocaleExceptionMessage(const ExceptionType severity,
492 % A description of each parameter follows:
494 % o severity: the severity of the exception.
496 % o tag: the message tag.
500 static const char *ExceptionSeverityToTag(const ExceptionType severity)
504 case ResourceLimitWarning: return("Resource/Limit/Warning/");
505 case TypeWarning: return("Type/Warning/");
506 case OptionWarning: return("Option/Warning/");
507 case DelegateWarning: return("Delegate/Warning/");
508 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
509 case CorruptImageWarning: return("Corrupt/Image/Warning/");
510 case FileOpenWarning: return("File/Open/Warning/");
511 case BlobWarning: return("Blob/Warning/");
512 case StreamWarning: return("Stream/Warning/");
513 case CacheWarning: return("Cache/Warning/");
514 case CoderWarning: return("Coder/Warning/");
515 case FilterWarning: return("Filter/Warning/");
516 case ModuleWarning: return("Module/Warning/");
517 case DrawWarning: return("Draw/Warning/");
518 case ImageWarning: return("Image/Warning/");
519 case WandWarning: return("Wand/Warning/");
520 case XServerWarning: return("XServer/Warning/");
521 case MonitorWarning: return("Monitor/Warning/");
522 case RegistryWarning: return("Registry/Warning/");
523 case ConfigureWarning: return("Configure/Warning/");
524 case PolicyWarning: return("Policy/Warning/");
525 case ResourceLimitError: return("Resource/Limit/Error/");
526 case TypeError: return("Type/Error/");
527 case OptionError: return("Option/Error/");
528 case DelegateError: return("Delegate/Error/");
529 case MissingDelegateError: return("Missing/Delegate/Error/");
530 case CorruptImageError: return("Corrupt/Image/Error/");
531 case FileOpenError: return("File/Open/Error/");
532 case BlobError: return("Blob/Error/");
533 case StreamError: return("Stream/Error/");
534 case CacheError: return("Cache/Error/");
535 case CoderError: return("Coder/Error/");
536 case FilterError: return("Filter/Error/");
537 case ModuleError: return("Module/Error/");
538 case DrawError: return("Draw/Error/");
539 case ImageError: return("Image/Error/");
540 case WandError: return("Wand/Error/");
541 case XServerError: return("XServer/Error/");
542 case MonitorError: return("Monitor/Error/");
543 case RegistryError: return("Registry/Error/");
544 case ConfigureError: return("Configure/Error/");
545 case PolicyError: return("Policy/Error/");
546 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
547 case TypeFatalError: return("Type/FatalError/");
548 case OptionFatalError: return("Option/FatalError/");
549 case DelegateFatalError: return("Delegate/FatalError/");
550 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
551 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
552 case FileOpenFatalError: return("File/Open/FatalError/");
553 case BlobFatalError: return("Blob/FatalError/");
554 case StreamFatalError: return("Stream/FatalError/");
555 case CacheFatalError: return("Cache/FatalError/");
556 case CoderFatalError: return("Coder/FatalError/");
557 case FilterFatalError: return("Filter/FatalError/");
558 case ModuleFatalError: return("Module/FatalError/");
559 case DrawFatalError: return("Draw/FatalError/");
560 case ImageFatalError: return("Image/FatalError/");
561 case WandFatalError: return("Wand/FatalError/");
562 case XServerFatalError: return("XServer/FatalError/");
563 case MonitorFatalError: return("Monitor/FatalError/");
564 case RegistryFatalError: return("Registry/FatalError/");
565 case ConfigureFatalError: return("Configure/FatalError/");
566 case PolicyFatalError: return("Policy/FatalError/");
572 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
576 message[MagickPathExtent];
581 assert(tag != (const char *) NULL);
582 (void) FormatLocaleString(message,MagickPathExtent,"Exception/%s%s",
583 ExceptionSeverityToTag(severity),tag);
584 locale_message=GetLocaleMessage(message);
585 if (locale_message == (const char *) NULL)
587 if (locale_message == message)
589 return(locale_message);
593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597 % I n h e r i t E x c e p t i o n %
601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
603 % InheritException() inherits an exception from a related exception.
605 % The format of the InheritException method is:
607 % InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
609 % A description of each parameter follows:
611 % o exception: the exception info.
613 % o relative: the related exception info.
616 MagickExport void InheritException(ExceptionInfo *exception,
617 const ExceptionInfo *relative)
619 register const ExceptionInfo
622 assert(exception != (ExceptionInfo *) NULL);
623 assert(exception->signature == MagickCoreSignature);
624 assert(relative != (ExceptionInfo *) NULL);
625 assert(relative->signature == MagickCoreSignature);
626 assert(exception != relative);
627 if (relative->exceptions == (void *) NULL)
629 LockSemaphoreInfo(relative->semaphore);
630 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
631 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
632 relative->exceptions);
633 while (p != (const ExceptionInfo *) NULL)
635 (void) ThrowException(exception,p->severity,p->reason,p->description);
636 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
637 relative->exceptions);
639 UnlockSemaphoreInfo(relative->semaphore);
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
647 % I n i t i a l i z e t E x c e p t i o n I n f o %
651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653 % InitializeExceptionInfo() initializes an exception to default values.
655 % The format of the InitializeExceptionInfo method is:
657 % InitializeExceptionInfo(ExceptionInfo *exception)
659 % A description of each parameter follows:
661 % o exception: the exception info.
664 MagickPrivate void InitializeExceptionInfo(ExceptionInfo *exception)
666 assert(exception != (ExceptionInfo *) NULL);
667 (void) ResetMagickMemory(exception,0,sizeof(*exception));
668 exception->severity=UndefinedException;
669 exception->exceptions=(void *) NewLinkedList(0);
670 exception->semaphore=AcquireSemaphoreInfo();
671 exception->signature=MagickCoreSignature;
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 % M a g i c k E r r o r %
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % MagickError() calls the exception handler methods with an error reason.
687 % The format of the MagickError method is:
689 % void MagickError(const ExceptionType error,const char *reason,
690 % const char *description)
692 % A description of each parameter follows:
694 % o exception: Specifies the numeric error category.
696 % o reason: Specifies the reason to display before terminating the
699 % o description: Specifies any description to the reason.
702 MagickExport void MagickError(const ExceptionType error,const char *reason,
703 const char *description)
705 if (error_handler != (ErrorHandler) NULL)
706 (*error_handler)(error,reason,description);
710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714 % M a g i c k F a t al E r r o r %
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 % MagickFatalError() calls the fatal exception handler methods with an error
723 % The format of the MagickError method is:
725 % void MagickFatalError(const ExceptionType error,const char *reason,
726 % const char *description)
728 % A description of each parameter follows:
730 % o exception: Specifies the numeric error category.
732 % o reason: Specifies the reason to display before terminating the
735 % o description: Specifies any description to the reason.
738 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
739 const char *description)
741 if (fatal_error_handler != (ErrorHandler) NULL)
742 (*fatal_error_handler)(error,reason,description);
746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
750 % M a g i c k W a r n i n g %
754 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
756 % MagickWarning() calls the warning handler methods with a warning reason.
758 % The format of the MagickWarning method is:
760 % void MagickWarning(const ExceptionType warning,const char *reason,
761 % const char *description)
763 % A description of each parameter follows:
765 % o warning: the warning severity.
767 % o reason: Define the reason for the warning.
769 % o description: Describe the warning.
772 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
773 const char *description)
775 if (warning_handler != (WarningHandler) NULL)
776 (*warning_handler)(warning,reason,description);
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784 % S e t E r r o r H a n d l e r %
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % SetErrorHandler() sets the exception handler to the specified method
791 % and returns the previous exception handler.
793 % The format of the SetErrorHandler method is:
795 % ErrorHandler SetErrorHandler(ErrorHandler handler)
797 % A description of each parameter follows:
799 % o handler: the method to handle errors.
802 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
807 previous_handler=error_handler;
808 error_handler=handler;
809 return(previous_handler);
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
817 % S e t F a t a l E r r o r H a n d l e r %
821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 % SetFatalErrorHandler() sets the fatal exception handler to the specified
824 % method and returns the previous fatal exception handler.
826 % The format of the SetErrorHandler method is:
828 % ErrorHandler SetErrorHandler(ErrorHandler handler)
830 % A description of each parameter follows:
832 % o handler: the method to handle errors.
835 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
840 previous_handler=fatal_error_handler;
841 fatal_error_handler=handler;
842 return(previous_handler);
846 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850 % S e t W a r n i n g H a n d l e r %
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856 % SetWarningHandler() sets the warning handler to the specified method
857 % and returns the previous warning handler.
859 % The format of the SetWarningHandler method is:
861 % ErrorHandler SetWarningHandler(ErrorHandler handler)
863 % A description of each parameter follows:
865 % o handler: the method to handle warnings.
868 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
873 previous_handler=warning_handler;
874 warning_handler=handler;
875 return(previous_handler);
879 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
883 % T h r o w E x c e p t i o n %
887 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
889 % ThrowException() throws an exception with the specified severity code,
890 % reason, and optional description.
892 % The format of the ThrowException method is:
894 % MagickBooleanType ThrowException(ExceptionInfo *exception,
895 % const ExceptionType severity,const char *reason,
896 % const char *description)
898 % A description of each parameter follows:
900 % o exception: the exception info.
902 % o severity: the severity of the exception.
904 % o reason: the reason for the exception.
906 % o description: the exception description.
909 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
910 const ExceptionType severity,const char *reason,const char *description)
912 register ExceptionInfo
915 assert(exception != (ExceptionInfo *) NULL);
916 assert(exception->signature == MagickCoreSignature);
917 LockSemaphoreInfo(exception->semaphore);
918 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
919 exception->exceptions);
920 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
921 (LocaleCompare(exception->reason,reason) == 0) &&
922 (LocaleCompare(exception->description,description) == 0))
924 UnlockSemaphoreInfo(exception->semaphore);
927 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
928 if (p == (ExceptionInfo *) NULL)
930 UnlockSemaphoreInfo(exception->semaphore);
931 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
933 (void) ResetMagickMemory(p,0,sizeof(*p));
934 p->severity=severity;
935 if (reason != (const char *) NULL)
936 p->reason=ConstantString(reason);
937 if (description != (const char *) NULL)
938 p->description=ConstantString(description);
939 p->signature=MagickCoreSignature;
940 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
941 if (p->severity >= exception->severity)
943 exception->severity=p->severity;
944 exception->reason=p->reason;
945 exception->description=p->description;
947 UnlockSemaphoreInfo(exception->semaphore);
952 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956 % T h r o w M a g i c k E x c e p t i o n %
960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
962 % ThrowMagickException logs an exception as determined by the log
963 % configuration file. If an error occurs, MagickFalse is returned
964 % otherwise MagickTrue.
966 % The format of the ThrowMagickException method is:
968 % MagickBooleanType ThrowFileException(ExceptionInfo *exception,
969 % const char *module,const char *function,const size_t line,
970 % const ExceptionType severity,const char *tag,const char *format,...)
972 % A description of each parameter follows:
974 % o exception: the exception info.
976 % o filename: the source module filename.
978 % o function: the function name.
980 % o line: the line number of the source module.
982 % o severity: Specifies the numeric error category.
984 % o tag: the locale tag.
986 % o format: the output format.
990 MagickExport MagickBooleanType ThrowMagickExceptionList(
991 ExceptionInfo *exception,const char *module,const char *function,
992 const size_t line,const ExceptionType severity,const char *tag,
993 const char *format,va_list operands)
996 message[MagickPathExtent],
997 path[MagickPathExtent],
998 reason[MagickPathExtent];
1013 assert(exception != (ExceptionInfo *) NULL);
1014 assert(exception->signature == MagickCoreSignature);
1015 locale=GetLocaleExceptionMessage(severity,tag);
1016 (void) CopyMagickString(reason,locale,MagickPathExtent);
1017 (void) ConcatenateMagickString(reason," ",MagickPathExtent);
1018 length=strlen(reason);
1019 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1020 n=vsnprintf(reason+length,MagickPathExtent-length,format,operands);
1022 n=vsprintf(reason+length,format,operands);
1025 reason[MagickPathExtent-1]='\0';
1026 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
1027 GetPathComponent(module,TailPath,path);
1029 if ((severity >= WarningException) && (severity < ErrorException))
1031 if ((severity >= ErrorException) && (severity < FatalErrorException))
1033 if (severity >= FatalErrorException)
1035 (void) FormatLocaleString(message,MagickPathExtent,"%s @ %s/%s/%s/%.20g",reason,
1036 type,path,function,(double) line);
1037 (void) ThrowException(exception,severity,message,(char *) NULL);
1041 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
1042 const char *module,const char *function,const size_t line,
1043 const ExceptionType severity,const char *tag,const char *format,...)
1051 va_start(operands,format);
1052 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,