root/WizardsToolkit/trunk/wizard/exception.c

Revision 459, 39.2 KB (checked in by cristy, 4 weeks ago)
Line 
1/*
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%                    Wizards Toolkit Exception Methods                        %
14%                                                                             %
15%                             Software Design                                 %
16%                               John Cristy                                   %
17%                                July 1993                                    %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
21%  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.wizards-toolkit.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 "wizard/studio.h"
44#include "wizard/client.h"
45#include "wizard/exception.h"
46#include "wizard/exception-private.h"
47#include "wizard/hashmap.h"
48#include "wizard/log.h"
49#include "wizard/memory_.h"
50#include "wizard/string_.h"
51#include "wizard/utility.h"
52#include "wizard/wizard.h"
53
54/*
55  Typedef declarations.
56*/
57struct _ExceptionInfo
58{
59  ExceptionType
60    severity;
61
62  char
63    *reason,
64    *description;
65
66  void
67    *exceptions;
68
69  WizardBooleanType
70    relinquish;
71
72  SemaphoreInfo
73    *semaphore;
74
75  unsigned long
76    signature;
77};
78
79/*
80  Forward declarations.
81*/
82#if defined(__cplusplus) || defined(c_plusplus)
83extern "C" {
84#endif
85
86static void
87  DefaultErrorHandler(const ExceptionType,const char *,const char *),
88  DefaultFatalErrorHandler(const ExceptionType,const char *,const char *),
89  DefaultWarningHandler(const ExceptionType,const char *,const char *);
90
91#if defined(__cplusplus) || defined(c_plusplus)
92}
93#endif
94
95/*
96  Global declarations.
97*/
98static ErrorHandler
99  error_handler = DefaultErrorHandler;
100
101static FatalErrorHandler
102  fatal_error_handler = DefaultFatalErrorHandler;
103
104static WarningHandler
105  warning_handler = DefaultWarningHandler;
106
107/*
108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109%                                                                             %
110%                                                                             %
111%                                                                             %
112%   A c q u i r e E x c e p t i o n I n f o                                   %
113%                                                                             %
114%                                                                             %
115%                                                                             %
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%
118%  AcquireExceptionInfo() allocates the ExceptionInfo structure.
119%
120%  The format of the AcquireExceptionInfo method is:
121%
122%      ExceptionInfo *AcquireExceptionInfo(void)
123%
124*/
125WizardExport ExceptionInfo *AcquireExceptionInfo(void)
126{
127  ExceptionInfo
128    *exception;
129
130  exception=(ExceptionInfo *) AcquireWizardMemory(sizeof(*exception));
131  if (exception == (ExceptionInfo *) NULL)
132    ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
133  (void) ResetWizardMemory(exception,0,sizeof(*exception));
134  GetExceptionInfo(exception);
135  exception->relinquish=WizardTrue;
136  exception->signature=WizardSignature;
137  return(exception);
138}
139
140/*
141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142%                                                                             %
143%                                                                             %
144%                                                                             %
145%   C l e a r W i z a r d E x c e p t i o n                                   %
146%                                                                             %
147%                                                                             %
148%                                                                             %
149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150%
151%  ClearWizardException() clears any exception that may not have been caught
152%  yet.
153%
154%  The format of the ClearWizardException method is:
155%
156%      ClearWizardException(ExceptionInfo *exception)
157%
158%  A description of each parameter follows:
159%
160%    o exception: The exception info.
161%
162*/
163
164static void *DestroyExceptionElement(void *exception)
165{
166  register ExceptionInfo
167    *p;
168
169  p=(ExceptionInfo *) exception;
170  if (p->reason != (char *) NULL)
171    p->reason=(char *) RelinquishWizardMemory(p->reason);
172  if (p->description != (char *) NULL)
173    p->description=(char *) RelinquishWizardMemory(p->description);
174  p=(ExceptionInfo *) RelinquishWizardMemory(p);
175  return((void *) NULL);
176}
177
178WizardExport void ClearWizardException(ExceptionInfo *exception)
179{
180  register ExceptionInfo
181    *p;
182
183  assert(exception != (ExceptionInfo *) NULL);
184  assert(exception->signature == WizardSignature);
185  if (exception->exceptions == (void *) NULL)
186    return;
187  (void) LockSemaphoreInfo(exception->semaphore);
188  p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
189    exception->exceptions);
190  while (p != (ExceptionInfo *) NULL)
191  {
192    p=(ExceptionInfo *) DestroyExceptionElement(p);
193    p=(ExceptionInfo *) RemoveLastElementFromLinkedList((LinkedListInfo *)
194      exception->exceptions);
195  }
196  exception->severity=UndefinedException;
197  exception->reason=(char *) NULL;
198  exception->description=(char *) NULL;
199  (void) UnlockSemaphoreInfo(exception->semaphore);
200  errno=0;
201}
202
203/*
204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205%                                                                             %
206%                                                                             %
207%                                                                             %
208%   C a t c h E x c e p t i o n                                               %
209%                                                                             %
210%                                                                             %
211%                                                                             %
212%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213%
214%  CatchException() returns if no exceptions is found otherwise it reports
215%  the exception as a warning, error, or fatal depending on the severity.
216%
217%  The format of the CatchException method is:
218%
219%      CatchException(ExceptionInfo *exception)
220%
221%  A description of each parameter follows:
222%
223%    o exception: The exception info.
224%
225*/
226WizardExport void CatchException(ExceptionInfo *exception)
227{
228  register const ExceptionInfo
229    *p;
230
231  assert(exception != (ExceptionInfo *) NULL);
232  assert(exception->signature == WizardSignature);
233  if (exception->exceptions == (void *) NULL)
234    return;
235  (void) LockSemaphoreInfo(exception->semaphore);
236  ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
237  p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
238    exception->exceptions);
239  while (p != (const ExceptionInfo *) NULL)
240  {
241    if ((p->severity >= WarningException) && (p->severity < ErrorException))
242      WizardWarning(p->severity,p->reason,p->description);
243    if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
244      WizardError(p->severity,p->reason,p->description);
245    if (exception->severity >= FatalErrorException)
246      WizardFatalError(p->severity,p->reason,p->description);
247    p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
248      exception->exceptions);
249  }
250  (void) UnlockSemaphoreInfo(exception->semaphore);
251  ClearWizardException(exception);
252}
253
254/*
255%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256%                                                                             %
257%                                                                             %
258%                                                                             %
259+   D e f a u l t E r r o r H a n d l e r                                     %
260%                                                                             %
261%                                                                             %
262%                                                                             %
263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264%
265%  DefaultErrorHandler() displays an error reason.
266%
267%  The format of the DefaultErrorHandler method is:
268%
269%      void WizardError(const ExceptionType severity,const char *reason,
270%        const char *description)
271%
272%  A description of each parameter follows:
273%
274%    o severity: Specifies the numeric error category.
275%
276%    o reason: Specifies the reason to display before terminating the
277%      program.
278%
279%    o description: Specifies any description to the reason.
280%
281*/
282static void DefaultErrorHandler(const ExceptionType wizard_unused(severity),
283  const char *reason,const char *description)
284{
285  if (reason == (char *) NULL)
286    return;
287  (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
288  if (description != (char *) NULL)
289    (void) fprintf(stderr," (%s)",description);
290  (void) fprintf(stderr,".\n");
291  (void) fflush(stderr);
292}
293
294/*
295%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296%                                                                             %
297%                                                                             %
298%                                                                             %
299+   D e f a u l t F a t a l E r r o r H a n d l e r                           %
300%                                                                             %
301%                                                                             %
302%                                                                             %
303%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304%
305%  DefaultFatalErrorHandler() displays an error reason and then terminates the
306%  program.
307%
308%  The format of the DefaultFatalErrorHandler method is:
309%
310%      void WizardFatalError(const ExceptionType severity,const char *reason,
311%        const char *description)
312%
313%  A description of each parameter follows:
314%
315%    o severity: Specifies the numeric error category.
316%
317%    o reason: Specifies the reason to display before terminating the
318%      program.
319%
320%    o description: Specifies any description to the reason.
321%
322*/
323static void DefaultFatalErrorHandler(const ExceptionType severity,
324  const char *reason,const char *description)
325{
326  if (reason == (char *) NULL)
327    return;
328  (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
329  if (description != (char *) NULL)
330    (void) fprintf(stderr," (%s)",description);
331  (void) fprintf(stderr,".\n");
332  (void) fflush(stderr);
333  WizardsToolkitTerminus();
334  exit(1);
335}
336
337/*
338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339%                                                                             %
340%                                                                             %
341%                                                                             %
342+   D e f a u l t W a r n i n g H a n d l e r                                 %
343%                                                                             %
344%                                                                             %
345%                                                                             %
346%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
347%
348%  DefaultWarningHandler() displays a warning reason.
349%
350%  The format of the DefaultWarningHandler method is:
351%
352%      void DefaultWarningHandler(const ExceptionType warning,
353%        const char *reason,const char *description)
354%
355%  A description of each parameter follows:
356%
357%    o warning: Specifies the numeric warning category.
358%
359%    o reason: Specifies the reason to display before terminating the
360%      program.
361%
362%    o description: Specifies any description to the reason.
363%
364*/
365static void DefaultWarningHandler(const ExceptionType wizard_unused(severity),
366  const char *reason,const char *description)
367{
368  if (reason == (char *) NULL)
369    return;
370  (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
371  if (description != (char *) NULL)
372    (void) fprintf(stderr," (%s)",description);
373  (void) fprintf(stderr,".\n");
374  (void) fflush(stderr);
375}
376
377/*
378%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379%                                                                             %
380%                                                                             %
381%                                                                             %
382%   D e s t r o y E x c e p t i o n I n f o                                   %
383%                                                                             %
384%                                                                             %
385%                                                                             %
386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
387%
388%  DestroyExceptionInfo() deallocates memory associated with an exception.
389%
390%  The format of the DestroyExceptionInfo method is:
391%
392%      ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
393%
394%  A description of each parameter follows:
395%
396%    o exception: The exception info.
397%
398*/
399WizardExport ExceptionInfo *DestroyExceptionInfo(ExceptionInfo *exception)
400{
401  assert(exception != (ExceptionInfo *) NULL);
402  assert(exception->signature == WizardSignature);
403  (void) LockSemaphoreInfo(exception->semaphore);
404  exception->severity=UndefinedException;
405  if (exception->exceptions != (void *) NULL)
406    exception->exceptions=(void *) DestroyLinkedList((LinkedListInfo *)
407      exception->exceptions,DestroyExceptionElement);
408  exception->signature=(~WizardSignature);
409  (void) UnlockSemaphoreInfo(exception->semaphore);
410  DestroySemaphoreInfo(&exception->semaphore);
411  if (exception->relinquish != WizardFalse)
412    exception=(ExceptionInfo *) RelinquishWizardMemory(exception);
413  return(exception);
414}
415
416/*
417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418%                                                                             %
419%                                                                             %
420%                                                                             %
421%   G e t E x c e p t i o n I n f o                                           %
422%                                                                             %
423%                                                                             %
424%                                                                             %
425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426%
427%  GetExceptionInfo() initializes an exception to default values.
428%
429%  The format of the GetExceptionInfo method is:
430%
431%      GetExceptionInfo(ExceptionInfo *exception)
432%
433%  A description of each parameter follows:
434%
435%    o exception: The exception info.
436%
437*/
438WizardExport void GetExceptionInfo(ExceptionInfo *exception)
439{
440  assert(exception != (ExceptionInfo *) NULL);
441  (void) ResetWizardMemory(exception,0,sizeof(*exception));
442  exception->severity=UndefinedException;
443  exception->exceptions=(void *) NewLinkedList(0);
444  exception->semaphore=AllocateSemaphoreInfo();
445  exception->signature=WizardSignature;
446}
447
448/*
449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450%                                                                             %
451%                                                                             %
452%                                                                             %
453%   G e t E x c e p t i o n M e s s a g e                                     %
454%                                                                             %
455%                                                                             %
456%                                                                             %
457%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
458%
459%  GetExceptionMessage() returns the error message defined by the specified
460%  error code.
461%
462%  The format of the GetExceptionMessage method is:
463%
464%      char *GetExceptionMessage(const int error)
465%
466%  A description of each parameter follows:
467%
468%    o error: the error code.
469%
470*/
471WizardExport char *GetExceptionMessage(const int error)
472{
473 char
474   exception[MaxTextExtent];
475
476#if defined(WIZARDSTOOLKIT_HAVE_STRERROR_R)
477  (void) strerror_r(error,exception,sizeof(exception));
478#else
479  (void) CopyWizardString(exception,strerror(error),sizeof(exception));
480#endif
481  return(ConstantString(exception));
482}
483
484/*
485%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486%                                                                             %
487%                                                                             %
488%                                                                             %
489%   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                         %
490%                                                                             %
491%                                                                             %
492%                                                                             %
493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494%
495%  GetLocaleExceptionMessage() converts a enumerated exception severity and tag
496%  to a message in the current locale.
497%
498%  The format of the GetLocaleExceptionMessage method is:
499%
500%      const char *GetLocaleExceptionMessage(const ExceptionType severity,
501%        const char *tag)
502%
503%  A description of each parameter follows:
504%
505%    o severity: the severity of the exception.
506%
507%    o tag: the message tag.
508%
509*/
510
511static const char *ExceptionSeverityToTag(const ExceptionType severity)
512{
513  switch (severity)
514  {
515    case OptionWarning: return("Option/Warning/");
516    case RandomWarning: return("Random/Warning/");
517    case HashWarning: return("Hash/Warning/");
518    case MACWarning: return("MAC/Warning/");
519    case EntropyWarning: return("Entropy/Warning/");
520    case ConfigureWarning: return("Configure/Warning/");
521    case CipherWarning: return("Cipher/Warning/");
522    case KeymapWarning: return("Keymap/Warning/");
523    case AuthenticateWarning: return("Authenticate/Warning/");
524    case KeyringWarning: return("Keyring/Warning/");
525    case ParseWarning: return("Parse/Warning/");
526    case UserWarning: return("User/Warning/");
527    case SplayTreeWarning: return("SplayTree/Warning/");
528    case HashmapWarning: return("Hashmap/Warning/");
529    case LogWarning: return("Log/Warning/");
530    case StringWarning: return("String/Warning/");
531    case FileWarning: return("File/Warning/");
532    case BlobWarning: return("Blob/Warning/");
533    case ResourceWarning: return("ResourceLimit/Warning/");
534    case OptionError: return("Option/Error/");
535    case RandomError: return("Random/Error/");
536    case HashError: return("Hash/Error/");
537    case MACError: return("MAC/Error/");
538    case EntropyError: return("Entropy/Error/");
539    case ConfigureError: return("Configure/Error/");
540    case CipherError: return("Cipher/Error/");
541    case KeymapError: return("Keymap/Error/");
542    case AuthenticateError: return("Authenticate/Error/");
543    case KeyringError: return("Keyring/Error/");
544    case ParseError: return("Parse/Error/");
545    case UserError: return("User/Error/");
546    case SplayTreeError: return("SplayTree/Error/");
547    case StringError: return("String/Error/");
548    case FileError: return("File/Error/");
549    case BlobError: return("Blob/Error/");
550    case ResourceError: return("ResourceLimit/Error/");
551    case OptionFatalError: return("Option/FatalError/");
552    case RandomFatalError: return("Random/FatalError/");
553    case HashFatalError: return("Hash/FatalError/");
554    case MACFatalError: return("MAC/FatalError/");
555    case EntropyFatalError: return("Entropy/FatalError/");
556    case ConfigureFatalError: return("Configure/FatalError/");
557    case CipherFatalError: return("Cipher/FatalError/");
558    case KeymapFatalError: return("Keymap/FatalError/");
559    case AuthenticateFatalError: return("Authenticate/FatalError/");
560    case KeyringFatalError: return("Keyring/FatalError/");
561    case ParseFatalError: return("Parse/FatalError/");
562    case UserFatalError: return("User/FatalError/");
563    case SplayTreeFatalError: return("SplayTree/FatalError/");
564    case HashmapFatalError: return("Hashmap/FatalError/");
565    case LogFatalError: return("Log/FatalError/");
566    case StringFatalError: return("String/FatalError/");
567    case FileFatalError: return("File/FatalError/");
568    case BlobFatalError: return("Blob/FatalError/");
569    case ResourceFatalError: return("ResourceLimit/FatalError/");
570    default: break;
571  }
572  return("");
573}
574
575WizardExport const char *GetLocaleExceptionMessage(const ExceptionType severity,
576  const char *tag)
577{
578#if defined(WIZARDSTOOLKIT_LOCALE)
579  char
580    message[MaxTextExtent];
581
582  const char
583    *locale_message;
584
585  assert(tag != (const char *) NULL);
586  (void) FormatWizardString(message,MaxTextExtent,"Exception/%s%s",
587    ExceptionSeverityToTag(severity),tag);
588  locale_message=GetLocaleMessage(message);
589  if (locale_message == (const char *) NULL)
590    return(tag);
591  if (locale_message == message)
592    return(tag);
593  return(locale_message);
594#else
595  return(tag);
596#endif
597}
598
599/*
600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601%                                                                             %
602%                                                                             %
603%                                                                             %
604%   G e t E x c e p t i o n S e v e r i t y                                   %
605%                                                                             %
606%                                                                             %
607%                                                                             %
608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
609%
610%  GetExceptionSeverity() returns the exception severity.
611%
612%  The format of the GetExceptionSeverity method is:
613%
614%      ExceptionType GetExceptionSeverity(const ExceptionInfo *exception)
615%
616%  A description of each parameter follows:
617%
618%    o exception: The exception.
619%
620*/
621WizardExport ExceptionType GetExceptionSeverity(const ExceptionInfo *exception)
622{
623  assert(exception != (ExceptionInfo *) NULL);
624  assert(exception->signature == WizardSignature);
625  return(exception->severity);
626}
627
628/*
629%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630%                                                                             %
631%                                                                             %
632%                                                                             %
633%   I n h e r i t E x c e p t i o n                                           %
634%                                                                             %
635%                                                                             %
636%                                                                             %
637%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638%
639%  InheritException() inherits an exception from a related exception.
640%
641%  The format of the InheritException method is:
642%
643%      InheritException(ExceptionInfo *exception,const ExceptionInfo *relative)
644%
645%  A description of each parameter follows:
646%
647%    o exception: The exception info.
648%
649%    o relative: The related exception info.
650%
651%
652*/
653WizardExport void InheritException(ExceptionInfo *exception,
654  const ExceptionInfo *relative)
655{
656  register const ExceptionInfo
657    *p;
658
659  assert(exception != (ExceptionInfo *) NULL);
660  assert(exception->signature == WizardSignature);
661  assert(relative != (ExceptionInfo *) NULL);
662  assert(relative->signature == WizardSignature);
663  if (relative->exceptions == (void *) NULL)
664    return;
665  (void) LockSemaphoreInfo(exception->semaphore);
666  ResetLinkedListIterator((LinkedListInfo *) relative->exceptions);
667  p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
668    relative->exceptions);
669  while (p != (const ExceptionInfo *) NULL)
670  {
671    (void) ThrowException(exception,p->severity,p->reason,p->description);
672    p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
673      relative->exceptions);
674  }
675  (void) UnlockSemaphoreInfo(exception->semaphore);
676}
677
678/*
679%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680%                                                                             %
681%                                                                             %
682%                                                                             %
683%   W i z a r d E r r o r                                                     %
684%                                                                             %
685%                                                                             %
686%                                                                             %
687%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688%
689%  WizardError() calls the exception handler methods with an error reason.
690%
691%  The format of the WizardError method is:
692%
693%      void WizardError(const ExceptionType error,const char *reason,
694%        const char *description)
695%
696%  A description of each parameter follows:
697%
698%    o exception: Specifies the numeric error category.
699%
700%    o reason: Specifies the reason to display before terminating the
701%      program.
702%
703%    o description: Specifies any description to the reason.
704%
705%
706*/
707WizardExport void WizardError(const ExceptionType error,const char *reason,
708  const char *description)
709{
710  if (error_handler != (ErrorHandler) NULL)
711    (*error_handler)(error,reason,description);
712}
713
714/*
715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716%                                                                             %
717%                                                                             %
718%                                                                             %
719%   W i z a r d F a t al E r r o r                                            %
720%                                                                             %
721%                                                                             %
722%                                                                             %
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724%
725%  WizardFatalError() calls the fatal exception handler methods with an error
726%  reason.
727%
728%  The format of the WizardError method is:
729%
730%      void WizardFatalError(const ExceptionType error,const char *reason,
731%        const char *description)
732%
733%  A description of each parameter follows:
734%
735%    o exception: Specifies the numeric error category.
736%
737%    o reason: Specifies the reason to display before terminating the
738%      program.
739%
740%    o description: Specifies any description to the reason.
741%
742*/
743WizardExport void WizardFatalError(const ExceptionType error,const char *reason,
744  const char *description)
745{
746  if (fatal_error_handler != (ErrorHandler) NULL)
747    (*fatal_error_handler)(error,reason,description);
748}
749
750/*
751%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752%                                                                             %
753%                                                                             %
754%                                                                             %
755%   W i z a r d W a r n i n g                                                 %
756%                                                                             %
757%                                                                             %
758%                                                                             %
759%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760%
761%  WizardWarning() calls the warning handler methods with a warning reason.
762%
763%  The format of the WizardWarning method is:
764%
765%      void WizardWarning(const ExceptionType warning,const char *reason,
766%        const char *description)
767%
768%  A description of each parameter follows:
769%
770%    o warning: The warning severity.
771%
772%    o reason: Define the reason for the warning.
773%
774%    o description: Describe the warning.
775%
776*/
777WizardExport void WizardWarning(const ExceptionType warning,const char *reason,
778  const char *description)
779{
780  if (warning_handler != (WarningHandler) NULL)
781    (*warning_handler)(warning,reason,description);
782}
783
784/*
785%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
786%                                                                             %
787%                                                                             %
788%                                                                             %
789%   S e t E r r o r H a n d l e r                                             %
790%                                                                             %
791%                                                                             %
792%                                                                             %
793%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794%
795%  SetErrorHandler() sets the exception handler to the specified method
796%  and returns the previous exception handler.
797%
798%  The format of the SetErrorHandler method is:
799%
800%      ErrorHandler SetErrorHandler(ErrorHandler handler)
801%
802%  A description of each parameter follows:
803%
804%    o handler: The method to handle errors.
805%
806*/
807WizardExport ErrorHandler SetErrorHandler(ErrorHandler handler)
808{
809  ErrorHandler
810    previous_handler;
811
812  previous_handler=error_handler;
813  error_handler=handler;
814  return(previous_handler);
815}
816
817/*
818%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
819%                                                                             %
820%                                                                             %
821%                                                                             %
822%   S e t F a t a l E r r o r H a n d l e r                                   %
823%                                                                             %
824%                                                                             %
825%                                                                             %
826%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827%
828%  SetFatalErrorHandler() sets the fatal exception handler to the specified
829%  method and returns the previous fatal exception handler.
830%
831%  The format of the SetErrorHandler method is:
832%
833%      ErrorHandler SetErrorHandler(ErrorHandler handler)
834%
835%  A description of each parameter follows:
836%
837%    o handler: The method to handle errors.
838%
839*/
840WizardExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
841{
842  FatalErrorHandler
843    previous_handler;
844
845  previous_handler=fatal_error_handler;
846  fatal_error_handler=handler;
847  return(previous_handler);
848}
849
850/*
851%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852%                                                                             %
853%                                                                             %
854%                                                                             %
855%   S e t W a r n i n g H a n d l e r                                         %
856%                                                                             %
857%                                                                             %
858%                                                                             %
859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860%
861%  SetWarningHandler() sets the warning handler to the specified method
862%  and returns the previous warning handler.
863%
864%  The format of the SetWarningHandler method is:
865%
866%      ErrorHandler SetWarningHandler(ErrorHandler handler)
867%
868%  A description of each parameter follows:
869%
870%    o handler: The method to handle warnings.
871%
872*/
873WizardExport WarningHandler SetWarningHandler(WarningHandler handler)
874{
875  WarningHandler
876    previous_handler;
877
878  previous_handler=warning_handler;
879  warning_handler=handler;
880  return(previous_handler);
881}
882
883/*
884%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
885%                                                                             %
886%                                                                             %
887%                                                                             %
888%   T h r o w E x c e p t i o n                                               %
889%                                                                             %
890%                                                                             %
891%                                                                             %
892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893%
894%  ThrowException() throws an exception with the specified severity code,
895%  reason, and optional description.
896%
897%  The format of the ThrowException method is:
898%
899%      WizardBooleanType ThrowException(ExceptionInfo *exception,
900%        const ExceptionType severity,const char *reason,
901%        const char *description)
902%
903%  A description of each parameter follows:
904%
905%    o exception: The exception info.
906%
907%    o severity: The severity of the exception.
908%
909%    o reason: The reason for the exception.
910%
911%    o description: The exception description.
912%
913*/
914WizardExport WizardBooleanType ThrowException(ExceptionInfo *exception,
915  const ExceptionType severity,const char *reason,const char *description)
916{
917  register ExceptionInfo
918    *p;
919
920  assert(exception != (ExceptionInfo *) NULL);
921  assert(exception->signature == WizardSignature);
922  if (exception->exceptions == (void *) NULL)
923    return(WizardTrue);
924  p=(ExceptionInfo *) GetLastValueInLinkedList((LinkedListInfo *)
925    exception->exceptions);
926  if ((p != (ExceptionInfo *) NULL) && (p->severity == severity) &&
927      (LocaleCompare(exception->reason,reason) == 0) &&
928      (LocaleCompare(exception->description,description) == 0))
929    return(WizardTrue);
930  p=(ExceptionInfo *) AcquireWizardMemory(sizeof(*p));
931  if (p == (ExceptionInfo *) NULL)
932    ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'");
933  (void) ResetWizardMemory(p,0,sizeof(*p));
934  p->severity=severity;
935  if (reason != (const char *) NULL)
936    p->reason=ConstantString(reason);
937  if (description != (const char *) NULL)
938    p->description=ConstantString(description);
939  p->signature=WizardSignature;
940  (void) AppendValueToLinkedList((LinkedListInfo *) exception->exceptions,p);
941  exception->severity=p->severity;
942  exception->reason=p->reason;
943  exception->description=p->description;
944  return(WizardTrue);
945}
946
947/*
948%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949%                                                                             %
950%                                                                             %
951%                                                                             %
952%   T h r o w W i z a r d E x c e p t i o n                                   %
953%                                                                             %
954%                                                                             %
955%                                                                             %
956%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957%
958%  ThrowWizardException logs an exception as determined by the log configuration
959%  file.  If an error occurs, WizardFalse is returned otherwise WizardTrue.
960%
961%  The format of the ThrowWizardException method is:
962%
963%      WizardBooleanType ThrowWizardException(ExceptionInfo *exception,
964%        const char *module,const char *function,const unsigned long line,
965%        const ExceptionType severity,const char *format,...)
966%
967%  A description of each parameter follows:
968%
969%    o exception: The exception info.
970%
971%    o filename: The source module filename.
972%
973%    o function: The function name.
974%
975%    o line: The line number of the source module.
976%
977%    o severity: Specifies the numeric error category.
978%
979%    o format: The output format.
980%
981*/
982
983WizardExport WizardBooleanType ThrowWizardExceptionList(
984  ExceptionInfo *exception,const char *module,const char *function,
985  const unsigned long line,const ExceptionType severity,const char *format,
986  va_list operands)
987{
988  char
989    message[MaxTextExtent],
990    reason[MaxTextExtent];
991
992  int
993    n;
994
995  WizardBooleanType
996    status;
997
998  assert(exception != (ExceptionInfo *) NULL);
999  assert(exception->signature == WizardSignature);
1000#if defined(WIZARDSTOOLKIT_HAVE_VSNPRINTF)
1001  n=vsnprintf(reason,MaxTextExtent,format,operands);
1002#else
1003  n=vsprintf(reason,format,operands);
1004#endif
1005  if (n < 0)
1006    reason[MaxTextExtent-1]='\0';
1007  status=LogWizardEvent(exception->severity >= ErrorException ?
1008    ExceptionEvent : WarningEvent,module,function,line,"%s",reason);
1009  (void) FormatWizardString(message,MaxTextExtent,"%s @ %s/%s/%ld",reason,
1010    module,function,line);
1011  (void) ThrowException(exception,severity,message,(char *) NULL);
1012  return(status);
1013}
1014
1015WizardExport WizardBooleanType ThrowWizardException(ExceptionInfo *exception,
1016  const char *module,const char *function,const unsigned long line,
1017  const ExceptionType severity,const char *format,...)
1018{
1019  WizardBooleanType
1020    status;
1021
1022  va_list
1023    operands;
1024
1025  va_start(operands,format);
1026  status=ThrowWizardExceptionList(exception,module,function,line,severity,
1027    format,operands);
1028  va_end(operands);
1029  return(status);
1030}
Note: See TracBrowser for help on using the browser.