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