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