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