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