MagickCore 7.1.2-32
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 MagickStatusType
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 status&=(MagickStatusType) LoadLogCache(cache,(const char *)
298 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
299 option=(const StringInfo *) GetNextValueInLinkedList(options);
300 }
301 options=DestroyConfigureOptions(options);
302 }
303#else
304 magick_unreferenced(filename);
305#endif
306 /*
307 Load built-in log map.
308 */
309 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
310 {
311 LogInfo
312 *log_info;
313
314 const LogMapInfo
315 *p;
316
317 p=LogMap+i;
318 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
319 if (log_info == (LogInfo *) NULL)
320 {
321 (void) ThrowMagickException(exception,GetMagickModule(),
322 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->filename);
323 continue;
324 }
325 (void) memset(log_info,0,sizeof(*log_info));
326 log_info->path=ConstantString("[built-in]");
327 GetTimerInfo((TimerInfo *) &log_info->timer);
328 log_info->event_mask=p->event_mask;
329 log_info->handler_mask=p->handler_mask;
330 log_info->filename=ConstantString(p->filename);
331 log_info->format=ConstantString(p->format);
332 log_info->signature=MagickCoreSignature;
333 status&=(MagickStatusType) AppendValueToLinkedList(cache,log_info);
334 if (status == MagickFalse)
335 (void) ThrowMagickException(exception,GetMagickModule(),
336 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
337 }
338 return(cache);
339}
340␌
341/*
342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343% %
344% %
345% %
346% C l o s e M a g i c k L o g %
347% %
348% %
349% %
350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351%
352% CloseMagickLog() closes the Magick log.
353%
354% The format of the CloseMagickLog method is:
355%
356% CloseMagickLog(void)
357%
358*/
359MagickExport void CloseMagickLog(void)
360{
361 ExceptionInfo
362 *exception;
363
364 LogInfo
365 *log_info;
366
367 if (IsEventLogging() == MagickFalse)
368 return;
369 exception=AcquireExceptionInfo();
370 log_info=GetLogInfo("*",exception);
371 exception=DestroyExceptionInfo(exception);
372 if (log_info == (LogInfo *) NULL)
373 return;
374 LockSemaphoreInfo(log_semaphore);
375 if ((log_info != (LogInfo *) NULL) && (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{
404 ExceptionInfo
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
448 ElementInfo
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
536 ElementInfo
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
631 ElementInfo
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 {
729 ElementInfo
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 char
833 limit[MagickPathExtent];
834
835 if (log_info[i]->stealth != MagickFalse)
836 continue;
837 if ((path == (const char *) NULL) ||
838 (LocaleCompare(path,log_info[i]->path) != 0))
839 {
840 if (log_info[i]->path != (char *) NULL)
841 (void) FormatLocaleFile(file,"\nPath: %s\nHandler: ",
842 log_info[i]->path);
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 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
854 }
855 (void) FormatLocaleFile(file,"\n\n");
856 }
857 (void) FormatLocaleFile(file,
858 "Generations Limit Filename\n");
859 (void) FormatLocaleFile(file,"---------------------------------------------"
860 "----------------------------------\n");
861 (void) FormatLocaleFile(file,"%g ",(double) log_info[i]->generations);
862 (void) CopyMagickString(limit,"unlimited",MagickPathExtent);
863 if (log_info[i]->limit > 0)
864 (void) FormatMagickSize(log_info[i]->limit,MagickTrue,"B",
865 MagickFormatExtent,limit);
866 (void) FormatLocaleFile(file," %9s",limit);
867 if (log_info[i]->filename != (char *) NULL)
868 (void) FormatLocaleFile(file," %s",log_info[i]->filename);
869 (void) FormatLocaleFile(file,"\n");
870 if (log_info[i]->format != (char *) NULL)
871 (void) FormatLocaleFile(file," Format: %s\n",log_info[i]->format);
872 (void) FormatLocaleFile(file,"\n");
873 path=log_info[i]->path;
874 }
875 (void) fflush(file);
876 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
877 return(MagickTrue);
878}
879␌
880#if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
881/*
882%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
883% %
884% %
885% %
886% L o a d L o g C a c h e %
887% %
888% %
889% %
890%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
891%
892% LoadLogCache() loads the log configurations which provides a
893% mapping between log attributes and log name.
894%
895% The format of the LoadLogCache method is:
896%
897% MagickBooleanType LoadLogCache(LinkedListInfo *cache,const char *xml,
898% const char *filename,const size_t depth,ExceptionInfo *exception)
899%
900% A description of each parameter follows:
901%
902% o xml: The log list in XML format.
903%
904% o filename: The log list filename.
905%
906% o depth: depth of <include /> statements.
907%
908% o exception: return any errors or warnings in this structure.
909%
910*/
911static MagickBooleanType LoadLogCache(LinkedListInfo *cache,const char *xml,
912 const char *filename,const size_t depth,ExceptionInfo *exception)
913{
914 char
915 keyword[MagickPathExtent],
916 *token;
917
918 const char
919 *q;
920
921 LogInfo
922 *log_info = (LogInfo *) NULL;
923
924 MagickStatusType
925 status;
926
927 size_t
928 extent;
929
930 /*
931 Load the log map file.
932 */
933 if (xml == (const char *) NULL)
934 return(MagickFalse);
935 status=MagickTrue;
936 token=AcquireString(xml);
937 extent=strlen(token)+MagickPathExtent;
938 for (q=(const char *) xml; *q != '\0'; )
939 {
940 /*
941 Interpret XML.
942 */
943 (void) GetNextToken(q,&q,extent,token);
944 if (*token == '\0')
945 break;
946 (void) CopyMagickString(keyword,token,MagickPathExtent);
947 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
948 {
949 int
950 bracket_depth = 0,
951 quote = 0;
952
953 /*
954 DOCTYPE element.
955 */
956 for ( ; *q != '\0'; q++)
957 {
958 if (quote != 0)
959 {
960 if (*q == quote)
961 quote=0;
962 }
963 else
964 {
965 if ((*q == '"') || (*q == '\''))
966 quote=(*q);
967 else
968 if (*q == '[')
969 bracket_depth++;
970 else
971 if (*q == ']')
972 {
973 if (bracket_depth > 0)
974 bracket_depth--;
975 }
976 else
977 if ((*q == '>') && (bracket_depth == 0))
978 {
979 q++; /* consume final '>' */
980 break;
981 }
982 }
983 }
984 continue;
985 }
986 if (LocaleNCompare(keyword,"<!--",4) == 0)
987 {
988 /*
989 Comment element.
990 */
991 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
992 (void) GetNextToken(q,&q,extent,token);
993 continue;
994 }
995 if (LocaleCompare(keyword,"<include") == 0)
996 {
997 /*
998 Include element.
999 */
1000 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1001 {
1002 (void) CopyMagickString(keyword,token,MagickPathExtent);
1003 (void) GetNextToken(q,&q,extent,token);
1004 if (*token != '=')
1005 continue;
1006 (void) GetNextToken(q,&q,extent,token);
1007 if (LocaleCompare(keyword,"file") == 0)
1008 {
1009 if (depth >= MagickMaxRecursionDepth)
1010 (void) ThrowMagickException(exception,GetMagickModule(),
1011 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1012 else
1013 {
1014 char
1015 path[MagickPathExtent],
1016 *file_xml;
1017
1018 GetPathComponent(filename,HeadPath,path);
1019 if (*path != '\0')
1020 (void) ConcatenateMagickString(path,DirectorySeparator,
1021 MagickPathExtent);
1022 if (*token == *DirectorySeparator)
1023 (void) CopyMagickString(path,token,MagickPathExtent);
1024 else
1025 (void) ConcatenateMagickString(path,token,MagickPathExtent);
1026 file_xml=FileToXML(path,~0UL);
1027 if (file_xml != (char *) NULL)
1028 {
1029 status&=(MagickStatusType) LoadLogCache(cache,file_xml,
1030 path,depth+1,exception);
1031 file_xml=DestroyString(file_xml);
1032 }
1033 }
1034 }
1035 }
1036 continue;
1037 }
1038 if (LocaleCompare(keyword,"<logmap>") == 0)
1039 {
1040 /*
1041 Allocate memory for the log list.
1042 */
1043 log_info=(LogInfo *) AcquireCriticalMemory(sizeof(*log_info));
1044 (void) memset(log_info,0,sizeof(*log_info));
1045 log_info->path=ConstantString(filename);
1046 GetTimerInfo((TimerInfo *) &log_info->timer);
1047 log_info->signature=MagickCoreSignature;
1048 continue;
1049 }
1050 if (log_info == (LogInfo *) NULL)
1051 continue;
1052 if (LocaleCompare(keyword,"</logmap>") == 0)
1053 {
1054 status=AppendValueToLinkedList(cache,log_info);
1055 if (status == MagickFalse)
1056 (void) ThrowMagickException(exception,GetMagickModule(),
1057 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1058 log_info=(LogInfo *) NULL;
1059 continue;
1060 }
1061 (void) GetNextToken(q,(const char **) NULL,extent,token);
1062 if (*token != '=')
1063 continue;
1064 (void) GetNextToken(q,&q,extent,token);
1065 (void) GetNextToken(q,&q,extent,token);
1066 switch (*keyword)
1067 {
1068 case 'E':
1069 case 'e':
1070 {
1071 if (LocaleCompare((char *) keyword,"events") == 0)
1072 {
1073 log_info->event_mask=(LogEventType) (log_info->event_mask |
1074 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1075 break;
1076 }
1077 break;
1078 }
1079 case 'F':
1080 case 'f':
1081 {
1082 if (LocaleCompare((char *) keyword,"filename") == 0)
1083 {
1084 if (log_info->filename != (char *) NULL)
1085 log_info->filename=(char *)
1086 RelinquishMagickMemory(log_info->filename);
1087 log_info->filename=ConstantString(token);
1088 break;
1089 }
1090 if (LocaleCompare((char *) keyword,"format") == 0)
1091 {
1092 if (log_info->format != (char *) NULL)
1093 log_info->format=(char *)
1094 RelinquishMagickMemory(log_info->format);
1095 log_info->format=ConstantString(token);
1096 break;
1097 }
1098 break;
1099 }
1100 case 'G':
1101 case 'g':
1102 {
1103 if (LocaleCompare((char *) keyword,"generations") == 0)
1104 {
1105 if (LocaleCompare(token,"unlimited") == 0)
1106 {
1107 log_info->generations=(~0UL);
1108 break;
1109 }
1110 log_info->generations=StringToUnsignedLong(token);
1111 break;
1112 }
1113 break;
1114 }
1115 case 'L':
1116 case 'l':
1117 {
1118 if (LocaleCompare((char *) keyword,"limit") == 0)
1119 {
1120 if (LocaleCompare(token,"unlimited") == 0)
1121 {
1122 log_info->limit=(~0UL);
1123 break;
1124 }
1125 log_info->limit=StringToMagickSizeType(token,100.0);
1126 if (log_info->limit < 1024)
1127 log_info->limit=1024*1024*StringToUnsignedLong(token);
1128 break;
1129 }
1130 break;
1131 }
1132 case 'O':
1133 case 'o':
1134 {
1135 if (LocaleCompare((char *) keyword,"output") == 0)
1136 {
1137 log_info->handler_mask=(LogHandlerType)
1138 (log_info->handler_mask | ParseLogHandlers(token));
1139 break;
1140 }
1141 break;
1142 }
1143 default:
1144 break;
1145 }
1146 }
1147 token=DestroyString(token);
1148 if (cache == (LinkedListInfo *) NULL)
1149 return(MagickFalse);
1150 return(status != 0 ? MagickTrue : MagickFalse);
1151}
1152#endif
1153␌
1154/*
1155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1156% %
1157% %
1158% %
1159+ L o g C o m p o n e n t G e n e s i s %
1160% %
1161% %
1162% %
1163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1164%
1165% LogComponentGenesis() instantiates the log component.
1166%
1167% The format of the LogComponentGenesis method is:
1168%
1169% MagickBooleanType LogComponentGenesis(void)
1170%
1171*/
1172MagickPrivate MagickBooleanType LogComponentGenesis(void)
1173{
1174 ExceptionInfo
1175 *exception;
1176
1177 if (log_semaphore == (SemaphoreInfo *) NULL)
1178 log_semaphore=AcquireSemaphoreInfo();
1179 exception=AcquireExceptionInfo();
1180 (void) GetLogInfo("*",exception);
1181 exception=DestroyExceptionInfo(exception);
1182 return(MagickTrue);
1183}
1184␌
1185/*
1186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1187% %
1188% %
1189% %
1190+ L o g C o m p o n e n t T e r m i n u s %
1191% %
1192% %
1193% %
1194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1195%
1196% LogComponentTerminus() destroys the logging component.
1197%
1198% The format of the LogComponentTerminus method is:
1199%
1200% LogComponentTerminus(void)
1201%
1202*/
1203
1204static void *DestroyLogElement(void *log_info)
1205{
1206 LogInfo
1207 *p;
1208
1209 p=(LogInfo *) log_info;
1210 if (p->file != (FILE *) NULL)
1211 {
1212 (void) FormatLocaleFile(p->file,"</log>\n");
1213 (void) fclose(p->file);
1214 p->file=(FILE *) NULL;
1215 }
1216 if (p->format != (char *) NULL)
1217 p->format=DestroyString(p->format);
1218 if (p->path != (char *) NULL)
1219 p->path=DestroyString(p->path);
1220 if (p->filename != (char *) NULL)
1221 p->filename=DestroyString(p->filename);
1222 if (p->event_semaphore != (SemaphoreInfo *) NULL)
1223 RelinquishSemaphoreInfo(&p->event_semaphore);
1224 p=(LogInfo *) RelinquishMagickMemory(p);
1225 return((void *) NULL);
1226}
1227
1228MagickPrivate void LogComponentTerminus(void)
1229{
1230 if (log_semaphore == (SemaphoreInfo *) NULL)
1231 ActivateSemaphoreInfo(&log_semaphore);
1232 LockSemaphoreInfo(log_semaphore);
1233 if (log_cache != (LinkedListInfo *) NULL)
1234 log_cache=DestroyLinkedList(log_cache,DestroyLogElement);
1235 event_logging=MagickFalse;
1236 UnlockSemaphoreInfo(log_semaphore);
1237 RelinquishSemaphoreInfo(&log_semaphore);
1238}
1239␌
1240/*
1241%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1242% %
1243% %
1244% %
1245% L o g M a g i c k E v e n t %
1246% %
1247% %
1248% %
1249%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1250%
1251% LogMagickEvent() logs an event as determined by the log configuration file.
1252% If an error occurs, MagickFalse is returned otherwise MagickTrue.
1253%
1254% The format of the LogMagickEvent method is:
1255%
1256% MagickBooleanType LogMagickEvent(const LogEventType type,
1257% const char *module,const char *function,const size_t line,
1258% const char *format,...)
1259%
1260% A description of each parameter follows:
1261%
1262% o type: the event type.
1263%
1264% o filename: the source module filename.
1265%
1266% o function: the function name.
1267%
1268% o line: the line number of the source module.
1269%
1270% o format: the output format.
1271%
1272*/
1273
1274static void FormatLogFilename(const LogInfo *log_info,const ssize_t generation,
1275 char *filename)
1276{
1277 char
1278 *q;
1279
1280 const char
1281 *p;
1282
1283 /*
1284 Translate event in "human readable" format.
1285 */
1286 assert(log_info != (LogInfo *) NULL);
1287 q=filename;
1288 for (p=log_info->filename; *p != '\0'; p++)
1289 {
1290 /*
1291 The format of the filename is defined by embedding special format
1292 characters:
1293
1294 %c client name
1295 %n log name
1296 %p process id
1297 %v version
1298 %% percent sign
1299 */
1300 if (*p != '%')
1301 {
1302 *q++=(*p);
1303 continue;
1304 }
1305 p++;
1306 if (*p == '\0')
1307 break;
1308 switch (*p)
1309 {
1310 case '\0':
1311 {
1312 p--;
1313 break;
1314 }
1315 case 'c':
1316 {
1317 q+=(ptrdiff_t) CopyMagickString(q,GetClientName(),MagickPathExtent);
1318 break;
1319 }
1320 case 'g':
1321 {
1322 if (log_info->generations == 0)
1323 {
1324 (void) CopyMagickString(q,"0",MagickPathExtent);
1325 q++;
1326 break;
1327 }
1328 q+=(ptrdiff_t) FormatLocaleString(q,MagickPathExtent,"%.17g",(double)
1329 (generation % log_info->generations));
1330 break;
1331 }
1332 case 'n':
1333 {
1334 q+=(ptrdiff_t) CopyMagickString(q,GetLogName(),MagickPathExtent);
1335 break;
1336 }
1337 case 'p':
1338 {
1339 q+=(ptrdiff_t) FormatLocaleString(q,MagickPathExtent,"%.17g",(double)
1340 getpid());
1341 break;
1342 }
1343 case 'v':
1344 {
1345 q+=(ptrdiff_t) CopyMagickString(q,MagickLibVersionText,
1346 MagickPathExtent);
1347 break;
1348 }
1349 case '%':
1350 {
1351 *q++=(*p);
1352 break;
1353 }
1354 default:
1355 {
1356 *q++='%';
1357 *q++=(*p);
1358 break;
1359 }
1360 }
1361 }
1362 *q='\0';
1363}
1364
1365static char *TranslateEvent(const char *module,const char *function,
1366 const size_t line,const char *domain,const char *event)
1367{
1368 char
1369 *text;
1370
1371 double
1372 elapsed_time,
1373 user_time;
1374
1375 ExceptionInfo
1376 *exception;
1377
1378 LogInfo
1379 *log_info;
1380
1381 char
1382 *q;
1383
1384 const char
1385 *p;
1386
1387 size_t
1388 extent;
1389
1390 time_t
1391 seconds;
1392
1393 exception=AcquireExceptionInfo();
1394 log_info=GetLogInfo("*",exception);
1395 exception=DestroyExceptionInfo(exception);
1396 text=AcquireString(event);
1397 if (log_info == (LogInfo *) NULL)
1398 return(text);
1399 seconds=GetMagickTime();
1400 elapsed_time=GetElapsedTime(&log_info->timer);
1401 user_time=GetUserTime(&log_info->timer);
1402 if (log_info->format == (char *) NULL)
1403 return(text);
1404 extent=strlen(event)+MagickPathExtent;
1405 if (LocaleCompare(log_info->format,"xml") == 0)
1406 {
1407 char
1408 timestamp[MagickTimeExtent];
1409
1410 /*
1411 Translate event in "XML" format.
1412 */
1413 (void) FormatMagickTime(seconds,sizeof(timestamp),timestamp);
1414 (void) FormatLocaleString(text,extent,
1415 "<entry>\n"
1416 " <timestamp>%s</timestamp>\n"
1417 " <elapsed-time>%lu:%02lu.%06lu</elapsed-time>\n"
1418 " <user-time>%0.3f</user-time>\n"
1419 " <process-id>%.17g</process-id>\n"
1420 " <thread-id>%.17g</thread-id>\n"
1421 " <module>%s</module>\n"
1422 " <function>%s</function>\n"
1423 " <line>%.17g</line>\n"
1424 " <domain>%s</domain>\n"
1425 " <event>%s</event>\n"
1426 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
1427 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
1428 (1000000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
1429 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
1430 (double) line,domain,event);
1431 return(text);
1432 }
1433 /*
1434 Translate event in "human readable" format.
1435 */
1436 q=text;
1437 for (p=log_info->format; *p != '\0'; p++)
1438 {
1439 *q='\0';
1440 if ((size_t) (q-text+MagickPathExtent) >= extent)
1441 {
1442 extent<<=1;
1443 text=(char *) ResizeQuantumMemory(text,extent,sizeof(*text));
1444 if (text == (char *) NULL)
1445 return((char *) NULL);
1446 q=text+strlen(text);
1447 }
1448 /*
1449 The format of the log is defined by embedding special format characters:
1450
1451 %c client name
1452 %d domain
1453 %e event
1454 %f function
1455 %i thread id
1456 %l line
1457 %m module
1458 %n log name
1459 %p process id
1460 %r real CPU time
1461 %t wall clock time
1462 %u user CPU time
1463 %v version
1464 %% percent sign
1465 \n newline
1466 \r carriage return
1467 */
1468 if ((*p == '\\') && (*(p+1) == 'r'))
1469 {
1470 *q++='\r';
1471 p++;
1472 continue;
1473 }
1474 if ((*p == '\\') && (*(p+1) == 'n'))
1475 {
1476 *q++='\n';
1477 p++;
1478 continue;
1479 }
1480 if (*p != '%')
1481 {
1482 *q++=(*p);
1483 continue;
1484 }
1485 p++;
1486 if (*p == '\0')
1487 break;
1488 switch (*p)
1489 {
1490 case 'c':
1491 {
1492 q+=(ptrdiff_t) CopyMagickString(q,GetClientName(),extent-(q-text));
1493 break;
1494 }
1495 case 'd':
1496 {
1497 q+=(ptrdiff_t) CopyMagickString(q,domain,extent-(q-text));
1498 break;
1499 }
1500 case 'e':
1501 {
1502 q+=(ptrdiff_t) CopyMagickString(q,event,extent-(q-text));
1503 break;
1504 }
1505 case 'f':
1506 {
1507 q+=(ptrdiff_t) CopyMagickString(q,function,extent-(q-text));
1508 break;
1509 }
1510 case 'i':
1511 {
1512 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%.17g",(double)
1513 GetMagickThreadSignature());
1514 break;
1515 }
1516 case 'l':
1517 {
1518 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%.17g",(double)
1519 line);
1520 break;
1521 }
1522 case 'm':
1523 {
1524 const char
1525 *r;
1526
1527 for (r=module+strlen(module)-1; r > module; r--)
1528 if (*r == *DirectorySeparator)
1529 {
1530 r++;
1531 break;
1532 }
1533 q+=(ptrdiff_t) CopyMagickString(q,r,extent-(q-text));
1534 break;
1535 }
1536 case 'n':
1537 {
1538 q+=(ptrdiff_t) CopyMagickString(q,GetLogName(),extent-(q-text));
1539 break;
1540 }
1541 case 'p':
1542 {
1543 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%.17g",(double)
1544 getpid());
1545 break;
1546 }
1547 case 'r':
1548 {
1549 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%lu:%02lu.%03lu",
1550 (unsigned long) (elapsed_time/60.0),(unsigned long) floor(fmod(
1551 elapsed_time,60.0)),(unsigned long) (1000.0*(elapsed_time-floor(
1552 elapsed_time))+0.5));
1553 break;
1554 }
1555 case 't':
1556 {
1557 q+=(ptrdiff_t) FormatMagickTime(seconds,extent-(q-text),q);
1558 break;
1559 }
1560 case 'u':
1561 {
1562 q+=(ptrdiff_t) FormatLocaleString(q,extent-(q-text),"%0.3fu",user_time);
1563 break;
1564 }
1565 case 'v':
1566 {
1567 q+=(ptrdiff_t) CopyMagickString(q,MagickLibVersionText,extent-(q-text));
1568 break;
1569 }
1570 case '%':
1571 {
1572 *q++=(*p);
1573 break;
1574 }
1575 default:
1576 {
1577 *q++='%';
1578 *q++=(*p);
1579 break;
1580 }
1581 }
1582 }
1583 *q='\0';
1584 return(text);
1585}
1586
1587MagickExport MagickBooleanType LogMagickEventList(const LogEventType type,
1588 const char *module,const char *function,const size_t line,const char *format,
1589 va_list operands)
1590{
1591 char
1592 event[MagickPathExtent],
1593 *text;
1594
1595 const char
1596 *domain;
1597
1598 ExceptionInfo
1599 *exception;
1600
1601 int
1602 n;
1603
1604 LogInfo
1605 *log_info;
1606
1607 exception=AcquireExceptionInfo();
1608 log_info=(LogInfo *) GetLogInfo("*",exception);
1609 exception=DestroyExceptionInfo(exception);
1610 if (log_info == (LogInfo *) NULL)
1611 return(MagickFalse);
1612 if (log_info->event_semaphore == (SemaphoreInfo *) NULL)
1613 ActivateSemaphoreInfo(&log_info->event_semaphore);
1614 LockSemaphoreInfo(log_info->event_semaphore);
1615 if ((log_info->event_mask & type) == 0)
1616 {
1617 UnlockSemaphoreInfo(log_info->event_semaphore);
1618 return(MagickTrue);
1619 }
1620 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1621 n=vsnprintf(event,MagickPathExtent,format,operands);
1622 if (n < 0)
1623 event[MagickPathExtent-1]='\0';
1624 text=TranslateEvent(module,function,line,domain,event);
1625 if (text == (char *) NULL)
1626 {
1627 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1628 UnlockSemaphoreInfo(log_info->event_semaphore);
1629 return(MagickFalse);
1630 }
1631 if ((log_info->handler_mask & ConsoleHandler) != 0)
1632 {
1633 (void) FormatLocaleFile(stderr,"%s\n",text);
1634 (void) fflush(stderr);
1635 }
1636 if ((log_info->handler_mask & DebugHandler) != 0)
1637 {
1638#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1639 OutputDebugString(text);
1640 OutputDebugString("\n");
1641#endif
1642 }
1643 if ((log_info->handler_mask & EventHandler) != 0)
1644 {
1645#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1646 (void) NTReportEvent(text,MagickFalse);
1647#endif
1648 }
1649 if ((log_info->handler_mask & FileHandler) != 0)
1650 {
1651 struct stat
1652 file_info;
1653
1654 file_info.st_size=0;
1655 if (log_info->file != (FILE *) NULL)
1656 (void) fstat(fileno(log_info->file),&file_info);
1657 if (file_info.st_size > (MagickOffsetType) log_info->limit)
1658 {
1659 ssize_t
1660 i;
1661
1662 /*
1663 Close log.
1664 */
1665 (void) FormatLocaleFile(log_info->file,"</log>\n");
1666 (void) FormatLocaleFile(log_info->file,"</logEntries>\n");
1667 (void) fclose(log_info->file);
1668 for (i=(ssize_t) log_info->generations-1; i > 0; i--)
1669 {
1670 char
1671 new_filename[MagickPathExtent],
1672 old_filename[MagickPathExtent];
1673
1674 /*
1675 Rotate logs.
1676 */
1677 FormatLogFilename(log_info,i-1,old_filename);
1678 FormatLogFilename(log_info,i,new_filename);
1679 (void) rename_utf8(old_filename,new_filename);
1680 }
1681 log_info->file=(FILE *) NULL;
1682 }
1683 if (log_info->file == (FILE *) NULL)
1684 {
1685 char
1686 log_filename[MagickPathExtent];
1687
1688 FormatLogFilename(log_info,0,log_filename);
1689 log_info->append=IsPathAccessible(log_filename);
1690 log_info->file=fopen_utf8(log_filename,"ab");
1691 if (log_info->file == (FILE *) NULL)
1692 {
1693 UnlockSemaphoreInfo(log_info->event_semaphore);
1694 return(MagickFalse);
1695 }
1696 if (log_info->append == MagickFalse)
1697 {
1698 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1699 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1700 (void) FormatLocaleFile(log_info->file,"<logEntries>\n");
1701 }
1702 (void) FormatLocaleFile(log_info->file,"<log>\n");
1703 }
1704 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
1705 (void) fflush(log_info->file);
1706 }
1707 if ((log_info->handler_mask & MethodHandler) != 0)
1708 {
1709 if (log_info->method != (MagickLogMethod) NULL)
1710 log_info->method(type,text);
1711 }
1712 if ((log_info->handler_mask & StdoutHandler) != 0)
1713 {
1714 (void) FormatLocaleFile(stdout,"%s\n",text);
1715 (void) fflush(stdout);
1716 }
1717 if ((log_info->handler_mask & StderrHandler) != 0)
1718 {
1719 (void) FormatLocaleFile(stderr,"%s\n",text);
1720 (void) fflush(stderr);
1721 }
1722 text=(char *) RelinquishMagickMemory(text);
1723 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1724 UnlockSemaphoreInfo(log_info->event_semaphore);
1725 return(MagickTrue);
1726}
1727
1728MagickExport MagickBooleanType LogMagickEvent(const LogEventType type,
1729 const char *module,const char *function,const size_t line,
1730 const char *format,...)
1731{
1732 va_list
1733 operands;
1734
1735 MagickBooleanType
1736 status;
1737
1738 if (IsEventLogging() == MagickFalse)
1739 return(MagickFalse);
1740 va_start(operands,format);
1741 status=LogMagickEventList(type,module,function,line,format,operands);
1742 va_end(operands);
1743 return(status);
1744}
1745␌
1746#if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
1747/*
1748%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1749% %
1750% %
1751% %
1752% P a r s e L o g H a n d l e r s %
1753% %
1754% %
1755% %
1756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1757%
1758% ParseLogHandlers() parses a string defining which handlers takes a log
1759% message and exports them.
1760%
1761% The format of the ParseLogHandlers method is:
1762%
1763% LogHandlerType ParseLogHandlers(const char *handlers)
1764%
1765% A description of each parameter follows:
1766%
1767% o handlers: one or more handlers separated by commas.
1768%
1769*/
1770static LogHandlerType ParseLogHandlers(const char *handlers)
1771{
1772 LogHandlerType
1773 handler_mask;
1774
1775 const char
1776 *p;
1777
1778 ssize_t
1779 i;
1780
1781 size_t
1782 length;
1783
1784 handler_mask=NoHandler;
1785 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1786 {
1787 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1788 (*p == ',')))
1789 p++;
1790 for (i=0; *LogHandlers[i].name != '\0'; i++)
1791 {
1792 length=strlen(LogHandlers[i].name);
1793 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1794 {
1795 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1796 break;
1797 }
1798 }
1799 if (*LogHandlers[i].name == '\0')
1800 return(UndefinedHandler);
1801 }
1802 return(handler_mask);
1803}
1804#endif
1805␌
1806/*
1807%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1808% %
1809% %
1810% %
1811% S e t L o g E v e n t M a s k %
1812% %
1813% %
1814% %
1815%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1816%
1817% SetLogEventMask() accepts a list that determines which events to log. All
1818% other events are ignored. By default, no debug is enabled. This method
1819% returns the previous log event mask.
1820%
1821% The format of the SetLogEventMask method is:
1822%
1823% LogEventType SetLogEventMask(const char *events)
1824%
1825% A description of each parameter follows:
1826%
1827% o events: log these events.
1828%
1829*/
1830MagickExport LogEventType SetLogEventMask(const char *events)
1831{
1832 ExceptionInfo
1833 *exception;
1834
1835 LogEventType
1836 event_mask;
1837
1838 LogInfo
1839 *log_info;
1840
1841 ssize_t
1842 option;
1843
1844 event_mask=UndefinedEvents;
1845 exception=AcquireExceptionInfo();
1846 log_info=GetLogInfo("*",exception);
1847 exception=DestroyExceptionInfo(exception);
1848 if (log_info == (LogInfo *) NULL)
1849 return(event_mask);
1850 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1851 LockSemaphoreInfo(log_semaphore);
1852 event_mask=log_info->event_mask;
1853 if (option == -1)
1854 log_info->event_mask=UndefinedEvents;
1855 else
1856 log_info->event_mask=(LogEventType) option;
1857 CheckEventLogging();
1858 UnlockSemaphoreInfo(log_semaphore);
1859 return(event_mask);
1860}
1861␌
1862/*
1863%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1864% %
1865% %
1866% %
1867% S e t L o g F o r m a t %
1868% %
1869% %
1870% %
1871%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1872%
1873% SetLogFormat() sets the format for the "human readable" log record.
1874%
1875% The format of the LogMagickFormat method is:
1876%
1877% SetLogFormat(const char *format)
1878%
1879% A description of each parameter follows:
1880%
1881% o format: the log record format.
1882%
1883*/
1884MagickExport void SetLogFormat(const char *format)
1885{
1886 LogInfo
1887 *log_info;
1888
1889 ExceptionInfo
1890 *exception;
1891
1892 exception=AcquireExceptionInfo();
1893 log_info=(LogInfo *) GetLogInfo("*",exception);
1894 exception=DestroyExceptionInfo(exception);
1895 if (log_info == (LogInfo *) NULL)
1896 return;
1897 LockSemaphoreInfo(log_semaphore);
1898 if (log_info->format != (char *) NULL)
1899 log_info->format=DestroyString(log_info->format);
1900 log_info->format=ConstantString(format);
1901 UnlockSemaphoreInfo(log_semaphore);
1902}
1903␌
1904/*
1905%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1906% %
1907% %
1908% %
1909% S e t L o g M e t h o d %
1910% %
1911% %
1912% %
1913%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1914%
1915% SetLogMethod() sets the method that will be called when an event is logged.
1916%
1917% The format of the SetLogMethod method is:
1918%
1919% void SetLogMethod(MagickLogMethod method)
1920%
1921% A description of each parameter follows:
1922%
1923% o method: pointer to a method that will be called when LogMagickEvent is
1924% being called.
1925%
1926*/
1927MagickExport void SetLogMethod(MagickLogMethod method)
1928{
1929 ExceptionInfo
1930 *exception;
1931
1932 LogInfo
1933 *log_info;
1934
1935 exception=AcquireExceptionInfo();
1936 log_info=(LogInfo *) GetLogInfo("*",exception);
1937 exception=DestroyExceptionInfo(exception);
1938 if (log_info == (LogInfo *) NULL)
1939 return;
1940 LockSemaphoreInfo(log_semaphore);
1941 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1942 log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1943 MethodHandler);
1944 log_info->method=method;
1945 UnlockSemaphoreInfo(log_semaphore);
1946}
1947␌
1948/*
1949%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1950% %
1951% %
1952% %
1953% S e t L o g N a m e %
1954% %
1955% %
1956% %
1957%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1958%
1959% SetLogName() sets the log name and returns it.
1960%
1961% The format of the SetLogName method is:
1962%
1963% const char *SetLogName(const char *name)
1964%
1965% A description of each parameter follows:
1966%
1967% o log_name: SetLogName() returns the current client name.
1968%
1969% o name: Specifies the new client name.
1970%
1971*/
1972MagickExport const char *SetLogName(const char *name)
1973{
1974 if ((name != (char *) NULL) && (*name != '\0'))
1975 (void) CopyMagickString(log_name,name,MagickPathExtent);
1976 return(log_name);
1977}