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-2011 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 + D e f a u l t E r r o r H a n d l e r %
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % DefaultErrorHandler() displays an error reason.
242 % The format of the DefaultErrorHandler method is:
244 % void MagickError(const ExceptionType severity,const char *reason,
245 % const char *description)
247 % A description of each parameter follows:
249 % o severity: Specifies the numeric error category.
251 % o reason: Specifies the reason to display before terminating the
254 % o description: Specifies any description to the reason.
257 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
258 const char *reason,const char *description)
260 if (reason == (char *) NULL)
262 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
263 if (description != (char *) NULL)
264 (void) FormatLocaleFile(stderr," (%s)",description);
265 (void) FormatLocaleFile(stderr,".\n");
266 (void) fflush(stderr);
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 + D e f a u l t F a t a l E r r o r H a n d l e r %
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % DefaultFatalErrorHandler() displays an error reason and then terminates the
283 % The format of the DefaultFatalErrorHandler method is:
285 % void MagickFatalError(const ExceptionType severity,const char *reason,
286 % const char *description)
288 % A description of each parameter follows:
290 % o severity: Specifies the numeric error category.
292 % o reason: Specifies the reason to display before terminating the
295 % o description: Specifies any description to the reason.
298 static void DefaultFatalErrorHandler(
299 const ExceptionType magick_unused(severity),
300 const char *reason,const char *description)
302 if (reason == (char *) NULL)
304 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
305 if (description != (char *) NULL)
306 (void) FormatLocaleFile(stderr," (%s)",description);
307 (void) FormatLocaleFile(stderr,".\n");
308 (void) fflush(stderr);
309 MagickCoreTerminus();
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 + D e f a u l t W a r n i n g H a n d l e r %
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 % DefaultWarningHandler() displays a warning reason.
326 % The format of the DefaultWarningHandler method is:
328 % void DefaultWarningHandler(const ExceptionType severity,
329 % const char *reason,const char *description)
331 % A description of each parameter follows:
333 % o severity: Specifies the numeric warning category.
335 % o reason: Specifies the reason to display before terminating the
338 % o description: Specifies any description to the reason.
341 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
342 const char *reason,const char *description)
344 if (reason == (char *) NULL)
346 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
347 if (description != (char *) NULL)
348 (void) FormatLocaleFile(stderr," (%s)",description);
349 (void) FormatLocaleFile(stderr,".\n");
350 (void) fflush(stderr);
354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 % D e s t r o y E x c e p t i o n I n f o %
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 % DestroyExceptionInfo() deallocates memory associated with an exception.
366 % The format of the DestroyExceptionInfo method is:
368 % ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
370 % A description of each parameter follows:
372 % o exception: the exception info.
375 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
380 assert(exception != (ExceptionInfo *) NULL);
381 assert(exception->signature == MagickSignature);
382 if (exception->semaphore == (SemaphoreInfo *) NULL)
383 AcquireSemaphoreInfo(&exception->semaphore);
384 LockSemaphoreInfo(exception->semaphore);
385 exception->severity=UndefinedException;
386 if (exception->exceptions != (void *) NULL)
387 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
388 exception->exceptions,DestroyExceptionElement);
389 relinquish=exception->relinquish;
390 if (exception->relinquish != MagickFalse)
391 exception->signature=(~MagickSignature);
392 UnlockSemaphoreInfo(exception->semaphore);
393 DestroySemaphoreInfo(&exception->semaphore);
394 if (relinquish != MagickFalse)
395 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 % G e t E x c e p t i o n I n f o %
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 % GetExceptionInfo() initializes an exception to default values.
412 % The format of the GetExceptionInfo method is:
414 % GetExceptionInfo(ExceptionInfo *exception)
416 % A description of each parameter follows:
418 % o exception: the exception info.
421 MagickExport void GetExceptionInfo(ExceptionInfo *exception)
423 assert(exception != (ExceptionInfo *) NULL);
424 (void) ResetMagickMemory(exception,0,sizeof(*exception));
425 exception->severity=UndefinedException;
426 exception->exceptions=(void *) NewLinkedList(0);
427 exception->semaphore=AllocateSemaphoreInfo();
428 exception->signature=MagickSignature;
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[MaxTextExtent];
460 #if defined(MAGICKCORE_HAVE_STRERROR_R)
461 #if !defined(_GNU_SOURCE)
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[MaxTextExtent];
581 assert(tag != (const char *) NULL);
582 (void) FormatLocaleString(message,MaxTextExtent,"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 == MagickSignature);
624 assert(relative != (ExceptionInfo *) NULL);
625 assert(relative->signature == MagickSignature);
626 if (relative->exceptions == (void *) NULL)
628 LockSemaphoreInfo(exception->semaphore);
629 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
630 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
631 relative->exceptions);
632 while (p != (const ExceptionInfo *) NULL)
634 (void) ThrowException(exception,p->severity,p->reason,p->description);
635 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
636 relative->exceptions);
638 UnlockSemaphoreInfo(exception->semaphore);
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 % M a g i c k E r r o r %
650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 % MagickError() calls the exception handler methods with an error reason.
654 % The format of the MagickError method is:
656 % void MagickError(const ExceptionType error,const char *reason,
657 % const char *description)
659 % A description of each parameter follows:
661 % o exception: Specifies the numeric error category.
663 % o reason: Specifies the reason to display before terminating the
666 % o description: Specifies any description to the reason.
669 MagickExport void MagickError(const ExceptionType error,const char *reason,
670 const char *description)
672 if (error_handler != (ErrorHandler) NULL)
673 (*error_handler)(error,reason,description);
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 % M a g i c k F a t al E r r o r %
685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687 % MagickFatalError() calls the fatal exception handler methods with an error
690 % The format of the MagickError method is:
692 % void MagickFatalError(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 MagickFatalError(const ExceptionType error,const char *reason,
706 const char *description)
708 if (fatal_error_handler != (ErrorHandler) NULL)
709 (*fatal_error_handler)(error,reason,description);
713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
717 % M a g i c k W a r n i n g %
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 % MagickWarning() calls the warning handler methods with a warning reason.
725 % The format of the MagickWarning method is:
727 % void MagickWarning(const ExceptionType warning,const char *reason,
728 % const char *description)
730 % A description of each parameter follows:
732 % o warning: the warning severity.
734 % o reason: Define the reason for the warning.
736 % o description: Describe the warning.
739 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
740 const char *description)
742 if (warning_handler != (WarningHandler) NULL)
743 (*warning_handler)(warning,reason,description);
747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
751 % S e t E r r o r H a n d l e r %
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % SetErrorHandler() sets the exception handler to the specified method
758 % and returns the previous exception handler.
760 % The format of the SetErrorHandler method is:
762 % ErrorHandler SetErrorHandler(ErrorHandler handler)
764 % A description of each parameter follows:
766 % o handler: the method to handle errors.
769 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
774 previous_handler=error_handler;
775 error_handler=handler;
776 return(previous_handler);
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784 % S e t F a t a l E r r o r H a n d l e r %
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % SetFatalErrorHandler() sets the fatal exception handler to the specified
791 % method and returns the previous fatal 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 FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
807 previous_handler=fatal_error_handler;
808 fatal_error_handler=handler;
809 return(previous_handler);
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
817 % S e t W a r n i n g H a n d l e r %
821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 % SetWarningHandler() sets the warning handler to the specified method
824 % and returns the previous warning handler.
826 % The format of the SetWarningHandler method is:
828 % ErrorHandler SetWarningHandler(ErrorHandler handler)
830 % A description of each parameter follows:
832 % o handler: the method to handle warnings.
835 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
840 previous_handler=warning_handler;
841 warning_handler=handler;
842 return(previous_handler);
846 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
850 % T h r o w E x c e p t i o n %
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856 % ThrowException() throws an exception with the specified severity code,
857 % reason, and optional description.
859 % The format of the ThrowException method is:
861 % MagickBooleanType ThrowException(ExceptionInfo *exception,
862 % const ExceptionType severity,const char *reason,
863 % const char *description)
865 % A description of each parameter follows:
867 % o exception: the exception info.
869 % o severity: the severity of the exception.
871 % o reason: the reason for the exception.
873 % o description: the exception description.
876 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
877 const ExceptionType severity,const char *reason,const char *description)
879 register ExceptionInfo
882 assert(exception != (ExceptionInfo *) NULL);
883 assert(exception->signature == MagickSignature);
884 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
885 exception->exceptions);
886 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
887 (LocaleCompare(exception->reason,reason) == 0) &&
888 (LocaleCompare(exception->description,description) == 0))
890 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
891 if (p == (ExceptionInfo *) NULL)
892 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
893 (void) ResetMagickMemory(p,0,sizeof(*p));
894 p->severity=severity;
895 if (reason != (const char *) NULL)
896 p->reason=ConstantString(reason);
897 if (description != (const char *) NULL)
898 p->description=ConstantString(description);
899 p->signature=MagickSignature;
900 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
901 exception->severity=p->severity;
902 exception->reason=p->reason;
903 exception->description=p->description;
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912 % T h r o w M a g i c k E x c e p t i o n %
916 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 % ThrowMagickException logs an exception as determined by the log configuration
919 % file. If an error occurs, MagickFalse is returned otherwise MagickTrue.
921 % The format of the ThrowMagickException method is:
923 % MagickBooleanType ThrowFileException(ExceptionInfo *exception,
924 % const char *module,const char *function,const size_t line,
925 % const ExceptionType severity,const char *tag,const char *format,...)
927 % A description of each parameter follows:
929 % o exception: the exception info.
931 % o filename: the source module filename.
933 % o function: the function name.
935 % o line: the line number of the source module.
937 % o severity: Specifies the numeric error category.
939 % o tag: the locale tag.
941 % o format: the output format.
945 static MagickBooleanType ThrowMagickExceptionList(ExceptionInfo *exception,
946 const char *module,const char *function,const size_t line,
947 const ExceptionType severity,const char *tag,const char *format,
951 message[MaxTextExtent],
953 reason[MaxTextExtent];
968 assert(exception != (ExceptionInfo *) NULL);
969 assert(exception->signature == MagickSignature);
970 locale=GetLocaleExceptionMessage(severity,tag);
971 (void) CopyMagickString(reason,locale,MaxTextExtent);
972 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
973 length=strlen(reason);
974 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
975 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
977 n=vsprintf(reason+length,format,operands);
980 reason[MaxTextExtent-1]='\0';
981 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
982 GetPathComponent(module,TailPath,path);
984 if ((severity >= WarningException) && (severity < ErrorException))
986 if ((severity >= ErrorException) && (severity < FatalErrorException))
988 if (severity >= FatalErrorException)
990 (void) FormatLocaleString(message,MaxTextExtent,"%s @ %s/%s/%s/%.20g",reason,
991 type,path,function,(double) line);
992 (void) ThrowException(exception,severity,message,(char *) NULL);
996 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
997 const char *module,const char *function,const size_t line,
998 const ExceptionType severity,const char *tag,const char *format,...)
1006 va_start(operands,format);
1007 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,