]> granicus.if.org Git - imagemagick/blob - MagickCore/log.c
(no commit message)
[imagemagick] / MagickCore / log.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                             L       OOO    GGGG                             %
7 %                             L      O   O  G                                 %
8 %                             L      O   O  G GG                              %
9 %                             L      O   O  G   G                             %
10 %                             LLLLL   OOO    GGG                              %
11 %                                                                             %
12 %                                                                             %
13 %                             MagickCore Log Events                           %
14 %                                                                             %
15 %                               Software Design                               %
16 %                                    Cristy                                   %
17 %                                September 2002                               %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2014 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.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 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/configure.h"
46 #include "MagickCore/configure-private.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/hashmap.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/log-private.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/nt-base-private.h"
54 #include "MagickCore/option.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/timer.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/string-private.h"
59 #include "MagickCore/token.h"
60 #include "MagickCore/thread_.h"
61 #include "MagickCore/thread-private.h"
62 #include "MagickCore/utility.h"
63 #include "MagickCore/utility-private.h"
64 #include "MagickCore/version.h"
65 #include "MagickCore/xml-tree.h"
66 #include "MagickCore/xml-tree-private.h"
67 \f
68 /*
69   Define declarations.
70 */
71 #define LogFilename  "log.xml"
72 \f
73 /*
74   Typedef declarations.
75 */
76 typedef enum
77 {
78   UndefinedHandler = 0x0000,
79   NoHandler = 0x0000,
80   ConsoleHandler = 0x0001,
81   StdoutHandler = 0x0002,
82   StderrHandler = 0x0004,
83   FileHandler = 0x0008,
84   DebugHandler = 0x0010,
85   EventHandler = 0x0020,
86   MethodHandler = 0x0040
87 } LogHandlerType;
88
89 typedef struct _EventInfo
90 {
91   char
92     *name;
93
94   LogEventType
95     event;
96 } EventInfo;
97
98 typedef struct _HandlerInfo
99 {
100   const char
101     *name;
102
103   LogHandlerType
104     handler;
105 } HandlerInfo;
106
107 struct _LogInfo
108 {
109   LogEventType
110     event_mask;
111
112   LogHandlerType
113     handler_mask;
114
115   char
116     *path,
117     *name,
118     *filename,
119     *format;
120
121   size_t
122     generations,
123     limit;
124
125   FILE
126     *file;
127
128   size_t
129     generation;
130
131   MagickBooleanType
132     append,
133     stealth;
134
135   TimerInfo
136     timer;
137
138   size_t
139     signature;
140
141   MagickLogMethod
142     method;
143 };
144
145 typedef struct _LogMapInfo
146 {
147   const LogEventType
148     event_mask;
149
150   const LogHandlerType
151     handler_mask;
152
153   const char
154     *filename,
155     *format;
156 } LogMapInfo;
157 \f
158 /*
159   Static declarations.
160 */
161 static const HandlerInfo
162   LogHandlers[] =
163   {
164     { "Console", ConsoleHandler },
165     { "Debug", DebugHandler },
166     { "Event", EventHandler },
167     { "File", FileHandler },
168     { "None", NoHandler },
169     { "Stderr", StderrHandler },
170     { "Stdout", StdoutHandler },
171     { (char *) NULL, UndefinedHandler }
172   };
173
174 static const LogMapInfo
175   LogMap[] =
176   {
177     { NoEvents, ConsoleHandler, "Magick-%g.log",
178       "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n  %e" }
179   };
180
181 static char
182   log_name[MaxTextExtent] = "Magick";
183
184 static LinkedListInfo
185   *log_cache = (LinkedListInfo *) NULL;
186
187 static SemaphoreInfo
188   *event_semaphore = (SemaphoreInfo *) NULL,
189   *log_semaphore = (SemaphoreInfo *) NULL;
190 \f
191 /*
192   Forward declarations.
193 */
194 static LogHandlerType
195   ParseLogHandlers(const char *);
196
197 static LogInfo
198   *GetLogInfo(const char *,ExceptionInfo *);
199
200 static MagickBooleanType
201   IsLogCacheInstantiated(ExceptionInfo *),
202   LoadLogCache(LinkedListInfo *,const char *,const char *,const size_t,
203     ExceptionInfo *);
204 \f
205 /*
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 %                                                                             %
208 %                                                                             %
209 %                                                                             %
210 %  A c q u i r e L o g C a c h e                                              %
211 %                                                                             %
212 %                                                                             %
213 %                                                                             %
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 %
216 %  AcquireLogCache() caches one or more log configurations which provides a
217 %  mapping between log attributes and log name.
218 %
219 %  The format of the AcquireLogCache method is:
220 %
221 %      LinkedListInfo *AcquireLogCache(const char *filename,
222 %        ExceptionInfo *exception)
223 %
224 %  A description of each parameter follows:
225 %
226 %    o filename: the log configuration filename.
227 %
228 %    o exception: return any errors or warnings in this structure.
229 %
230 */
231 static LinkedListInfo *AcquireLogCache(const char *filename,
232   ExceptionInfo *exception)
233 {
234   const StringInfo
235     *option;
236
237   LinkedListInfo
238     *log_cache,
239     *options;
240
241   MagickStatusType
242     status;
243
244   register ssize_t
245     i;
246
247   /*
248     Load external log map.
249   */
250   log_cache=NewLinkedList(0);
251   if (log_cache == (LinkedListInfo *) NULL)
252     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
253   status=MagickTrue;
254   options=GetConfigureOptions(filename,exception);
255   option=(const StringInfo *) GetNextValueInLinkedList(options);
256   while (option != (const StringInfo *) NULL)
257   {
258     status&=LoadLogCache(log_cache,(const char *) GetStringInfoDatum(option),
259       GetStringInfoPath(option),0,exception);
260     option=(const StringInfo *) GetNextValueInLinkedList(options);
261   }
262   options=DestroyConfigureOptions(options);
263   /*
264     Load built-in log map.
265   */
266   for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
267   {
268     LogInfo
269       *log_info;
270
271     register const LogMapInfo
272       *p;
273
274     p=LogMap+i;
275     log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
276     if (log_info == (LogInfo *) NULL)
277       {
278         (void) ThrowMagickException(exception,GetMagickModule(),
279           ResourceLimitError,"MemoryAllocationFailed","`%s'",p->filename);
280         continue;
281       }
282     (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
283     log_info->path=ConstantString("[built-in]");
284     GetTimerInfo((TimerInfo *) &log_info->timer);
285     log_info->event_mask=p->event_mask;
286     log_info->handler_mask=p->handler_mask;
287     log_info->filename=ConstantString(p->filename);
288     log_info->format=ConstantString(p->format);
289     log_info->signature=MagickSignature;
290     status&=AppendValueToLinkedList(log_cache,log_info);
291     if (status == MagickFalse)
292       (void) ThrowMagickException(exception,GetMagickModule(),
293         ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
294   }
295   return(log_cache);
296 }
297 \f
298 /*
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 %                                                                             %
301 %                                                                             %
302 %                                                                             %
303 %   C l o s e M a g i c k L o g                                               %
304 %                                                                             %
305 %                                                                             %
306 %                                                                             %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 %
309 %  CloseMagickLog() closes the Magick log.
310 %
311 %  The format of the CloseMagickLog method is:
312 %
313 %      CloseMagickLog(void)
314 %
315 */
316 MagickExport void CloseMagickLog(void)
317 {
318   ExceptionInfo
319     *exception;
320
321   LogInfo
322     *log_info;
323
324   if (IsEventLogging() == MagickFalse)
325     return;
326   exception=AcquireExceptionInfo();
327   log_info=GetLogInfo("*",exception);
328   exception=DestroyExceptionInfo(exception);
329   LockSemaphoreInfo(log_semaphore);
330   if (log_info->file != (FILE *) NULL)
331     {
332       (void) FormatLocaleFile(log_info->file,"</log>\n");
333       (void) fclose(log_info->file);
334       log_info->file=(FILE *) NULL;
335     }
336   UnlockSemaphoreInfo(log_semaphore);
337 }
338 \f
339 /*
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 %                                                                             %
342 %                                                                             %
343 %                                                                             %
344 +   G e t L o g I n f o                                                       %
345 %                                                                             %
346 %                                                                             %
347 %                                                                             %
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 %
350 %  GetLogInfo() searches the log list for the specified name and if found
351 %  returns attributes for that log.
352 %
353 %  The format of the GetLogInfo method is:
354 %
355 %      LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
356 %
357 %  A description of each parameter follows:
358 %
359 %    o name: the log name.
360 %
361 %    o exception: return any errors or warnings in this structure.
362 %
363 */
364 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
365 {
366   register LogInfo
367     *p;
368
369   assert(exception != (ExceptionInfo *) NULL);
370   if (IsLogCacheInstantiated(exception) == MagickFalse)
371     return((LogInfo *) NULL);
372   /*
373     Search for log tag.
374   */
375   LockSemaphoreInfo(log_semaphore);
376   ResetLinkedListIterator(log_cache);
377   p=(LogInfo *) GetNextValueInLinkedList(log_cache);
378   if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
379     {
380       UnlockSemaphoreInfo(log_semaphore);
381       return(p);
382     }
383   while (p != (LogInfo *) NULL)
384   {
385     if (LocaleCompare(name,p->name) == 0)
386       break;
387     p=(LogInfo *) GetNextValueInLinkedList(log_cache);
388   }
389   if (p != (LogInfo *) NULL)
390     (void) InsertValueInLinkedList(log_cache,0,
391       RemoveElementByValueFromLinkedList(log_cache,p));
392   UnlockSemaphoreInfo(log_semaphore);
393   return(p);
394 }
395 \f
396 /*
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 %                                                                             %
399 %                                                                             %
400 %                                                                             %
401 %   G e t L o g I n f o L i s t                                               %
402 %                                                                             %
403 %                                                                             %
404 %                                                                             %
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 %
407 %  GetLogInfoList() returns any logs that match the specified pattern.
408 %
409 %  The format of the GetLogInfoList function is:
410 %
411 %      const LogInfo **GetLogInfoList(const char *pattern,
412 %        size_t *number_preferences,ExceptionInfo *exception)
413 %
414 %  A description of each parameter follows:
415 %
416 %    o pattern: Specifies a pointer to a text string containing a pattern.
417 %
418 %    o number_preferences:  This integer returns the number of logs in the list.
419 %
420 %    o exception: return any errors or warnings in this structure.
421 %
422 */
423 #if defined(__cplusplus) || defined(c_plusplus)
424 extern "C" {
425 #endif
426
427 static int LogInfoCompare(const void *x,const void *y)
428 {
429   const LogInfo
430     **p,
431     **q;
432
433   p=(const LogInfo **) x,
434   q=(const LogInfo **) y;
435   if (LocaleCompare((*p)->path,(*q)->path) == 0)
436     return(LocaleCompare((*p)->name,(*q)->name));
437   return(LocaleCompare((*p)->path,(*q)->path));
438 }
439
440 #if defined(__cplusplus) || defined(c_plusplus)
441 }
442 #endif
443
444 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
445   size_t *number_preferences,ExceptionInfo *exception)
446 {
447   const LogInfo
448     **preferences;
449
450   register const LogInfo
451     *p;
452
453   register ssize_t
454     i;
455
456   /*
457     Allocate log list.
458   */
459   assert(pattern != (char *) NULL);
460   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
461   assert(number_preferences != (size_t *) NULL);
462   *number_preferences=0;
463   p=GetLogInfo("*",exception);
464   if (p == (const LogInfo *) NULL)
465     return((const LogInfo **) NULL);
466   preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
467     GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
468   if (preferences == (const LogInfo **) NULL)
469     return((const LogInfo **) NULL);
470   /*
471     Generate log list.
472   */
473   LockSemaphoreInfo(log_semaphore);
474   ResetLinkedListIterator(log_cache);
475   p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
476   for (i=0; p != (const LogInfo *) NULL; )
477   {
478     if ((p->stealth == MagickFalse) &&
479         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
480       preferences[i++]=p;
481     p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
482   }
483   UnlockSemaphoreInfo(log_semaphore);
484   qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
485   preferences[i]=(LogInfo *) NULL;
486   *number_preferences=(size_t) i;
487   return(preferences);
488 }
489 \f
490 /*
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 %                                                                             %
493 %                                                                             %
494 %                                                                             %
495 %   G e t L o g L i s t                                                       %
496 %                                                                             %
497 %                                                                             %
498 %                                                                             %
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 %
501 %  GetLogList() returns any logs that match the specified pattern.
502 %
503 %  The format of the GetLogList function is:
504 %
505 %      char **GetLogList(const char *pattern,size_t *number_preferences,
506 %        ExceptionInfo *exception)
507 %
508 %  A description of each parameter follows:
509 %
510 %    o pattern: Specifies a pointer to a text string containing a pattern.
511 %
512 %    o number_preferences:  This integer returns the number of logs in the list.
513 %
514 %    o exception: return any errors or warnings in this structure.
515 %
516 */
517
518 #if defined(__cplusplus) || defined(c_plusplus)
519 extern "C" {
520 #endif
521
522 static int LogCompare(const void *x,const void *y)
523 {
524   register const char
525     **p,
526     **q;
527
528   p=(const char **) x;
529   q=(const char **) y;
530   return(LocaleCompare(*p,*q));
531 }
532
533 #if defined(__cplusplus) || defined(c_plusplus)
534 }
535 #endif
536
537 MagickExport char **GetLogList(const char *pattern,
538   size_t *number_preferences,ExceptionInfo *exception)
539 {
540   char
541     **preferences;
542
543   register const LogInfo
544     *p;
545
546   register ssize_t
547     i;
548
549   /*
550     Allocate log list.
551   */
552   assert(pattern != (char *) NULL);
553   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
554   assert(number_preferences != (size_t *) NULL);
555   *number_preferences=0;
556   p=GetLogInfo("*",exception);
557   if (p == (const LogInfo *) NULL)
558     return((char **) NULL);
559   preferences=(char **) AcquireQuantumMemory((size_t)
560     GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
561   if (preferences == (char **) NULL)
562     return((char **) NULL);
563   /*
564     Generate log list.
565   */
566   LockSemaphoreInfo(log_semaphore);
567   ResetLinkedListIterator(log_cache);
568   p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
569   for (i=0; p != (const LogInfo *) NULL; )
570   {
571     if ((p->stealth == MagickFalse) &&
572         (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
573       preferences[i++]=ConstantString(p->name);
574     p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
575   }
576   UnlockSemaphoreInfo(log_semaphore);
577   qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
578   preferences[i]=(char *) NULL;
579   *number_preferences=(size_t) i;
580   return(preferences);
581 }
582 \f
583 /*
584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 %                                                                             %
586 %                                                                             %
587 %                                                                             %
588 %   G e t L o g N a m e                                                       %
589 %                                                                             %
590 %                                                                             %
591 %                                                                             %
592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593 %
594 %  GetLogName() returns the current log name.
595 %
596 %  The format of the GetLogName method is:
597 %
598 %      const char *GetLogName(void)
599 %
600 */
601 MagickExport const char *GetLogName(void)
602 {
603   return(log_name);
604 }
605 \f
606 /*
607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
608 %                                                                             %
609 %                                                                             %
610 %                                                                             %
611 +   I s L o g C a c h e I n s t a n t i a t e d                               %
612 %                                                                             %
613 %                                                                             %
614 %                                                                             %
615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
616 %
617 %  IsLogCacheInstantiated() determines if the log list is instantiated.  If
618 %  not, it instantiates the list and returns it.
619 %
620 %  The format of the IsLogInstantiated method is:
621 %
622 %      MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
623 %
624 %  A description of each parameter follows.
625 %
626 %    o exception: return any errors or warnings in this structure.
627 %
628 */
629 static MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
630 {
631   if (log_cache == (LinkedListInfo *) NULL)
632     {
633       if (log_semaphore == (SemaphoreInfo *) NULL)
634         ActivateSemaphoreInfo(&log_semaphore);
635       LockSemaphoreInfo(log_semaphore);
636       if (log_cache == (LinkedListInfo *) NULL)
637         log_cache=AcquireLogCache(LogFilename,exception);
638       UnlockSemaphoreInfo(log_semaphore);
639     }
640   return(log_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
641 }
642 \f
643 /*
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 %                                                                             %
646 %                                                                             %
647 %                                                                             %
648 %  I s E v e n t L o g g i n g                                                %
649 %                                                                             %
650 %                                                                             %
651 %                                                                             %
652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653 %
654 %  IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
655 %  MagickFalse.
656 %
657 %  The format of the IsEventLogging method is:
658 %
659 %      MagickBooleanType IsEventLogging(void)
660 %
661 */
662 MagickExport MagickBooleanType IsEventLogging(void)
663 {
664   const LogInfo
665     *log_info;
666
667   ExceptionInfo
668     *exception;
669
670   if ((log_cache == (LinkedListInfo *) NULL) ||
671       (IsLinkedListEmpty(log_cache) != MagickFalse))
672     return(MagickFalse);
673   exception=AcquireExceptionInfo();
674   log_info=GetLogInfo("*",exception);
675   exception=DestroyExceptionInfo(exception);
676   return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
677 }
678 /*
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 %                                                                             %
681 %                                                                             %
682 %                                                                             %
683 %  L i s t L o g I n f o                                                      %
684 %                                                                             %
685 %                                                                             %
686 %                                                                             %
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688 %
689 %  ListLogInfo() lists the log info to a file.
690 %
691 %  The format of the ListLogInfo method is:
692 %
693 %      MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
694 %
695 %  A description of each parameter follows.
696 %
697 %    o file:  An pointer to a FILE.
698 %
699 %    o exception: return any errors or warnings in this structure.
700 %
701 */
702 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
703 {
704 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
705
706   const char
707     *path;
708
709   const LogInfo
710     **log_info;
711
712   register ssize_t
713     i;
714
715   size_t
716     number_aliases;
717
718   ssize_t
719     j;
720
721   if (file == (const FILE *) NULL)
722     file=stdout;
723   log_info=GetLogInfoList("*",&number_aliases,exception);
724   if (log_info == (const LogInfo **) NULL)
725     return(MagickFalse);
726   j=0;
727   path=(const char *) NULL;
728   for (i=0; i < (ssize_t) number_aliases; i++)
729   {
730     if (log_info[i]->stealth != MagickFalse)
731       continue;
732     if ((path == (const char *) NULL) ||
733         (LocaleCompare(path,log_info[i]->path) != 0))
734       {
735         size_t
736           length;
737
738         if (log_info[i]->path != (char *) NULL)
739           (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
740         length=0;
741         for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
742         {
743           size_t
744             mask;
745
746           mask=1;
747           mask<<=j;
748           if ((log_info[i]->handler_mask & mask) != 0)
749             {
750               (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
751               length+=strlen(LogHandlers[j].name);
752             }
753           if (LogHandlers[j].name == (const char *) NULL)
754             break;
755         }
756         for (j=(ssize_t) length; j <= 12; j++)
757           (void) FormatLocaleFile(file," ");
758         (void) FormatLocaleFile(file," Generations     Limit  Format\n");
759         (void) FormatLocaleFile(file,"-----------------------------------------"
760           "--------------------------------------\n");
761       }
762     path=log_info[i]->path;
763     if (log_info[i]->filename != (char *) NULL)
764       {
765         (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
766         for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
767           (void) FormatLocaleFile(file," ");
768       }
769     (void) FormatLocaleFile(file,"%9g  ",(double) log_info[i]->generations);
770     (void) FormatLocaleFile(file,"%8g   ",(double) log_info[i]->limit);
771     if (log_info[i]->format != (char *) NULL)
772       (void) FormatLocaleFile(file,"%s",log_info[i]->format);
773     (void) FormatLocaleFile(file,"\n");
774   }
775   (void) fflush(file);
776   log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
777   return(MagickTrue);
778 }
779 \f
780 /*
781 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782 %                                                                             %
783 %                                                                             %
784 %                                                                             %
785 +   L o g C o m p o n e n t G e n e s i s                                     %
786 %                                                                             %
787 %                                                                             %
788 %                                                                             %
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 %
791 %  LogComponentGenesis() instantiates the log component.
792 %
793 %  The format of the LogComponentGenesis method is:
794 %
795 %      MagickBooleanType LogComponentGenesis(void)
796 %
797 */
798 MagickPrivate MagickBooleanType LogComponentGenesis(void)
799 {
800   ExceptionInfo
801     *exception;
802
803   if (log_semaphore == (SemaphoreInfo *) NULL)
804     log_semaphore=AcquireSemaphoreInfo();
805   exception=AcquireExceptionInfo();
806   (void) GetLogInfo("*",exception);
807   exception=DestroyExceptionInfo(exception);
808   event_semaphore=AcquireSemaphoreInfo();
809   return(MagickTrue);
810 }
811 \f
812 /*
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814 %                                                                             %
815 %                                                                             %
816 %                                                                             %
817 +   L o g C o m p o n e n t T e r m i n u s                                   %
818 %                                                                             %
819 %                                                                             %
820 %                                                                             %
821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822 %
823 %  LogComponentTerminus() destroys the logging component.
824 %
825 %  The format of the LogComponentTerminus method is:
826 %
827 %      LogComponentTerminus(void)
828 %
829 */
830
831 static void *DestroyLogElement(void *log_info)
832 {
833   register LogInfo
834     *p;
835
836   p=(LogInfo *) log_info;
837   if (p->file != (FILE *) NULL)
838     {
839       (void) FormatLocaleFile(p->file,"</log>\n");
840       (void) fclose(p->file);
841       p->file=(FILE *) NULL;
842     }
843   if (p->format != (char *) NULL)
844     p->format=DestroyString(p->format);
845   if (p->path != (char *) NULL)
846     p->path=DestroyString(p->path);
847   if (p->filename != (char *) NULL)
848     p->filename=DestroyString(p->filename);
849   p=(LogInfo *) RelinquishMagickMemory(p);
850   return((void *) NULL);
851 }
852
853 MagickPrivate void LogComponentTerminus(void)
854 {
855   if (event_semaphore == (SemaphoreInfo *) NULL)
856     ActivateSemaphoreInfo(&event_semaphore);
857   LockSemaphoreInfo(event_semaphore);
858   UnlockSemaphoreInfo(event_semaphore);
859   RelinquishSemaphoreInfo(&event_semaphore);
860   if (log_semaphore == (SemaphoreInfo *) NULL)
861     ActivateSemaphoreInfo(&log_semaphore);
862   LockSemaphoreInfo(log_semaphore);
863   if (log_cache != (LinkedListInfo *) NULL)
864     log_cache=DestroyLinkedList(log_cache,DestroyLogElement);
865   UnlockSemaphoreInfo(log_semaphore);
866   RelinquishSemaphoreInfo(&log_semaphore);
867 }
868 \f
869 /*
870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
871 %                                                                             %
872 %                                                                             %
873 %                                                                             %
874 %   L o g M a g i c k E v e n t                                               %
875 %                                                                             %
876 %                                                                             %
877 %                                                                             %
878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879 %
880 %  LogMagickEvent() logs an event as determined by the log configuration file.
881 %  If an error occurs, MagickFalse is returned otherwise MagickTrue.
882 %
883 %  The format of the LogMagickEvent method is:
884 %
885 %      MagickBooleanType LogMagickEvent(const LogEventType type,
886 %        const char *module,const char *function,const size_t line,
887 %        const char *format,...)
888 %
889 %  A description of each parameter follows:
890 %
891 %    o type: the event type.
892 %
893 %    o filename: the source module filename.
894 %
895 %    o function: the function name.
896 %
897 %    o line: the line number of the source module.
898 %
899 %    o format: the output format.
900 %
901 */
902 static char *TranslateEvent(const LogEventType magick_unused(type),
903   const char *module,const char *function,const size_t line,
904   const char *domain,const char *event)
905 {
906   char
907     *text;
908
909   double
910     elapsed_time,
911     user_time;
912
913   ExceptionInfo
914     *exception;
915
916   LogInfo
917     *log_info;
918
919   register char
920     *q;
921
922   register const char
923     *p;
924
925   size_t
926     extent;
927
928   time_t
929     seconds;
930
931   exception=AcquireExceptionInfo();
932   log_info=(LogInfo *) GetLogInfo("*",exception);
933   exception=DestroyExceptionInfo(exception);
934   seconds=time((time_t *) NULL);
935   elapsed_time=GetElapsedTime(&log_info->timer);
936   user_time=GetUserTime(&log_info->timer);
937   text=AcquireString(event);
938   if (log_info->format == (char *) NULL)
939     return(text);
940   extent=strlen(event)+MaxTextExtent;
941   if (LocaleCompare(log_info->format,"xml") == 0)
942     {
943       char
944         timestamp[MaxTextExtent];
945
946       /*
947         Translate event in "XML" format.
948       */
949       (void) FormatMagickTime(seconds,extent,timestamp);
950       (void) FormatLocaleString(text,extent,
951         "<entry>\n"
952         "  <timestamp>%s</timestamp>\n"
953         "  <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
954         "  <user-time>%0.3f</user-time>\n"
955         "  <process-id>%.20g</process-id>\n"
956         "  <thread-id>%.20g</thread-id>\n"
957         "  <module>%s</module>\n"
958         "  <function>%s</function>\n"
959         "  <line>%.20g</line>\n"
960         "  <domain>%s</domain>\n"
961         "  <event>%s</event>\n"
962         "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
963         (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
964         (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
965         (double) getpid(),(double) GetMagickThreadSignature(),module,function,
966         (double) line,domain,event);
967       return(text);
968     }
969   /*
970     Translate event in "human readable" format.
971   */
972   q=text;
973   for (p=log_info->format; *p != '\0'; p++)
974   {
975     *q='\0';
976     if ((size_t) (q-text+MaxTextExtent) >= extent)
977       {
978         extent+=MaxTextExtent;
979         text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
980           sizeof(*text));
981         if (text == (char *) NULL)
982           return((char *) NULL);
983         q=text+strlen(text);
984       }
985     /*
986       The format of the log is defined by embedding special format characters:
987
988         %c   client name
989         %d   domain
990         %e   event
991         %f   function
992         %g   generation
993         %l   line
994         %m   module
995         %n   log name
996         %p   process id
997         %r   real CPU time
998         %t   wall clock time
999         %u   user CPU time
1000         %v   version
1001         %%   percent sign
1002         \n   newline
1003         \r   carriage return
1004     */
1005     if ((*p == '\\') && (*(p+1) == 'r'))
1006       {
1007         *q++='\r';
1008         p++;
1009         continue;
1010       }
1011     if ((*p == '\\') && (*(p+1) == 'n'))
1012       {
1013         *q++='\n';
1014         p++;
1015         continue;
1016       }
1017     if (*p != '%')
1018       {
1019         *q++=(*p);
1020         continue;
1021       }
1022     p++;
1023     switch (*p)
1024     {
1025       case 'c':
1026       {
1027         q+=CopyMagickString(q,GetClientName(),extent);
1028         break;
1029       }
1030       case 'd':
1031       {
1032         q+=CopyMagickString(q,domain,extent);
1033         break;
1034       }
1035       case 'e':
1036       {
1037         q+=CopyMagickString(q,event,extent);
1038         break;
1039       }
1040       case 'f':
1041       {
1042         q+=CopyMagickString(q,function,extent);
1043         break;
1044       }
1045       case 'g':
1046       {
1047         if (log_info->generations == 0)
1048           {
1049             (void) CopyMagickString(q,"0",extent);
1050             q++;
1051             break;
1052           }
1053         q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1054           log_info->generations));
1055         break;
1056       }
1057       case 'l':
1058       {
1059         q+=FormatLocaleString(q,extent,"%.20g",(double) line);
1060         break;
1061       }
1062       case 'm':
1063       {
1064         register const char
1065           *p;
1066
1067         for (p=module+strlen(module)-1; p > module; p--)
1068           if (*p == *DirectorySeparator)
1069             {
1070               p++;
1071               break;
1072             }
1073         q+=CopyMagickString(q,p,extent);
1074         break;
1075       }
1076       case 'n':
1077       {
1078         q+=CopyMagickString(q,GetLogName(),extent);
1079         break;
1080       }
1081       case 'p':
1082       {
1083         q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1084         break;
1085       }
1086       case 'r':
1087       {
1088         q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
1089           (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
1090           (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
1091         break;
1092       }
1093       case 't':
1094       {
1095         q+=FormatMagickTime(seconds,extent,q);
1096         break;
1097       }
1098       case 'u':
1099       {
1100         q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
1101         break;
1102       }
1103       case 'v':
1104       {
1105         q+=CopyMagickString(q,MagickLibVersionText,extent);
1106         break;
1107       }
1108       case '%':
1109       {
1110         *q++=(*p);
1111         break;
1112       }
1113       default:
1114       {
1115         *q++='%';
1116         *q++=(*p);
1117         break;
1118       }
1119     }
1120   }
1121   *q='\0';
1122   return(text);
1123 }
1124
1125 static char *TranslateFilename(const LogInfo *log_info)
1126 {
1127   char
1128     *filename;
1129
1130   register char
1131     *q;
1132
1133   register const char
1134     *p;
1135
1136   size_t
1137     extent;
1138
1139   /*
1140     Translate event in "human readable" format.
1141   */
1142   assert(log_info != (LogInfo *) NULL);
1143   assert(log_info->filename != (char *) NULL);
1144   filename=AcquireString((char *) NULL);
1145   extent=MaxTextExtent;
1146   q=filename;
1147   for (p=log_info->filename; *p != '\0'; p++)
1148   {
1149     *q='\0';
1150     if ((size_t) (q-filename+MaxTextExtent) >= extent)
1151       {
1152         extent+=MaxTextExtent;
1153         filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1154           sizeof(*filename));
1155         if (filename == (char *) NULL)
1156           return((char *) NULL);
1157         q=filename+strlen(filename);
1158       }
1159     /*
1160       The format of the filename is defined by embedding special format
1161       characters:
1162
1163         %c   client name
1164         %n   log name
1165         %p   process id
1166         %v   version
1167         %%   percent sign
1168     */
1169     if (*p != '%')
1170       {
1171         *q++=(*p);
1172         continue;
1173       }
1174     p++;
1175     switch (*p)
1176     {
1177       case 'c':
1178       {
1179         q+=CopyMagickString(q,GetClientName(),extent);
1180         break;
1181       }
1182       case 'g':
1183       {
1184         if (log_info->generations == 0)
1185           {
1186             (void) CopyMagickString(q,"0",extent);
1187             q++;
1188             break;
1189           }
1190         q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1191           log_info->generations));
1192         break;
1193       }
1194       case 'n':
1195       {
1196         q+=CopyMagickString(q,GetLogName(),extent);
1197         break;
1198       }
1199       case 'p':
1200       {
1201         q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1202         break;
1203       }
1204       case 'v':
1205       {
1206         q+=CopyMagickString(q,MagickLibVersionText,extent);
1207         break;
1208       }
1209       case '%':
1210       {
1211         *q++=(*p);
1212         break;
1213       }
1214       default:
1215       {
1216         *q++='%';
1217         *q++=(*p);
1218         break;
1219       }
1220     }
1221   }
1222   *q='\0';
1223   return(filename);
1224 }
1225
1226 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1227   const char *function,const size_t line,const char *format,
1228   va_list operands)
1229 {
1230   char
1231     event[MaxTextExtent],
1232     *text;
1233
1234   const char
1235     *domain;
1236
1237   ExceptionInfo
1238     *exception;
1239
1240   int
1241     n;
1242
1243   LogInfo
1244     *log_info;
1245
1246   if (IsEventLogging() == MagickFalse)
1247     return(MagickFalse);
1248   exception=AcquireExceptionInfo();
1249   log_info=(LogInfo *) GetLogInfo("*",exception);
1250   exception=DestroyExceptionInfo(exception);
1251   if (event_semaphore == (SemaphoreInfo *) NULL)
1252     ActivateSemaphoreInfo(&event_semaphore);
1253   LockSemaphoreInfo(event_semaphore);
1254   if ((log_info->event_mask & type) == 0)
1255     {
1256       UnlockSemaphoreInfo(event_semaphore);
1257       return(MagickTrue);
1258     }
1259   domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1260 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1261   n=vsnprintf(event,MaxTextExtent,format,operands);
1262 #else
1263   n=vsprintf(event,format,operands);
1264 #endif
1265   if (n < 0)
1266     event[MaxTextExtent-1]='\0';
1267   text=TranslateEvent(type,module,function,line,domain,event);
1268   if (text == (char *) NULL)
1269     {
1270       (void) ContinueTimer((TimerInfo *) &log_info->timer);
1271       UnlockSemaphoreInfo(event_semaphore);
1272       return(MagickFalse);
1273     }
1274   if ((log_info->handler_mask & ConsoleHandler) != 0)
1275     {
1276       (void) FormatLocaleFile(stderr,"%s\n",text);
1277       (void) fflush(stderr);
1278     }
1279   if ((log_info->handler_mask & DebugHandler) != 0)
1280     {
1281 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1282       OutputDebugString(text);
1283       OutputDebugString("\n");
1284 #endif
1285     }
1286   if ((log_info->handler_mask & EventHandler) != 0)
1287     {
1288 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1289       (void) NTReportEvent(text,MagickFalse);
1290 #endif
1291     }
1292   if ((log_info->handler_mask & FileHandler) != 0)
1293     {
1294       struct stat
1295         file_info;
1296
1297       file_info.st_size=0;
1298       if (log_info->file != (FILE *) NULL)
1299         (void) fstat(fileno(log_info->file),&file_info);
1300       if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
1301         {
1302           (void) FormatLocaleFile(log_info->file,"</log>\n");
1303           (void) fclose(log_info->file);
1304           log_info->file=(FILE *) NULL;
1305         }
1306       if (log_info->file == (FILE *) NULL)
1307         {
1308           char
1309             *filename;
1310
1311           filename=TranslateFilename(log_info);
1312           if (filename == (char *) NULL)
1313             {
1314               (void) ContinueTimer((TimerInfo *) &log_info->timer);
1315               UnlockSemaphoreInfo(event_semaphore);
1316               return(MagickFalse);
1317             }
1318           log_info->append=IsPathAccessible(filename);
1319           log_info->file=fopen_utf8(filename,"ab");
1320           filename=(char  *) RelinquishMagickMemory(filename);
1321           if (log_info->file == (FILE *) NULL)
1322             {
1323               UnlockSemaphoreInfo(event_semaphore);
1324               return(MagickFalse);
1325             }
1326           log_info->generation++;
1327           if (log_info->append == MagickFalse)
1328             (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1329               "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1330           (void) FormatLocaleFile(log_info->file,"<log>\n");
1331         }
1332       (void) FormatLocaleFile(log_info->file,"  <event>%s</event>\n",text);
1333       (void) fflush(log_info->file);
1334     }
1335   if ((log_info->handler_mask & MethodHandler) != 0)
1336     {
1337       if (log_info->method != (MagickLogMethod) NULL)
1338         log_info->method(type,text);
1339     }
1340   if ((log_info->handler_mask & StdoutHandler) != 0)
1341     {
1342       (void) FormatLocaleFile(stdout,"%s\n",text);
1343       (void) fflush(stdout);
1344     }
1345   if ((log_info->handler_mask & StderrHandler) != 0)
1346     {
1347       (void) FormatLocaleFile(stderr,"%s\n",text);
1348       (void) fflush(stderr);
1349     }
1350   text=(char  *) RelinquishMagickMemory(text);
1351   (void) ContinueTimer((TimerInfo *) &log_info->timer);
1352   UnlockSemaphoreInfo(event_semaphore);
1353   return(MagickTrue);
1354 }
1355
1356 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1357   const char *function,const size_t line,const char *format,...)
1358 {
1359   va_list
1360     operands;
1361
1362   MagickBooleanType
1363     status;
1364
1365   va_start(operands,format);
1366   status=LogMagickEventList(type,module,function,line,format,operands);
1367   va_end(operands);
1368   return(status);
1369 }
1370 \f
1371 /*
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1373 %                                                                             %
1374 %                                                                             %
1375 %                                                                             %
1376 +   L o a d L o g L i s t                                                     %
1377 %                                                                             %
1378 %                                                                             %
1379 %                                                                             %
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1381 %
1382 %  LoadLogCache() loads the log configurations which provides a
1383 %  mapping between log attributes and log name.
1384 %
1385 %  The format of the LoadLogCache method is:
1386 %
1387 %      MagickBooleanType LoadLogCache(LinkedListInfo *log_cache,const char *xml,
1388 %        const char *filename,const size_t depth,ExceptionInfo *exception)
1389 %
1390 %  A description of each parameter follows:
1391 %
1392 %    o xml:  The log list in XML format.
1393 %
1394 %    o filename:  The log list filename.
1395 %
1396 %    o depth: depth of <include /> statements.
1397 %
1398 %    o exception: return any errors or warnings in this structure.
1399 %
1400 */
1401 static MagickBooleanType LoadLogCache(LinkedListInfo *log_cache,const char *xml,
1402   const char *filename,const size_t depth,ExceptionInfo *exception)
1403 {
1404   char
1405     keyword[MaxTextExtent],
1406     *token;
1407
1408   const char
1409     *q;
1410
1411   LogInfo
1412     *log_info = (LogInfo *) NULL;
1413
1414   MagickStatusType
1415     status;
1416
1417   /*
1418     Load the log map file.
1419   */
1420   if (xml == (const char *) NULL)
1421     return(MagickFalse);
1422   status=MagickTrue;
1423   token=AcquireString((const char *) xml);
1424   for (q=(const char *) xml; *q != '\0'; )
1425   {
1426     /*
1427       Interpret XML.
1428     */
1429     GetMagickToken(q,&q,token);
1430     if (*token == '\0')
1431       break;
1432     (void) CopyMagickString(keyword,token,MaxTextExtent);
1433     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1434       {
1435         /*
1436           Doctype element.
1437         */
1438         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1439           GetMagickToken(q,&q,token);
1440         continue;
1441       }
1442     if (LocaleNCompare(keyword,"<!--",4) == 0)
1443       {
1444         /*
1445           Comment element.
1446         */
1447         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1448           GetMagickToken(q,&q,token);
1449         continue;
1450       }
1451     if (LocaleCompare(keyword,"<include") == 0)
1452       {
1453         /*
1454           Include element.
1455         */
1456         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1457         {
1458           (void) CopyMagickString(keyword,token,MaxTextExtent);
1459           GetMagickToken(q,&q,token);
1460           if (*token != '=')
1461             continue;
1462           GetMagickToken(q,&q,token);
1463           if (LocaleCompare(keyword,"file") == 0)
1464             {
1465               if (depth > 200)
1466                 (void) ThrowMagickException(exception,GetMagickModule(),
1467                   ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1468               else
1469                 {
1470                   char
1471                     path[MaxTextExtent],
1472                     *xml;
1473
1474                   GetPathComponent(filename,HeadPath,path);
1475                   if (*path != '\0')
1476                     (void) ConcatenateMagickString(path,DirectorySeparator,
1477                       MaxTextExtent);
1478                   if (*token == *DirectorySeparator)
1479                     (void) CopyMagickString(path,token,MaxTextExtent);
1480                   else
1481                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
1482                   xml=FileToXML(path,~0UL);
1483                   if (xml != (char *) NULL)
1484                     {
1485                       status&=LoadLogCache(log_cache,xml,path,depth+1,
1486                         exception);
1487                       xml=DestroyString(xml);
1488                     }
1489                 }
1490             }
1491         }
1492         continue;
1493       }
1494     if (LocaleCompare(keyword,"<logmap>") == 0)
1495       {
1496         /*
1497           Allocate memory for the log list.
1498         */
1499         log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1500         if (log_info == (LogInfo *) NULL)
1501           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1502         (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1503         log_info->path=ConstantString(filename);
1504         GetTimerInfo((TimerInfo *) &log_info->timer);
1505         log_info->signature=MagickSignature;
1506         continue;
1507       }
1508     if (log_info == (LogInfo *) NULL)
1509       continue;
1510     if (LocaleCompare(keyword,"</logmap>") == 0)
1511       {
1512         status=AppendValueToLinkedList(log_cache,log_info);
1513         if (status == MagickFalse)
1514           (void) ThrowMagickException(exception,GetMagickModule(),
1515             ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1516         log_info=(LogInfo *) NULL;
1517         continue;
1518       }
1519     GetMagickToken(q,(const char **) NULL,token);
1520     if (*token != '=')
1521       continue;
1522     GetMagickToken(q,&q,token);
1523     GetMagickToken(q,&q,token);
1524     switch (*keyword)
1525     {
1526       case 'E':
1527       case 'e':
1528       {
1529         if (LocaleCompare((char *) keyword,"events") == 0)
1530           {
1531             log_info->event_mask=(LogEventType) (log_info->event_mask |
1532               ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1533             break;
1534           }
1535         break;
1536       }
1537       case 'F':
1538       case 'f':
1539       {
1540         if (LocaleCompare((char *) keyword,"filename") == 0)
1541           {
1542             if (log_info->filename != (char *) NULL)
1543               log_info->filename=(char *)
1544                 RelinquishMagickMemory(log_info->filename);
1545             log_info->filename=ConstantString(token);
1546             break;
1547           }
1548         if (LocaleCompare((char *) keyword,"format") == 0)
1549           {
1550             if (log_info->format != (char *) NULL)
1551               log_info->format=(char *)
1552                 RelinquishMagickMemory(log_info->format);
1553             log_info->format=ConstantString(token);
1554             break;
1555           }
1556         break;
1557       }
1558       case 'G':
1559       case 'g':
1560       {
1561         if (LocaleCompare((char *) keyword,"generations") == 0)
1562           {
1563             if (LocaleCompare(token,"unlimited") == 0)
1564               {
1565                 log_info->generations=(~0UL);
1566                 break;
1567               }
1568             log_info->generations=StringToUnsignedLong(token);
1569             break;
1570           }
1571         break;
1572       }
1573       case 'L':
1574       case 'l':
1575       {
1576         if (LocaleCompare((char *) keyword,"limit") == 0)
1577           {
1578             if (LocaleCompare(token,"unlimited") == 0)
1579               {
1580                 log_info->limit=(~0UL);
1581                 break;
1582               }
1583             log_info->limit=StringToUnsignedLong(token);
1584             break;
1585           }
1586         break;
1587       }
1588       case 'O':
1589       case 'o':
1590       {
1591         if (LocaleCompare((char *) keyword,"output") == 0)
1592           {
1593             log_info->handler_mask=(LogHandlerType)
1594               (log_info->handler_mask | ParseLogHandlers(token));
1595             break;
1596           }
1597         break;
1598       }
1599       default:
1600         break;
1601     }
1602   }
1603   token=DestroyString(token);
1604   if (log_cache == (LinkedListInfo *) NULL)
1605     return(MagickFalse);
1606   return(status != 0 ? MagickTrue : MagickFalse);
1607 }
1608 \f
1609 /*
1610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1611 %                                                                             %
1612 %                                                                             %
1613 %                                                                             %
1614 +   P a r s e L o g H a n d l e r s                                           %
1615 %                                                                             %
1616 %                                                                             %
1617 %                                                                             %
1618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1619 %
1620 %  ParseLogHandlers() parses a string defining which handlers takes a log
1621 %  message and exports them.
1622 %
1623 %  The format of the ParseLogHandlers method is:
1624 %
1625 %      LogHandlerType ParseLogHandlers(const char *handlers)
1626 %
1627 %  A description of each parameter follows:
1628 %
1629 %    o handlers: one or more handlers separated by commas.
1630 %
1631 */
1632 static LogHandlerType ParseLogHandlers(const char *handlers)
1633 {
1634   LogHandlerType
1635     handler_mask;
1636
1637   register const char
1638     *p;
1639
1640   register ssize_t
1641     i;
1642
1643   size_t
1644     length;
1645
1646   handler_mask=NoHandler;
1647   for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1648   {
1649     while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1650            (*p == ',')))
1651       p++;
1652     for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1653     {
1654       length=strlen(LogHandlers[i].name);
1655       if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1656         {
1657           handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1658           break;
1659         }
1660     }
1661     if (LogHandlers[i].name == (char *) NULL)
1662       return(UndefinedHandler);
1663   }
1664   return(handler_mask);
1665 }
1666 \f
1667 /*
1668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1669 %                                                                             %
1670 %                                                                             %
1671 %                                                                             %
1672 %   S e t L o g E v e n t M a s k                                             %
1673 %                                                                             %
1674 %                                                                             %
1675 %                                                                             %
1676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1677 %
1678 %  SetLogEventMask() accepts a list that determines which events to log.  All
1679 %  other events are ignored.  By default, no debug is enabled.  This method
1680 %  returns the previous log event mask.
1681 %
1682 %  The format of the SetLogEventMask method is:
1683 %
1684 %      LogEventType SetLogEventMask(const char *events)
1685 %
1686 %  A description of each parameter follows:
1687 %
1688 %    o events: log these events.
1689 %
1690 */
1691 MagickExport LogEventType SetLogEventMask(const char *events)
1692 {
1693   ExceptionInfo
1694     *exception;
1695
1696   LogInfo
1697     *log_info;
1698
1699   ssize_t
1700     option;
1701
1702   exception=AcquireExceptionInfo();
1703   log_info=(LogInfo *) GetLogInfo("*",exception);
1704   exception=DestroyExceptionInfo(exception);
1705   option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1706   LockSemaphoreInfo(log_semaphore);
1707   log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1708   log_info->event_mask=(LogEventType) option;
1709   if (option == -1)
1710     log_info->event_mask=UndefinedEvents;
1711   UnlockSemaphoreInfo(log_semaphore);
1712   return(log_info->event_mask);
1713 }
1714 \f
1715 /*
1716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1717 %                                                                             %
1718 %                                                                             %
1719 %                                                                             %
1720 %   S e t L o g F o r m a t                                                   %
1721 %                                                                             %
1722 %                                                                             %
1723 %                                                                             %
1724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1725 %
1726 %  SetLogFormat() sets the format for the "human readable" log record.
1727 %
1728 %  The format of the LogMagickFormat method is:
1729 %
1730 %      SetLogFormat(const char *format)
1731 %
1732 %  A description of each parameter follows:
1733 %
1734 %    o format: the log record format.
1735 %
1736 */
1737 MagickExport void SetLogFormat(const char *format)
1738 {
1739   LogInfo
1740     *log_info;
1741
1742   ExceptionInfo
1743     *exception;
1744
1745   exception=AcquireExceptionInfo();
1746   log_info=(LogInfo *) GetLogInfo("*",exception);
1747   exception=DestroyExceptionInfo(exception);
1748   LockSemaphoreInfo(log_semaphore);
1749   if (log_info->format != (char *) NULL)
1750     log_info->format=DestroyString(log_info->format);
1751   log_info->format=ConstantString(format);
1752   UnlockSemaphoreInfo(log_semaphore);
1753 }
1754
1755 /*
1756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1757 %                                                                             %
1758 %                                                                             %
1759 %                                                                             %
1760 %   S e t L o g M e t h o d                                                   %
1761 %                                                                             %
1762 %                                                                             %
1763 %                                                                             %
1764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1765 %
1766 %  SetLogMethod() sets the method that will be called when an event is logged.
1767 %
1768 %  The format of the SetLogMethod method is:
1769 %
1770 %      void SetLogMethod(MagickLogMethod method)
1771 %
1772 %  A description of each parameter follows:
1773 %
1774 %    o method: pointer to a method that will be called when LogMagickEvent is
1775 %      being called.
1776 %
1777 */
1778 MagickExport void SetLogMethod(MagickLogMethod method)
1779 {
1780   ExceptionInfo
1781     *exception;
1782
1783   LogInfo
1784     *log_info;
1785
1786   exception=AcquireExceptionInfo();
1787   log_info=(LogInfo *) GetLogInfo("*",exception);
1788   exception=DestroyExceptionInfo(exception);
1789   LockSemaphoreInfo(log_semaphore);
1790   log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1791   log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1792     MethodHandler);
1793   log_info->method=method;
1794   UnlockSemaphoreInfo(log_semaphore);
1795 }
1796 \f
1797 /*
1798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1799 %                                                                             %
1800 %                                                                             %
1801 %                                                                             %
1802 %   S e t L o g N a m e                                                       %
1803 %                                                                             %
1804 %                                                                             %
1805 %                                                                             %
1806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1807 %
1808 %  SetLogName() sets the log name and returns it.
1809 %
1810 %  The format of the SetLogName method is:
1811 %
1812 %      const char *SetLogName(const char *name)
1813 %
1814 %  A description of each parameter follows:
1815 %
1816 %    o log_name: SetLogName() returns the current client name.
1817 %
1818 %    o name: Specifies the new client name.
1819 %
1820 */
1821 MagickExport const char *SetLogName(const char *name)
1822 {
1823   if ((name != (char *) NULL) && (*name != '\0'))
1824     (void) CopyMagickString(log_name,name,MaxTextExtent);
1825   return(log_name);
1826 }