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