blob: 0e46466c8b94f96b4e7a67d2562c2164cbebb7f9 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
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 %
11% %
12% %
13% MagickCore Exception Methods %
14% %
15% Software Design %
16% John Cristy %
17% July 1993 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
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"
54
55/*
56 Forward declarations.
57*/
58#if defined(__cplusplus) || defined(c_plusplus)
59extern "C" {
60#endif
61
62static void
63 DefaultErrorHandler(const ExceptionType,const char *,const char *),
64 DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
65 DefaultWarningHandler(const ExceptionType,const char *,const char *);
66
67#if defined(__cplusplus) || defined(c_plusplus)
68}
69#endif
70
71/*
72 Global declarations.
73*/
74static ErrorHandler
75 error_handler = DefaultErrorHandler;
76
77static FatalErrorHandler
78 fatal_error_handler = DefaultFatalErrorHandler;
79
80static WarningHandler
81 warning_handler = DefaultWarningHandler;
82
83/*
84%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85% %
86% %
87% %
88% A c q u i r e E x c e p t i o n I n f o %
89% %
90% %
91% %
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93%
94% AcquireExceptionInfo() allocates the ExceptionInfo structure.
95%
96% The format of the AcquireExceptionInfo method is:
97%
98% ExceptionInfo *AcquireExceptionInfo(void)
99%
100*/
101MagickExport ExceptionInfo *AcquireExceptionInfo(void)
102{
103 ExceptionInfo
104 *exception;
105
cristy90823212009-12-12 20:48:33 +0000106 exception=(ExceptionInfo *) AcquireAlignedMemory(1,sizeof(*exception));
cristy3ed852e2009-09-05 21:47:34 +0000107 if (exception == (ExceptionInfo *) NULL)
108 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
109 GetExceptionInfo(exception);
110 exception->relinquish=MagickTrue;
111 return(exception);
112}
113
114/*
115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116% %
117% %
118% %
119% C l e a r M a g i c k E x c e p t i o n %
120% %
121% %
122% %
123%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124%
125% ClearMagickException() clears any exception that may not have been caught
126% yet.
127%
128% The format of the ClearMagickException method is:
129%
130% ClearMagickException(ExceptionInfo *exception)
131%
132% A description of each parameter follows:
133%
134% o exception: the exception info.
135%
136*/
137
138static void *DestroyExceptionElement(void *exception)
139{
140 register ExceptionInfo
141 *p;
142
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);
150}
151
152MagickExport void ClearMagickException(ExceptionInfo *exception)
153{
154 register ExceptionInfo
155 *p;
156
157 assert(exception != (ExceptionInfo *) NULL);
158 assert(exception->signature == MagickSignature);
159 if (exception->exceptions == (void *) NULL)
160 return;
cristya45da9d2009-10-25 21:29:37 +0000161 (void) LockSemaphoreInfo(exception->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000162 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
163 exception->exceptions);
164 while (p != (ExceptionInfo *) NULL)
165 {
166 (void) DestroyExceptionElement(p);
167 p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
168 exception->exceptions);
169 }
170 exception->severity=UndefinedException;
171 exception->reason=(char *) NULL;
172 exception->description=(char *) NULL;
cristya45da9d2009-10-25 21:29:37 +0000173 (void) UnlockSemaphoreInfo(exception->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000174 errno=0;
175}
176
177/*
178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179% %
180% %
181% %
182% C a t c h E x c e p t i o n %
183% %
184% %
185% %
186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187%
188% CatchException() returns if no exceptions is found otherwise it reports
189% the exception as a warning, error, or fatal depending on the severity.
190%
191% The format of the CatchException method is:
192%
193% CatchException(ExceptionInfo *exception)
194%
195% A description of each parameter follows:
196%
197% o exception: the exception info.
198%
199*/
200MagickExport void CatchException(ExceptionInfo *exception)
201{
202 register const ExceptionInfo
203 *p;
204
205 assert(exception != (ExceptionInfo *) NULL);
206 assert(exception->signature == MagickSignature);
207 if (exception->exceptions == (void *) NULL)
208 return;
cristya45da9d2009-10-25 21:29:37 +0000209 (void) LockSemaphoreInfo(exception->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000210 ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
211 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
212 exception->exceptions);
213 while (p != (const ExceptionInfo *) NULL)
214 {
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);
223 }
cristya45da9d2009-10-25 21:29:37 +0000224 (void) UnlockSemaphoreInfo(exception->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000225 ClearMagickException(exception);
226}
227
228/*
229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230% %
231% %
232% %
233+ D e f a u l t E r r o r H a n d l e r %
234% %
235% %
236% %
237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238%
239% DefaultErrorHandler() displays an error reason.
240%
241% The format of the DefaultErrorHandler method is:
242%
243% void MagickError(const ExceptionType severity,const char *reason,
244% const char *description)
245%
246% A description of each parameter follows:
247%
248% o severity: Specifies the numeric error category.
249%
250% o reason: Specifies the reason to display before terminating the
251% program.
252%
253% o description: Specifies any description to the reason.
254%
255*/
256static void DefaultErrorHandler(const ExceptionType magick_unused(severity),
257 const char *reason,const char *description)
258{
259 if (reason == (char *) NULL)
260 return;
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);
266}
267
268/*
269%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
270% %
271% %
272% %
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 %
274% %
275% %
276% %
277%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278%
279% DefaultFatalErrorHandler() displays an error reason and then terminates the
280% program.
281%
282% The format of the DefaultFatalErrorHandler method is:
283%
284% void MagickFatalError(const ExceptionType severity,const char *reason,
285% const char *description)
286%
287% A description of each parameter follows:
288%
289% o severity: Specifies the numeric error category.
290%
291% o reason: Specifies the reason to display before terminating the
292% program.
293%
294% o description: Specifies any description to the reason.
295%
296*/
297static void DefaultFatalErrorHandler(
298 const ExceptionType magick_unused(severity),
299 const char *reason,const char *description)
300{
301 if (reason == (char *) NULL)
302 return;
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();
309 exit(1);
310}
311
312/*
313%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314% %
315% %
316% %
317+ D e f a u l t W a r n i n g H a n d l e r %
318% %
319% %
320% %
321%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322%
323% DefaultWarningHandler() displays a warning reason.
324%
325% The format of the DefaultWarningHandler method is:
326%
cristy3b743bb2009-09-14 16:07:59 +0000327% void DefaultWarningHandler(const ExceptionType severity,
cristy3ed852e2009-09-05 21:47:34 +0000328% const char *reason,const char *description)
329%
330% A description of each parameter follows:
331%
cristy3b743bb2009-09-14 16:07:59 +0000332% o severity: Specifies the numeric warning category.
cristy3ed852e2009-09-05 21:47:34 +0000333%
334% o reason: Specifies the reason to display before terminating the
335% program.
336%
337% o description: Specifies any description to the reason.
338%
339*/
340static void DefaultWarningHandler(const ExceptionType magick_unused(severity),
341 const char *reason,const char *description)
342{
343 if (reason == (char *) NULL)
344 return;
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);
350}
351
352/*
353%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354% %
355% %
356% %
357% D e s t r o y E x c e p t i o n I n f o %
358% %
359% %
360% %
361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362%
363% DestroyExceptionInfo() deallocates memory associated with an exception.
364%
365% The format of the DestroyExceptionInfo method is:
366%
367% ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
368%
369% A description of each parameter follows:
370%
371% o exception: the exception info.
372%
373*/
374MagickExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
375{
376 MagickBooleanType
377 relinquish;
378
379 assert(exception != (ExceptionInfo *) NULL);
380 assert(exception->signature == MagickSignature);
cristya45da9d2009-10-25 21:29:37 +0000381 (void) LockSemaphoreInfo(exception->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000382 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);
cristya45da9d2009-10-25 21:29:37 +0000389 (void) UnlockSemaphoreInfo(exception->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000390 DestroySemaphoreInfo(&exception->semaphore);
391 if (relinquish != MagickFalse)
392 exception=(ExceptionInfo *) RelinquishMagickMemory(exception);
393 return(exception);
394}
395
396/*
397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398% %
399% %
400% %
401% G e t E x c e p t i o n I n f o %
402% %
403% %
404% %
405%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406%
407% GetExceptionInfo() initializes an exception to default values.
408%
409% The format of the GetExceptionInfo method is:
410%
411% GetExceptionInfo(ExceptionInfo *exception)
412%
413% A description of each parameter follows:
414%
415% o exception: the exception info.
416%
417*/
418MagickExport void GetExceptionInfo(ExceptionInfo *exception)
419{
420 assert(exception != (ExceptionInfo *) NULL);
421 (void) ResetMagickMemory(exception,0,sizeof(*exception));
422 exception->severity=UndefinedException;
423 exception->exceptions=(void *) NewLinkedList(0);
cristy18b17442009-10-25 18:36:48 +0000424 exception->semaphore=AllocateSemaphoreInfo();
cristy3ed852e2009-09-05 21:47:34 +0000425 exception->signature=MagickSignature;
426}
427
428/*
429%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430% %
431% %
432% %
433% G e t E x c e p t i o n M e s s a g e %
434% %
435% %
436% %
437%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
438%
439% GetExceptionMessage() returns the error message defined by the specified
440% error code.
441%
442% The format of the GetExceptionMessage method is:
443%
444% char *GetExceptionMessage(const int error)
445%
446% A description of each parameter follows:
447%
448% o error: the error code.
449%
450*/
451MagickExport char *GetExceptionMessage(const int error)
452{
453 char
454 exception[MaxTextExtent];
455
456#if defined(MAGICKCORE_HAVE_STRERROR_R)
457 (void) strerror_r(error,exception,sizeof(exception));
458#else
459 (void) CopyMagickString(exception,strerror(error),sizeof(exception));
460#endif
461 return(ConstantString(exception));
462}
463
464/*
465%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466% %
467% %
468% %
469% 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 %
470% %
471% %
472% %
473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
474%
475% GetLocaleExceptionMessage() converts a enumerated exception severity and tag
476% to a message in the current locale.
477%
478% The format of the GetLocaleExceptionMessage method is:
479%
480% const char *GetLocaleExceptionMessage(const ExceptionType severity,
481% const char *tag)
482%
483% A description of each parameter follows:
484%
485% o severity: the severity of the exception.
486%
487% o tag: the message tag.
488%
489*/
490
491static const char *ExceptionSeverityToTag(const ExceptionType severity)
492{
493 switch (severity)
494 {
495 case ResourceLimitWarning: return("Resource/Limit/Warning/");
496 case TypeWarning: return("Type/Warning/");
497 case OptionWarning: return("Option/Warning/");
498 case DelegateWarning: return("Delegate/Warning/");
499 case MissingDelegateWarning: return("Missing/Delegate/Warning/");
500 case CorruptImageWarning: return("Corrupt/Image/Warning/");
501 case FileOpenWarning: return("File/Open/Warning/");
502 case BlobWarning: return("Blob/Warning/");
503 case StreamWarning: return("Stream/Warning/");
504 case CacheWarning: return("Cache/Warning/");
505 case CoderWarning: return("Coder/Warning/");
506 case ModuleWarning: return("Module/Warning/");
507 case DrawWarning: return("Draw/Warning/");
508 case ImageWarning: return("Image/Warning/");
509 case WandWarning: return("Wand/Warning/");
510 case XServerWarning: return("XServer/Warning/");
511 case MonitorWarning: return("Monitor/Warning/");
512 case RegistryWarning: return("Registry/Warning/");
513 case ConfigureWarning: return("Configure/Warning/");
514 case PolicyWarning: return("Policy/Warning/");
515 case ResourceLimitError: return("Resource/Limit/Error/");
516 case TypeError: return("Type/Error/");
517 case OptionError: return("Option/Error/");
518 case DelegateError: return("Delegate/Error/");
519 case MissingDelegateError: return("Missing/Delegate/Error/");
520 case CorruptImageError: return("Corrupt/Image/Error/");
521 case FileOpenError: return("File/Open/Error/");
522 case BlobError: return("Blob/Error/");
523 case StreamError: return("Stream/Error/");
524 case CacheError: return("Cache/Error/");
525 case CoderError: return("Coder/Error/");
526 case ModuleError: return("Module/Error/");
527 case DrawError: return("Draw/Error/");
528 case ImageError: return("Image/Error/");
529 case WandError: return("Wand/Error/");
530 case XServerError: return("XServer/Error/");
531 case MonitorError: return("Monitor/Error/");
532 case RegistryError: return("Registry/Error/");
533 case ConfigureError: return("Configure/Error/");
534 case PolicyError: return("Policy/Error/");
535 case ResourceLimitFatalError: return("Resource/Limit/FatalError/");
536 case TypeFatalError: return("Type/FatalError/");
537 case OptionFatalError: return("Option/FatalError/");
538 case DelegateFatalError: return("Delegate/FatalError/");
539 case MissingDelegateFatalError: return("Missing/Delegate/FatalError/");
540 case CorruptImageFatalError: return("Corrupt/Image/FatalError/");
541 case FileOpenFatalError: return("File/Open/FatalError/");
542 case BlobFatalError: return("Blob/FatalError/");
543 case StreamFatalError: return("Stream/FatalError/");
544 case CacheFatalError: return("Cache/FatalError/");
545 case CoderFatalError: return("Coder/FatalError/");
546 case ModuleFatalError: return("Module/FatalError/");
547 case DrawFatalError: return("Draw/FatalError/");
548 case ImageFatalError: return("Image/FatalError/");
549 case WandFatalError: return("Wand/FatalError/");
550 case XServerFatalError: return("XServer/FatalError/");
551 case MonitorFatalError: return("Monitor/FatalError/");
552 case RegistryFatalError: return("Registry/FatalError/");
553 case ConfigureFatalError: return("Configure/FatalError/");
554 case PolicyFatalError: return("Policy/FatalError/");
555 default: break;
556 }
557 return("");
558}
559
560MagickExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
561 const char *tag)
562{
563 char
564 message[MaxTextExtent];
565
566 const char
567 *locale_message;
568
569 assert(tag != (const char *) NULL);
570 (void) FormatMagickString(message,MaxTextExtent,"Exception/%s%s",
571 ExceptionSeverityToTag(severity),tag);
572 locale_message=GetLocaleMessage(message);
573 if (locale_message == (const char *) NULL)
574 return(tag);
575 if (locale_message == message)
576 return(tag);
577 return(locale_message);
578}
579
580/*
581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582% %
583% %
584% %
585% I n h e r i t E x c e p t i o n %
586% %
587% %
588% %
589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590%
591% InheritException() inherits an exception from a related exception.
592%
593% The format of the InheritException method is:
594%
595% InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
596%
597% A description of each parameter follows:
598%
599% o exception: the exception info.
600%
601% o relative: the related exception info.
602%
603*/
604MagickExport void InheritException(ExceptionInfo *exception,
605 const ExceptionInfo *relative)
606{
607 register const ExceptionInfo
608 *p;
609
610 assert(exception != (ExceptionInfo *) NULL);
611 assert(exception->signature == MagickSignature);
612 assert(relative != (ExceptionInfo *) NULL);
613 assert(relative->signature == MagickSignature);
614 if (relative->exceptions == (void *) NULL)
615 return;
cristya45da9d2009-10-25 21:29:37 +0000616 (void) LockSemaphoreInfo(exception->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000617 ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
618 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
619 relative->exceptions);
620 while (p != (const ExceptionInfo *) NULL)
621 {
622 (void) ThrowException(exception,p->severity,p->reason,p->description);
623 p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
624 relative->exceptions);
625 }
cristya45da9d2009-10-25 21:29:37 +0000626 (void) UnlockSemaphoreInfo(exception->semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000627}
628
629/*
630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631% %
632% %
633% %
634% M a g i c k E r r o r %
635% %
636% %
637% %
638%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639%
640% MagickError() calls the exception handler methods with an error reason.
641%
642% The format of the MagickError method is:
643%
644% void MagickError(const ExceptionType error,const char *reason,
645% const char *description)
646%
647% A description of each parameter follows:
648%
649% o exception: Specifies the numeric error category.
650%
651% o reason: Specifies the reason to display before terminating the
652% program.
653%
654% o description: Specifies any description to the reason.
655%
656*/
657MagickExport void MagickError(const ExceptionType error,const char *reason,
658 const char *description)
659{
660 if (error_handler != (ErrorHandler) NULL)
661 (*error_handler)(error,reason,description);
662}
663
664/*
665%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
666% %
667% %
668% %
669% M a g i c k F a t al E r r o r %
670% %
671% %
672% %
673%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674%
675% MagickFatalError() calls the fatal exception handler methods with an error
676% reason.
677%
678% The format of the MagickError method is:
679%
680% void MagickFatalError(const ExceptionType error,const char *reason,
681% const char *description)
682%
683% A description of each parameter follows:
684%
685% o exception: Specifies the numeric error category.
686%
687% o reason: Specifies the reason to display before terminating the
688% program.
689%
690% o description: Specifies any description to the reason.
691%
692*/
693MagickExport void MagickFatalError(const ExceptionType error,const char *reason,
694 const char *description)
695{
696 if (fatal_error_handler != (ErrorHandler) NULL)
697 (*fatal_error_handler)(error,reason,description);
698}
699
700/*
701%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
702% %
703% %
704% %
705% M a g i c k W a r n i n g %
706% %
707% %
708% %
709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710%
711% MagickWarning() calls the warning handler methods with a warning reason.
712%
713% The format of the MagickWarning method is:
714%
715% void MagickWarning(const ExceptionType warning,const char *reason,
716% const char *description)
717%
718% A description of each parameter follows:
719%
720% o warning: the warning severity.
721%
722% o reason: Define the reason for the warning.
723%
724% o description: Describe the warning.
725%
726*/
727MagickExport void MagickWarning(const ExceptionType warning,const char *reason,
728 const char *description)
729{
730 if (warning_handler != (WarningHandler) NULL)
731 (*warning_handler)(warning,reason,description);
732}
733
734/*
735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736% %
737% %
738% %
739% S e t E r r o r H a n d l e r %
740% %
741% %
742% %
743%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
744%
745% SetErrorHandler() sets the exception handler to the specified method
746% and returns the previous exception handler.
747%
748% The format of the SetErrorHandler method is:
749%
750% ErrorHandler SetErrorHandler(ErrorHandler handler)
751%
752% A description of each parameter follows:
753%
754% o handler: the method to handle errors.
755%
756*/
757MagickExport ErrorHandler SetErrorHandler(ErrorHandler handler)
758{
759 ErrorHandler
760 previous_handler;
761
762 previous_handler=error_handler;
763 error_handler=handler;
764 return(previous_handler);
765}
766
767/*
768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769% %
770% %
771% %
772% S e t F a t a l E r r o r H a n d l e r %
773% %
774% %
775% %
776%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777%
778% SetFatalErrorHandler() sets the fatal exception handler to the specified
779% method and returns the previous fatal exception handler.
780%
781% The format of the SetErrorHandler method is:
782%
783% ErrorHandler SetErrorHandler(ErrorHandler handler)
784%
785% A description of each parameter follows:
786%
787% o handler: the method to handle errors.
788%
789*/
790MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
791{
792 FatalErrorHandler
793 previous_handler;
794
795 previous_handler=fatal_error_handler;
796 fatal_error_handler=handler;
797 return(previous_handler);
798}
799
800/*
801%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802% %
803% %
804% %
805% S e t W a r n i n g H a n d l e r %
806% %
807% %
808% %
809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810%
811% SetWarningHandler() sets the warning handler to the specified method
812% and returns the previous warning handler.
813%
814% The format of the SetWarningHandler method is:
815%
816% ErrorHandler SetWarningHandler(ErrorHandler handler)
817%
818% A description of each parameter follows:
819%
820% o handler: the method to handle warnings.
821%
822*/
823MagickExport WarningHandler SetWarningHandler(WarningHandler handler)
824{
825 WarningHandler
826 previous_handler;
827
828 previous_handler=warning_handler;
829 warning_handler=handler;
830 return(previous_handler);
831}
832
833/*
834%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835% %
836% %
837% %
838% T h r o w E x c e p t i o n %
839% %
840% %
841% %
842%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
843%
844% ThrowException() throws an exception with the specified severity code,
845% reason, and optional description.
846%
847% The format of the ThrowException method is:
848%
849% MagickBooleanType ThrowException(ExceptionInfo *exception,
850% const ExceptionType severity,const char *reason,
851% const char *description)
852%
853% A description of each parameter follows:
854%
855% o exception: the exception info.
856%
857% o severity: the severity of the exception.
858%
859% o reason: the reason for the exception.
860%
861% o description: the exception description.
862%
863*/
864MagickExport MagickBooleanType ThrowException(ExceptionInfo *exception,
865 const ExceptionType severity,const char *reason,const char *description)
866{
867 register ExceptionInfo
868 *p;
869
870 assert(exception != (ExceptionInfo *) NULL);
871 assert(exception->signature == MagickSignature);
872 p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
873 exception->exceptions);
874 if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
875 (LocaleCompare(exception->reason,reason) == 0) &&
876 (LocaleCompare(exception->description,description) == 0))
877 return(MagickTrue);
cristy90823212009-12-12 20:48:33 +0000878 p=(ExceptionInfo *) AcquireAlignedMemory(1,sizeof(*p));
cristy3ed852e2009-09-05 21:47:34 +0000879 if (p == (ExceptionInfo *) NULL)
880 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
881 (void) ResetMagickMemory(p,0,sizeof(*p));
882 p->severity=severity;
883 if (reason != (const char *) NULL)
884 p->reason=ConstantString(reason);
885 if (description != (const char *) NULL)
886 p->description=ConstantString(description);
887 p->signature=MagickSignature;
888 (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
889 exception->severity=p->severity;
890 exception->reason=p->reason;
891 exception->description=p->description;
892 return(MagickTrue);
893}
894
895/*
896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897% %
898% %
899% %
900% T h r o w M a g i c k E x c e p t i o n %
901% %
902% %
903% %
904%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905%
906% ThrowMagickException logs an exception as determined by the log configuration
907% file. If an error occurs, MagickFalse is returned otherwise MagickTrue.
908%
909% The format of the ThrowMagickException method is:
910%
911% MagickBooleanType ThrowFileException(ExceptionInfo *exception,
912% const char *module,const char *function,const unsigned long line,
913% const ExceptionType severity,const char *tag,const char *format,...)
914%
915% A description of each parameter follows:
916%
917% o exception: the exception info.
918%
919% o filename: the source module filename.
920%
921% o function: the function name.
922%
923% o line: the line number of the source module.
924%
925% o severity: Specifies the numeric error category.
926%
927% o tag: the locale tag.
928%
929% o format: the output format.
930%
931*/
932
933MagickExport MagickBooleanType ThrowMagickExceptionList(
934 ExceptionInfo *exception,const char *module,const char *function,
935 const unsigned long line,const ExceptionType severity,const char *tag,
936 const char *format,va_list operands)
937{
938 char
939 message[MaxTextExtent],
940 path[MaxTextExtent],
941 reason[MaxTextExtent];
942
943 const char
944 *locale;
945
946 int
947 n;
948
949 MagickBooleanType
950 status;
951
952 size_t
953 length;
954
955 assert(exception != (ExceptionInfo *) NULL);
956 assert(exception->signature == MagickSignature);
957 locale=GetLocaleExceptionMessage(severity,tag);
958 (void) CopyMagickString(reason,locale,MaxTextExtent);
959 (void) ConcatenateMagickString(reason," ",MaxTextExtent);
960 length=strlen(reason);
961#if defined(MAGICKCORE_HAVE_VSNPRINTF)
962 n=vsnprintf(reason+length,MaxTextExtent-length,format,operands);
963#else
964 n=vsprintf(reason+length,format,operands);
965#endif
966 if (n < 0)
967 reason[MaxTextExtent-1]='\0';
968 status=LogMagickEvent(ExceptionEvent,module,function,line,"%s",reason);
969 GetPathComponent(module,TailPath,path);
970 (void) FormatMagickString(message,MaxTextExtent,"%s @ %s/%s/%ld",reason,path,
971 function,line);
972 (void) ThrowException(exception,severity,message,(char *) NULL);
973 return(status);
974}
975
976MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception,
977 const char *module,const char *function,const unsigned long line,
978 const ExceptionType severity,const char *tag,const char *format,...)
979{
980 MagickBooleanType
981 status;
982
983 va_list
984 operands;
985
986 va_start(operands,format);
987 status=ThrowMagickExceptionList(exception,module,function,line,severity,tag,
988 format,operands);
989 va_end(operands);
990 return(status);
991}