]> 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-2010 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) fprintf(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   ssize_t
621     j;
622
623   register ssize_t
624     i;
625
626   size_t
627     number_aliases;
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) fprintf(file,"\nPath: %s\n\n",log_info[i]->path);
645         (void) fprintf(file,"Filename       Generations     Limit  Format\n");
646         (void) fprintf(file,"-------------------------------------------------"
647           "------------------------------\n");
648       }
649     path=log_info[i]->path;
650     if (log_info[i]->filename != (char *) NULL)
651       {
652         (void) fprintf(file,"%s",log_info[i]->filename);
653         for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
654           (void) fprintf(file," ");
655       }
656     (void) fprintf(file,"%9g  ",(double) log_info[i]->generations);
657     (void) FormatMagickSize(MegabytesToBytes(log_info[i]->limit),MagickFalse,
658       limit);
659     (void) fprintf(file,"%8sB  ",limit);
660     if (log_info[i]->format != (char *) NULL)
661       (void) fprintf(file,"%s",log_info[i]->format);
662     (void) fprintf(file,"\n");
663   }
664   (void) fflush(file);
665   log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
666   return(MagickTrue);
667 }
668 \f
669 /*
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
671 %                                                                             %
672 %                                                                             %
673 %                                                                             %
674 +   L o g C o m p o n e n t G e n e s i s                                     %
675 %                                                                             %
676 %                                                                             %
677 %                                                                             %
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 %
680 %  LogComponentGenesis() instantiates the log component.
681 %
682 %  The format of the LogComponentGenesis method is:
683 %
684 %      MagickBooleanType LogComponentGenesis(void)
685 %
686 */
687 MagickExport MagickBooleanType LogComponentGenesis(void)
688 {
689   AcquireSemaphoreInfo(&log_semaphore);
690   return(MagickTrue);
691 }
692 \f
693 /*
694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695 %                                                                             %
696 %                                                                             %
697 %                                                                             %
698 +   L o g C o m p o n e n t T e r m i n u s                                   %
699 %                                                                             %
700 %                                                                             %
701 %                                                                             %
702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703 %
704 %  LogComponentTerminus() destroys the logging component.
705 %
706 %  The format of the LogComponentTerminus method is:
707 %
708 %      LogComponentTerminus(void)
709 %
710 */
711
712 static void *DestroyLogElement(void *log_info)
713 {
714   register LogInfo
715     *p;
716
717   p=(LogInfo *) log_info;
718   if (p->file != (FILE *) NULL)
719     {
720       if (p->append == MagickFalse)
721         (void) fprintf(p->file,"</log>\n");
722       (void) fclose(p->file);
723       p->file=(FILE *) NULL;
724     }
725   if (p->exempt == MagickFalse)
726     {
727       if (p->format != (char *) NULL)
728         p->format=DestroyString(p->format);
729       if (p->path != (char *) NULL)
730         p->path=DestroyString(p->path);
731       if (p->filename != (char *) NULL)
732         p->filename=DestroyString(p->filename);
733     }
734   p=(LogInfo *) RelinquishMagickMemory(p);
735   return((void *) NULL);
736 }
737
738 MagickExport void LogComponentTerminus(void)
739 {
740   if (log_semaphore == (SemaphoreInfo *) NULL)
741     AcquireSemaphoreInfo(&log_semaphore);
742   LockSemaphoreInfo(log_semaphore);
743   if (log_list != (LinkedListInfo *) NULL)
744     log_list=DestroyLinkedList(log_list,DestroyLogElement);
745   instantiate_log=MagickFalse;
746   UnlockSemaphoreInfo(log_semaphore);
747   DestroySemaphoreInfo(&log_semaphore);
748 }
749 \f
750 /*
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
752 %                                                                             %
753 %                                                                             %
754 %                                                                             %
755 %   L o g M a g i c k E v e n t                                               %
756 %                                                                             %
757 %                                                                             %
758 %                                                                             %
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 %
761 %  LogMagickEvent() logs an event as determined by the log configuration file.
762 %  If an error occurs, MagickFalse is returned otherwise MagickTrue.
763 %
764 %  The format of the LogMagickEvent method is:
765 %
766 %      MagickBooleanType LogMagickEvent(const LogEventType type,
767 %        const char *module,const char *function,const size_t line,
768 %        const char *format,...)
769 %
770 %  A description of each parameter follows:
771 %
772 %    o type: the event type.
773 %
774 %    o filename: the source module filename.
775 %
776 %    o function: the function name.
777 %
778 %    o line: the line number of the source module.
779 %
780 %    o format: the output format.
781 %
782 */
783 static char *TranslateEvent(const LogEventType magick_unused(type),
784   const char *module,const char *function,const size_t line,
785   const char *domain,const char *event)
786 {
787   char
788     *text;
789
790   double
791     elapsed_time,
792     user_time;
793
794   ExceptionInfo
795     *exception;
796
797   LogInfo
798     *log_info;
799
800   register char
801     *q;
802
803   register const char
804     *p;
805
806   size_t
807     extent;
808
809   time_t
810     seconds;
811
812   exception=AcquireExceptionInfo();
813   log_info=(LogInfo *) GetLogInfo("*",exception);
814   exception=DestroyExceptionInfo(exception);
815   seconds=time((time_t *) NULL);
816   elapsed_time=GetElapsedTime(&log_info->timer);
817   user_time=GetUserTime(&log_info->timer);
818   text=AcquireString(event);
819   if (log_info->format == (char *) NULL)
820     return(text);
821   extent=strlen(event)+MaxTextExtent;
822   if (LocaleCompare(log_info->format,"xml") == 0)
823     {
824       char
825         timestamp[MaxTextExtent];
826
827       /*
828         Translate event in "XML" format.
829       */
830       (void) FormatMagickTime(seconds,extent,timestamp);
831       (void) FormatMagickString(text,extent,
832         "<entry>\n"
833         "  <timestamp>%s</timestamp>\n"
834         "  <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
835         "  <user-time>%0.3f</user-time>\n"
836         "  <process-id>%.20g</process-id>\n"
837         "  <thread-id>%.20g</thread-id>\n"
838         "  <module>%s</module>\n"
839         "  <function>%s</function>\n"
840         "  <line>%.20g</line>\n"
841         "  <domain>%s</domain>\n"
842         "  <event>%s</event>\n"
843         "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
844         (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
845         (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
846         (double) getpid(),(double) GetMagickThreadSignature(),module,function,
847         (double) line,domain,event);
848       return(text);
849     }
850   /*
851     Translate event in "human readable" format.
852   */
853   q=text;
854   for (p=log_info->format; *p != '\0'; p++)
855   {
856     *q='\0';
857     if ((size_t) (q-text+MaxTextExtent) >= extent)
858       {
859         extent+=MaxTextExtent;
860         text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
861           sizeof(*text));
862         if (text == (char *) NULL)
863           return((char *) NULL);
864         q=text+strlen(text);
865       }
866     /*
867       The format of the log is defined by embedding special format characters:
868
869         %c   client name
870         %d   domain
871         %e   event
872         %f   function
873         %g   generation
874         %l   line
875         %m   module
876         %n   log name
877         %p   process id
878         %r   real CPU time
879         %t   wall clock time
880         %u   user CPU time
881         %v   version
882         %%   percent sign
883         \n   newline
884         \r   carriage return
885     */
886     if ((*p == '\\') && (*(p+1) == 'r'))
887       {
888         *q++='\r';
889         p++;
890         continue;
891       }
892     if ((*p == '\\') && (*(p+1) == 'n'))
893       {
894         *q++='\n';
895         p++;
896         continue;
897       }
898     if (*p != '%')
899       {
900         *q++=(*p);
901         continue;
902       }
903     p++;
904     switch (*p)
905     {
906       case 'c':
907       {
908         q+=CopyMagickString(q,GetClientName(),extent);
909         break;
910       }
911       case 'd':
912       {
913         q+=CopyMagickString(q,domain,extent);
914         break;
915       }
916       case 'e':
917       {
918         q+=CopyMagickString(q,event,extent);
919         break;
920       }
921       case 'f':
922       {
923         q+=CopyMagickString(q,function,extent);
924         break;
925       }
926       case 'g':
927       {
928         if (log_info->generations == 0)
929           {
930             (void) CopyMagickString(q,"0",extent);
931             q++;
932             break;
933           }
934         q+=FormatMagickString(q,extent,"%.20g",(double) (log_info->generation %
935           log_info->generations));
936         break;
937       }
938       case 'l':
939       {
940         q+=FormatMagickString(q,extent,"%.20g",(double) line);
941         break;
942       }
943       case 'm':
944       {
945         register const char
946           *p;
947
948         for (p=module+strlen(module)-1; p > module; p--)
949           if (*p == *DirectorySeparator)
950             {
951               p++;
952               break;
953             }
954         q+=CopyMagickString(q,p,extent);
955         break;
956       }
957       case 'n':
958       {
959         q+=CopyMagickString(q,GetLogName(),extent);
960         break;
961       }
962       case 'p':
963       {
964         q+=FormatMagickString(q,extent,"%.20g",(double) getpid());
965         break;
966       }
967       case 'r':
968       {
969         q+=FormatMagickString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
970           (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
971           (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
972         break;
973       }
974       case 't':
975       {
976         q+=FormatMagickTime(seconds,extent,q);
977         break;
978       }
979       case 'u':
980       {
981         q+=FormatMagickString(q,extent,"%0.3fu",user_time);
982         break;
983       }
984       case 'v':
985       {
986         q+=CopyMagickString(q,MagickLibVersionText,extent);
987         break;
988       }
989       case '%':
990       {
991         *q++=(*p);
992         break;
993       }
994       default:
995       {
996         *q++='%';
997         *q++=(*p);
998         break;
999       }
1000     }
1001   }
1002   *q='\0';
1003   return(text);
1004 }
1005
1006 static char *TranslateFilename(const LogInfo *log_info)
1007 {
1008   char
1009     *filename;
1010
1011   register char
1012     *q;
1013
1014   register const char
1015     *p;
1016
1017   size_t
1018     extent;
1019
1020   /*
1021     Translate event in "human readable" format.
1022   */
1023   assert(log_info != (LogInfo *) NULL);
1024   assert(log_info->filename != (char *) NULL);
1025   filename=AcquireString((char *) NULL);
1026   extent=MaxTextExtent;
1027   q=filename;
1028   for (p=log_info->filename; *p != '\0'; p++)
1029   {
1030     *q='\0';
1031     if ((size_t) (q-filename+MaxTextExtent) >= extent)
1032       {
1033         extent+=MaxTextExtent;
1034         filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1035           sizeof(*filename));
1036         if (filename == (char *) NULL)
1037           return((char *) NULL);
1038         q=filename+strlen(filename);
1039       }
1040     /*
1041       The format of the filename is defined by embedding special format
1042       characters:
1043
1044         %c   client name
1045         %n   log name
1046         %p   process id
1047         %v   version
1048         %%   percent sign
1049     */
1050     if (*p != '%')
1051       {
1052         *q++=(*p);
1053         continue;
1054       }
1055     p++;
1056     switch (*p)
1057     {
1058       case 'c':
1059       {
1060         q+=CopyMagickString(q,GetClientName(),extent);
1061         break;
1062       }
1063       case 'g':
1064       {
1065         if (log_info->generations == 0)
1066           {
1067             (void) CopyMagickString(q,"0",extent);
1068             q++;
1069             break;
1070           }
1071         q+=FormatMagickString(q,extent,"%.20g",(double) (log_info->generation %
1072           log_info->generations));
1073         break;
1074       }
1075       case 'n':
1076       {
1077         q+=CopyMagickString(q,GetLogName(),extent);
1078         break;
1079       }
1080       case 'p':
1081       {
1082         q+=FormatMagickString(q,extent,"%.20g",(double) getpid());
1083         break;
1084       }
1085       case 'v':
1086       {
1087         q+=CopyMagickString(q,MagickLibVersionText,extent);
1088         break;
1089       }
1090       case '%':
1091       {
1092         *q++=(*p);
1093         break;
1094       }
1095       default:
1096       {
1097         *q++='%';
1098         *q++=(*p);
1099         break;
1100       }
1101     }
1102   }
1103   *q='\0';
1104   return(filename);
1105 }
1106
1107 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1108   const char *function,const size_t line,const char *format,
1109   va_list operands)
1110 {
1111   char
1112     event[MaxTextExtent],
1113     *text;
1114
1115   const char
1116     *domain;
1117
1118   ExceptionInfo
1119     *exception;
1120
1121   int
1122     n;
1123
1124   LogInfo
1125     *log_info;
1126
1127   if (IsEventLogging() == MagickFalse)
1128     return(MagickFalse);
1129   exception=AcquireExceptionInfo();
1130   log_info=(LogInfo *) GetLogInfo("*",exception);
1131   exception=DestroyExceptionInfo(exception);
1132   LockSemaphoreInfo(log_semaphore);
1133   if ((log_info->event_mask & type) == 0)
1134     {
1135       UnlockSemaphoreInfo(log_semaphore);
1136       return(MagickTrue);
1137     }
1138   domain=MagickOptionToMnemonic(MagickLogEventOptions,type);
1139 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1140   n=vsnprintf(event,MaxTextExtent,format,operands);
1141 #else
1142   n=vsprintf(event,format,operands);
1143 #endif
1144   if (n < 0)
1145     event[MaxTextExtent-1]='\0';
1146   text=TranslateEvent(type,module,function,line,domain,event);
1147   if (text == (char *) NULL)
1148     {
1149       (void) ContinueTimer((TimerInfo *) &log_info->timer);
1150       UnlockSemaphoreInfo(log_semaphore);
1151       return(MagickFalse);
1152     }
1153   if ((log_info->handler_mask & ConsoleHandler) != 0)
1154     {
1155       (void) fprintf(stderr,"%s\n",text);
1156       (void) fflush(stderr);
1157     }
1158   if ((log_info->handler_mask & DebugHandler) != 0)
1159     {
1160 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1161       OutputDebugString(text);
1162 #endif
1163     }
1164   if ((log_info->handler_mask & EventHandler) != 0)
1165     {
1166 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1167       (void) NTReportEvent(text,MagickFalse);
1168 #endif
1169     }
1170   if ((log_info->handler_mask & FileHandler) != 0)
1171     {
1172       struct stat
1173         file_info;
1174
1175       file_info.st_size=0;
1176       if (log_info->file != (FILE *) NULL)
1177         (void) fstat(fileno(log_info->file),&file_info);
1178       if (file_info.st_size > (off_t) (1024*1024*log_info->limit))
1179         {
1180           (void) fprintf(log_info->file,"</log>\n");
1181           (void) fclose(log_info->file);
1182           log_info->file=(FILE *) NULL;
1183         }
1184       if (log_info->file == (FILE *) NULL)
1185         {
1186           char
1187             *filename;
1188
1189           filename=TranslateFilename(log_info);
1190           if (filename == (char *) NULL)
1191             {
1192               (void) ContinueTimer((TimerInfo *) &log_info->timer);
1193               UnlockSemaphoreInfo(log_semaphore);
1194               return(MagickFalse);
1195             }
1196           log_info->append=IsPathAccessible(filename);
1197           log_info->file=OpenMagickStream(filename,"ab");
1198           filename=(char  *) RelinquishMagickMemory(filename);
1199           if (log_info->file == (FILE *) NULL)
1200             {
1201               UnlockSemaphoreInfo(log_semaphore);
1202               return(MagickFalse);
1203             }
1204           log_info->generation++;
1205           if (log_info->append == MagickFalse)
1206             {
1207               (void) fprintf(log_info->file,"<?xml version=\"1.0\" "
1208                 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1209               (void) fprintf(log_info->file,"<log>\n");
1210             }
1211         }
1212       (void) fprintf(log_info->file,"%s\n",text);
1213       (void) fflush(log_info->file);
1214     }
1215   if ((log_info->handler_mask & StdoutHandler) != 0)
1216     {
1217       (void) fprintf(stdout,"%s\n",text);
1218       (void) fflush(stdout);
1219     }
1220   if ((log_info->handler_mask & StderrHandler) != 0)
1221     {
1222       (void) fprintf(stderr,"%s\n",text);
1223       (void) fflush(stderr);
1224     }
1225   text=(char  *) RelinquishMagickMemory(text);
1226   (void) ContinueTimer((TimerInfo *) &log_info->timer);
1227   UnlockSemaphoreInfo(log_semaphore);
1228   return(MagickTrue);
1229 }
1230
1231 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1232   const char *function,const size_t line,const char *format,...)
1233 {
1234   va_list
1235     operands;
1236
1237   MagickBooleanType
1238     status;
1239
1240   va_start(operands,format);
1241   status=LogMagickEventList(type,module,function,line,format,operands);
1242   va_end(operands);
1243   return(status);
1244 }
1245 \f
1246 /*
1247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1248 %                                                                             %
1249 %                                                                             %
1250 %                                                                             %
1251 +   L o a d L o g L i s t                                                     %
1252 %                                                                             %
1253 %                                                                             %
1254 %                                                                             %
1255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 %
1257 %  LoadLogList() loads the log configuration file which provides a
1258 %  mapping between log attributes and log name.
1259 %
1260 %  The format of the LoadLogList method is:
1261 %
1262 %      MagickBooleanType LoadLogList(const char *xml,const char *filename,
1263 %        const size_t depth,ExceptionInfo *exception)
1264 %
1265 %  A description of each parameter follows:
1266 %
1267 %    o xml:  The log list in XML format.
1268 %
1269 %    o filename:  The log list filename.
1270 %
1271 %    o depth: depth of <include /> statements.
1272 %
1273 %    o exception: return any errors or warnings in this structure.
1274 %
1275 */
1276 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1277   const size_t depth,ExceptionInfo *exception)
1278 {
1279   char
1280     keyword[MaxTextExtent],
1281     *token;
1282
1283   const char
1284     *q;
1285
1286   LogInfo
1287     *log_info = (LogInfo *) NULL;
1288
1289   MagickStatusType
1290     status;
1291
1292   /*
1293     Load the log map file.
1294   */
1295   if (xml == (const char *) NULL)
1296     return(MagickFalse);
1297   if (log_list == (LinkedListInfo *) NULL)
1298     {
1299       log_list=NewLinkedList(0);
1300       if (log_list == (LinkedListInfo *) NULL)
1301         {
1302           ThrowFileException(exception,ResourceLimitError,
1303             "MemoryAllocationFailed",filename);
1304           return(MagickFalse);
1305         }
1306     }
1307   status=MagickTrue;
1308   token=AcquireString((const char *) xml);
1309   for (q=(const char *) xml; *q != '\0'; )
1310   {
1311     /*
1312       Interpret XML.
1313     */
1314     GetMagickToken(q,&q,token);
1315     if (*token == '\0')
1316       break;
1317     (void) CopyMagickString(keyword,token,MaxTextExtent);
1318     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1319       {
1320         /*
1321           Doctype element.
1322         */
1323         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1324           GetMagickToken(q,&q,token);
1325         continue;
1326       }
1327     if (LocaleNCompare(keyword,"<!--",4) == 0)
1328       {
1329         /*
1330           Comment element.
1331         */
1332         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1333           GetMagickToken(q,&q,token);
1334         continue;
1335       }
1336     if (LocaleCompare(keyword,"<include") == 0)
1337       {
1338         /*
1339           Include element.
1340         */
1341         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1342         {
1343           (void) CopyMagickString(keyword,token,MaxTextExtent);
1344           GetMagickToken(q,&q,token);
1345           if (*token != '=')
1346             continue;
1347           GetMagickToken(q,&q,token);
1348           if (LocaleCompare(keyword,"file") == 0)
1349             {
1350               if (depth > 200)
1351                 (void) ThrowMagickException(exception,GetMagickModule(),
1352                   ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1353               else
1354                 {
1355                   char
1356                     path[MaxTextExtent],
1357                     *xml;
1358
1359                   GetPathComponent(filename,HeadPath,path);
1360                   if (*path != '\0')
1361                     (void) ConcatenateMagickString(path,DirectorySeparator,
1362                       MaxTextExtent);
1363                   if (*token == *DirectorySeparator)
1364                     (void) CopyMagickString(path,token,MaxTextExtent);
1365                   else
1366                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
1367                   xml=FileToString(path,~0,exception);
1368                   if (xml != (char *) NULL)
1369                     {
1370                       status|=LoadLogList(xml,path,depth+1,exception);
1371                       xml=DestroyString(xml);
1372                     }
1373                 }
1374             }
1375         }
1376         continue;
1377       }
1378     if (LocaleCompare(keyword,"<logmap>") == 0)
1379       {
1380         /*
1381           Allocate memory for the log list.
1382         */
1383         log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1384         if (log_info == (LogInfo *) NULL)
1385           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1386         (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1387         log_info->path=ConstantString(filename);
1388         GetTimerInfo((TimerInfo *) &log_info->timer);
1389         log_info->exempt=MagickFalse;
1390         log_info->signature=MagickSignature;
1391         continue;
1392       }
1393     if (log_info == (LogInfo *) NULL)
1394       continue;
1395     if (LocaleCompare(keyword,"</logmap>") == 0)
1396       {
1397         status=AppendValueToLinkedList(log_list,log_info);
1398         if (status == MagickFalse)
1399           (void) ThrowMagickException(exception,GetMagickModule(),
1400             ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1401         log_info=(LogInfo *) NULL;
1402       }
1403     GetMagickToken(q,(const char **) NULL,token);
1404     if (*token != '=')
1405       continue;
1406     GetMagickToken(q,&q,token);
1407     GetMagickToken(q,&q,token);
1408     switch (*keyword)
1409     {
1410       case 'E':
1411       case 'e':
1412       {
1413         if (LocaleCompare((char *) keyword,"events") == 0)
1414           {
1415             log_info->event_mask=(LogEventType) (log_info->event_mask |
1416               ParseMagickOption(MagickLogEventOptions,MagickTrue,token));
1417             break;
1418           }
1419         break;
1420       }
1421       case 'F':
1422       case 'f':
1423       {
1424         if (LocaleCompare((char *) keyword,"filename") == 0)
1425           {
1426             if (log_info->filename != (char *) NULL)
1427               log_info->filename=(char *)
1428                 RelinquishMagickMemory(log_info->filename);
1429             log_info->filename=ConstantString(token);
1430             break;
1431           }
1432         if (LocaleCompare((char *) keyword,"format") == 0)
1433           {
1434             if (log_info->format != (char *) NULL)
1435               log_info->format=(char *)
1436                 RelinquishMagickMemory(log_info->format);
1437             log_info->format=ConstantString(token);
1438             break;
1439           }
1440         break;
1441       }
1442       case 'G':
1443       case 'g':
1444       {
1445         if (LocaleCompare((char *) keyword,"generations") == 0)
1446           {
1447             if (LocaleCompare(token,"unlimited") == 0)
1448               {
1449                 log_info->generations=(~0UL);
1450                 break;
1451               }
1452             log_info->generations=StringToUnsignedLong(token);
1453             break;
1454           }
1455         break;
1456       }
1457       case 'L':
1458       case 'l':
1459       {
1460         if (LocaleCompare((char *) keyword,"limit") == 0)
1461           {
1462             if (LocaleCompare(token,"unlimited") == 0)
1463               {
1464                 log_info->limit=(~0UL);
1465                 break;
1466               }
1467             log_info->limit=StringToUnsignedLong(token);
1468             break;
1469           }
1470         break;
1471       }
1472       case 'O':
1473       case 'o':
1474       {
1475         if (LocaleCompare((char *) keyword,"output") == 0)
1476           {
1477             log_info->handler_mask=(LogHandlerType)
1478               (log_info->handler_mask | ParseLogHandlers(token));
1479             break;
1480           }
1481         break;
1482       }
1483       default:
1484         break;
1485     }
1486   }
1487   token=DestroyString(token);
1488   if (log_list == (LinkedListInfo *) NULL)
1489     return(MagickFalse);
1490   return(status != 0 ? MagickTrue : MagickFalse);
1491 }
1492 \f
1493 /*
1494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1495 %                                                                             %
1496 %                                                                             %
1497 %                                                                             %
1498 %  L o a d L o g L i s t s                                                    %
1499 %                                                                             %
1500 %                                                                             %
1501 %                                                                             %
1502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 %
1504 %  LoadLogLists() loads one or more log configuration file which provides a
1505 %  mapping between log attributes and log name.
1506 %
1507 %  The format of the LoadLogLists method is:
1508 %
1509 %      MagickBooleanType LoadLogLists(const char *filename,
1510 %        ExceptionInfo *exception)
1511 %
1512 %  A description of each parameter follows:
1513 %
1514 %    o filename: the log configuration filename.
1515 %
1516 %    o exception: return any errors or warnings in this structure.
1517 %
1518 */
1519 static MagickBooleanType LoadLogLists(const char *filename,
1520   ExceptionInfo *exception)
1521 {
1522   const StringInfo
1523     *option;
1524
1525   LinkedListInfo
1526     *options;
1527
1528   MagickStatusType
1529     status;
1530
1531   register ssize_t
1532     i;
1533
1534   /*
1535     Load built-in log map.
1536   */
1537   status=MagickFalse;
1538   if (log_list == (LinkedListInfo *) NULL)
1539     {
1540       log_list=NewLinkedList(0);
1541       if (log_list == (LinkedListInfo *) NULL)
1542         {
1543           ThrowFileException(exception,ResourceLimitError,
1544             "MemoryAllocationFailed",filename);
1545           return(MagickFalse);
1546         }
1547     }
1548   for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1549   {
1550     LogInfo
1551       *log_info;
1552
1553     register const LogMapInfo
1554       *p;
1555
1556     p=LogMap+i;
1557     log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1558     if (log_info == (LogInfo *) NULL)
1559       {
1560         (void) ThrowMagickException(exception,GetMagickModule(),
1561           ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1562         continue;
1563       }
1564     (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1565     log_info->path=(char *) "[built-in]";
1566     GetTimerInfo((TimerInfo *) &log_info->timer);
1567     log_info->event_mask=p->event_mask;
1568     log_info->handler_mask=p->handler_mask;
1569     log_info->filename=ConstantString(p->filename);
1570     log_info->format=ConstantString(p->format);
1571     log_info->exempt=MagickTrue;
1572     log_info->signature=MagickSignature;
1573     status=AppendValueToLinkedList(log_list,log_info);
1574     if (status == MagickFalse)
1575       (void) ThrowMagickException(exception,GetMagickModule(),
1576         ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1577   }
1578   /*
1579     Load external log map.
1580   */
1581   options=GetConfigureOptions(filename,exception);
1582   option=(const StringInfo *) GetNextValueInLinkedList(options);
1583   while (option != (const StringInfo *) NULL)
1584   {
1585     status|=LoadLogList((const char *) GetStringInfoDatum(option),
1586       GetStringInfoPath(option),0,exception);
1587     option=(const StringInfo *) GetNextValueInLinkedList(options);
1588   }
1589   options=DestroyConfigureOptions(options);
1590   return(status != 0 ? MagickTrue : MagickFalse);
1591 }
1592 \f
1593 /*
1594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1595 %                                                                             %
1596 %                                                                             %
1597 %                                                                             %
1598 +   P a r s e L o g H a n d l e r s                                           %
1599 %                                                                             %
1600 %                                                                             %
1601 %                                                                             %
1602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603 %
1604 %  ParseLogHandlers() parses a string defining which handlers takes a log
1605 %  message and exports them.
1606 %
1607 %  The format of the ParseLogHandlers method is:
1608 %
1609 %      LogHandlerType ParseLogHandlers(const char *handlers)
1610 %
1611 %  A description of each parameter follows:
1612 %
1613 %    o handlers: one or more handlers separated by commas.
1614 %
1615 */
1616 static LogHandlerType ParseLogHandlers(const char *handlers)
1617 {
1618   LogHandlerType
1619     handler_mask;
1620
1621   register const char
1622     *p;
1623
1624   register ssize_t
1625     i;
1626
1627   size_t
1628     length;
1629
1630   handler_mask=NoHandler;
1631   for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1632   {
1633     while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1634            (*p == ',')))
1635       p++;
1636     for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1637     {
1638       length=strlen(LogHandlers[i].name);
1639       if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1640         {
1641           handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1642           break;
1643         }
1644     }
1645     if (LogHandlers[i].name == (char *) NULL)
1646       return(UndefinedHandler);
1647   }
1648   return(handler_mask);
1649 }
1650 \f
1651 /*
1652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1653 %                                                                             %
1654 %                                                                             %
1655 %                                                                             %
1656 %   S e t L o g E v e n t M a s k                                             %
1657 %                                                                             %
1658 %                                                                             %
1659 %                                                                             %
1660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1661 %
1662 %  SetLogEventMask() accepts a list that determines which events to log.  All
1663 %  other events are ignored.  By default, no debug is enabled.  This method
1664 %  returns the previous log event mask.
1665 %
1666 %  The format of the SetLogEventMask method is:
1667 %
1668 %      LogEventType SetLogEventMask(const char *events)
1669 %
1670 %  A description of each parameter follows:
1671 %
1672 %    o events: log these events.
1673 %
1674 */
1675 MagickExport LogEventType SetLogEventMask(const char *events)
1676 {
1677   ExceptionInfo
1678     *exception;
1679
1680   LogInfo
1681     *log_info;
1682
1683   ssize_t
1684     option;
1685
1686   exception=AcquireExceptionInfo();
1687   log_info=(LogInfo *) GetLogInfo("*",exception);
1688   exception=DestroyExceptionInfo(exception);
1689   option=ParseMagickOption(MagickLogEventOptions,MagickTrue,events);
1690   LockSemaphoreInfo(log_semaphore);
1691   log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1692   log_info->event_mask=(LogEventType) option;
1693   if (option == -1)
1694     log_info->event_mask=UndefinedEvents;
1695   UnlockSemaphoreInfo(log_semaphore);
1696   return(log_info->event_mask);
1697 }
1698 \f
1699 /*
1700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1701 %                                                                             %
1702 %                                                                             %
1703 %                                                                             %
1704 %   S e t L o g F o r m a t                                                   %
1705 %                                                                             %
1706 %                                                                             %
1707 %                                                                             %
1708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1709 %
1710 %  SetLogFormat() sets the format for the "human readable" log record.
1711 %
1712 %  The format of the LogMagickFormat method is:
1713 %
1714 %      SetLogFormat(const char *format)
1715 %
1716 %  A description of each parameter follows:
1717 %
1718 %    o format: the log record format.
1719 %
1720 */
1721 MagickExport void SetLogFormat(const char *format)
1722 {
1723   LogInfo
1724     *log_info;
1725
1726   ExceptionInfo
1727     *exception;
1728
1729   exception=AcquireExceptionInfo();
1730   log_info=(LogInfo *) GetLogInfo("*",exception);
1731   exception=DestroyExceptionInfo(exception);
1732   LockSemaphoreInfo(log_semaphore);
1733   if (log_info->format != (char *) NULL)
1734     log_info->format=DestroyString(log_info->format);
1735   log_info->format=ConstantString(format);
1736   UnlockSemaphoreInfo(log_semaphore);
1737 }
1738 \f
1739 /*
1740 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1741 %                                                                             %
1742 %                                                                             %
1743 %                                                                             %
1744 %   S e t L o g N a m e                                                       %
1745 %                                                                             %
1746 %                                                                             %
1747 %                                                                             %
1748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1749 %
1750 %  SetLogName() sets the log name and returns it.
1751 %
1752 %  The format of the SetLogName method is:
1753 %
1754 %      const char *SetLogName(const char *name)
1755 %
1756 %  A description of each parameter follows:
1757 %
1758 %    o log_name: SetLogName() returns the current client name.
1759 %
1760 %    o name: Specifies the new client name.
1761 %
1762 */
1763 MagickExport const char *SetLogName(const char *name)
1764 {
1765   if ((name != (char *) NULL) && (*name != '\0'))
1766     (void) CopyMagickString(log_name,name,MaxTextExtent);
1767   return(log_name);
1768 }