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-2009 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 "magick/studio.h"
44 #include "magick/client.h"
45 #include "magick/exception.h"
46 #include "magick/exception-private.h"
47 #include "magick/hashmap.h"
48 #include "magick/locale_.h"
49 #include "magick/log.h"
50 #include "magick/magick.h"
51 #include "magick/memory_.h"
52 #include "magick/string_.h"
53 #include "magick/utility.h"
58 #if defined(__cplusplus) || defined(c_plusplus)
63 DefaultErrorHandler(const ExceptionType,const char *,const char *),
64 DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
65 DefaultWarningHandler(const ExceptionType,const char *,const char *);
67 #if defined(__cplusplus) || defined(c_plusplus)
75 error_handler = DefaultErrorHandler;
77 static FatalErrorHandler
78 fatal_error_handler = DefaultFatalErrorHandler;
81 warning_handler = DefaultWarningHandler;
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % A c q u i r e E x c e p t i o n I n f o %
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 % AcquireExceptionInfo() allocates the ExceptionInfo structure.
96 % The format of the AcquireExceptionInfo method is:
98 % ExceptionInfo *AcquireExceptionInfo(void)
101 MagickExport ExceptionInfo *AcquireExceptionInfo(void)
106 exception=(ExceptionInfo *) AcquireMagickMemory(sizeof(*exception));
107 if (exception == (ExceptionInfo *) NULL)
108 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
109 GetExceptionInfo(exception);
110 exception->relinquish=MagickTrue;
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 % C l e a r M a g i c k E x c e p t i o n %
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % ClearMagickException() clears any exception that may not have been caught
128 % The format of the ClearMagickException method is:
130 % ClearMagickException(ExceptionInfo *exception)
132 % A description of each parameter follows:
134 % o exception: the exception info.
138 static void *DestroyExceptionElement(void *exception)
140 register ExceptionInfo
143 p=(ExceptionInfo *) exception;
144 if (p->reason != (char *) NULL)
145 p->reason=DestroyString(p->reason);
146 if (p->description != (char *) NULL)
147 p->description=DestroyString(p->description);
148 p=(ExceptionInfo *) RelinquishMagickMemory(p);
149 return((void *) NULL);
152 MagickExport void ClearMagickException(ExceptionInfo *exception)
154 register ExceptionInfo
157 assert(exception != (ExceptionInfo *) NULL);
158 assert(exception->signature == MagickSignature);
159 if (exception->exceptions == (void *) NULL)
161 AcquireSemaphoreInfo(&exception->semaphore);
162 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
163 exception->exceptions);
164 while (p != (ExceptionInfo *) NULL)
166 (void) DestroyExceptionElement(p);
167 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
168 exception->exceptions);
170 exception->severity=UndefinedException;
171 exception->reason=(char *) NULL;
172 exception->description=(char *) NULL;
173 RelinquishSemaphoreInfo(exception->semaphore);
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % C a t c h E x c e p t i o n %
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % CatchException() returns if no exceptions is found otherwise it reports
189 % the exception as a warning, error, or fatal depending on the severity.
191 % The format of the CatchException method is:
193 % CatchException(ExceptionInfo *exception)
195 % A description of each parameter follows:
197 % o exception: the exception info.
200 MagickExport void CatchException(ExceptionInfo *exception)
202 register const ExceptionInfo
205 assert(exception != (ExceptionInfo *) NULL);
206 assert(exception->signature == MagickSignature);
207 if (exception->exceptions == (void *) NULL)
209 AcquireSemaphoreInfo(&exception->semaphore);
210 ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
211 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
212 exception->exceptions);
213 while (p != (const ExceptionInfo *) NULL)
215 if ((p->severity >= WarningException) && (p->severity < ErrorException))
216 MagickWarning(p->severity,p->reason,p->description);
217 if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
218 MagickError(p->severity,p->reason,p->description);
219 if (exception->severity >= FatalErrorException)
220 MagickFatalError(p->severity,p->reason,p->description);
221 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
222 exception->exceptions);
224 RelinquishSemaphoreInfo(exception->semaphore);
225 ClearMagickException(exception);
229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 + D e f a u l t E r r o r H a n d l e r %
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % DefaultErrorHandler() displays an error reason.
241 % The format of the DefaultErrorHandler method is:
243 % void MagickError(const ExceptionType severity,const char *reason,
244 % const char *description)
246 % A description of each parameter follows:
248 % o severity: Specifies the numeric error category.
250 % o reason: Specifies the reason to display before terminating the
253 % o description: Specifies any description to the reason.
256 static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
257 const char *reason,const char *description)
259 if (reason == (char *) NULL)
261 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
262 if (description != (char *) NULL)
263 (void) fprintf(stderr," (%s)",description);
264 (void) fprintf(stderr,".\n");
265 (void) fflush(stderr);
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 + D e f a u l t F a t a l E r r o r H a n d l e r %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % DefaultFatalErrorHandler() displays an error reason and then terminates the
282 % The format of the DefaultFatalErrorHandler method is:
284 % void MagickFatalError(const ExceptionType severity,const char *reason,
285 % const char *description)
287 % A description of each parameter follows:
289 % o severity: Specifies the numeric error category.
291 % o reason: Specifies the reason to display before terminating the
294 % o description: Specifies any description to the reason.
297 static void DefaultFatalErrorHandler(
298 const ExceptionType magick_unused(severity),
299 const char *reason,const char *description)
301 if (reason == (char *) NULL)
303 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
304 if (description != (char *) NULL)
305 (void) fprintf(stderr," (%s)",description);
306 (void) fprintf(stderr,".\n");
307 (void) fflush(stderr);
308 MagickCoreTerminus();
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 + D e f a u l t W a r n i n g H a n d l e r %
321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 % DefaultWarningHandler() displays a warning reason.
325 % The format of the DefaultWarningHandler method is:
327 % void DefaultWarningHandler(const ExceptionType warning,
328 % const char *reason,const char *description)
330 % A description of each parameter follows:
332 % o warning: Specifies the numeric warning category.
334 % o reason: Specifies the reason to display before terminating the
337 % o description: Specifies any description to the reason.
340 static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
341 const char *reason,const char *description)
343 if (reason == (char *) NULL)
345 (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
346 if (description != (char *) NULL)
347 (void) fprintf(stderr," (%s)",description);
348 (void) fprintf(stderr,".\n");
349 (void) fflush(stderr);
353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357 % D e s t r o y E x c e p t i o n I n f o %
361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
363 % DestroyExceptionInfo() deallocates memory associated with an exception.
365 % The format of the DestroyExceptionInfo method is:
367 % ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
369 % A description of each parameter follows:
371 % o exception: the exception info.
374 MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
379 assert(exception != (ExceptionInfo *) NULL);
380 assert(exception->signature == MagickSignature);
381 AcquireSemaphoreInfo(&exception->semaphore);
382 exception->severity=UndefinedException;
383 if (exception->exceptions != (void *) NULL)
384 exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
385 exception->exceptions,DestroyExceptionElement);
386 relinquish=exception->relinquish;
387 if (exception->relinquish != MagickFalse)
388 exception->signature=(~MagickSignature);
389 RelinquishSemaphoreInfo(exception->semaphore);
390 DestroySemaphoreInfo(&exception->semaphore);
391 if (relinquish != MagickFalse)
392 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 % G e t E x c e p t i o n I n f o %
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 % GetExceptionInfo() initializes an exception to default values.
409 % The format of the GetExceptionInfo method is:
411 % GetExceptionInfo(ExceptionInfo *exception)
413 % A description of each parameter follows:
415 % o exception: the exception info.
418 MagickExport void GetExceptionInfo(ExceptionInfo *exception)
420 assert(exception != (ExceptionInfo *) NULL);
421 (void) ResetMagickMemory(exception,0,sizeof(*exception));
422 exception->severity=UndefinedException;
423 exception->exceptions=(void *) NewLinkedList(0);
424 exception->signature=MagickSignature;
428 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
432 % G e t E x c e p t i o n M e s s a g e %
436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438 % GetExceptionMessage() returns the error message defined by the specified
441 % The format of the GetExceptionMessage method is:
443 % char *GetExceptionMessage(const int error)
445 % A description of each parameter follows:
447 % o error: the error code.
450 MagickExport char *GetExceptionMessage(const int error)
453 exception[MaxTextExtent];
455 #if defined(MAGICKCORE_HAVE_STRERROR_R)
456 (void) strerror_r(error,exception,sizeof(exception));
458 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
460 return(ConstantString(exception));
464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468 % 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 %
472 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474 % GetLocaleExceptionMessage() converts a enumerated exception severity and tag
475 % to a message in the current locale.
477 % The format of the GetLocaleExceptionMessage method is:
479 % const char *GetLocaleExceptionMessage(const ExceptionType severity,
482 % A description of each parameter follows:
484 % o severity: the severity of the exception.
486 % o tag: the message tag.
490 static const char *ExceptionSeverityToTag(const ExceptionType severity)
494 case ResourceLimitWarning: return("Resource/Limit/Warning/");
495 case TypeWarning: return("Type/Warning/");
496 case OptionWarning: return("Option/Warning/");
497 case DelegateWarning: return("Delegate/Warning/");
498 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
499 case CorruptImageWarning: return("Corrupt/Image/Warning/");
500 case FileOpenWarning: return("File/Open/Warning/");
501 case BlobWarning: return("Blob/Warning/");
502 case StreamWarning: return("Stream/Warning/");
503 case CacheWarning: return("Cache/Warning/");
504 case CoderWarning: return("Coder/Warning/");
505 case ModuleWarning: return("Module/Warning/");
506 case DrawWarning: return("Draw/Warning/");
507 case ImageWarning: return("Image/Warning/");
508 case WandWarning: return("Wand/Warning/");
509 case XServerWarning: return("XServer/Warning/");
510 case MonitorWarning: return("Monitor/Warning/");
511 case RegistryWarning: return("Registry/Warning/");
512 case ConfigureWarning: return("Configure/Warning/");
513 case PolicyWarning: return("Policy/Warning/");
514 case ResourceLimitError: return("Resource/Limit/Error/");
515 case TypeError: return("Type/Error/");
516 case OptionError: return("Option/Error/");
517 case DelegateError: return("Delegate/Error/");
518 case MissingDelegateError: return("Missing/Delegate/Error/");
519 case CorruptImageError: return("Corrupt/Image/Error/");
520 case FileOpenError: return("File/Open/Error/");
521 case BlobError: return("Blob/Error/");
522 case StreamError: return("Stream/Error/");
523 case CacheError: return("Cache/Error/");
524 case CoderError: return("Coder/Error/");
525 case ModuleError: return("Module/Error/");
526 case DrawError: return("Draw/Error/");
527 case ImageError: return("Image/Error/");
528 case WandError: return("Wand/Error/");
529 case XServerError: return("XServer/Error/");
530 case MonitorError: return("Monitor/Error/");
531 case RegistryError: return("Registry/Error/");
532 case ConfigureError: return("Configure/Error/");
533 case PolicyError: return("Policy/Error/");
534 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
535 case TypeFatalError: return("Type/FatalError/");
536 case OptionFatalError: return("Option/FatalError/");
537 case DelegateFatalError: return("Delegate/FatalError/");
538 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
539 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
540 case FileOpenFatalError: return("File/Open/FatalError/");
541 case BlobFatalError: return("Blob/FatalError/");
542 case StreamFatalError: return("Stream/FatalError/");
543 case CacheFatalError: return("Cache/FatalError/");
544 case CoderFatalError: return("Coder/FatalError/");
545 case ModuleFatalError: return("Module/FatalError/");
546 case DrawFatalError: return("Draw/FatalError/");
547 case ImageFatalError: return("Image/FatalError/");
548 case WandFatalError: return("Wand/FatalError/");
549 case XServerFatalError: return("XServer/FatalError/");
550 case MonitorFatalError: return("Monitor/FatalError/");
551 case RegistryFatalError: return("Registry/FatalError/");
552 case ConfigureFatalError: return("Configure/FatalError/");
553 case PolicyFatalError: return("Policy/FatalError/");
559 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
563 message[MaxTextExtent];
568 assert(tag != (const char *) NULL);
569 (void) FormatMagickString(message,MaxTextExtent,"Exception/%s%s",
570 ExceptionSeverityToTag(severity),tag);
571 locale_message=GetLocaleMessage(message);
572 if (locale_message == (const char *) NULL)
574 if (locale_message == message)
576 return(locale_message);
580 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 % I n h e r i t E x c e p t i o n %
588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590 % InheritException() inherits an exception from a related exception.
592 % The format of the InheritException method is:
594 % InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
596 % A description of each parameter follows:
598 % o exception: the exception info.
600 % o relative: the related exception info.
603 MagickExport void InheritException(ExceptionInfo *exception,
604 const ExceptionInfo *relative)
606 register const ExceptionInfo
609 assert(exception != (ExceptionInfo *) NULL);
610 assert(exception->signature == MagickSignature);
611 assert(relative != (ExceptionInfo *) NULL);
612 assert(relative->signature == MagickSignature);
613 if (relative->exceptions == (void *) NULL)
615 AcquireSemaphoreInfo(&exception->semaphore);
616 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
617 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
618 relative->exceptions);
619 while (p != (const ExceptionInfo *) NULL)
621 (void) ThrowException(exception,p->severity,p->reason,p->description);
622 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
623 relative->exceptions);
625 RelinquishSemaphoreInfo(exception->semaphore);
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 % M a g i c k E r r o r %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % MagickError() calls the exception handler methods with an error reason.
641 % The format of the MagickError method is:
643 % void MagickError(const ExceptionType error,const char *reason,
644 % const char *description)
646 % A description of each parameter follows:
648 % o exception: Specifies the numeric error category.
650 % o reason: Specifies the reason to display before terminating the
653 % o description: Specifies any description to the reason.
656 MagickExport void MagickError(const ExceptionType error,const char *reason,
657 const char *description)
659 if (error_handler != (ErrorHandler) NULL)
660 (*error_handler)(error,reason,description);
664 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
668 % M a g i c k F a t al E r r o r %
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 % MagickFatalError() calls the fatal exception handler methods with an error
677 % The format of the MagickError method is:
679 % void MagickFatalError(const ExceptionType error,const char *reason,
680 % const char *description)
682 % A description of each parameter follows:
684 % o exception: Specifies the numeric error category.
686 % o reason: Specifies the reason to display before terminating the
689 % o description: Specifies any description to the reason.
692 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
693 const char *description)
695 if (fatal_error_handler != (ErrorHandler) NULL)
696 (*fatal_error_handler)(error,reason,description);
700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704 % M a g i c k W a r n i n g %
708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710 % MagickWarning() calls the warning handler methods with a warning reason.
712 % The format of the MagickWarning method is:
714 % void MagickWarning(const ExceptionType warning,const char *reason,
715 % const char *description)
717 % A description of each parameter follows:
719 % o warning: the warning severity.
721 % o reason: Define the reason for the warning.
723 % o description: Describe the warning.
726 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
727 const char *description)
729 if (warning_handler != (WarningHandler) NULL)
730 (*warning_handler)(warning,reason,description);
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
738 % S e t E r r o r H a n d l e r %
742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
744 % SetErrorHandler() sets the exception handler to the specified method
745 % and returns the previous exception handler.
747 % The format of the SetErrorHandler method is:
749 % ErrorHandler SetErrorHandler(ErrorHandler handler)
751 % A description of each parameter follows:
753 % o handler: the method to handle errors.
756 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
761 previous_handler=error_handler;
762 error_handler=handler;
763 return(previous_handler);
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
771 % S e t F a t a l E r r o r H a n d l e r %
775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777 % SetFatalErrorHandler() sets the fatal exception handler to the specified
778 % method and returns the previous fatal exception handler.
780 % The format of the SetErrorHandler method is:
782 % ErrorHandler SetErrorHandler(ErrorHandler handler)
784 % A description of each parameter follows:
786 % o handler: the method to handle errors.
789 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
794 previous_handler=fatal_error_handler;
795 fatal_error_handler=handler;
796 return(previous_handler);
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804 % S e t W a r n i n g H a n d l e r %
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810 % SetWarningHandler() sets the warning handler to the specified method
811 % and returns the previous warning handler.
813 % The format of the SetWarningHandler method is:
815 % ErrorHandler SetWarningHandler(ErrorHandler handler)
817 % A description of each parameter follows:
819 % o handler: the method to handle warnings.
822 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
827 previous_handler=warning_handler;
828 warning_handler=handler;
829 return(previous_handler);
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
837 % T h r o w E x c e p t i o n %
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
843 % ThrowException() throws an exception with the specified severity code,
844 % reason, and optional description.
846 % The format of the ThrowException method is:
848 % MagickBooleanType ThrowException(ExceptionInfo *exception,
849 % const ExceptionType severity,const char *reason,
850 % const char *description)
852 % A description of each parameter follows:
854 % o exception: the exception info.
856 % o severity: the severity of the exception.
858 % o reason: the reason for the exception.
860 % o description: the exception description.
863 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
864 const ExceptionType severity,const char *reason,const char *description)
866 register ExceptionInfo
869 assert(exception != (ExceptionInfo *) NULL);
870 assert(exception->signature == MagickSignature);
871 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
872 exception->exceptions);
873 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
874 (LocaleCompare(exception->reason,reason) == 0) &&
875 (LocaleCompare(exception->description,description) == 0))
877 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
878 if (p == (ExceptionInfo *) NULL)
879 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
880 (void) ResetMagickMemory(p,0,sizeof(*p));
881 p->severity=severity;
882 if (reason != (const char *) NULL)
883 p->reason=ConstantString(reason);
884 if (description != (const char *) NULL)
885 p->description=ConstantString(description);
886 p->signature=MagickSignature;
887 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
888 exception->severity=p->severity;
889 exception->reason=p->reason;
890 exception->description=p->description;
895 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 % T h r o w M a g i c k E x c e p t i o n %
903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905 % ThrowMagickException logs an exception as determined by the log configuration
906 % file. If an error occurs, MagickFalse is returned otherwise MagickTrue.
908 % The format of the ThrowMagickException method is:
910 % MagickBooleanType ThrowFileException(ExceptionInfo *exception,
911 % const char *module,const char *function,const unsigned long line,
912 % const ExceptionType severity,const char *tag,const char *format,...)
914 % A description of each parameter follows:
916 % o exception: the exception info.
918 % o filename: the source module filename.
920 % o function: the function name.
922 % o line: the line number of the source module.
924 % o severity: Specifies the numeric error category.
926 % o tag: the locale tag.
928 % o format: the output format.
932 MagickExport MagickBooleanType ThrowMagickExceptionList(
933 ExceptionInfo *exception,const char *module,const char *function,
934 const unsigned long line,const ExceptionType severity,const char *tag,
935 const char *format,va_list operands)
938 message[MaxTextExtent],
940 reason[MaxTextExtent];
954 assert(exception != (ExceptionInfo *) NULL);
955 assert(exception->signature == MagickSignature);
956 locale=GetLocaleExceptionMessage(severity,tag);
957 (void) CopyMagickString(reason,locale,MaxTextExtent);
958 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
959 length=strlen(reason);
960 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
961 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
963 n=vsprintf(reason+length,format,operands);
966 reason[MaxTextExtent-1]='\0';
967 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
968 GetPathComponent(module,TailPath,path);
969 (void) FormatMagickString(message,MaxTextExtent,"%s @ %s/%s/%ld",reason,path,
971 (void) ThrowException(exception,severity,message,(char *) NULL);
975 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
976 const char *module,const char *function,const unsigned long line,
977 const ExceptionType severity,const char *tag,const char *format,...)
985 va_start(operands,format);
986 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,