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