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