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