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