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"
70 #define LogFilename "log.xml"
77 UndefinedHandler = 0x0000,
79 ConsoleHandler = 0x0001,
80 StdoutHandler = 0x0002,
81 StderrHandler = 0x0004,
83 DebugHandler = 0x0010,
84 EventHandler = 0x0020,
85 MethodHandler = 0x0040
88 typedef struct _EventInfo
97 typedef struct _HandlerInfo
144 typedef struct _LogMapInfo
160 static const HandlerInfo
163 { "Console", ConsoleHandler },
164 { "Debug", DebugHandler },
165 { "Event", EventHandler },
166 { "File", FileHandler },
167 { "None", NoHandler },
168 { "Stderr", StderrHandler },
169 { "Stdout", StdoutHandler },
170 { (char *) NULL, UndefinedHandler }
173 static const LogMapInfo
176 { NoEvents, ConsoleHandler, "Magick-%g.log",
177 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n %e" }
181 log_name[MaxTextExtent] = "Magick";
183 static LinkedListInfo
184 *log_list = (LinkedListInfo *) NULL;
187 *log_semaphore = (SemaphoreInfo *) NULL;
189 static volatile MagickBooleanType
190 instantiate_log = MagickFalse;
193 Forward declarations.
195 static LogHandlerType
196 ParseLogHandlers(const char *);
199 *GetLogInfo(const char *,ExceptionInfo *);
201 static MagickBooleanType
202 InitializeLogList(ExceptionInfo *),
203 LoadLogLists(const char *,ExceptionInfo *);
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 % C l o s e M a g i c k L o g %
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 % CloseMagickLog() closes the Magick log.
218 % The format of the CloseMagickLog method is:
220 % CloseMagickLog(void)
223 MagickExport void CloseMagickLog(void)
231 if (IsEventLogging() == MagickFalse)
233 exception=AcquireExceptionInfo();
234 log_info=GetLogInfo("*",exception);
235 exception=DestroyExceptionInfo(exception);
236 LockSemaphoreInfo(log_semaphore);
237 if (log_info->file != (FILE *) NULL)
239 (void) FormatLocaleFile(log_info->file,"</log>\n");
240 (void) fclose(log_info->file);
241 log_info->file=(FILE *) NULL;
243 UnlockSemaphoreInfo(log_semaphore);
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 + G e t L o g I n f o %
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % GetLogInfo() searches the log list for the specified name and if found
258 % returns attributes for that log.
260 % The format of the GetLogInfo method is:
262 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
264 % A description of each parameter follows:
266 % o name: the log name.
268 % o exception: return any errors or warnings in this structure.
271 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
276 assert(exception != (ExceptionInfo *) NULL);
277 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
278 if (InitializeLogList(exception) == MagickFalse)
279 return((LogInfo *) NULL);
280 if ((log_list == (LinkedListInfo *) NULL) ||
281 (IsLinkedListEmpty(log_list) != MagickFalse))
282 return((LogInfo *) NULL);
283 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
284 return((LogInfo *) GetValueFromLinkedList(log_list,0));
288 LockSemaphoreInfo(log_semaphore);
289 ResetLinkedListIterator(log_list);
290 p=(LogInfo *) GetNextValueInLinkedList(log_list);
291 while (p != (LogInfo *) NULL)
293 if (LocaleCompare(name,p->name) == 0)
295 p=(LogInfo *) GetNextValueInLinkedList(log_list);
297 if (p != (LogInfo *) NULL)
298 (void) InsertValueInLinkedList(log_list,0,
299 RemoveElementByValueFromLinkedList(log_list,p));
300 UnlockSemaphoreInfo(log_semaphore);
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % G e t L o g I n f o L i s t %
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 % GetLogInfoList() returns any logs that match the specified pattern.
317 % The format of the GetLogInfoList function is:
319 % const LogInfo **GetLogInfoList(const char *pattern,
320 % size_t *number_preferences,ExceptionInfo *exception)
322 % A description of each parameter follows:
324 % o pattern: Specifies a pointer to a text string containing a pattern.
326 % o number_preferences: This integer returns the number of logs in the list.
328 % o exception: return any errors or warnings in this structure.
331 #if defined(__cplusplus) || defined(c_plusplus)
335 static int LogInfoCompare(const void *x,const void *y)
341 p=(const LogInfo **) x,
342 q=(const LogInfo **) y;
343 if (LocaleCompare((*p)->path,(*q)->path) == 0)
344 return(LocaleCompare((*p)->name,(*q)->name));
345 return(LocaleCompare((*p)->path,(*q)->path));
348 #if defined(__cplusplus) || defined(c_plusplus)
352 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
353 size_t *number_preferences,ExceptionInfo *exception)
358 register const LogInfo
367 assert(pattern != (char *) NULL);
368 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
369 assert(number_preferences != (size_t *) NULL);
370 *number_preferences=0;
371 p=GetLogInfo("*",exception);
372 if (p == (const LogInfo *) NULL)
373 return((const LogInfo **) NULL);
374 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
375 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
376 if (preferences == (const LogInfo **) NULL)
377 return((const LogInfo **) NULL);
381 LockSemaphoreInfo(log_semaphore);
382 ResetLinkedListIterator(log_list);
383 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
384 for (i=0; p != (const LogInfo *) NULL; )
386 if ((p->stealth == MagickFalse) &&
387 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
389 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
391 UnlockSemaphoreInfo(log_semaphore);
392 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
393 preferences[i]=(LogInfo *) NULL;
394 *number_preferences=(size_t) i;
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 % G e t L o g L i s t %
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 % GetLogList() returns any logs that match the specified pattern.
411 % The format of the GetLogList function is:
413 % char **GetLogList(const char *pattern,size_t *number_preferences,
414 % ExceptionInfo *exception)
416 % A description of each parameter follows:
418 % o pattern: Specifies a pointer to a text string containing a pattern.
420 % o number_preferences: This integer returns the number of logs in the list.
422 % o exception: return any errors or warnings in this structure.
426 #if defined(__cplusplus) || defined(c_plusplus)
430 static int LogCompare(const void *x,const void *y)
438 return(LocaleCompare(*p,*q));
441 #if defined(__cplusplus) || defined(c_plusplus)
445 MagickExport char **GetLogList(const char *pattern,
446 size_t *number_preferences,ExceptionInfo *exception)
451 register const LogInfo
460 assert(pattern != (char *) NULL);
461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
462 assert(number_preferences != (size_t *) NULL);
463 *number_preferences=0;
464 p=GetLogInfo("*",exception);
465 if (p == (const LogInfo *) NULL)
466 return((char **) NULL);
467 preferences=(char **) AcquireQuantumMemory((size_t)
468 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
469 if (preferences == (char **) NULL)
470 return((char **) NULL);
474 LockSemaphoreInfo(log_semaphore);
475 ResetLinkedListIterator(log_list);
476 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
477 for (i=0; p != (const LogInfo *) NULL; )
479 if ((p->stealth == MagickFalse) &&
480 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
481 preferences[i++]=ConstantString(p->name);
482 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
484 UnlockSemaphoreInfo(log_semaphore);
485 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
486 preferences[i]=(char *) NULL;
487 *number_preferences=(size_t) i;
492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 % G e t L o g N a m e %
500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502 % GetLogName() returns the current log name.
504 % The format of the GetLogName method is:
506 % const char *GetLogName(void)
509 MagickExport const char *GetLogName(void)
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 + I n i t i a l i z e L o g L i s t %
523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525 % InitializeLogList() initialize the log list.
527 % The format of the InitializeLogList method is:
529 % MagickBooleanType InitializeLogList(ExceptionInfo *exception)
531 % A description of each parameter follows.
533 % o exception: return any errors or warnings in this structure.
536 static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
538 if ((log_list == (LinkedListInfo *) NULL) ||
539 (instantiate_log == MagickFalse))
541 if (log_semaphore == (SemaphoreInfo *) NULL)
542 ActivateSemaphoreInfo(&log_semaphore);
543 LockSemaphoreInfo(log_semaphore);
544 if ((log_list == (LinkedListInfo *) NULL) ||
545 (instantiate_log == MagickFalse))
547 (void) LoadLogLists(LogFilename,exception);
548 instantiate_log=MagickTrue;
550 UnlockSemaphoreInfo(log_semaphore);
552 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560 % I s E v e n t L o g g i n g %
564 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
566 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
569 % The format of the IsEventLogging method is:
571 % MagickBooleanType IsEventLogging(void)
574 MagickExport MagickBooleanType IsEventLogging(void)
582 if ((log_list == (LinkedListInfo *) NULL) ||
583 (IsLinkedListEmpty(log_list) != MagickFalse))
585 exception=AcquireExceptionInfo();
586 log_info=GetLogInfo("*",exception);
587 exception=DestroyExceptionInfo(exception);
588 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
595 % L i s t L o g I n f o %
599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601 % ListLogInfo() lists the log info to a file.
603 % The format of the ListLogInfo method is:
605 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
607 % A description of each parameter follows.
609 % o file: An pointer to a FILE.
611 % o exception: return any errors or warnings in this structure.
614 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
616 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
633 if (file == (const FILE *) NULL)
635 log_info=GetLogInfoList("*",&number_aliases,exception);
636 if (log_info == (const LogInfo **) NULL)
639 path=(const char *) NULL;
640 for (i=0; i < (ssize_t) number_aliases; i++)
642 if (log_info[i]->stealth != MagickFalse)
644 if ((path == (const char *) NULL) ||
645 (LocaleCompare(path,log_info[i]->path) != 0))
650 if (log_info[i]->path != (char *) NULL)
651 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
653 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
660 if ((log_info[i]->handler_mask & mask) != 0)
662 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
663 length+=strlen(LogHandlers[j].name);
666 for (j=(ssize_t) length; j <= 12; j++)
667 (void) FormatLocaleFile(file," ");
668 (void) FormatLocaleFile(file," Generations Limit Format\n");
669 (void) FormatLocaleFile(file,"-----------------------------------------"
670 "--------------------------------------\n");
672 path=log_info[i]->path;
673 if (log_info[i]->filename != (char *) NULL)
675 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
676 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
677 (void) FormatLocaleFile(file," ");
679 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
680 (void) FormatLocaleFile(file,"%8g ",(double) log_info[i]->limit);
681 if (log_info[i]->format != (char *) NULL)
682 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
683 (void) FormatLocaleFile(file,"\n");
686 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695 + L o g C o m p o n e n t G e n e s i s %
699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
701 % LogComponentGenesis() instantiates the log component.
703 % The format of the LogComponentGenesis method is:
705 % MagickBooleanType LogComponentGenesis(void)
708 MagickPrivate MagickBooleanType LogComponentGenesis(void)
713 log_semaphore=AcquireSemaphoreInfo();
714 exception=AcquireExceptionInfo();
715 (void) GetLogInfo("*",exception);
716 exception=DestroyExceptionInfo(exception);
721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
725 + L o g C o m p o n e n t T e r m i n u s %
729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % LogComponentTerminus() destroys the logging component.
733 % The format of the LogComponentTerminus method is:
735 % LogComponentTerminus(void)
739 static void *DestroyLogElement(void *log_info)
744 p=(LogInfo *) log_info;
745 if (p->file != (FILE *) NULL)
747 (void) FormatLocaleFile(p->file,"</log>\n");
748 (void) fclose(p->file);
749 p->file=(FILE *) NULL;
751 if (p->format != (char *) NULL)
752 p->format=DestroyString(p->format);
753 if (p->path != (char *) NULL)
754 p->path=DestroyString(p->path);
755 if (p->filename != (char *) NULL)
756 p->filename=DestroyString(p->filename);
757 p=(LogInfo *) RelinquishMagickMemory(p);
758 return((void *) NULL);
761 MagickPrivate void LogComponentTerminus(void)
763 if (log_semaphore == (SemaphoreInfo *) NULL)
764 ActivateSemaphoreInfo(&log_semaphore);
765 LockSemaphoreInfo(log_semaphore);
766 if (log_list != (LinkedListInfo *) NULL)
767 log_list=DestroyLinkedList(log_list,DestroyLogElement);
768 instantiate_log=MagickFalse;
769 UnlockSemaphoreInfo(log_semaphore);
770 RelinquishSemaphoreInfo(&log_semaphore);
774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778 % L o g M a g i c k E v e n t %
782 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784 % LogMagickEvent() logs an event as determined by the log configuration file.
785 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
787 % The format of the LogMagickEvent method is:
789 % MagickBooleanType LogMagickEvent(const LogEventType type,
790 % const char *module,const char *function,const size_t line,
791 % const char *format,...)
793 % A description of each parameter follows:
795 % o type: the event type.
797 % o filename: the source module filename.
799 % o function: the function name.
801 % o line: the line number of the source module.
803 % o format: the output format.
806 static char *TranslateEvent(const LogEventType magick_unused(type),
807 const char *module,const char *function,const size_t line,
808 const char *domain,const char *event)
835 exception=AcquireExceptionInfo();
836 log_info=(LogInfo *) GetLogInfo("*",exception);
837 exception=DestroyExceptionInfo(exception);
838 seconds=time((time_t *) NULL);
839 elapsed_time=GetElapsedTime(&log_info->timer);
840 user_time=GetUserTime(&log_info->timer);
841 text=AcquireString(event);
842 if (log_info->format == (char *) NULL)
844 extent=strlen(event)+MaxTextExtent;
845 if (LocaleCompare(log_info->format,"xml") == 0)
848 timestamp[MaxTextExtent];
851 Translate event in "XML" format.
853 (void) FormatMagickTime(seconds,extent,timestamp);
854 (void) FormatLocaleString(text,extent,
856 " <timestamp>%s</timestamp>\n"
857 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
858 " <user-time>%0.3f</user-time>\n"
859 " <process-id>%.20g</process-id>\n"
860 " <thread-id>%.20g</thread-id>\n"
861 " <module>%s</module>\n"
862 " <function>%s</function>\n"
863 " <line>%.20g</line>\n"
864 " <domain>%s</domain>\n"
865 " <event>%s</event>\n"
866 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
867 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
868 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
869 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
870 (double) line,domain,event);
874 Translate event in "human readable" format.
877 for (p=log_info->format; *p != '\0'; p++)
880 if ((size_t) (q-text+MaxTextExtent) >= extent)
882 extent+=MaxTextExtent;
883 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
885 if (text == (char *) NULL)
886 return((char *) NULL);
890 The format of the log is defined by embedding special format characters:
909 if ((*p == '\\') && (*(p+1) == 'r'))
915 if ((*p == '\\') && (*(p+1) == 'n'))
931 q+=CopyMagickString(q,GetClientName(),extent);
936 q+=CopyMagickString(q,domain,extent);
941 q+=CopyMagickString(q,event,extent);
946 q+=CopyMagickString(q,function,extent);
951 if (log_info->generations == 0)
953 (void) CopyMagickString(q,"0",extent);
957 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
958 log_info->generations));
963 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
971 for (p=module+strlen(module)-1; p > module; p--)
972 if (*p == *DirectorySeparator)
977 q+=CopyMagickString(q,p,extent);
982 q+=CopyMagickString(q,GetLogName(),extent);
987 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
992 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
993 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
994 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
999 q+=FormatMagickTime(seconds,extent,q);
1004 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
1009 q+=CopyMagickString(q,MagickLibVersionText,extent);
1029 static char *TranslateFilename(const LogInfo *log_info)
1044 Translate event in "human readable" format.
1046 assert(log_info != (LogInfo *) NULL);
1047 assert(log_info->filename != (char *) NULL);
1048 filename=AcquireString((char *) NULL);
1049 extent=MaxTextExtent;
1051 for (p=log_info->filename; *p != '\0'; p++)
1054 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1056 extent+=MaxTextExtent;
1057 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1059 if (filename == (char *) NULL)
1060 return((char *) NULL);
1061 q=filename+strlen(filename);
1064 The format of the filename is defined by embedding special format
1083 q+=CopyMagickString(q,GetClientName(),extent);
1088 if (log_info->generations == 0)
1090 (void) CopyMagickString(q,"0",extent);
1094 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1095 log_info->generations));
1100 q+=CopyMagickString(q,GetLogName(),extent);
1105 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1110 q+=CopyMagickString(q,MagickLibVersionText,extent);
1130 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1131 const char *function,const size_t line,const char *format,
1135 event[MaxTextExtent],
1150 if (IsEventLogging() == MagickFalse)
1151 return(MagickFalse);
1152 exception=AcquireExceptionInfo();
1153 log_info=(LogInfo *) GetLogInfo("*",exception);
1154 exception=DestroyExceptionInfo(exception);
1155 LockSemaphoreInfo(log_semaphore);
1156 if ((log_info->event_mask & type) == 0)
1158 UnlockSemaphoreInfo(log_semaphore);
1161 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1162 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1163 n=vsnprintf(event,MaxTextExtent,format,operands);
1165 n=vsprintf(event,format,operands);
1168 event[MaxTextExtent-1]='\0';
1169 text=TranslateEvent(type,module,function,line,domain,event);
1170 if (text == (char *) NULL)
1172 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1173 UnlockSemaphoreInfo(log_semaphore);
1174 return(MagickFalse);
1176 if ((log_info->handler_mask & ConsoleHandler) != 0)
1178 (void) FormatLocaleFile(stderr,"%s\n",text);
1179 (void) fflush(stderr);
1181 if ((log_info->handler_mask & DebugHandler) != 0)
1183 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1184 OutputDebugString(text);
1185 OutputDebugString("\n");
1188 if ((log_info->handler_mask & EventHandler) != 0)
1190 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1191 (void) NTReportEvent(text,MagickFalse);
1194 if ((log_info->handler_mask & FileHandler) != 0)
1199 file_info.st_size=0;
1200 if (log_info->file != (FILE *) NULL)
1201 (void) fstat(fileno(log_info->file),&file_info);
1202 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
1204 (void) FormatLocaleFile(log_info->file,"</log>\n");
1205 (void) fclose(log_info->file);
1206 log_info->file=(FILE *) NULL;
1208 if (log_info->file == (FILE *) NULL)
1213 filename=TranslateFilename(log_info);
1214 if (filename == (char *) NULL)
1216 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1217 UnlockSemaphoreInfo(log_semaphore);
1218 return(MagickFalse);
1220 log_info->append=IsPathAccessible(filename);
1221 log_info->file=fopen_utf8(filename,"ab");
1222 filename=(char *) RelinquishMagickMemory(filename);
1223 if (log_info->file == (FILE *) NULL)
1225 UnlockSemaphoreInfo(log_semaphore);
1226 return(MagickFalse);
1228 log_info->generation++;
1229 if (log_info->append == MagickFalse)
1230 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1231 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1232 (void) FormatLocaleFile(log_info->file,"<log>\n");
1234 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
1235 (void) fflush(log_info->file);
1237 if ((log_info->handler_mask & MethodHandler) != 0)
1239 if (log_info->method != (MagickLogMethod) NULL)
1240 log_info->method(type,text);
1242 if ((log_info->handler_mask & StdoutHandler) != 0)
1244 (void) FormatLocaleFile(stdout,"%s\n",text);
1245 (void) fflush(stdout);
1247 if ((log_info->handler_mask & StderrHandler) != 0)
1249 (void) FormatLocaleFile(stderr,"%s\n",text);
1250 (void) fflush(stderr);
1252 text=(char *) RelinquishMagickMemory(text);
1253 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1254 UnlockSemaphoreInfo(log_semaphore);
1258 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1259 const char *function,const size_t line,const char *format,...)
1267 va_start(operands,format);
1268 status=LogMagickEventList(type,module,function,line,format,operands);
1274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1278 + L o a d L o g L i s t %
1282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1284 % LoadLogList() loads the log configuration file which provides a
1285 % mapping between log attributes and log name.
1287 % The format of the LoadLogList method is:
1289 % MagickBooleanType LoadLogList(const char *xml,const char *filename,
1290 % const size_t depth,ExceptionInfo *exception)
1292 % A description of each parameter follows:
1294 % o xml: The log list in XML format.
1296 % o filename: The log list filename.
1298 % o depth: depth of <include /> statements.
1300 % o exception: return any errors or warnings in this structure.
1303 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1304 const size_t depth,ExceptionInfo *exception)
1307 keyword[MaxTextExtent],
1314 *log_info = (LogInfo *) NULL;
1320 Load the log map file.
1322 if (xml == (const char *) NULL)
1323 return(MagickFalse);
1324 if (log_list == (LinkedListInfo *) NULL)
1326 log_list=NewLinkedList(0);
1327 if (log_list == (LinkedListInfo *) NULL)
1329 ThrowFileException(exception,ResourceLimitError,
1330 "MemoryAllocationFailed",filename);
1331 return(MagickFalse);
1335 token=AcquireString((const char *) xml);
1336 for (q=(const char *) xml; *q != '\0'; )
1341 GetMagickToken(q,&q,token);
1344 (void) CopyMagickString(keyword,token,MaxTextExtent);
1345 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1350 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1351 GetMagickToken(q,&q,token);
1354 if (LocaleNCompare(keyword,"<!--",4) == 0)
1359 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1360 GetMagickToken(q,&q,token);
1363 if (LocaleCompare(keyword,"<include") == 0)
1368 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1370 (void) CopyMagickString(keyword,token,MaxTextExtent);
1371 GetMagickToken(q,&q,token);
1374 GetMagickToken(q,&q,token);
1375 if (LocaleCompare(keyword,"file") == 0)
1378 (void) ThrowMagickException(exception,GetMagickModule(),
1379 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1383 path[MaxTextExtent],
1386 GetPathComponent(filename,HeadPath,path);
1388 (void) ConcatenateMagickString(path,DirectorySeparator,
1390 if (*token == *DirectorySeparator)
1391 (void) CopyMagickString(path,token,MaxTextExtent);
1393 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1394 xml=FileToString(path,~0UL,exception);
1395 if (xml != (char *) NULL)
1397 status&=LoadLogList(xml,path,depth+1,exception);
1398 xml=DestroyString(xml);
1405 if (LocaleCompare(keyword,"<logmap>") == 0)
1408 Allocate memory for the log list.
1410 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1411 if (log_info == (LogInfo *) NULL)
1412 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1413 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1414 log_info->path=ConstantString(filename);
1415 GetTimerInfo((TimerInfo *) &log_info->timer);
1416 log_info->signature=MagickSignature;
1419 if (log_info == (LogInfo *) NULL)
1421 if (LocaleCompare(keyword,"</logmap>") == 0)
1423 status=AppendValueToLinkedList(log_list,log_info);
1424 if (status == MagickFalse)
1425 (void) ThrowMagickException(exception,GetMagickModule(),
1426 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1427 log_info=(LogInfo *) NULL;
1430 GetMagickToken(q,(const char **) NULL,token);
1433 GetMagickToken(q,&q,token);
1434 GetMagickToken(q,&q,token);
1440 if (LocaleCompare((char *) keyword,"events") == 0)
1442 log_info->event_mask=(LogEventType) (log_info->event_mask |
1443 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1451 if (LocaleCompare((char *) keyword,"filename") == 0)
1453 if (log_info->filename != (char *) NULL)
1454 log_info->filename=(char *)
1455 RelinquishMagickMemory(log_info->filename);
1456 log_info->filename=ConstantString(token);
1459 if (LocaleCompare((char *) keyword,"format") == 0)
1461 if (log_info->format != (char *) NULL)
1462 log_info->format=(char *)
1463 RelinquishMagickMemory(log_info->format);
1464 log_info->format=ConstantString(token);
1472 if (LocaleCompare((char *) keyword,"generations") == 0)
1474 if (LocaleCompare(token,"unlimited") == 0)
1476 log_info->generations=(~0UL);
1479 log_info->generations=StringToUnsignedLong(token);
1487 if (LocaleCompare((char *) keyword,"limit") == 0)
1489 if (LocaleCompare(token,"unlimited") == 0)
1491 log_info->limit=(~0UL);
1494 log_info->limit=StringToUnsignedLong(token);
1502 if (LocaleCompare((char *) keyword,"output") == 0)
1504 log_info->handler_mask=(LogHandlerType)
1505 (log_info->handler_mask | ParseLogHandlers(token));
1514 token=DestroyString(token);
1515 if (log_list == (LinkedListInfo *) NULL)
1516 return(MagickFalse);
1517 return(status != 0 ? MagickTrue : MagickFalse);
1521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1525 % L o a d L o g L i s t s %
1529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1531 % LoadLogLists() loads one or more log configuration file which provides a
1532 % mapping between log attributes and log name.
1534 % The format of the LoadLogLists method is:
1536 % MagickBooleanType LoadLogLists(const char *filename,
1537 % ExceptionInfo *exception)
1539 % A description of each parameter follows:
1541 % o filename: the log configuration filename.
1543 % o exception: return any errors or warnings in this structure.
1546 static MagickBooleanType LoadLogLists(const char *filename,
1547 ExceptionInfo *exception)
1562 Load external log map.
1564 if (log_list == (LinkedListInfo *) NULL)
1566 log_list=NewLinkedList(0);
1567 if (log_list == (LinkedListInfo *) NULL)
1569 ThrowFileException(exception,ResourceLimitError,
1570 "MemoryAllocationFailed",filename);
1571 return(MagickFalse);
1575 options=GetConfigureOptions(filename,exception);
1576 option=(const StringInfo *) GetNextValueInLinkedList(options);
1577 while (option != (const StringInfo *) NULL)
1579 status&=LoadLogList((const char *) GetStringInfoDatum(option),
1580 GetStringInfoPath(option),0,exception);
1581 option=(const StringInfo *) GetNextValueInLinkedList(options);
1583 options=DestroyConfigureOptions(options);
1585 Load built-in log map.
1587 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1592 register const LogMapInfo
1596 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1597 if (log_info == (LogInfo *) NULL)
1599 (void) ThrowMagickException(exception,GetMagickModule(),
1600 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->filename);
1603 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1604 log_info->path=ConstantString("[built-in]");
1605 GetTimerInfo((TimerInfo *) &log_info->timer);
1606 log_info->event_mask=p->event_mask;
1607 log_info->handler_mask=p->handler_mask;
1608 log_info->filename=ConstantString(p->filename);
1609 log_info->format=ConstantString(p->format);
1610 log_info->signature=MagickSignature;
1611 status&=AppendValueToLinkedList(log_list,log_info);
1612 if (status == MagickFalse)
1613 (void) ThrowMagickException(exception,GetMagickModule(),
1614 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1616 return(status != 0 ? MagickTrue : MagickFalse);
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 + P a r s e L o g H a n d l e r s %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630 % ParseLogHandlers() parses a string defining which handlers takes a log
1631 % message and exports them.
1633 % The format of the ParseLogHandlers method is:
1635 % LogHandlerType ParseLogHandlers(const char *handlers)
1637 % A description of each parameter follows:
1639 % o handlers: one or more handlers separated by commas.
1642 static LogHandlerType ParseLogHandlers(const char *handlers)
1656 handler_mask=NoHandler;
1657 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1659 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1662 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1664 length=strlen(LogHandlers[i].name);
1665 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1667 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1671 if (LogHandlers[i].name == (char *) NULL)
1672 return(UndefinedHandler);
1674 return(handler_mask);
1678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1682 % S e t L o g E v e n t M a s k %
1686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1688 % SetLogEventMask() accepts a list that determines which events to log. All
1689 % other events are ignored. By default, no debug is enabled. This method
1690 % returns the previous log event mask.
1692 % The format of the SetLogEventMask method is:
1694 % LogEventType SetLogEventMask(const char *events)
1696 % A description of each parameter follows:
1698 % o events: log these events.
1701 MagickExport LogEventType SetLogEventMask(const char *events)
1712 exception=AcquireExceptionInfo();
1713 log_info=(LogInfo *) GetLogInfo("*",exception);
1714 exception=DestroyExceptionInfo(exception);
1715 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1716 LockSemaphoreInfo(log_semaphore);
1717 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1718 log_info->event_mask=(LogEventType) option;
1720 log_info->event_mask=UndefinedEvents;
1721 UnlockSemaphoreInfo(log_semaphore);
1722 return(log_info->event_mask);
1726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730 % S e t L o g F o r m a t %
1734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1736 % SetLogFormat() sets the format for the "human readable" log record.
1738 % The format of the LogMagickFormat method is:
1740 % SetLogFormat(const char *format)
1742 % A description of each parameter follows:
1744 % o format: the log record format.
1747 MagickExport void SetLogFormat(const char *format)
1755 exception=AcquireExceptionInfo();
1756 log_info=(LogInfo *) GetLogInfo("*",exception);
1757 exception=DestroyExceptionInfo(exception);
1758 LockSemaphoreInfo(log_semaphore);
1759 if (log_info->format != (char *) NULL)
1760 log_info->format=DestroyString(log_info->format);
1761 log_info->format=ConstantString(format);
1762 UnlockSemaphoreInfo(log_semaphore);
1766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1770 % S e t L o g M e t h o d %
1774 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1776 % SetLogMethod() sets the method that will be called when an event is logged.
1778 % The format of the SetLogMethod method is:
1780 % void SetLogMethod(MagickLogMethod method)
1782 % A description of each parameter follows:
1784 % o method: pointer to a method that will be called when LogMagickEvent is
1788 MagickExport void SetLogMethod(MagickLogMethod method)
1796 exception=AcquireExceptionInfo();
1797 log_info=(LogInfo *) GetLogInfo("*",exception);
1798 exception=DestroyExceptionInfo(exception);
1799 LockSemaphoreInfo(log_semaphore);
1800 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1801 log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1803 log_info->method=method;
1804 UnlockSemaphoreInfo(log_semaphore);
1808 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1812 % S e t L o g N a m e %
1816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1818 % SetLogName() sets the log name and returns it.
1820 % The format of the SetLogName method is:
1822 % const char *SetLogName(const char *name)
1824 % A description of each parameter follows:
1826 % o log_name: SetLogName() returns the current client name.
1828 % o name: Specifies the new client name.
1831 MagickExport const char *SetLogName(const char *name)
1833 if ((name != (char *) NULL) && (*name != '\0'))
1834 (void) CopyMagickString(log_name,name,MaxTextExtent);