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 (void) strerror_r(error,exception,sizeof(exception));
463 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
465 return(ConstantString(exception));
469 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 % 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 %
477 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479 % GetLocaleExceptionMessage() converts a enumerated exception severity and tag
480 % to a message in the current locale.
482 % The format of the GetLocaleExceptionMessage method is:
484 % const char *GetLocaleExceptionMessage(const ExceptionType severity,
487 % A description of each parameter follows:
489 % o severity: the severity of the exception.
491 % o tag: the message tag.
495 static const char *ExceptionSeverityToTag(const ExceptionType severity)
499 case ResourceLimitWarning: return("Resource/Limit/Warning/");
500 case TypeWarning: return("Type/Warning/");
501 case OptionWarning: return("Option/Warning/");
502 case DelegateWarning: return("Delegate/Warning/");
503 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
504 case CorruptImageWarning: return("Corrupt/Image/Warning/");
505 case FileOpenWarning: return("File/Open/Warning/");
506 case BlobWarning: return("Blob/Warning/");
507 case StreamWarning: return("Stream/Warning/");
508 case CacheWarning: return("Cache/Warning/");
509 case CoderWarning: return("Coder/Warning/");
510 case FilterWarning: return("Filter/Warning/");
511 case ModuleWarning: return("Module/Warning/");
512 case DrawWarning: return("Draw/Warning/");
513 case ImageWarning: return("Image/Warning/");
514 case WandWarning: return("Wand/Warning/");
515 case XServerWarning: return("XServer/Warning/");
516 case MonitorWarning: return("Monitor/Warning/");
517 case RegistryWarning: return("Registry/Warning/");
518 case ConfigureWarning: return("Configure/Warning/");
519 case PolicyWarning: return("Policy/Warning/");
520 case ResourceLimitError: return("Resource/Limit/Error/");
521 case TypeError: return("Type/Error/");
522 case OptionError: return("Option/Error/");
523 case DelegateError: return("Delegate/Error/");
524 case MissingDelegateError: return("Missing/Delegate/Error/");
525 case CorruptImageError: return("Corrupt/Image/Error/");
526 case FileOpenError: return("File/Open/Error/");
527 case BlobError: return("Blob/Error/");
528 case StreamError: return("Stream/Error/");
529 case CacheError: return("Cache/Error/");
530 case CoderError: return("Coder/Error/");
531 case FilterError: return("Filter/Error/");
532 case ModuleError: return("Module/Error/");
533 case DrawError: return("Draw/Error/");
534 case ImageError: return("Image/Error/");
535 case WandError: return("Wand/Error/");
536 case XServerError: return("XServer/Error/");
537 case MonitorError: return("Monitor/Error/");
538 case RegistryError: return("Registry/Error/");
539 case ConfigureError: return("Configure/Error/");
540 case PolicyError: return("Policy/Error/");
541 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
542 case TypeFatalError: return("Type/FatalError/");
543 case OptionFatalError: return("Option/FatalError/");
544 case DelegateFatalError: return("Delegate/FatalError/");
545 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
546 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
547 case FileOpenFatalError: return("File/Open/FatalError/");
548 case BlobFatalError: return("Blob/FatalError/");
549 case StreamFatalError: return("Stream/FatalError/");
550 case CacheFatalError: return("Cache/FatalError/");
551 case CoderFatalError: return("Coder/FatalError/");
552 case FilterFatalError: return("Filter/FatalError/");
553 case ModuleFatalError: return("Module/FatalError/");
554 case DrawFatalError: return("Draw/FatalError/");
555 case ImageFatalError: return("Image/FatalError/");
556 case WandFatalError: return("Wand/FatalError/");
557 case XServerFatalError: return("XServer/FatalError/");
558 case MonitorFatalError: return("Monitor/FatalError/");
559 case RegistryFatalError: return("Registry/FatalError/");
560 case ConfigureFatalError: return("Configure/FatalError/");
561 case PolicyFatalError: return("Policy/FatalError/");
567 MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
571 message[MaxTextExtent];
576 assert(tag != (const char *) NULL);
577 (void) FormatLocaleString(message,MaxTextExtent,"Exception/%s%s",
578 ExceptionSeverityToTag(severity),tag);
579 locale_message=GetLocaleMessage(message);
580 if (locale_message == (const char *) NULL)
582 if (locale_message == message)
584 return(locale_message);
588 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
592 % I n h e r i t E x c e p t i o n %
596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
598 % InheritException() inherits an exception from a related exception.
600 % The format of the InheritException method is:
602 % InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
604 % A description of each parameter follows:
606 % o exception: the exception info.
608 % o relative: the related exception info.
611 MagickExport void InheritException(ExceptionInfo *exception,
612 const ExceptionInfo *relative)
614 register const ExceptionInfo
617 assert(exception != (ExceptionInfo *) NULL);
618 assert(exception->signature == MagickSignature);
619 assert(relative != (ExceptionInfo *) NULL);
620 assert(relative->signature == MagickSignature);
621 if (relative->exceptions == (void *) NULL)
623 LockSemaphoreInfo(exception->semaphore);
624 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
625 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
626 relative->exceptions);
627 while (p != (const ExceptionInfo *) NULL)
629 (void) ThrowException(exception,p->severity,p->reason,p->description);
630 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
631 relative->exceptions);
633 UnlockSemaphoreInfo(exception->semaphore);
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 % M a g i c k E r r o r %
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
647 % MagickError() calls the exception handler methods with an error reason.
649 % The format of the MagickError method is:
651 % void MagickError(const ExceptionType error,const char *reason,
652 % const char *description)
654 % A description of each parameter follows:
656 % o exception: Specifies the numeric error category.
658 % o reason: Specifies the reason to display before terminating the
661 % o description: Specifies any description to the reason.
664 MagickExport void MagickError(const ExceptionType error,const char *reason,
665 const char *description)
667 if (error_handler != (ErrorHandler) NULL)
668 (*error_handler)(error,reason,description);
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676 % M a g i c k F a t al E r r o r %
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 % MagickFatalError() calls the fatal exception handler methods with an error
685 % The format of the MagickError method is:
687 % void MagickFatalError(const ExceptionType error,const char *reason,
688 % const char *description)
690 % A description of each parameter follows:
692 % o exception: Specifies the numeric error category.
694 % o reason: Specifies the reason to display before terminating the
697 % o description: Specifies any description to the reason.
700 MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
701 const char *description)
703 if (fatal_error_handler != (ErrorHandler) NULL)
704 (*fatal_error_handler)(error,reason,description);
708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712 % M a g i c k W a r n i n g %
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718 % MagickWarning() calls the warning handler methods with a warning reason.
720 % The format of the MagickWarning method is:
722 % void MagickWarning(const ExceptionType warning,const char *reason,
723 % const char *description)
725 % A description of each parameter follows:
727 % o warning: the warning severity.
729 % o reason: Define the reason for the warning.
731 % o description: Describe the warning.
734 MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
735 const char *description)
737 if (warning_handler != (WarningHandler) NULL)
738 (*warning_handler)(warning,reason,description);
742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
746 % S e t E r r o r H a n d l e r %
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752 % SetErrorHandler() sets the exception handler to the specified method
753 % and returns the previous exception handler.
755 % The format of the SetErrorHandler method is:
757 % ErrorHandler SetErrorHandler(ErrorHandler handler)
759 % A description of each parameter follows:
761 % o handler: the method to handle errors.
764 MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
769 previous_handler=error_handler;
770 error_handler=handler;
771 return(previous_handler);
775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779 % S e t F a t a l E r r o r H a n d l e r %
783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 % SetFatalErrorHandler() sets the fatal exception handler to the specified
786 % method and returns the previous fatal exception handler.
788 % The format of the SetErrorHandler method is:
790 % ErrorHandler SetErrorHandler(ErrorHandler handler)
792 % A description of each parameter follows:
794 % o handler: the method to handle errors.
797 MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
802 previous_handler=fatal_error_handler;
803 fatal_error_handler=handler;
804 return(previous_handler);
808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 % S e t W a r n i n g H a n d l e r %
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
818 % SetWarningHandler() sets the warning handler to the specified method
819 % and returns the previous warning handler.
821 % The format of the SetWarningHandler method is:
823 % ErrorHandler SetWarningHandler(ErrorHandler handler)
825 % A description of each parameter follows:
827 % o handler: the method to handle warnings.
830 MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
835 previous_handler=warning_handler;
836 warning_handler=handler;
837 return(previous_handler);
841 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
845 % T h r o w E x c e p t i o n %
849 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
851 % ThrowException() throws an exception with the specified severity code,
852 % reason, and optional description.
854 % The format of the ThrowException method is:
856 % MagickBooleanType ThrowException(ExceptionInfo *exception,
857 % const ExceptionType severity,const char *reason,
858 % const char *description)
860 % A description of each parameter follows:
862 % o exception: the exception info.
864 % o severity: the severity of the exception.
866 % o reason: the reason for the exception.
868 % o description: the exception description.
871 MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
872 const ExceptionType severity,const char *reason,const char *description)
874 register ExceptionInfo
877 assert(exception != (ExceptionInfo *) NULL);
878 assert(exception->signature == MagickSignature);
879 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
880 exception->exceptions);
881 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
882 (LocaleCompare(exception->reason,reason) == 0) &&
883 (LocaleCompare(exception->description,description) == 0))
885 p=(ExceptionInfo *) AcquireMagickMemory(sizeof(*p));
886 if (p == (ExceptionInfo *) NULL)
887 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
888 (void) ResetMagickMemory(p,0,sizeof(*p));
889 p->severity=severity;
890 if (reason != (const char *) NULL)
891 p->reason=ConstantString(reason);
892 if (description != (const char *) NULL)
893 p->description=ConstantString(description);
894 p->signature=MagickSignature;
895 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
896 exception->severity=p->severity;
897 exception->reason=p->reason;
898 exception->description=p->description;
903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
907 % T h r o w M a g i c k E x c e p t i o n %
911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913 % ThrowMagickException logs an exception as determined by the log configuration
914 % file. If an error occurs, MagickFalse is returned otherwise MagickTrue.
916 % The format of the ThrowMagickException method is:
918 % MagickBooleanType ThrowFileException(ExceptionInfo *exception,
919 % const char *module,const char *function,const size_t line,
920 % const ExceptionType severity,const char *tag,const char *format,...)
922 % A description of each parameter follows:
924 % o exception: the exception info.
926 % o filename: the source module filename.
928 % o function: the function name.
930 % o line: the line number of the source module.
932 % o severity: Specifies the numeric error category.
934 % o tag: the locale tag.
936 % o format: the output format.
940 static MagickBooleanType ThrowMagickExceptionList(ExceptionInfo *exception,
941 const char *module,const char *function,const size_t line,
942 const ExceptionType severity,const char *tag,const char *format,
946 message[MaxTextExtent],
948 reason[MaxTextExtent];
963 assert(exception != (ExceptionInfo *) NULL);
964 assert(exception->signature == MagickSignature);
965 locale=GetLocaleExceptionMessage(severity,tag);
966 (void) CopyMagickString(reason,locale,MaxTextExtent);
967 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
968 length=strlen(reason);
969 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
970 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
972 n=vsprintf(reason+length,format,operands);
975 reason[MaxTextExtent-1]='\0';
976 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
977 GetPathComponent(module,TailPath,path);
979 if ((severity >= WarningException) && (severity < ErrorException))
981 if ((severity >= ErrorException) && (severity < FatalErrorException))
983 if (severity >= FatalErrorException)
985 (void) FormatLocaleString(message,MaxTextExtent,"%s @ %s/%s/%s/%.20g",reason,
986 type,path,function,(double) line);
987 (void) ThrowException(exception,severity,message,(char *) NULL);
991 MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
992 const char *module,const char *function,const size_t line,
993 const ExceptionType severity,const char *tag,const char *format,...)
1001 va_start(operands,format);
1002 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,