2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Log Events %
20 % Copyright 1999-2014 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 % http://www.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/hashmap.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/log-private.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/nt-base-private.h"
54 #include "MagickCore/option.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/timer.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/string-private.h"
59 #include "MagickCore/token.h"
60 #include "MagickCore/thread_.h"
61 #include "MagickCore/thread-private.h"
62 #include "MagickCore/utility.h"
63 #include "MagickCore/utility-private.h"
64 #include "MagickCore/version.h"
65 #include "MagickCore/xml-tree.h"
66 #include "MagickCore/xml-tree-private.h"
71 #define LogFilename "log.xml"
78 UndefinedHandler = 0x0000,
80 ConsoleHandler = 0x0001,
81 StdoutHandler = 0x0002,
82 StderrHandler = 0x0004,
84 DebugHandler = 0x0010,
85 EventHandler = 0x0020,
86 MethodHandler = 0x0040
89 typedef struct _EventInfo
98 typedef struct _HandlerInfo
145 typedef struct _LogMapInfo
161 static const HandlerInfo
164 { "Console", ConsoleHandler },
165 { "Debug", DebugHandler },
166 { "Event", EventHandler },
167 { "File", FileHandler },
168 { "None", NoHandler },
169 { "Stderr", StderrHandler },
170 { "Stdout", StdoutHandler },
171 { (char *) NULL, UndefinedHandler }
174 static const LogMapInfo
177 { NoEvents, ConsoleHandler, "Magick-%g.log",
178 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n %e" }
182 log_name[MaxTextExtent] = "Magick";
184 static LinkedListInfo
185 *log_cache = (LinkedListInfo *) NULL;
188 *event_semaphore = (SemaphoreInfo *) NULL,
189 *log_semaphore = (SemaphoreInfo *) NULL;
192 Forward declarations.
194 static LogHandlerType
195 ParseLogHandlers(const char *);
198 *GetLogInfo(const char *,ExceptionInfo *);
200 static MagickBooleanType
201 IsLogCacheInstantiated(ExceptionInfo *),
202 LoadLogCache(LinkedListInfo *,const char *,const char *,const size_t,
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 % A c q u i r e L o g C a c h e %
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % AcquireLogCache() caches one or more log configurations which provides a
217 % mapping between log attributes and log name.
219 % The format of the AcquireLogCache method is:
221 % LinkedListInfo *AcquireLogCache(const char *filename,
222 % ExceptionInfo *exception)
224 % A description of each parameter follows:
226 % o filename: the log configuration filename.
228 % o exception: return any errors or warnings in this structure.
231 static LinkedListInfo *AcquireLogCache(const char *filename,
232 ExceptionInfo *exception)
248 Load external log map.
250 log_cache=NewLinkedList(0);
251 if (log_cache == (LinkedListInfo *) NULL)
252 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
254 options=GetConfigureOptions(filename,exception);
255 option=(const StringInfo *) GetNextValueInLinkedList(options);
256 while (option != (const StringInfo *) NULL)
258 status&=LoadLogCache(log_cache,(const char *) GetStringInfoDatum(option),
259 GetStringInfoPath(option),0,exception);
260 option=(const StringInfo *) GetNextValueInLinkedList(options);
262 options=DestroyConfigureOptions(options);
264 Load built-in log map.
266 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
271 register const LogMapInfo
275 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
276 if (log_info == (LogInfo *) NULL)
278 (void) ThrowMagickException(exception,GetMagickModule(),
279 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->filename);
282 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
283 log_info->path=ConstantString("[built-in]");
284 GetTimerInfo((TimerInfo *) &log_info->timer);
285 log_info->event_mask=p->event_mask;
286 log_info->handler_mask=p->handler_mask;
287 log_info->filename=ConstantString(p->filename);
288 log_info->format=ConstantString(p->format);
289 log_info->signature=MagickSignature;
290 status&=AppendValueToLinkedList(log_cache,log_info);
291 if (status == MagickFalse)
292 (void) ThrowMagickException(exception,GetMagickModule(),
293 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % C l o s e M a g i c k L o g %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % CloseMagickLog() closes the Magick log.
311 % The format of the CloseMagickLog method is:
313 % CloseMagickLog(void)
316 MagickExport void CloseMagickLog(void)
324 if (IsEventLogging() == MagickFalse)
326 exception=AcquireExceptionInfo();
327 log_info=GetLogInfo("*",exception);
328 exception=DestroyExceptionInfo(exception);
329 LockSemaphoreInfo(log_semaphore);
330 if (log_info->file != (FILE *) NULL)
332 (void) FormatLocaleFile(log_info->file,"</log>\n");
333 (void) fclose(log_info->file);
334 log_info->file=(FILE *) NULL;
336 UnlockSemaphoreInfo(log_semaphore);
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 + G e t L o g I n f o %
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 % GetLogInfo() searches the log list for the specified name and if found
351 % returns attributes for that log.
353 % The format of the GetLogInfo method is:
355 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
357 % A description of each parameter follows:
359 % o name: the log name.
361 % o exception: return any errors or warnings in this structure.
364 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
369 assert(exception != (ExceptionInfo *) NULL);
370 if (IsLogCacheInstantiated(exception) == MagickFalse)
371 return((LogInfo *) NULL);
375 LockSemaphoreInfo(log_semaphore);
376 ResetLinkedListIterator(log_cache);
377 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
378 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
380 UnlockSemaphoreInfo(log_semaphore);
383 while (p != (LogInfo *) NULL)
385 if (LocaleCompare(name,p->name) == 0)
387 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
389 if (p != (LogInfo *) NULL)
390 (void) InsertValueInLinkedList(log_cache,0,
391 RemoveElementByValueFromLinkedList(log_cache,p));
392 UnlockSemaphoreInfo(log_semaphore);
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 % G e t L o g I n f o L i s t %
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 % GetLogInfoList() returns any logs that match the specified pattern.
409 % The format of the GetLogInfoList function is:
411 % const LogInfo **GetLogInfoList(const char *pattern,
412 % size_t *number_preferences,ExceptionInfo *exception)
414 % A description of each parameter follows:
416 % o pattern: Specifies a pointer to a text string containing a pattern.
418 % o number_preferences: This integer returns the number of logs in the list.
420 % o exception: return any errors or warnings in this structure.
423 #if defined(__cplusplus) || defined(c_plusplus)
427 static int LogInfoCompare(const void *x,const void *y)
433 p=(const LogInfo **) x,
434 q=(const LogInfo **) y;
435 if (LocaleCompare((*p)->path,(*q)->path) == 0)
436 return(LocaleCompare((*p)->name,(*q)->name));
437 return(LocaleCompare((*p)->path,(*q)->path));
440 #if defined(__cplusplus) || defined(c_plusplus)
444 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
445 size_t *number_preferences,ExceptionInfo *exception)
450 register const LogInfo
459 assert(pattern != (char *) NULL);
460 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
461 assert(number_preferences != (size_t *) NULL);
462 *number_preferences=0;
463 p=GetLogInfo("*",exception);
464 if (p == (const LogInfo *) NULL)
465 return((const LogInfo **) NULL);
466 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
467 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
468 if (preferences == (const LogInfo **) NULL)
469 return((const LogInfo **) NULL);
473 LockSemaphoreInfo(log_semaphore);
474 ResetLinkedListIterator(log_cache);
475 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
476 for (i=0; p != (const LogInfo *) NULL; )
478 if ((p->stealth == MagickFalse) &&
479 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
481 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
483 UnlockSemaphoreInfo(log_semaphore);
484 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
485 preferences[i]=(LogInfo *) NULL;
486 *number_preferences=(size_t) i;
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495 % G e t L o g L i s t %
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501 % GetLogList() returns any logs that match the specified pattern.
503 % The format of the GetLogList function is:
505 % char **GetLogList(const char *pattern,size_t *number_preferences,
506 % ExceptionInfo *exception)
508 % A description of each parameter follows:
510 % o pattern: Specifies a pointer to a text string containing a pattern.
512 % o number_preferences: This integer returns the number of logs in the list.
514 % o exception: return any errors or warnings in this structure.
518 #if defined(__cplusplus) || defined(c_plusplus)
522 static int LogCompare(const void *x,const void *y)
530 return(LocaleCompare(*p,*q));
533 #if defined(__cplusplus) || defined(c_plusplus)
537 MagickExport char **GetLogList(const char *pattern,
538 size_t *number_preferences,ExceptionInfo *exception)
543 register const LogInfo
552 assert(pattern != (char *) NULL);
553 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
554 assert(number_preferences != (size_t *) NULL);
555 *number_preferences=0;
556 p=GetLogInfo("*",exception);
557 if (p == (const LogInfo *) NULL)
558 return((char **) NULL);
559 preferences=(char **) AcquireQuantumMemory((size_t)
560 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
561 if (preferences == (char **) NULL)
562 return((char **) NULL);
566 LockSemaphoreInfo(log_semaphore);
567 ResetLinkedListIterator(log_cache);
568 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
569 for (i=0; p != (const LogInfo *) NULL; )
571 if ((p->stealth == MagickFalse) &&
572 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
573 preferences[i++]=ConstantString(p->name);
574 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
576 UnlockSemaphoreInfo(log_semaphore);
577 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
578 preferences[i]=(char *) NULL;
579 *number_preferences=(size_t) i;
584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588 % G e t L o g N a m e %
592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
594 % GetLogName() returns the current log name.
596 % The format of the GetLogName method is:
598 % const char *GetLogName(void)
601 MagickExport const char *GetLogName(void)
607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
611 + I s L o g C a c h e I n s t a n t i a t e d %
615 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
617 % IsLogCacheInstantiated() determines if the log list is instantiated. If
618 % not, it instantiates the list and returns it.
620 % The format of the IsLogInstantiated method is:
622 % MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
624 % A description of each parameter follows.
626 % o exception: return any errors or warnings in this structure.
629 static MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
631 if (log_cache == (LinkedListInfo *) NULL)
633 if (log_semaphore == (SemaphoreInfo *) NULL)
634 ActivateSemaphoreInfo(&log_semaphore);
635 LockSemaphoreInfo(log_semaphore);
636 if (log_cache == (LinkedListInfo *) NULL)
637 log_cache=AcquireLogCache(LogFilename,exception);
638 UnlockSemaphoreInfo(log_semaphore);
640 return(log_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
648 % I s E v e n t L o g g i n g %
652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
657 % The format of the IsEventLogging method is:
659 % MagickBooleanType IsEventLogging(void)
662 MagickExport MagickBooleanType IsEventLogging(void)
670 if ((log_cache == (LinkedListInfo *) NULL) ||
671 (IsLinkedListEmpty(log_cache) != MagickFalse))
673 exception=AcquireExceptionInfo();
674 log_info=GetLogInfo("*",exception);
675 exception=DestroyExceptionInfo(exception);
676 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 % L i s t L o g I n f o %
687 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
689 % ListLogInfo() lists the log info to a file.
691 % The format of the ListLogInfo method is:
693 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
695 % A description of each parameter follows.
697 % o file: An pointer to a FILE.
699 % o exception: return any errors or warnings in this structure.
702 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
704 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
721 if (file == (const FILE *) NULL)
723 log_info=GetLogInfoList("*",&number_aliases,exception);
724 if (log_info == (const LogInfo **) NULL)
727 path=(const char *) NULL;
728 for (i=0; i < (ssize_t) number_aliases; i++)
730 if (log_info[i]->stealth != MagickFalse)
732 if ((path == (const char *) NULL) ||
733 (LocaleCompare(path,log_info[i]->path) != 0))
738 if (log_info[i]->path != (char *) NULL)
739 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
741 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
748 if ((log_info[i]->handler_mask & mask) != 0)
750 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
751 length+=strlen(LogHandlers[j].name);
753 if (LogHandlers[j].name == (const char *) NULL)
756 for (j=(ssize_t) length; j <= 12; j++)
757 (void) FormatLocaleFile(file," ");
758 (void) FormatLocaleFile(file," Generations Limit Format\n");
759 (void) FormatLocaleFile(file,"-----------------------------------------"
760 "--------------------------------------\n");
762 path=log_info[i]->path;
763 if (log_info[i]->filename != (char *) NULL)
765 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
766 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
767 (void) FormatLocaleFile(file," ");
769 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
770 (void) FormatLocaleFile(file,"%8g ",(double) log_info[i]->limit);
771 if (log_info[i]->format != (char *) NULL)
772 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
773 (void) FormatLocaleFile(file,"\n");
776 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
781 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 + L o g C o m p o n e n t G e n e s i s %
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
791 % LogComponentGenesis() instantiates the log component.
793 % The format of the LogComponentGenesis method is:
795 % MagickBooleanType LogComponentGenesis(void)
798 MagickPrivate MagickBooleanType LogComponentGenesis(void)
803 if (log_semaphore == (SemaphoreInfo *) NULL)
804 log_semaphore=AcquireSemaphoreInfo();
805 exception=AcquireExceptionInfo();
806 (void) GetLogInfo("*",exception);
807 exception=DestroyExceptionInfo(exception);
808 event_semaphore=AcquireSemaphoreInfo();
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
817 + L o g C o m p o n e n t T e r m i n u s %
821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
823 % LogComponentTerminus() destroys the logging component.
825 % The format of the LogComponentTerminus method is:
827 % LogComponentTerminus(void)
831 static void *DestroyLogElement(void *log_info)
836 p=(LogInfo *) log_info;
837 if (p->file != (FILE *) NULL)
839 (void) FormatLocaleFile(p->file,"</log>\n");
840 (void) fclose(p->file);
841 p->file=(FILE *) NULL;
843 if (p->format != (char *) NULL)
844 p->format=DestroyString(p->format);
845 if (p->path != (char *) NULL)
846 p->path=DestroyString(p->path);
847 if (p->filename != (char *) NULL)
848 p->filename=DestroyString(p->filename);
849 p=(LogInfo *) RelinquishMagickMemory(p);
850 return((void *) NULL);
853 MagickPrivate void LogComponentTerminus(void)
855 if (event_semaphore == (SemaphoreInfo *) NULL)
856 ActivateSemaphoreInfo(&event_semaphore);
857 LockSemaphoreInfo(event_semaphore);
858 UnlockSemaphoreInfo(event_semaphore);
859 RelinquishSemaphoreInfo(&event_semaphore);
860 if (log_semaphore == (SemaphoreInfo *) NULL)
861 ActivateSemaphoreInfo(&log_semaphore);
862 LockSemaphoreInfo(log_semaphore);
863 if (log_cache != (LinkedListInfo *) NULL)
864 log_cache=DestroyLinkedList(log_cache,DestroyLogElement);
865 UnlockSemaphoreInfo(log_semaphore);
866 RelinquishSemaphoreInfo(&log_semaphore);
870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 % L o g M a g i c k E v e n t %
878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880 % LogMagickEvent() logs an event as determined by the log configuration file.
881 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
883 % The format of the LogMagickEvent method is:
885 % MagickBooleanType LogMagickEvent(const LogEventType type,
886 % const char *module,const char *function,const size_t line,
887 % const char *format,...)
889 % A description of each parameter follows:
891 % o type: the event type.
893 % o filename: the source module filename.
895 % o function: the function name.
897 % o line: the line number of the source module.
899 % o format: the output format.
902 static char *TranslateEvent(const LogEventType magick_unused(type),
903 const char *module,const char *function,const size_t line,
904 const char *domain,const char *event)
931 exception=AcquireExceptionInfo();
932 log_info=(LogInfo *) GetLogInfo("*",exception);
933 exception=DestroyExceptionInfo(exception);
934 seconds=time((time_t *) NULL);
935 elapsed_time=GetElapsedTime(&log_info->timer);
936 user_time=GetUserTime(&log_info->timer);
937 text=AcquireString(event);
938 if (log_info->format == (char *) NULL)
940 extent=strlen(event)+MaxTextExtent;
941 if (LocaleCompare(log_info->format,"xml") == 0)
944 timestamp[MaxTextExtent];
947 Translate event in "XML" format.
949 (void) FormatMagickTime(seconds,extent,timestamp);
950 (void) FormatLocaleString(text,extent,
952 " <timestamp>%s</timestamp>\n"
953 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
954 " <user-time>%0.3f</user-time>\n"
955 " <process-id>%.20g</process-id>\n"
956 " <thread-id>%.20g</thread-id>\n"
957 " <module>%s</module>\n"
958 " <function>%s</function>\n"
959 " <line>%.20g</line>\n"
960 " <domain>%s</domain>\n"
961 " <event>%s</event>\n"
962 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
963 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
964 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
965 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
966 (double) line,domain,event);
970 Translate event in "human readable" format.
973 for (p=log_info->format; *p != '\0'; p++)
976 if ((size_t) (q-text+MaxTextExtent) >= extent)
978 extent+=MaxTextExtent;
979 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
981 if (text == (char *) NULL)
982 return((char *) NULL);
986 The format of the log is defined by embedding special format characters:
1005 if ((*p == '\\') && (*(p+1) == 'r'))
1011 if ((*p == '\\') && (*(p+1) == 'n'))
1027 q+=CopyMagickString(q,GetClientName(),extent);
1032 q+=CopyMagickString(q,domain,extent);
1037 q+=CopyMagickString(q,event,extent);
1042 q+=CopyMagickString(q,function,extent);
1047 if (log_info->generations == 0)
1049 (void) CopyMagickString(q,"0",extent);
1053 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1054 log_info->generations));
1059 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
1067 for (p=module+strlen(module)-1; p > module; p--)
1068 if (*p == *DirectorySeparator)
1073 q+=CopyMagickString(q,p,extent);
1078 q+=CopyMagickString(q,GetLogName(),extent);
1083 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1088 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
1089 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
1090 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
1095 q+=FormatMagickTime(seconds,extent,q);
1100 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
1105 q+=CopyMagickString(q,MagickLibVersionText,extent);
1125 static char *TranslateFilename(const LogInfo *log_info)
1140 Translate event in "human readable" format.
1142 assert(log_info != (LogInfo *) NULL);
1143 assert(log_info->filename != (char *) NULL);
1144 filename=AcquireString((char *) NULL);
1145 extent=MaxTextExtent;
1147 for (p=log_info->filename; *p != '\0'; p++)
1150 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1152 extent+=MaxTextExtent;
1153 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1155 if (filename == (char *) NULL)
1156 return((char *) NULL);
1157 q=filename+strlen(filename);
1160 The format of the filename is defined by embedding special format
1179 q+=CopyMagickString(q,GetClientName(),extent);
1184 if (log_info->generations == 0)
1186 (void) CopyMagickString(q,"0",extent);
1190 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1191 log_info->generations));
1196 q+=CopyMagickString(q,GetLogName(),extent);
1201 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1206 q+=CopyMagickString(q,MagickLibVersionText,extent);
1226 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1227 const char *function,const size_t line,const char *format,
1231 event[MaxTextExtent],
1246 if (IsEventLogging() == MagickFalse)
1247 return(MagickFalse);
1248 exception=AcquireExceptionInfo();
1249 log_info=(LogInfo *) GetLogInfo("*",exception);
1250 exception=DestroyExceptionInfo(exception);
1251 if (event_semaphore == (SemaphoreInfo *) NULL)
1252 ActivateSemaphoreInfo(&event_semaphore);
1253 LockSemaphoreInfo(event_semaphore);
1254 if ((log_info->event_mask & type) == 0)
1256 UnlockSemaphoreInfo(event_semaphore);
1259 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1260 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1261 n=vsnprintf(event,MaxTextExtent,format,operands);
1263 n=vsprintf(event,format,operands);
1266 event[MaxTextExtent-1]='\0';
1267 text=TranslateEvent(type,module,function,line,domain,event);
1268 if (text == (char *) NULL)
1270 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1271 UnlockSemaphoreInfo(event_semaphore);
1272 return(MagickFalse);
1274 if ((log_info->handler_mask & ConsoleHandler) != 0)
1276 (void) FormatLocaleFile(stderr,"%s\n",text);
1277 (void) fflush(stderr);
1279 if ((log_info->handler_mask & DebugHandler) != 0)
1281 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1282 OutputDebugString(text);
1283 OutputDebugString("\n");
1286 if ((log_info->handler_mask & EventHandler) != 0)
1288 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1289 (void) NTReportEvent(text,MagickFalse);
1292 if ((log_info->handler_mask & FileHandler) != 0)
1297 file_info.st_size=0;
1298 if (log_info->file != (FILE *) NULL)
1299 (void) fstat(fileno(log_info->file),&file_info);
1300 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
1302 (void) FormatLocaleFile(log_info->file,"</log>\n");
1303 (void) fclose(log_info->file);
1304 log_info->file=(FILE *) NULL;
1306 if (log_info->file == (FILE *) NULL)
1311 filename=TranslateFilename(log_info);
1312 if (filename == (char *) NULL)
1314 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1315 UnlockSemaphoreInfo(event_semaphore);
1316 return(MagickFalse);
1318 log_info->append=IsPathAccessible(filename);
1319 log_info->file=fopen_utf8(filename,"ab");
1320 filename=(char *) RelinquishMagickMemory(filename);
1321 if (log_info->file == (FILE *) NULL)
1323 UnlockSemaphoreInfo(event_semaphore);
1324 return(MagickFalse);
1326 log_info->generation++;
1327 if (log_info->append == MagickFalse)
1328 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1329 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1330 (void) FormatLocaleFile(log_info->file,"<log>\n");
1332 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
1333 (void) fflush(log_info->file);
1335 if ((log_info->handler_mask & MethodHandler) != 0)
1337 if (log_info->method != (MagickLogMethod) NULL)
1338 log_info->method(type,text);
1340 if ((log_info->handler_mask & StdoutHandler) != 0)
1342 (void) FormatLocaleFile(stdout,"%s\n",text);
1343 (void) fflush(stdout);
1345 if ((log_info->handler_mask & StderrHandler) != 0)
1347 (void) FormatLocaleFile(stderr,"%s\n",text);
1348 (void) fflush(stderr);
1350 text=(char *) RelinquishMagickMemory(text);
1351 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1352 UnlockSemaphoreInfo(event_semaphore);
1356 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1357 const char *function,const size_t line,const char *format,...)
1365 va_start(operands,format);
1366 status=LogMagickEventList(type,module,function,line,format,operands);
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376 + L o a d L o g L i s t %
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 % LoadLogCache() loads the log configurations which provides a
1383 % mapping between log attributes and log name.
1385 % The format of the LoadLogCache method is:
1387 % MagickBooleanType LoadLogCache(LinkedListInfo *log_cache,const char *xml,
1388 % const char *filename,const size_t depth,ExceptionInfo *exception)
1390 % A description of each parameter follows:
1392 % o xml: The log list in XML format.
1394 % o filename: The log list filename.
1396 % o depth: depth of <include /> statements.
1398 % o exception: return any errors or warnings in this structure.
1401 static MagickBooleanType LoadLogCache(LinkedListInfo *log_cache,const char *xml,
1402 const char *filename,const size_t depth,ExceptionInfo *exception)
1405 keyword[MaxTextExtent],
1412 *log_info = (LogInfo *) NULL;
1418 Load the log map file.
1420 if (xml == (const char *) NULL)
1421 return(MagickFalse);
1423 token=AcquireString((const char *) xml);
1424 for (q=(const char *) xml; *q != '\0'; )
1429 GetMagickToken(q,&q,token);
1432 (void) CopyMagickString(keyword,token,MaxTextExtent);
1433 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1438 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1439 GetMagickToken(q,&q,token);
1442 if (LocaleNCompare(keyword,"<!--",4) == 0)
1447 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1448 GetMagickToken(q,&q,token);
1451 if (LocaleCompare(keyword,"<include") == 0)
1456 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1458 (void) CopyMagickString(keyword,token,MaxTextExtent);
1459 GetMagickToken(q,&q,token);
1462 GetMagickToken(q,&q,token);
1463 if (LocaleCompare(keyword,"file") == 0)
1466 (void) ThrowMagickException(exception,GetMagickModule(),
1467 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1471 path[MaxTextExtent],
1474 GetPathComponent(filename,HeadPath,path);
1476 (void) ConcatenateMagickString(path,DirectorySeparator,
1478 if (*token == *DirectorySeparator)
1479 (void) CopyMagickString(path,token,MaxTextExtent);
1481 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1482 xml=FileToXML(path,~0UL);
1483 if (xml != (char *) NULL)
1485 status&=LoadLogCache(log_cache,xml,path,depth+1,
1487 xml=DestroyString(xml);
1494 if (LocaleCompare(keyword,"<logmap>") == 0)
1497 Allocate memory for the log list.
1499 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1500 if (log_info == (LogInfo *) NULL)
1501 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1502 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1503 log_info->path=ConstantString(filename);
1504 GetTimerInfo((TimerInfo *) &log_info->timer);
1505 log_info->signature=MagickSignature;
1508 if (log_info == (LogInfo *) NULL)
1510 if (LocaleCompare(keyword,"</logmap>") == 0)
1512 status=AppendValueToLinkedList(log_cache,log_info);
1513 if (status == MagickFalse)
1514 (void) ThrowMagickException(exception,GetMagickModule(),
1515 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1516 log_info=(LogInfo *) NULL;
1519 GetMagickToken(q,(const char **) NULL,token);
1522 GetMagickToken(q,&q,token);
1523 GetMagickToken(q,&q,token);
1529 if (LocaleCompare((char *) keyword,"events") == 0)
1531 log_info->event_mask=(LogEventType) (log_info->event_mask |
1532 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1540 if (LocaleCompare((char *) keyword,"filename") == 0)
1542 if (log_info->filename != (char *) NULL)
1543 log_info->filename=(char *)
1544 RelinquishMagickMemory(log_info->filename);
1545 log_info->filename=ConstantString(token);
1548 if (LocaleCompare((char *) keyword,"format") == 0)
1550 if (log_info->format != (char *) NULL)
1551 log_info->format=(char *)
1552 RelinquishMagickMemory(log_info->format);
1553 log_info->format=ConstantString(token);
1561 if (LocaleCompare((char *) keyword,"generations") == 0)
1563 if (LocaleCompare(token,"unlimited") == 0)
1565 log_info->generations=(~0UL);
1568 log_info->generations=StringToUnsignedLong(token);
1576 if (LocaleCompare((char *) keyword,"limit") == 0)
1578 if (LocaleCompare(token,"unlimited") == 0)
1580 log_info->limit=(~0UL);
1583 log_info->limit=StringToUnsignedLong(token);
1591 if (LocaleCompare((char *) keyword,"output") == 0)
1593 log_info->handler_mask=(LogHandlerType)
1594 (log_info->handler_mask | ParseLogHandlers(token));
1603 token=DestroyString(token);
1604 if (log_cache == (LinkedListInfo *) NULL)
1605 return(MagickFalse);
1606 return(status != 0 ? MagickTrue : MagickFalse);
1610 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1614 + P a r s e L o g H a n d l e r s %
1618 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1620 % ParseLogHandlers() parses a string defining which handlers takes a log
1621 % message and exports them.
1623 % The format of the ParseLogHandlers method is:
1625 % LogHandlerType ParseLogHandlers(const char *handlers)
1627 % A description of each parameter follows:
1629 % o handlers: one or more handlers separated by commas.
1632 static LogHandlerType ParseLogHandlers(const char *handlers)
1646 handler_mask=NoHandler;
1647 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1649 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1652 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1654 length=strlen(LogHandlers[i].name);
1655 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1657 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1661 if (LogHandlers[i].name == (char *) NULL)
1662 return(UndefinedHandler);
1664 return(handler_mask);
1668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1672 % S e t L o g E v e n t M a s k %
1676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1678 % SetLogEventMask() accepts a list that determines which events to log. All
1679 % other events are ignored. By default, no debug is enabled. This method
1680 % returns the previous log event mask.
1682 % The format of the SetLogEventMask method is:
1684 % LogEventType SetLogEventMask(const char *events)
1686 % A description of each parameter follows:
1688 % o events: log these events.
1691 MagickExport LogEventType SetLogEventMask(const char *events)
1702 exception=AcquireExceptionInfo();
1703 log_info=(LogInfo *) GetLogInfo("*",exception);
1704 exception=DestroyExceptionInfo(exception);
1705 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1706 LockSemaphoreInfo(log_semaphore);
1707 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1708 log_info->event_mask=(LogEventType) option;
1710 log_info->event_mask=UndefinedEvents;
1711 UnlockSemaphoreInfo(log_semaphore);
1712 return(log_info->event_mask);
1716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1720 % S e t L o g F o r m a t %
1724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1726 % SetLogFormat() sets the format for the "human readable" log record.
1728 % The format of the LogMagickFormat method is:
1730 % SetLogFormat(const char *format)
1732 % A description of each parameter follows:
1734 % o format: the log record format.
1737 MagickExport void SetLogFormat(const char *format)
1745 exception=AcquireExceptionInfo();
1746 log_info=(LogInfo *) GetLogInfo("*",exception);
1747 exception=DestroyExceptionInfo(exception);
1748 LockSemaphoreInfo(log_semaphore);
1749 if (log_info->format != (char *) NULL)
1750 log_info->format=DestroyString(log_info->format);
1751 log_info->format=ConstantString(format);
1752 UnlockSemaphoreInfo(log_semaphore);
1756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1760 % S e t L o g M e t h o d %
1764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1766 % SetLogMethod() sets the method that will be called when an event is logged.
1768 % The format of the SetLogMethod method is:
1770 % void SetLogMethod(MagickLogMethod method)
1772 % A description of each parameter follows:
1774 % o method: pointer to a method that will be called when LogMagickEvent is
1778 MagickExport void SetLogMethod(MagickLogMethod method)
1786 exception=AcquireExceptionInfo();
1787 log_info=(LogInfo *) GetLogInfo("*",exception);
1788 exception=DestroyExceptionInfo(exception);
1789 LockSemaphoreInfo(log_semaphore);
1790 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1791 log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1793 log_info->method=method;
1794 UnlockSemaphoreInfo(log_semaphore);
1798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1802 % S e t L o g N a m e %
1806 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1808 % SetLogName() sets the log name and returns it.
1810 % The format of the SetLogName method is:
1812 % const char *SetLogName(const char *name)
1814 % A description of each parameter follows:
1816 % o log_name: SetLogName() returns the current client name.
1818 % o name: Specifies the new client name.
1821 MagickExport const char *SetLogName(const char *name)
1823 if ((name != (char *) NULL) && (*name != '\0'))
1824 (void) CopyMagickString(log_name,name,MaxTextExtent);