2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Log Events %
20 % Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % https://imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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/log.h"
51 #include "MagickCore/log-private.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/memory-private.h"
54 #include "MagickCore/nt-base-private.h"
55 #include "MagickCore/option.h"
56 #include "MagickCore/semaphore.h"
57 #include "MagickCore/timer.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/string-private.h"
60 #include "MagickCore/thread_.h"
61 #include "MagickCore/thread-private.h"
62 #include "MagickCore/timer-private.h"
63 #include "MagickCore/token.h"
64 #include "MagickCore/utility.h"
65 #include "MagickCore/utility-private.h"
66 #include "MagickCore/version.h"
67 #include "MagickCore/xml-tree.h"
68 #include "MagickCore/xml-tree-private.h"
73 #define LogFilename "log.xml"
80 UndefinedHandler = 0x0000,
82 ConsoleHandler = 0x0001,
83 StdoutHandler = 0x0002,
84 StderrHandler = 0x0004,
86 DebugHandler = 0x0010,
87 EventHandler = 0x0020,
88 MethodHandler = 0x0040
91 typedef struct _EventInfo
100 typedef struct _HandlerInfo
150 typedef struct _LogMapInfo
166 static const HandlerInfo
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 }
203 static const LogMapInfo
206 { NoEvents, ConsoleHandler, "Magick-%g.log",
207 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n %e" }
211 log_name[MagickPathExtent] = "Magick";
213 static LinkedListInfo
214 *log_cache = (LinkedListInfo *) NULL;
216 static MagickBooleanType
217 event_logging = MagickFalse;
220 *log_semaphore = (SemaphoreInfo *) NULL;
223 Forward declarations.
225 static LogHandlerType
226 ParseLogHandlers(const char *) magick_attribute((__pure__));
229 *GetLogInfo(const char *,ExceptionInfo *);
231 static MagickBooleanType
232 IsLogCacheInstantiated(ExceptionInfo *) magick_attribute((__pure__)),
233 LoadLogCache(LinkedListInfo *,const char *,const char *,const size_t,
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % A c q u i r e L o g C a c h e %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % AcquireLogCache() caches one or more log configurations which provides a
248 % mapping between log attributes and log name.
250 % The format of the AcquireLogCache method is:
252 % LinkedListInfo *AcquireLogCache(const char *filename,
253 % ExceptionInfo *exception)
255 % A description of each parameter follows:
257 % o filename: the log configuration filename.
259 % o exception: return any errors or warnings in this structure.
262 static LinkedListInfo *AcquireLogCache(const char *filename,
263 ExceptionInfo *exception)
275 Load external log map.
277 cache=NewLinkedList(0);
279 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
287 options=GetConfigureOptions(filename,exception);
288 option=(const StringInfo *) GetNextValueInLinkedList(options);
289 while (option != (const StringInfo *) NULL)
291 status&=LoadLogCache(cache,(const char *) GetStringInfoDatum(option),
292 GetStringInfoPath(option),0,exception);
293 option=(const StringInfo *) GetNextValueInLinkedList(options);
295 options=DestroyConfigureOptions(options);
299 Load built-in log map.
301 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
306 register const LogMapInfo
310 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
311 if (log_info == (LogInfo *) NULL)
313 (void) ThrowMagickException(exception,GetMagickModule(),
314 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->filename);
317 (void) memset(log_info,0,sizeof(*log_info));
318 log_info->path=ConstantString("[built-in]");
319 GetTimerInfo((TimerInfo *) &log_info->timer);
320 log_info->event_mask=p->event_mask;
321 log_info->handler_mask=p->handler_mask;
322 log_info->filename=ConstantString(p->filename);
323 log_info->format=ConstantString(p->format);
324 log_info->signature=MagickCoreSignature;
325 status&=AppendValueToLinkedList(cache,log_info);
326 if (status == MagickFalse)
327 (void) ThrowMagickException(exception,GetMagickModule(),
328 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 % C l o s e M a g i c k L o g %
342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 % CloseMagickLog() closes the Magick log.
346 % The format of the CloseMagickLog method is:
348 % CloseMagickLog(void)
351 MagickExport void CloseMagickLog(void)
359 if (IsEventLogging() == MagickFalse)
361 exception=AcquireExceptionInfo();
362 log_info=GetLogInfo("*",exception);
363 exception=DestroyExceptionInfo(exception);
364 LockSemaphoreInfo(log_semaphore);
365 if (log_info->file != (FILE *) NULL)
367 (void) FormatLocaleFile(log_info->file,"</log>\n");
368 (void) fclose(log_info->file);
369 log_info->file=(FILE *) NULL;
371 UnlockSemaphoreInfo(log_semaphore);
375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
379 + G e t L o g I n f o %
383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385 % GetLogInfo() searches the log list for the specified name and if found
386 % returns attributes for that log.
388 % The format of the GetLogInfo method is:
390 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
392 % A description of each parameter follows:
394 % o name: the log name.
396 % o exception: return any errors or warnings in this structure.
399 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
404 assert(exception != (ExceptionInfo *) NULL);
405 if (IsLogCacheInstantiated(exception) == MagickFalse)
406 return((LogInfo *) NULL);
410 LockSemaphoreInfo(log_semaphore);
411 ResetLinkedListIterator(log_cache);
412 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
413 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
415 UnlockSemaphoreInfo(log_semaphore);
418 while (p != (LogInfo *) NULL)
420 if (LocaleCompare(name,p->name) == 0)
422 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
424 if (p != (LogInfo *) NULL)
425 (void) InsertValueInLinkedList(log_cache,0,
426 RemoveElementByValueFromLinkedList(log_cache,p));
427 UnlockSemaphoreInfo(log_semaphore);
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % G e t L o g I n f o L i s t %
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 % GetLogInfoList() returns any logs that match the specified pattern.
444 % The format of the GetLogInfoList function is:
446 % const LogInfo **GetLogInfoList(const char *pattern,
447 % size_t *number_preferences,ExceptionInfo *exception)
449 % A description of each parameter follows:
451 % o pattern: Specifies a pointer to a text string containing a pattern.
453 % o number_preferences: This integer returns the number of logs in the list.
455 % o exception: return any errors or warnings in this structure.
458 #if defined(__cplusplus) || defined(c_plusplus)
462 static int LogInfoCompare(const void *x,const void *y)
468 p=(const LogInfo **) x,
469 q=(const LogInfo **) y;
470 if (LocaleCompare((*p)->path,(*q)->path) == 0)
471 return(LocaleCompare((*p)->name,(*q)->name));
472 return(LocaleCompare((*p)->path,(*q)->path));
475 #if defined(__cplusplus) || defined(c_plusplus)
479 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
480 size_t *number_preferences,ExceptionInfo *exception)
485 register const LogInfo
494 assert(pattern != (char *) NULL);
495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
496 assert(number_preferences != (size_t *) NULL);
497 *number_preferences=0;
498 p=GetLogInfo("*",exception);
499 if (p == (const LogInfo *) NULL)
500 return((const LogInfo **) NULL);
501 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
502 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
503 if (preferences == (const LogInfo **) NULL)
504 return((const LogInfo **) NULL);
508 LockSemaphoreInfo(log_semaphore);
509 ResetLinkedListIterator(log_cache);
510 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
511 for (i=0; p != (const LogInfo *) NULL; )
513 if ((p->stealth == MagickFalse) &&
514 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
516 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
518 UnlockSemaphoreInfo(log_semaphore);
519 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
520 preferences[i]=(LogInfo *) NULL;
521 *number_preferences=(size_t) i;
526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530 % G e t L o g L i s t %
534 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536 % GetLogList() returns any logs that match the specified pattern.
538 % The format of the GetLogList function is:
540 % char **GetLogList(const char *pattern,size_t *number_preferences,
541 % ExceptionInfo *exception)
543 % A description of each parameter follows:
545 % o pattern: Specifies a pointer to a text string containing a pattern.
547 % o number_preferences: This integer returns the number of logs in the list.
549 % o exception: return any errors or warnings in this structure.
553 #if defined(__cplusplus) || defined(c_plusplus)
557 static int LogCompare(const void *x,const void *y)
565 return(LocaleCompare(*p,*q));
568 #if defined(__cplusplus) || defined(c_plusplus)
572 MagickExport char **GetLogList(const char *pattern,size_t *number_preferences,
573 ExceptionInfo *exception)
578 register const LogInfo
587 assert(pattern != (char *) NULL);
588 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
589 assert(number_preferences != (size_t *) NULL);
590 *number_preferences=0;
591 p=GetLogInfo("*",exception);
592 if (p == (const LogInfo *) NULL)
593 return((char **) NULL);
594 preferences=(char **) AcquireQuantumMemory((size_t)
595 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
596 if (preferences == (char **) NULL)
597 return((char **) NULL);
601 LockSemaphoreInfo(log_semaphore);
602 ResetLinkedListIterator(log_cache);
603 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
604 for (i=0; p != (const LogInfo *) NULL; )
606 if ((p->stealth == MagickFalse) &&
607 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
608 preferences[i++]=ConstantString(p->name);
609 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
611 UnlockSemaphoreInfo(log_semaphore);
612 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
613 preferences[i]=(char *) NULL;
614 *number_preferences=(size_t) i;
619 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623 % G e t L o g N a m e %
627 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 % GetLogName() returns the current log name.
631 % The format of the GetLogName method is:
633 % const char *GetLogName(void)
636 MagickExport const char *GetLogName(void)
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 + I s L o g C a c h e I n s t a n t i a t e d %
650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 % IsLogCacheInstantiated() determines if the log list is instantiated. If
653 % not, it instantiates the list and returns it.
655 % The format of the IsLogInstantiated method is:
657 % MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
659 % A description of each parameter follows.
661 % o exception: return any errors or warnings in this structure.
665 static inline void CheckEventLogging()
668 Are we logging events?
670 if (IsLinkedListEmpty(log_cache) != MagickFalse)
671 event_logging=MagickFalse;
677 ResetLinkedListIterator(log_cache);
678 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
679 event_logging=(p != (LogInfo *) NULL) && (p->event_mask != NoEvents) ?
680 MagickTrue: MagickFalse;
684 static MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
686 if (log_cache == (LinkedListInfo *) NULL)
688 if (log_semaphore == (SemaphoreInfo *) NULL)
689 ActivateSemaphoreInfo(&log_semaphore);
690 LockSemaphoreInfo(log_semaphore);
691 if (log_cache == (LinkedListInfo *) NULL)
693 log_cache=AcquireLogCache(LogFilename,exception);
696 UnlockSemaphoreInfo(log_semaphore);
698 return(log_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 % I s E v e n t L o g g i n g %
710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
712 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
715 % The format of the IsEventLogging method is:
717 % MagickBooleanType IsEventLogging(void)
720 MagickExport MagickBooleanType IsEventLogging(void)
722 return(event_logging);
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 % L i s t L o g I n f o %
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736 % ListLogInfo() lists the log info to a file.
738 % The format of the ListLogInfo method is:
740 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
742 % A description of each parameter follows.
744 % o file: An pointer to a FILE.
746 % o exception: return any errors or warnings in this structure.
749 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
751 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
768 if (file == (const FILE *) NULL)
770 log_info=GetLogInfoList("*",&number_aliases,exception);
771 if (log_info == (const LogInfo **) NULL)
774 path=(const char *) NULL;
775 for (i=0; i < (ssize_t) number_aliases; i++)
777 if (log_info[i]->stealth != MagickFalse)
779 if ((path == (const char *) NULL) ||
780 (LocaleCompare(path,log_info[i]->path) != 0))
785 if (log_info[i]->path != (char *) NULL)
786 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
788 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
793 if (*LogHandlers[j].name == '\0')
797 if ((log_info[i]->handler_mask & mask) != 0)
799 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
800 length+=strlen(LogHandlers[j].name);
803 for (j=(ssize_t) length; j <= 12; j++)
804 (void) FormatLocaleFile(file," ");
805 (void) FormatLocaleFile(file," Generations Limit Format\n");
806 (void) FormatLocaleFile(file,"-----------------------------------------"
807 "--------------------------------------\n");
809 path=log_info[i]->path;
810 if (log_info[i]->filename != (char *) NULL)
812 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
813 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
814 (void) FormatLocaleFile(file," ");
816 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
817 (void) FormatLocaleFile(file,"%8g ",(double) log_info[i]->limit);
818 if (log_info[i]->format != (char *) NULL)
819 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
820 (void) FormatLocaleFile(file,"\n");
823 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
832 + L o g C o m p o n e n t G e n e s i s %
836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
838 % LogComponentGenesis() instantiates the log component.
840 % The format of the LogComponentGenesis method is:
842 % MagickBooleanType LogComponentGenesis(void)
845 MagickPrivate MagickBooleanType LogComponentGenesis(void)
850 if (log_semaphore == (SemaphoreInfo *) NULL)
851 log_semaphore=AcquireSemaphoreInfo();
852 exception=AcquireExceptionInfo();
853 (void) GetLogInfo("*",exception);
854 exception=DestroyExceptionInfo(exception);
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 + L o g C o m p o n e n t T e r m i n u s %
867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869 % LogComponentTerminus() destroys the logging component.
871 % The format of the LogComponentTerminus method is:
873 % LogComponentTerminus(void)
877 static void *DestroyLogElement(void *log_info)
882 p=(LogInfo *) log_info;
883 if (p->file != (FILE *) NULL)
885 (void) FormatLocaleFile(p->file,"</log>\n");
886 (void) fclose(p->file);
887 p->file=(FILE *) NULL;
889 if (p->format != (char *) NULL)
890 p->format=DestroyString(p->format);
891 if (p->path != (char *) NULL)
892 p->path=DestroyString(p->path);
893 if (p->filename != (char *) NULL)
894 p->filename=DestroyString(p->filename);
895 if (p->event_semaphore != (SemaphoreInfo *) NULL)
896 RelinquishSemaphoreInfo(&p->event_semaphore);
897 p=(LogInfo *) RelinquishMagickMemory(p);
898 return((void *) NULL);
901 MagickPrivate void LogComponentTerminus(void)
903 if (log_semaphore == (SemaphoreInfo *) NULL)
904 ActivateSemaphoreInfo(&log_semaphore);
905 LockSemaphoreInfo(log_semaphore);
906 if (log_cache != (LinkedListInfo *) NULL)
907 log_cache=DestroyLinkedList(log_cache,DestroyLogElement);
908 event_logging=MagickFalse;
909 UnlockSemaphoreInfo(log_semaphore);
910 RelinquishSemaphoreInfo(&log_semaphore);
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 % L o g M a g i c k E v e n t %
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % LogMagickEvent() logs an event as determined by the log configuration file.
925 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
927 % The format of the LogMagickEvent method is:
929 % MagickBooleanType LogMagickEvent(const LogEventType type,
930 % const char *module,const char *function,const size_t line,
931 % const char *format,...)
933 % A description of each parameter follows:
935 % o type: the event type.
937 % o filename: the source module filename.
939 % o function: the function name.
941 % o line: the line number of the source module.
943 % o format: the output format.
946 static char *TranslateEvent(const char *module,const char *function,
947 const size_t line,const char *domain,const char *event)
974 exception=AcquireExceptionInfo();
975 log_info=(LogInfo *) GetLogInfo("*",exception);
976 exception=DestroyExceptionInfo(exception);
977 seconds=GetMagickTime();
978 elapsed_time=GetElapsedTime(&log_info->timer);
979 user_time=GetUserTime(&log_info->timer);
980 text=AcquireString(event);
981 if (log_info->format == (char *) NULL)
983 extent=strlen(event)+MagickPathExtent;
984 if (LocaleCompare(log_info->format,"xml") == 0)
987 timestamp[MagickPathExtent];
990 Translate event in "XML" format.
992 (void) FormatMagickTime(seconds,extent,timestamp);
993 (void) FormatLocaleString(text,extent,
995 " <timestamp>%s</timestamp>\n"
996 " <elapsed-time>%lu:%02lu.%06lu</elapsed-time>\n"
997 " <user-time>%0.3f</user-time>\n"
998 " <process-id>%.20g</process-id>\n"
999 " <thread-id>%.20g</thread-id>\n"
1000 " <module>%s</module>\n"
1001 " <function>%s</function>\n"
1002 " <line>%.20g</line>\n"
1003 " <domain>%s</domain>\n"
1004 " <event>%s</event>\n"
1005 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
1006 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
1007 (1000000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
1008 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
1009 (double) line,domain,event);
1013 Translate event in "human readable" format.
1016 for (p=log_info->format; *p != '\0'; p++)
1019 if ((size_t) (q-text+MagickPathExtent) >= extent)
1021 extent+=MagickPathExtent;
1022 text=(char *) ResizeQuantumMemory(text,extent+MagickPathExtent,
1024 if (text == (char *) NULL)
1025 return((char *) NULL);
1026 q=text+strlen(text);
1029 The format of the log is defined by embedding special format characters:
1049 if ((*p == '\\') && (*(p+1) == 'r'))
1055 if ((*p == '\\') && (*(p+1) == 'n'))
1073 q+=CopyMagickString(q,GetClientName(),extent);
1078 q+=CopyMagickString(q,domain,extent);
1083 q+=CopyMagickString(q,event,extent);
1088 q+=CopyMagickString(q,function,extent);
1093 if (log_info->generations == 0)
1095 (void) CopyMagickString(q,"0",extent);
1099 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1100 log_info->generations));
1105 q+=FormatLocaleString(q,extent,"%.20g",(double)
1106 GetMagickThreadSignature());
1111 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
1119 for (r=module+strlen(module)-1; r > module; r--)
1120 if (*r == *DirectorySeparator)
1125 q+=CopyMagickString(q,r,extent);
1130 q+=CopyMagickString(q,GetLogName(),extent);
1135 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1140 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
1141 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
1142 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
1147 q+=FormatMagickTime(seconds,extent,q);
1152 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
1157 q+=CopyMagickString(q,MagickLibVersionText,extent);
1177 static char *TranslateFilename(const LogInfo *log_info)
1192 Translate event in "human readable" format.
1194 assert(log_info != (LogInfo *) NULL);
1195 assert(log_info->filename != (char *) NULL);
1196 filename=AcquireString((char *) NULL);
1197 extent=MagickPathExtent;
1199 for (p=log_info->filename; *p != '\0'; p++)
1202 if ((size_t) (q-filename+MagickPathExtent) >= extent)
1204 extent+=MagickPathExtent;
1205 filename=(char *) ResizeQuantumMemory(filename,extent+MagickPathExtent,
1207 if (filename == (char *) NULL)
1208 return((char *) NULL);
1209 q=filename+strlen(filename);
1212 The format of the filename is defined by embedding special format
1238 q+=CopyMagickString(q,GetClientName(),extent);
1243 if (log_info->generations == 0)
1245 (void) CopyMagickString(q,"0",extent);
1249 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1250 log_info->generations));
1255 q+=CopyMagickString(q,GetLogName(),extent);
1260 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1265 q+=CopyMagickString(q,MagickLibVersionText,extent);
1285 MagickExport MagickBooleanType LogMagickEventList(const LogEventType type,
1286 const char *module,const char *function,const size_t line,const char *format,
1290 event[MagickPathExtent],
1305 exception=AcquireExceptionInfo();
1306 log_info=(LogInfo *) GetLogInfo("*",exception);
1307 exception=DestroyExceptionInfo(exception);
1308 if (log_info->event_semaphore == (SemaphoreInfo *) NULL)
1309 ActivateSemaphoreInfo(&log_info->event_semaphore);
1310 LockSemaphoreInfo(log_info->event_semaphore);
1311 if ((log_info->event_mask & type) == 0)
1313 UnlockSemaphoreInfo(log_info->event_semaphore);
1316 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1317 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1318 n=vsnprintf(event,MagickPathExtent,format,operands);
1320 n=vsprintf(event,format,operands);
1323 event[MagickPathExtent-1]='\0';
1324 text=TranslateEvent(module,function,line,domain,event);
1325 if (text == (char *) NULL)
1327 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1328 UnlockSemaphoreInfo(log_info->event_semaphore);
1329 return(MagickFalse);
1331 if ((log_info->handler_mask & ConsoleHandler) != 0)
1333 (void) FormatLocaleFile(stderr,"%s\n",text);
1334 (void) fflush(stderr);
1336 if ((log_info->handler_mask & DebugHandler) != 0)
1338 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1339 OutputDebugString(text);
1340 OutputDebugString("\n");
1343 if ((log_info->handler_mask & EventHandler) != 0)
1345 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1346 (void) NTReportEvent(text,MagickFalse);
1349 if ((log_info->handler_mask & FileHandler) != 0)
1354 file_info.st_size=0;
1355 if (log_info->file != (FILE *) NULL)
1356 (void) fstat(fileno(log_info->file),&file_info);
1357 if (file_info.st_size > (MagickOffsetType) (1024*1024*log_info->limit))
1359 (void) FormatLocaleFile(log_info->file,"</log>\n");
1360 (void) fclose(log_info->file);
1361 log_info->file=(FILE *) NULL;
1363 if (log_info->file == (FILE *) NULL)
1368 filename=TranslateFilename(log_info);
1369 if (filename == (char *) NULL)
1371 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1372 UnlockSemaphoreInfo(log_info->event_semaphore);
1373 return(MagickFalse);
1375 log_info->append=IsPathAccessible(filename);
1376 log_info->file=fopen_utf8(filename,"ab");
1377 filename=(char *) RelinquishMagickMemory(filename);
1378 if (log_info->file == (FILE *) NULL)
1380 UnlockSemaphoreInfo(log_info->event_semaphore);
1381 return(MagickFalse);
1383 log_info->generation++;
1384 if (log_info->append == MagickFalse)
1385 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1386 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1387 (void) FormatLocaleFile(log_info->file,"<log>\n");
1389 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
1390 (void) fflush(log_info->file);
1392 if ((log_info->handler_mask & MethodHandler) != 0)
1394 if (log_info->method != (MagickLogMethod) NULL)
1395 log_info->method(type,text);
1397 if ((log_info->handler_mask & StdoutHandler) != 0)
1399 (void) FormatLocaleFile(stdout,"%s\n",text);
1400 (void) fflush(stdout);
1402 if ((log_info->handler_mask & StderrHandler) != 0)
1404 (void) FormatLocaleFile(stderr,"%s\n",text);
1405 (void) fflush(stderr);
1407 text=(char *) RelinquishMagickMemory(text);
1408 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1409 UnlockSemaphoreInfo(log_info->event_semaphore);
1413 MagickExport MagickBooleanType LogMagickEvent(const LogEventType type,
1414 const char *module,const char *function,const size_t line,
1415 const char *format,...)
1423 if (IsEventLogging() == MagickFalse)
1424 return(MagickFalse);
1425 va_start(operands,format);
1426 status=LogMagickEventList(type,module,function,line,format,operands);
1432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1436 + L o a d L o g C a c h e %
1440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442 % LoadLogCache() loads the log configurations which provides a
1443 % mapping between log attributes and log name.
1445 % The format of the LoadLogCache method is:
1447 % MagickBooleanType LoadLogCache(LinkedListInfo *cache,const char *xml,
1448 % const char *filename,const size_t depth,ExceptionInfo *exception)
1450 % A description of each parameter follows:
1452 % o xml: The log list in XML format.
1454 % o filename: The log list filename.
1456 % o depth: depth of <include /> statements.
1458 % o exception: return any errors or warnings in this structure.
1461 static MagickBooleanType LoadLogCache(LinkedListInfo *cache,const char *xml,
1462 const char *filename,const size_t depth,ExceptionInfo *exception)
1465 keyword[MagickPathExtent],
1472 *log_info = (LogInfo *) NULL;
1481 Load the log map file.
1483 if (xml == (const char *) NULL)
1484 return(MagickFalse);
1486 token=AcquireString(xml);
1487 extent=strlen(token)+MagickPathExtent;
1488 for (q=(const char *) xml; *q != '\0'; )
1493 (void) GetNextToken(q,&q,extent,token);
1496 (void) CopyMagickString(keyword,token,MagickPathExtent);
1497 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1502 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1503 (void) GetNextToken(q,&q,extent,token);
1506 if (LocaleNCompare(keyword,"<!--",4) == 0)
1511 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1512 (void) GetNextToken(q,&q,extent,token);
1515 if (LocaleCompare(keyword,"<include") == 0)
1520 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1522 (void) CopyMagickString(keyword,token,MagickPathExtent);
1523 (void) GetNextToken(q,&q,extent,token);
1526 (void) GetNextToken(q,&q,extent,token);
1527 if (LocaleCompare(keyword,"file") == 0)
1529 if (depth > MagickMaxRecursionDepth)
1530 (void) ThrowMagickException(exception,GetMagickModule(),
1531 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1535 path[MagickPathExtent],
1538 GetPathComponent(filename,HeadPath,path);
1540 (void) ConcatenateMagickString(path,DirectorySeparator,
1542 if (*token == *DirectorySeparator)
1543 (void) CopyMagickString(path,token,MagickPathExtent);
1545 (void) ConcatenateMagickString(path,token,MagickPathExtent);
1546 file_xml=FileToXML(path,~0UL);
1547 if (file_xml != (char *) NULL)
1549 status&=LoadLogCache(cache,file_xml,path,depth+1,
1551 file_xml=DestroyString(file_xml);
1558 if (LocaleCompare(keyword,"<logmap>") == 0)
1561 Allocate memory for the log list.
1563 log_info=(LogInfo *) AcquireCriticalMemory(sizeof(*log_info));
1564 (void) memset(log_info,0,sizeof(*log_info));
1565 log_info->path=ConstantString(filename);
1566 GetTimerInfo((TimerInfo *) &log_info->timer);
1567 log_info->signature=MagickCoreSignature;
1570 if (log_info == (LogInfo *) NULL)
1572 if (LocaleCompare(keyword,"</logmap>") == 0)
1574 status=AppendValueToLinkedList(cache,log_info);
1575 if (status == MagickFalse)
1576 (void) ThrowMagickException(exception,GetMagickModule(),
1577 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1578 log_info=(LogInfo *) NULL;
1581 (void) GetNextToken(q,(const char **) NULL,extent,token);
1584 (void) GetNextToken(q,&q,extent,token);
1585 (void) GetNextToken(q,&q,extent,token);
1591 if (LocaleCompare((char *) keyword,"events") == 0)
1593 log_info->event_mask=(LogEventType) (log_info->event_mask |
1594 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1602 if (LocaleCompare((char *) keyword,"filename") == 0)
1604 if (log_info->filename != (char *) NULL)
1605 log_info->filename=(char *)
1606 RelinquishMagickMemory(log_info->filename);
1607 log_info->filename=ConstantString(token);
1610 if (LocaleCompare((char *) keyword,"format") == 0)
1612 if (log_info->format != (char *) NULL)
1613 log_info->format=(char *)
1614 RelinquishMagickMemory(log_info->format);
1615 log_info->format=ConstantString(token);
1623 if (LocaleCompare((char *) keyword,"generations") == 0)
1625 if (LocaleCompare(token,"unlimited") == 0)
1627 log_info->generations=(~0UL);
1630 log_info->generations=StringToUnsignedLong(token);
1638 if (LocaleCompare((char *) keyword,"limit") == 0)
1640 if (LocaleCompare(token,"unlimited") == 0)
1642 log_info->limit=(~0UL);
1645 log_info->limit=StringToUnsignedLong(token);
1653 if (LocaleCompare((char *) keyword,"output") == 0)
1655 log_info->handler_mask=(LogHandlerType)
1656 (log_info->handler_mask | ParseLogHandlers(token));
1665 token=DestroyString(token);
1666 if (cache == (LinkedListInfo *) NULL)
1667 return(MagickFalse);
1668 return(status != 0 ? MagickTrue : MagickFalse);
1672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1676 + P a r s e L o g H a n d l e r s %
1680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1682 % ParseLogHandlers() parses a string defining which handlers takes a log
1683 % message and exports them.
1685 % The format of the ParseLogHandlers method is:
1687 % LogHandlerType ParseLogHandlers(const char *handlers)
1689 % A description of each parameter follows:
1691 % o handlers: one or more handlers separated by commas.
1694 static LogHandlerType ParseLogHandlers(const char *handlers)
1708 handler_mask=NoHandler;
1709 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1711 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1714 for (i=0; *LogHandlers[i].name != '\0'; i++)
1716 length=strlen(LogHandlers[i].name);
1717 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1719 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1723 if (*LogHandlers[i].name == '\0')
1724 return(UndefinedHandler);
1726 return(handler_mask);
1730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1734 % S e t L o g E v e n t M a s k %
1738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1740 % SetLogEventMask() accepts a list that determines which events to log. All
1741 % other events are ignored. By default, no debug is enabled. This method
1742 % returns the previous log event mask.
1744 % The format of the SetLogEventMask method is:
1746 % LogEventType SetLogEventMask(const char *events)
1748 % A description of each parameter follows:
1750 % o events: log these events.
1753 MagickExport LogEventType SetLogEventMask(const char *events)
1764 exception=AcquireExceptionInfo();
1765 log_info=(LogInfo *) GetLogInfo("*",exception);
1766 exception=DestroyExceptionInfo(exception);
1767 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1768 LockSemaphoreInfo(log_semaphore);
1769 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1770 log_info->event_mask=(LogEventType) option;
1772 log_info->event_mask=UndefinedEvents;
1773 CheckEventLogging();
1774 UnlockSemaphoreInfo(log_semaphore);
1775 return(log_info->event_mask);
1779 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1783 % S e t L o g F o r m a t %
1787 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1789 % SetLogFormat() sets the format for the "human readable" log record.
1791 % The format of the LogMagickFormat method is:
1793 % SetLogFormat(const char *format)
1795 % A description of each parameter follows:
1797 % o format: the log record format.
1800 MagickExport void SetLogFormat(const char *format)
1808 exception=AcquireExceptionInfo();
1809 log_info=(LogInfo *) GetLogInfo("*",exception);
1810 exception=DestroyExceptionInfo(exception);
1811 LockSemaphoreInfo(log_semaphore);
1812 if (log_info->format != (char *) NULL)
1813 log_info->format=DestroyString(log_info->format);
1814 log_info->format=ConstantString(format);
1815 UnlockSemaphoreInfo(log_semaphore);
1819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1823 % S e t L o g M e t h o d %
1827 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829 % SetLogMethod() sets the method that will be called when an event is logged.
1831 % The format of the SetLogMethod method is:
1833 % void SetLogMethod(MagickLogMethod method)
1835 % A description of each parameter follows:
1837 % o method: pointer to a method that will be called when LogMagickEvent is
1841 MagickExport void SetLogMethod(MagickLogMethod method)
1849 exception=AcquireExceptionInfo();
1850 log_info=(LogInfo *) GetLogInfo("*",exception);
1851 exception=DestroyExceptionInfo(exception);
1852 LockSemaphoreInfo(log_semaphore);
1853 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1854 log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1856 log_info->method=method;
1857 UnlockSemaphoreInfo(log_semaphore);
1861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1865 % S e t L o g N a m e %
1869 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1871 % SetLogName() sets the log name and returns it.
1873 % The format of the SetLogName method is:
1875 % const char *SetLogName(const char *name)
1877 % A description of each parameter follows:
1879 % o log_name: SetLogName() returns the current client name.
1881 % o name: Specifies the new client name.
1884 MagickExport const char *SetLogName(const char *name)
1886 if ((name != (char *) NULL) && (*name != '\0'))
1887 (void) CopyMagickString(log_name,name,MagickPathExtent);