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