MagickCore 7.1.2-33
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/license/ %
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/resource-private.h"
57#include "MagickCore/semaphore.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.h"
63#include "MagickCore/timer-private.h"
64#include "MagickCore/token.h"
65#include "MagickCore/utility.h"
66#include "MagickCore/utility-private.h"
67#include "MagickCore/version.h"
68#include "MagickCore/xml-tree.h"
69#include "MagickCore/xml-tree-private.h"
70␌
71/*
72 Define declarations.
73*/
74#define LogFilename "log.xml"
75␌
76/*
77 Typedef declarations.
78*/
79typedef enum
80{
81 UndefinedHandler = 0x0000,
82 NoHandler = 0x0000,
83 ConsoleHandler = 0x0001,
84 StdoutHandler = 0x0002,
85 StderrHandler = 0x0004,
86 FileHandler = 0x0008,
87 DebugHandler = 0x0010,
88 EventHandler = 0x0020,
89 MethodHandler = 0x0040
90} LogHandlerType;
91
92typedef struct _EventInfo
93{
94 char
95 *name;
96
97 LogEventType
98 event;
99} EventInfo;
100
101typedef struct _HandlerInfo
102{
103 const char
104 name[10];
105
106 LogHandlerType
107 handler;
108} HandlerInfo;
109
111{
112 LogEventType
113 event_mask;
114
115 LogHandlerType
116 handler_mask;
117
118 char
119 *path,
120 *name,
121 *filename,
122 *format;
123
124 size_t
125 generations;
126
127 FILE
128 *file;
129
130 MagickBooleanType
131 append,
132 stealth;
133
134 MagickSizeType
135 limit;
136
137 TimerInfo
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{
271 LinkedListInfo
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
290 LinkedListInfo
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{
363 ExceptionInfo
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 if (log_info == (LogInfo *) NULL)
375 return;
376 LockSemaphoreInfo(log_semaphore);
377 if ((log_info != (LogInfo *) NULL) && (log_info->file != (FILE *) NULL))
378 {
379 (void) FormatLocaleFile(log_info->file,"</log>\n");
380 (void) fclose(log_info->file);
381 log_info->file=(FILE *) NULL;
382 }
383 UnlockSemaphoreInfo(log_semaphore);
384}
385␌
386/*
387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388% %
389% %
390% %
391% G e t L o g E v e n t M a s k %
392% %
393% %
394% %
395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396%
397% GetLogEventMask() returns the current log event mask.
398%
399% The format of the GetLogEventMask method is:
400%
401% LogEventType GetLogEventMask(void)
402%
403*/
404MagickExport LogEventType GetLogEventMask(void)
405{
406 ExceptionInfo
407 *exception;
408
409 LogInfo
410 *log_info;
411
412 exception=AcquireExceptionInfo();
413 log_info=GetLogInfo("*",exception);
414 exception=DestroyExceptionInfo(exception);
415 if (log_info == (const LogInfo *) NULL)
416 return(NoEvents);
417 return(log_info->event_mask);
418}
419␌
420/*
421%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422% %
423% %
424% %
425+ G e t L o g I n f o %
426% %
427% %
428% %
429%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
430%
431% GetLogInfo() searches the log list for the specified name and if found
432% returns attributes for that log.
433%
434% The format of the GetLogInfo method is:
435%
436% LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
437%
438% A description of each parameter follows:
439%
440% o name: the log name.
441%
442% o exception: return any errors or warnings in this structure.
443%
444*/
445static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
446{
447 LogInfo
448 *log_info;
449
450 ElementInfo
451 *p;
452
453 assert(exception != (ExceptionInfo *) NULL);
454 if (IsLogCacheInstantiated(exception) == MagickFalse)
455 return((LogInfo *) NULL);
456 /*
457 Search for log tag.
458 */
459 log_info=(LogInfo *) NULL;
460 LockSemaphoreInfo(log_semaphore);
461 p=GetHeadElementInLinkedList(log_cache);
462 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
463 {
464 if (p != (ElementInfo *) NULL)
465 log_info=(LogInfo *) p->value;
466 UnlockSemaphoreInfo(log_semaphore);
467 return(log_info);
468 }
469 while (p != (ElementInfo *) NULL)
470 {
471 log_info=(LogInfo* ) p->value;
472 if (LocaleCompare(name,log_info->name) == 0)
473 break;
474 p=p->next;
475 }
476 if (p == (ElementInfo *) NULL)
477 log_info=(LogInfo *) NULL;
478 else
479 SetHeadElementInLinkedList(log_cache,p);
480 UnlockSemaphoreInfo(log_semaphore);
481 return(log_info);
482}
483␌
484/*
485%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486% %
487% %
488% %
489% G e t L o g I n f o L i s t %
490% %
491% %
492% %
493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494%
495% GetLogInfoList() returns any logs that match the specified pattern.
496%
497% The format of the GetLogInfoList function is:
498%
499% const LogInfo **GetLogInfoList(const char *pattern,
500% size_t *number_preferences,ExceptionInfo *exception)
501%
502% A description of each parameter follows:
503%
504% o pattern: Specifies a pointer to a text string containing a pattern.
505%
506% o number_preferences: This integer returns the number of logs in the list.
507%
508% o exception: return any errors or warnings in this structure.
509%
510*/
511#if defined(__cplusplus) || defined(c_plusplus)
512extern "C" {
513#endif
514
515static int LogInfoCompare(const void *x,const void *y)
516{
517 const LogInfo
518 **p,
519 **q;
520
521 p=(const LogInfo **) x,
522 q=(const LogInfo **) y;
523 if (LocaleCompare((*p)->path,(*q)->path) == 0)
524 return(LocaleCompare((*p)->name,(*q)->name));
525 return(LocaleCompare((*p)->path,(*q)->path));
526}
527
528#if defined(__cplusplus) || defined(c_plusplus)
529}
530#endif
531
532MagickExport const LogInfo **GetLogInfoList(const char *pattern,
533 size_t *number_preferences,ExceptionInfo *exception)
534{
535 const LogInfo
536 **preferences;
537
538 ElementInfo
539 *p;
540
541 ssize_t
542 i;
543
544 assert(pattern != (char *) NULL);
545 assert(number_preferences != (size_t *) NULL);
546 if (IsEventLogging() != MagickFalse)
547 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
548 *number_preferences=0;
549 if (IsLogCacheInstantiated(exception) == MagickFalse)
550 return((const LogInfo **) NULL);
551 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
552 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
553 if (preferences == (const LogInfo **) NULL)
554 return((const LogInfo **) NULL);
555 LockSemaphoreInfo(log_semaphore);
556 p=GetHeadElementInLinkedList(log_cache);
557 for (i=0; p != (ElementInfo *) NULL; )
558 {
559 const LogInfo
560 *log_info;
561
562 log_info=(const LogInfo *) p->value;
563 if ((log_info->stealth == MagickFalse) &&
564 (GlobExpression(log_info->name,pattern,MagickFalse) != MagickFalse))
565 preferences[i++]=log_info;
566 p=p->next;
567 }
568 UnlockSemaphoreInfo(log_semaphore);
569 if (i == 0)
570 preferences=(const LogInfo **) RelinquishMagickMemory((void*) preferences);
571 else
572 {
573 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
574 preferences[i]=(LogInfo *) NULL;
575 }
576 *number_preferences=(size_t) i;
577 return(preferences);
578}
579␌
580/*
581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582% %
583% %
584% %
585% G e t L o g L i s t %
586% %
587% %
588% %
589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590%
591% GetLogList() returns any logs that match the specified pattern.
592%
593% The format of the GetLogList function is:
594%
595% char **GetLogList(const char *pattern,size_t *number_preferences,
596% ExceptionInfo *exception)
597%
598% A description of each parameter follows:
599%
600% o pattern: Specifies a pointer to a text string containing a pattern.
601%
602% o number_preferences: This integer returns the number of logs in the list.
603%
604% o exception: return any errors or warnings in this structure.
605%
606*/
607
608#if defined(__cplusplus) || defined(c_plusplus)
609extern "C" {
610#endif
611
612static int LogCompare(const void *x,const void *y)
613{
614 const char
615 **p,
616 **q;
617
618 p=(const char **) x;
619 q=(const char **) y;
620 return(LocaleCompare(*p,*q));
621}
622
623#if defined(__cplusplus) || defined(c_plusplus)
624}
625#endif
626
627MagickExport char **GetLogList(const char *pattern,size_t *number_preferences,
628 ExceptionInfo *exception)
629{
630 char
631 **preferences;
632
633 ElementInfo
634 *p;
635
636 ssize_t
637 i;
638
639 assert(pattern != (char *) NULL);
640 assert(number_preferences != (size_t *) NULL);
641 if (IsEventLogging() != MagickFalse)
642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
643 *number_preferences=0;
644 if (IsLogCacheInstantiated(exception) == MagickFalse)
645 return((char **) NULL);
646 preferences=(char **) AcquireQuantumMemory((size_t)
647 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
648 if (preferences == (char **) NULL)
649 return((char **) NULL);
650 LockSemaphoreInfo(log_semaphore);
651 p=GetHeadElementInLinkedList(log_cache);
652 for (i=0; p != (ElementInfo *) NULL; )
653 {
654 const LogInfo
655 *log_info;
656
657 log_info=(const LogInfo *) p->value;
658 if ((log_info->stealth == MagickFalse) &&
659 (GlobExpression(log_info->name,pattern,MagickFalse) != MagickFalse))
660 preferences[i++]=ConstantString(log_info->name);
661 p=p->next;
662 }
663 UnlockSemaphoreInfo(log_semaphore);
664 if (i == 0)
665 preferences=(char **) RelinquishMagickMemory(preferences);
666 else
667 {
668 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
669 preferences[i]=(char *) NULL;
670 }
671 *number_preferences=(size_t) i;
672 return(preferences);
673}
674␌
675/*
676%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677% %
678% %
679% %
680% G e t L o g N a m e %
681% %
682% %
683% %
684%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685%
686% GetLogName() returns the current log name.
687%
688% The format of the GetLogName method is:
689%
690% const char *GetLogName(void)
691%
692*/
693MagickExport const char *GetLogName(void)
694{
695 return(log_name);
696}
697␌
698/*
699%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700% %
701% %
702% %
703+ I s L o g C a c h e I n s t a n t i a t e d %
704% %
705% %
706% %
707%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
708%
709% IsLogCacheInstantiated() determines if the log list is instantiated. If
710% not, it instantiates the list and returns it.
711%
712% The format of the IsLogInstantiated method is:
713%
714% MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
715%
716% A description of each parameter follows.
717%
718% o exception: return any errors or warnings in this structure.
719%
720*/
721
722static inline void CheckEventLogging(void)
723{
724 /*
725 Are we logging events?
726 */
727 if (IsLinkedListEmpty(log_cache) != MagickFalse)
728 event_logging=MagickFalse;
729 else
730 {
731 ElementInfo
732 *p;
733
734 p=GetHeadElementInLinkedList(log_cache);
735 event_logging=(p != (ElementInfo *) NULL) &&
736 (((LogInfo *) p->value)->event_mask != NoEvents) ?
737 MagickTrue: MagickFalse;
738 }
739}
740
741static MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
742{
743 if (log_cache == (LinkedListInfo *) NULL)
744 {
745 if (log_semaphore == (SemaphoreInfo *) NULL)
746 ActivateSemaphoreInfo(&log_semaphore);
747 LockSemaphoreInfo(log_semaphore);
748 if (log_cache == (LinkedListInfo *) NULL)
749 {
750 log_cache=AcquireLogCache(LogFilename,exception);
751 CheckEventLogging();
752 }
753 UnlockSemaphoreInfo(log_semaphore);
754 }
755 return(log_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
756}
757␌
758/*
759%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760% %
761% %
762% %
763% I s E v e n t L o g g i n g %
764% %
765% %
766% %
767%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768%
769% IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
770% MagickFalse.
771%
772% The format of the IsEventLogging method is:
773%
774% MagickBooleanType IsEventLogging(void)
775%
776*/
777MagickExport MagickBooleanType IsEventLogging(void)
778{
779 return(event_logging);
780}
781␌
782/*
783%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784% %
785% %
786% %
787% L i s t L o g I n f o %
788% %
789% %
790% %
791%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792%
793% ListLogInfo() lists the log info to a file.
794%
795% The format of the ListLogInfo method is:
796%
797% MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
798%
799% A description of each parameter follows.
800%
801% o file: An pointer to a FILE.
802%
803% o exception: return any errors or warnings in this structure.
804%
805*/
806MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
807{
808#define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
809
810 const char
811 *path;
812
813 const LogInfo
814 **log_info;
815
816 ssize_t
817 i;
818
819 size_t
820 number_aliases;
821
822 ssize_t
823 j;
824
825 if (file == (const FILE *) NULL)
826 file=stdout;
827 log_info=GetLogInfoList("*",&number_aliases,exception);
828 if (log_info == (const LogInfo **) NULL)
829 return(MagickFalse);
830 j=0;
831 path=(const char *) NULL;
832 for (i=0; i < (ssize_t) number_aliases; i++)
833 {
834 char
835 limit[MagickPathExtent];
836
837 if (log_info[i]->stealth != MagickFalse)
838 continue;
839 if ((path == (const char *) NULL) ||
840 (LocaleCompare(path,log_info[i]->path) != 0))
841 {
842 if (log_info[i]->path != (char *) NULL)
843 (void) FormatLocaleFile(file,"\nPath: %s\nHandler: ",
844 log_info[i]->path);
845 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
846 {
847 size_t
848 mask;
849
850 if (*LogHandlers[j].name == '\0')
851 break;
852 mask=1;
853 mask<<=j;
854 if (((size_t) log_info[i]->handler_mask & mask) != 0)
855 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
856 }
857 (void) FormatLocaleFile(file,"\n\n");
858 }
859 (void) FormatLocaleFile(file,
860 "Generations Limit Filename\n");
861 (void) FormatLocaleFile(file,"---------------------------------------------"
862 "----------------------------------\n");
863 (void) FormatLocaleFile(file,"%g ",(double) log_info[i]->generations);
864 (void) CopyMagickString(limit,"unlimited",MagickPathExtent);
865 if (log_info[i]->limit > 0)
866 (void) FormatMagickSize(log_info[i]->limit,MagickTrue,"B",
867 MagickFormatExtent,limit);
868 (void) FormatLocaleFile(file," %9s",limit);
869 if (log_info[i]->filename != (char *) NULL)
870 (void) FormatLocaleFile(file," %s",log_info[i]->filename);
871 (void) FormatLocaleFile(file,"\n");
872 if (log_info[i]->format != (char *) NULL)
873 (void) FormatLocaleFile(file," Format: %s\n",log_info[i]->format);
874 (void) FormatLocaleFile(file,"\n");
875 path=log_info[i]->path;
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=StringToMagickSizeType(token,100.0);
1105 if (log_info->limit < 1024)
1106 log_info->limit=1024*1024*StringToUnsignedLong(token);
1107 break;
1108 }
1109 break;
1110 }
1111 case 'O':
1112 case 'o':
1113 {
1114 if (LocaleCompare((char *) keyword,"output") == 0)
1115 {
1116 log_info->handler_mask=(LogHandlerType)
1117 (log_info->handler_mask | ParseLogHandlers(token));
1118 break;
1119 }
1120 break;
1121 }
1122 default:
1123 break;
1124 }
1125 }
1126 token=DestroyString(token);
1127 if (cache == (LinkedListInfo *) NULL)
1128 return(MagickFalse);
1129 return(status);
1130}
1131#endif
1132␌
1133/*
1134%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1135% %
1136% %
1137% %
1138+ L o g C o m p o n e n t G e n e s i s %
1139% %
1140% %
1141% %
1142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1143%
1144% LogComponentGenesis() instantiates the log component.
1145%
1146% The format of the LogComponentGenesis method is:
1147%
1148% MagickBooleanType LogComponentGenesis(void)
1149%
1150*/
1151MagickPrivate MagickBooleanType LogComponentGenesis(void)
1152{
1153 ExceptionInfo
1154 *exception;
1155
1156 if (log_semaphore == (SemaphoreInfo *) NULL)
1157 log_semaphore=AcquireSemaphoreInfo();
1158 exception=AcquireExceptionInfo();
1159 (void) GetLogInfo("*",exception);
1160 exception=DestroyExceptionInfo(exception);
1161 return(MagickTrue);
1162}
1163␌
1164/*
1165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1166% %
1167% %
1168% %
1169+ L o g C o m p o n e n t T e r m i n u s %
1170% %
1171% %
1172% %
1173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1174%
1175% LogComponentTerminus() destroys the logging component.
1176%
1177% The format of the LogComponentTerminus method is:
1178%
1179% LogComponentTerminus(void)
1180%
1181*/
1182
1183static void *DestroyLogElement(void *log_info)
1184{
1185 LogInfo
1186 *p;
1187
1188 p=(LogInfo *) log_info;
1189 if (p->file != (FILE *) NULL)
1190 {
1191 (void) FormatLocaleFile(p->file,"</log>\n");
1192 (void) fclose(p->file);
1193 p->file=(FILE *) NULL;
1194 }
1195 if (p->format != (char *) NULL)
1196 p->format=DestroyString(p->format);
1197 if (p->path != (char *) NULL)
1198 p->path=DestroyString(p->path);
1199 if (p->filename != (char *) NULL)
1200 p->filename=DestroyString(p->filename);
1201 if (p->event_semaphore != (SemaphoreInfo *) NULL)
1202 RelinquishSemaphoreInfo(&p->event_semaphore);
1203 p=(LogInfo *) RelinquishMagickMemory(p);
1204 return((void *) NULL);
1205}
1206
1207MagickPrivate void LogComponentTerminus(void)
1208{
1209 if (log_semaphore == (SemaphoreInfo *) NULL)
1210 ActivateSemaphoreInfo(&log_semaphore);
1211 LockSemaphoreInfo(log_semaphore);
1212 if (log_cache != (LinkedListInfo *) NULL)
1213 log_cache=DestroyLinkedList(log_cache,DestroyLogElement);
1214 event_logging=MagickFalse;
1215 UnlockSemaphoreInfo(log_semaphore);
1216 RelinquishSemaphoreInfo(&log_semaphore);
1217}
1218␌
1219/*
1220%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1221% %
1222% %
1223% %
1224% L o g M a g i c k E v e n t %
1225% %
1226% %
1227% %
1228%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1229%
1230% LogMagickEvent() logs an event as determined by the log configuration file.
1231% If an error occurs, MagickFalse is returned otherwise MagickTrue.
1232%
1233% The format of the LogMagickEvent method is:
1234%
1235% MagickBooleanType LogMagickEvent(const LogEventType type,
1236% const char *module,const char *function,const size_t line,
1237% const char *format,...)
1238%
1239% A description of each parameter follows:
1240%
1241% o type: the event type.
1242%
1243% o filename: the source module filename.
1244%
1245% o function: the function name.
1246%
1247% o line: the line number of the source module.
1248%
1249% o format: the output format.
1250%
1251*/
1252
1253static void FormatLogFilename(const LogInfo *log_info,const ssize_t generation,
1254 char *filename)
1255{
1256 char
1257 *q;
1258
1259 const char
1260 *p;
1261
1262 /*
1263 Translate event in "human readable" format.
1264 */
1265 assert(log_info != (LogInfo *) NULL);
1266 q=filename;
1267 for (p=log_info->filename; *p != '\0'; p++)
1268 {
1269 /*
1270 The format of the filename is defined by embedding special format
1271 characters:
1272
1273 %c client name
1274 %n log name
1275 %p process id
1276 %v version
1277 %% percent sign
1278 */
1279 if (*p != '%')
1280 {
1281 *q++=(*p);
1282 continue;
1283 }
1284 p++;
1285 if (*p == '\0')
1286 break;
1287 switch (*p)
1288 {
1289 case '\0':
1290 {
1291 p--;
1292 break;
1293 }
1294 case 'c':
1295 {
1296 q+=(ptrdiff_t) CopyMagickString(q,GetClientName(),MagickPathExtent);
1297 break;
1298 }
1299 case 'g':
1300 {
1301 if (log_info->generations == 0)
1302 {
1303 (void) CopyMagickString(q,"0",MagickPathExtent);
1304 q++;
1305 break;
1306 }
1307 q+=(ptrdiff_t) FormatLocaleString(q,MagickPathExtent,"%.17g",(double)
1308 (generation % log_info->generations));
1309 break;
1310 }
1311 case 'n':
1312 {
1313 q+=(ptrdiff_t) CopyMagickString(q,GetLogName(),MagickPathExtent);
1314 break;
1315 }
1316 case 'p':
1317 {
1318 q+=(ptrdiff_t) FormatLocaleString(q,MagickPathExtent,"%.17g",(double)
1319 getpid());
1320 break;
1321 }
1322 case 'v':
1323 {
1324 q+=(ptrdiff_t) CopyMagickString(q,MagickLibVersionText,
1325 MagickPathExtent);
1326 break;
1327 }
1328 case '%':
1329 {
1330 *q++=(*p);
1331 break;
1332 }
1333 default:
1334 {
1335 *q++='%';
1336 *q++=(*p);
1337 break;
1338 }
1339 }
1340 }
1341 *q='\0';
1342}
1343
1344static char *TranslateEvent(const char *module,const char *function,
1345 const size_t line,const char *domain,const char *event)
1346{
1347 char
1348 *text;
1349
1350 double
1351 elapsed_time,
1352 user_time;
1353
1354 ExceptionInfo
1355 *exception;
1356
1357 LogInfo
1358 *log_info;
1359
1360 char
1361 *q;
1362
1363 const char
1364 *p;
1365
1366 size_t
1367 extent;
1368
1369 time_t
1370 seconds;
1371
1372 exception=AcquireExceptionInfo();
1373 log_info=GetLogInfo("*",exception);
1374 exception=DestroyExceptionInfo(exception);
1375 text=AcquireString(event);
1376 if (log_info == (LogInfo *) NULL)
1377 return(text);
1378 seconds=GetMagickTime();
1379 elapsed_time=GetElapsedTime(&log_info->timer);
1380 user_time=GetUserTime(&log_info->timer);
1381 if (log_info->format == (char *) NULL)
1382 return(text);
1383 extent=strlen(event)+MagickPathExtent;
1384 if (LocaleCompare(log_info->format,"xml") == 0)
1385 {
1386 char
1387 timestamp[MagickTimeExtent];
1388
1389 /*
1390 Translate event in "XML" format.
1391 */
1392 (void) FormatMagickTime(seconds,sizeof(timestamp),timestamp);
1393 (void) FormatLocaleString(text,extent,
1394 "<entry>\n"
1395 " <timestamp>%s</timestamp>\n"
1396 " <elapsed-time>%lu:%02lu.%06lu</elapsed-time>\n"
1397 " <user-time>%0.3f</user-time>\n"
1398 " <process-id>%.17g</process-id>\n"
1399 " <thread-id>%.17g</thread-id>\n"
1400 " <module>%s</module>\n"
1401 " <function>%s</function>\n"
1402 " <line>%.17g</line>\n"
1403 " <domain>%s</domain>\n"
1404 " <event>%s</event>\n"
1405 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
1406 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
1407 (1000000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
1408 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
1409 (double) line,domain,event);
1410 return(text);
1411 }
1412 /*
1413 Translate event in "human readable" format.
1414 */
1415 q=text;
1416 for (p=log_info->format; *p != '\0'; p++)
1417 {
1418 *q='\0';
1419 if ((size_t) (q-text+MagickPathExtent) >= extent)
1420 {
1421 extent<<=1;
1422 text=(char *) ResizeQuantumMemory(text,extent,sizeof(*text));
1423 if (text == (char *) NULL)
1424 return((char *) NULL);
1425 q=text+strlen(text);
1426 }
1427 /*
1428 The format of the log is defined by embedding special format characters:
1429
1430 %c client name
1431 %d domain
1432 %e event
1433 %f function
1434 %i thread id
1435 %l line
1436 %m module
1437 %n log name
1438 %p process id
1439 %r real CPU time
1440 %t wall clock time
1441 %u user CPU time
1442 %v version
1443 %% percent sign
1444 \n newline
1445 \r carriage return
1446 */
1447 if ((*p == '\\') && (*(p+1) == 'r'))
1448 {
1449 *q++='\r';
1450 p++;
1451 continue;
1452 }
1453 if ((*p == '\\') && (*(p+1) == 'n'))
1454 {
1455 *q++='\n';
1456 p++;
1457 continue;
1458 }
1459 if (*p != '%')
1460 {
1461 *q++=(*p);
1462 continue;
1463 }
1464 p++;
1465 if (*p == '\0')
1466 break;
1467 switch (*p)
1468 {
1469 case 'c':
1470 {
1471 q+=(ptrdiff_t) CopyMagickString(q,GetClientName(),extent-(q-text));
1472 break;
1473 }
1474 case 'd':
1475 {
1476 q+=(ptrdiff_t) CopyMagickString(q,domain,extent-(q-text));
1477 break;
1478 }
1479 case 'e':
1480 {
1481 q+=(ptrdiff_t) CopyMagickString(q,event,extent-(q-text));
1482 break;
1483 }
1484 case 'f':
1485 {
1486 q+=(ptrdiff_t) CopyMagickString(q,function,extent-(q-text));
1487 break;
1488 }
1489 case 'i':
1490 {
1491 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%.17g",(double)
1492 GetMagickThreadSignature());
1493 break;
1494 }
1495 case 'l':
1496 {
1497 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%.17g",(double)
1498 line);
1499 break;
1500 }
1501 case 'm':
1502 {
1503 const char
1504 *r;
1505
1506 for (r=module+strlen(module)-1; r > module; r--)
1507 if (*r == *DirectorySeparator)
1508 {
1509 r++;
1510 break;
1511 }
1512 q+=(ptrdiff_t) CopyMagickString(q,r,extent-(q-text));
1513 break;
1514 }
1515 case 'n':
1516 {
1517 q+=(ptrdiff_t) CopyMagickString(q,GetLogName(),extent-(q-text));
1518 break;
1519 }
1520 case 'p':
1521 {
1522 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%.17g",(double)
1523 getpid());
1524 break;
1525 }
1526 case 'r':
1527 {
1528 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%lu:%02lu.%03lu",
1529 (unsigned long) (elapsed_time/60.0),(unsigned long) floor(fmod(
1530 elapsed_time,60.0)),(unsigned long) (1000.0*(elapsed_time-floor(
1531 elapsed_time))+0.5));
1532 break;
1533 }
1534 case 't':
1535 {
1536 q+=(ptrdiff_t) FormatMagickTime(seconds,extent-(q-text),q);
1537 break;
1538 }
1539 case 'u':
1540 {
1541 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%0.3fu",user_time);
1542 break;
1543 }
1544 case 'v':
1545 {
1546 q+=(ptrdiff_t) CopyMagickString(q,MagickLibVersionText,extent-(q-text));
1547 break;
1548 }
1549 case '%':
1550 {
1551 *q++=(*p);
1552 break;
1553 }
1554 default:
1555 {
1556 *q++='%';
1557 *q++=(*p);
1558 break;
1559 }
1560 }
1561 }
1562 *q='\0';
1563 return(text);
1564}
1565
1566MagickExport MagickBooleanType LogMagickEventList(const LogEventType type,
1567 const char *module,const char *function,const size_t line,const char *format,
1568 va_list operands)
1569{
1570 char
1571 event[MagickPathExtent],
1572 *text;
1573
1574 const char
1575 *domain;
1576
1577 ExceptionInfo
1578 *exception;
1579
1580 int
1581 n;
1582
1583 LogInfo
1584 *log_info;
1585
1586 exception=AcquireExceptionInfo();
1587 log_info=(LogInfo *) GetLogInfo("*",exception);
1588 exception=DestroyExceptionInfo(exception);
1589 if (log_info == (LogInfo *) NULL)
1590 return(MagickFalse);
1591 if (log_info->event_semaphore == (SemaphoreInfo *) NULL)
1592 ActivateSemaphoreInfo(&log_info->event_semaphore);
1593 LockSemaphoreInfo(log_info->event_semaphore);
1594 if ((log_info->event_mask & type) == 0)
1595 {
1596 UnlockSemaphoreInfo(log_info->event_semaphore);
1597 return(MagickTrue);
1598 }
1599 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1600 n=vsnprintf(event,MagickPathExtent,format,operands);
1601 if (n < 0)
1602 event[MagickPathExtent-1]='\0';
1603 text=TranslateEvent(module,function,line,domain,event);
1604 if (text == (char *) NULL)
1605 {
1606 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1607 UnlockSemaphoreInfo(log_info->event_semaphore);
1608 return(MagickFalse);
1609 }
1610 if ((log_info->handler_mask & ConsoleHandler) != 0)
1611 {
1612 (void) FormatLocaleFile(stderr,"%s\n",text);
1613 (void) fflush(stderr);
1614 }
1615 if ((log_info->handler_mask & DebugHandler) != 0)
1616 {
1617#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1618 OutputDebugString(text);
1619 OutputDebugString("\n");
1620#endif
1621 }
1622 if ((log_info->handler_mask & EventHandler) != 0)
1623 {
1624#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1625 (void) NTReportEvent(text,MagickFalse);
1626#endif
1627 }
1628 if ((log_info->handler_mask & FileHandler) != 0)
1629 {
1630 struct stat
1631 file_info;
1632
1633 file_info.st_size=0;
1634 if (log_info->file != (FILE *) NULL)
1635 (void) fstat(fileno(log_info->file),&file_info);
1636 if (file_info.st_size > (MagickOffsetType) log_info->limit)
1637 {
1638 ssize_t
1639 i;
1640
1641 /*
1642 Close log.
1643 */
1644 (void) FormatLocaleFile(log_info->file,"</log>\n");
1645 (void) FormatLocaleFile(log_info->file,"</logEntries>\n");
1646 (void) fclose(log_info->file);
1647 for (i=(ssize_t) log_info->generations-1; i > 0; i--)
1648 {
1649 char
1650 new_filename[MagickPathExtent],
1651 old_filename[MagickPathExtent];
1652
1653 /*
1654 Rotate logs.
1655 */
1656 FormatLogFilename(log_info,i-1,old_filename);
1657 FormatLogFilename(log_info,i,new_filename);
1658 (void) rename_utf8(old_filename,new_filename);
1659 }
1660 log_info->file=(FILE *) NULL;
1661 }
1662 if (log_info->file == (FILE *) NULL)
1663 {
1664 char
1665 log_filename[MagickPathExtent];
1666
1667 FormatLogFilename(log_info,0,log_filename);
1668 log_info->append=IsPathAccessible(log_filename);
1669 log_info->file=fopen_utf8(log_filename,"ab");
1670 if (log_info->file == (FILE *) NULL)
1671 {
1672 UnlockSemaphoreInfo(log_info->event_semaphore);
1673 return(MagickFalse);
1674 }
1675 if (log_info->append == MagickFalse)
1676 {
1677 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1678 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1679 (void) FormatLocaleFile(log_info->file,"<logEntries>\n");
1680 }
1681 (void) FormatLocaleFile(log_info->file,"<log>\n");
1682 }
1683 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
1684 (void) fflush(log_info->file);
1685 }
1686 if ((log_info->handler_mask & MethodHandler) != 0)
1687 {
1688 if (log_info->method != (MagickLogMethod) NULL)
1689 log_info->method(type,text);
1690 }
1691 if ((log_info->handler_mask & StdoutHandler) != 0)
1692 {
1693 (void) FormatLocaleFile(stdout,"%s\n",text);
1694 (void) fflush(stdout);
1695 }
1696 if ((log_info->handler_mask & StderrHandler) != 0)
1697 {
1698 (void) FormatLocaleFile(stderr,"%s\n",text);
1699 (void) fflush(stderr);
1700 }
1701 text=(char *) RelinquishMagickMemory(text);
1702 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1703 UnlockSemaphoreInfo(log_info->event_semaphore);
1704 return(MagickTrue);
1705}
1706
1707MagickExport MagickBooleanType LogMagickEvent(const LogEventType type,
1708 const char *module,const char *function,const size_t line,
1709 const char *format,...)
1710{
1711 va_list
1712 operands;
1713
1714 MagickBooleanType
1715 status;
1716
1717 if (IsEventLogging() == MagickFalse)
1718 return(MagickFalse);
1719 va_start(operands,format);
1720 status=LogMagickEventList(type,module,function,line,format,operands);
1721 va_end(operands);
1722 return(status);
1723}
1724␌
1725#if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
1726/*
1727%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1728% %
1729% %
1730% %
1731% P a r s e L o g H a n d l e r s %
1732% %
1733% %
1734% %
1735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1736%
1737% ParseLogHandlers() parses a string defining which handlers takes a log
1738% message and exports them.
1739%
1740% The format of the ParseLogHandlers method is:
1741%
1742% LogHandlerType ParseLogHandlers(const char *handlers)
1743%
1744% A description of each parameter follows:
1745%
1746% o handlers: one or more handlers separated by commas.
1747%
1748*/
1749static LogHandlerType ParseLogHandlers(const char *handlers)
1750{
1751 LogHandlerType
1752 handler_mask;
1753
1754 const char
1755 *p;
1756
1757 ssize_t
1758 i;
1759
1760 size_t
1761 length;
1762
1763 handler_mask=NoHandler;
1764 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1765 {
1766 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1767 (*p == ',')))
1768 p++;
1769 for (i=0; *LogHandlers[i].name != '\0'; i++)
1770 {
1771 length=strlen(LogHandlers[i].name);
1772 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1773 {
1774 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1775 break;
1776 }
1777 }
1778 if (*LogHandlers[i].name == '\0')
1779 return(UndefinedHandler);
1780 }
1781 return(handler_mask);
1782}
1783#endif
1784␌
1785/*
1786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1787% %
1788% %
1789% %
1790% S e t L o g E v e n t M a s k %
1791% %
1792% %
1793% %
1794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1795%
1796% SetLogEventMask() accepts a list that determines which events to log. All
1797% other events are ignored. By default, no debug is enabled. This method
1798% returns the previous log event mask.
1799%
1800% The format of the SetLogEventMask method is:
1801%
1802% LogEventType SetLogEventMask(const char *events)
1803%
1804% A description of each parameter follows:
1805%
1806% o events: log these events.
1807%
1808*/
1809MagickExport LogEventType SetLogEventMask(const char *events)
1810{
1811 ExceptionInfo
1812 *exception;
1813
1814 LogEventType
1815 event_mask;
1816
1817 LogInfo
1818 *log_info;
1819
1820 ssize_t
1821 option;
1822
1823 event_mask=UndefinedEvents;
1824 exception=AcquireExceptionInfo();
1825 log_info=GetLogInfo("*",exception);
1826 exception=DestroyExceptionInfo(exception);
1827 if (log_info == (LogInfo *) NULL)
1828 return(event_mask);
1829 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1830 LockSemaphoreInfo(log_semaphore);
1831 event_mask=log_info->event_mask;
1832 if (option == -1)
1833 log_info->event_mask=UndefinedEvents;
1834 else
1835 log_info->event_mask=(LogEventType) option;
1836 CheckEventLogging();
1837 UnlockSemaphoreInfo(log_semaphore);
1838 return(event_mask);
1839}
1840␌
1841/*
1842%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1843% %
1844% %
1845% %
1846% S e t L o g F o r m a t %
1847% %
1848% %
1849% %
1850%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1851%
1852% SetLogFormat() sets the format for the "human readable" log record.
1853%
1854% The format of the LogMagickFormat method is:
1855%
1856% SetLogFormat(const char *format)
1857%
1858% A description of each parameter follows:
1859%
1860% o format: the log record format.
1861%
1862*/
1863MagickExport void SetLogFormat(const char *format)
1864{
1865 LogInfo
1866 *log_info;
1867
1868 ExceptionInfo
1869 *exception;
1870
1871 exception=AcquireExceptionInfo();
1872 log_info=(LogInfo *) GetLogInfo("*",exception);
1873 exception=DestroyExceptionInfo(exception);
1874 if (log_info == (LogInfo *) NULL)
1875 return;
1876 LockSemaphoreInfo(log_semaphore);
1877 if (log_info->format != (char *) NULL)
1878 log_info->format=DestroyString(log_info->format);
1879 log_info->format=ConstantString(format);
1880 UnlockSemaphoreInfo(log_semaphore);
1881}
1882␌
1883/*
1884%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1885% %
1886% %
1887% %
1888% S e t L o g M e t h o d %
1889% %
1890% %
1891% %
1892%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1893%
1894% SetLogMethod() sets the method that will be called when an event is logged.
1895%
1896% The format of the SetLogMethod method is:
1897%
1898% void SetLogMethod(MagickLogMethod method)
1899%
1900% A description of each parameter follows:
1901%
1902% o method: pointer to a method that will be called when LogMagickEvent is
1903% being called.
1904%
1905*/
1906MagickExport void SetLogMethod(MagickLogMethod method)
1907{
1908 ExceptionInfo
1909 *exception;
1910
1911 LogInfo
1912 *log_info;
1913
1914 exception=AcquireExceptionInfo();
1915 log_info=(LogInfo *) GetLogInfo("*",exception);
1916 exception=DestroyExceptionInfo(exception);
1917 if (log_info == (LogInfo *) NULL)
1918 return;
1919 LockSemaphoreInfo(log_semaphore);
1920 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1921 log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1922 MethodHandler);
1923 log_info->method=method;
1924 UnlockSemaphoreInfo(log_semaphore);
1925}
1926␌
1927/*
1928%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1929% %
1930% %
1931% %
1932% S e t L o g N a m e %
1933% %
1934% %
1935% %
1936%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1937%
1938% SetLogName() sets the log name and returns it.
1939%
1940% The format of the SetLogName method is:
1941%
1942% const char *SetLogName(const char *name)
1943%
1944% A description of each parameter follows:
1945%
1946% o log_name: SetLogName() returns the current client name.
1947%
1948% o name: Specifies the new client name.
1949%
1950*/
1951MagickExport const char *SetLogName(const char *name)
1952{
1953 if ((name != (char *) NULL) && (*name != '\0'))
1954 (void) CopyMagickString(log_name,name,MagickPathExtent);
1955 return(log_name);
1956}