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/option.h"
54 #include "MagickCore/semaphore.h"
55 #include "MagickCore/timer.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/string-private.h"
58 #include "MagickCore/token.h"
59 #include "MagickCore/thread_.h"
60 #include "MagickCore/thread-private.h"
61 #include "MagickCore/utility.h"
62 #include "MagickCore/utility-private.h"
63 #include "MagickCore/version.h"
64 #include "MagickCore/xml-tree.h"
69 #define LogFilename "log.xml"
76 UndefinedHandler = 0x0000,
78 ConsoleHandler = 0x0001,
79 StdoutHandler = 0x0002,
80 StderrHandler = 0x0004,
82 DebugHandler = 0x0010,
83 EventHandler = 0x0020,
84 MethodHandler = 0x0040
87 typedef struct _EventInfo
96 typedef struct _HandlerInfo
143 typedef struct _LogMapInfo
159 static const HandlerInfo
162 { "Console", ConsoleHandler },
163 { "Debug", DebugHandler },
164 { "Event", EventHandler },
165 { "File", FileHandler },
166 { "None", NoHandler },
167 { "Stderr", StderrHandler },
168 { "Stdout", StdoutHandler },
169 { (char *) NULL, UndefinedHandler }
172 static const LogMapInfo
175 { NoEvents, ConsoleHandler, "Magick-%g.log",
176 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n %e" }
180 log_name[MaxTextExtent] = "Magick";
182 static LinkedListInfo
183 *log_list = (LinkedListInfo *) NULL;
186 *log_semaphore = (SemaphoreInfo *) NULL;
188 static volatile MagickBooleanType
189 instantiate_log = MagickFalse;
192 Forward declarations.
194 static LogHandlerType
195 ParseLogHandlers(const char *);
198 *GetLogInfo(const char *,ExceptionInfo *);
200 static MagickBooleanType
201 InitializeLogList(ExceptionInfo *),
202 LoadLogLists(const char *,ExceptionInfo *);
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % C l o s e M a g i c k L o g %
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 % CloseMagickLog() closes the Magick log.
217 % The format of the CloseMagickLog method is:
219 % CloseMagickLog(void)
222 MagickExport void CloseMagickLog(void)
230 if (IsEventLogging() == MagickFalse)
232 exception=AcquireExceptionInfo();
233 log_info=GetLogInfo("*",exception);
234 exception=DestroyExceptionInfo(exception);
235 LockSemaphoreInfo(log_semaphore);
236 if (log_info->file != (FILE *) NULL)
238 (void) FormatLocaleFile(log_info->file,"</log>\n");
239 (void) fclose(log_info->file);
240 log_info->file=(FILE *) NULL;
242 UnlockSemaphoreInfo(log_semaphore);
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 + G e t L o g I n f o %
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % GetLogInfo() searches the log list for the specified name and if found
257 % returns attributes for that log.
259 % The format of the GetLogInfo method is:
261 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
263 % A description of each parameter follows:
265 % o name: the log name.
267 % o exception: return any errors or warnings in this structure.
270 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
275 assert(exception != (ExceptionInfo *) NULL);
276 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
277 if (InitializeLogList(exception) == MagickFalse)
278 return((LogInfo *) NULL);
279 if ((log_list == (LinkedListInfo *) NULL) ||
280 (IsLinkedListEmpty(log_list) != MagickFalse))
281 return((LogInfo *) NULL);
282 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
283 return((LogInfo *) GetValueFromLinkedList(log_list,0));
287 LockSemaphoreInfo(log_semaphore);
288 ResetLinkedListIterator(log_list);
289 p=(LogInfo *) GetNextValueInLinkedList(log_list);
290 while (p != (LogInfo *) NULL)
292 if (LocaleCompare(name,p->name) == 0)
294 p=(LogInfo *) GetNextValueInLinkedList(log_list);
296 if (p != (LogInfo *) NULL)
297 (void) InsertValueInLinkedList(log_list,0,
298 RemoveElementByValueFromLinkedList(log_list,p));
299 UnlockSemaphoreInfo(log_semaphore);
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 % G e t L o g I n f o L i s t %
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 % GetLogInfoList() returns any logs that match the specified pattern.
316 % The format of the GetLogInfoList function is:
318 % const LogInfo **GetLogInfoList(const char *pattern,
319 % size_t *number_preferences,ExceptionInfo *exception)
321 % A description of each parameter follows:
323 % o pattern: Specifies a pointer to a text string containing a pattern.
325 % o number_preferences: This integer returns the number of logs in the list.
327 % o exception: return any errors or warnings in this structure.
330 #if defined(__cplusplus) || defined(c_plusplus)
334 static int LogInfoCompare(const void *x,const void *y)
340 p=(const LogInfo **) x,
341 q=(const LogInfo **) y;
342 if (LocaleCompare((*p)->path,(*q)->path) == 0)
343 return(LocaleCompare((*p)->name,(*q)->name));
344 return(LocaleCompare((*p)->path,(*q)->path));
347 #if defined(__cplusplus) || defined(c_plusplus)
351 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
352 size_t *number_preferences,ExceptionInfo *exception)
357 register const LogInfo
366 assert(pattern != (char *) NULL);
367 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
368 assert(number_preferences != (size_t *) NULL);
369 *number_preferences=0;
370 p=GetLogInfo("*",exception);
371 if (p == (const LogInfo *) NULL)
372 return((const LogInfo **) NULL);
373 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
374 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
375 if (preferences == (const LogInfo **) NULL)
376 return((const LogInfo **) NULL);
380 LockSemaphoreInfo(log_semaphore);
381 ResetLinkedListIterator(log_list);
382 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
383 for (i=0; p != (const LogInfo *) NULL; )
385 if ((p->stealth == MagickFalse) &&
386 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
388 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
390 UnlockSemaphoreInfo(log_semaphore);
391 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
392 preferences[i]=(LogInfo *) NULL;
393 *number_preferences=(size_t) i;
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 % G e t L o g L i s t %
406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 % GetLogList() returns any logs that match the specified pattern.
410 % The format of the GetLogList function is:
412 % char **GetLogList(const char *pattern,size_t *number_preferences,
413 % ExceptionInfo *exception)
415 % A description of each parameter follows:
417 % o pattern: Specifies a pointer to a text string containing a pattern.
419 % o number_preferences: This integer returns the number of logs in the list.
421 % o exception: return any errors or warnings in this structure.
425 #if defined(__cplusplus) || defined(c_plusplus)
429 static int LogCompare(const void *x,const void *y)
437 return(LocaleCompare(*p,*q));
440 #if defined(__cplusplus) || defined(c_plusplus)
444 MagickExport char **GetLogList(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((char **) NULL);
466 preferences=(char **) AcquireQuantumMemory((size_t)
467 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
468 if (preferences == (char **) NULL)
469 return((char **) NULL);
473 LockSemaphoreInfo(log_semaphore);
474 ResetLinkedListIterator(log_list);
475 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
476 for (i=0; p != (const LogInfo *) NULL; )
478 if ((p->stealth == MagickFalse) &&
479 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
480 preferences[i++]=ConstantString(p->name);
481 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
483 UnlockSemaphoreInfo(log_semaphore);
484 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
485 preferences[i]=(char *) NULL;
486 *number_preferences=(size_t) i;
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495 % G e t L o g N a m e %
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501 % GetLogName() returns the current log name.
503 % The format of the GetLogName method is:
505 % const char *GetLogName(void)
508 MagickExport const char *GetLogName(void)
514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 + I n i t i a l i z e L o g L i s t %
522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 % InitializeLogList() initialize the log list.
526 % The format of the InitializeLogList method is:
528 % MagickBooleanType InitializeLogList(ExceptionInfo *exception)
530 % A description of each parameter follows.
532 % o exception: return any errors or warnings in this structure.
535 static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
537 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
539 if (log_semaphore == (SemaphoreInfo *) NULL)
540 AcquireSemaphoreInfo(&log_semaphore);
541 LockSemaphoreInfo(log_semaphore);
542 if ((log_list == (LinkedListInfo *) NULL) &&
543 (instantiate_log == MagickFalse))
545 (void) LoadLogLists(LogFilename,exception);
546 instantiate_log=MagickTrue;
548 UnlockSemaphoreInfo(log_semaphore);
550 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 % I s E v e n t L o g g i n g %
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
567 % The format of the IsEventLogging method is:
569 % MagickBooleanType IsEventLogging(void)
572 MagickExport MagickBooleanType IsEventLogging(void)
580 if ((log_list == (LinkedListInfo *) NULL) ||
581 (IsLinkedListEmpty(log_list) != MagickFalse))
583 exception=AcquireExceptionInfo();
584 log_info=GetLogInfo("*",exception);
585 exception=DestroyExceptionInfo(exception);
586 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593 % L i s t L o g I n f o %
597 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599 % ListLogInfo() lists the log info to a file.
601 % The format of the ListLogInfo method is:
603 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
605 % A description of each parameter follows.
607 % o file: An pointer to a FILE.
609 % o exception: return any errors or warnings in this structure.
612 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
614 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
631 if (file == (const FILE *) NULL)
633 log_info=GetLogInfoList("*",&number_aliases,exception);
634 if (log_info == (const LogInfo **) NULL)
637 path=(const char *) NULL;
638 for (i=0; i < (ssize_t) number_aliases; i++)
640 if (log_info[i]->stealth != MagickFalse)
642 if ((path == (const char *) NULL) ||
643 (LocaleCompare(path,log_info[i]->path) != 0))
648 if (log_info[i]->path != (char *) NULL)
649 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
651 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
658 if ((log_info[i]->handler_mask & mask) != 0)
660 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
661 length+=strlen(LogHandlers[j].name);
664 for (j=(ssize_t) length; j <= 12; j++)
665 (void) FormatLocaleFile(file," ");
666 (void) FormatLocaleFile(file," Generations Limit Format\n");
667 (void) FormatLocaleFile(file,"-----------------------------------------"
668 "--------------------------------------\n");
670 path=log_info[i]->path;
671 if (log_info[i]->filename != (char *) NULL)
673 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
674 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
675 (void) FormatLocaleFile(file," ");
677 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
678 (void) FormatLocaleFile(file,"%8g ",(double) log_info[i]->limit);
679 if (log_info[i]->format != (char *) NULL)
680 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
681 (void) FormatLocaleFile(file,"\n");
684 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
693 + L o g C o m p o n e n t G e n e s i s %
697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699 % LogComponentGenesis() instantiates the log component.
701 % The format of the LogComponentGenesis method is:
703 % MagickBooleanType LogComponentGenesis(void)
706 MagickPrivate MagickBooleanType LogComponentGenesis(void)
711 AcquireSemaphoreInfo(&log_semaphore);
712 exception=AcquireExceptionInfo();
713 (void) GetLogInfo("*",exception);
714 exception=DestroyExceptionInfo(exception);
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 + L o g C o m p o n e n t T e r m i n u s %
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % LogComponentTerminus() destroys the logging component.
731 % The format of the LogComponentTerminus method is:
733 % LogComponentTerminus(void)
737 static void *DestroyLogElement(void *log_info)
742 p=(LogInfo *) log_info;
743 if (p->file != (FILE *) NULL)
745 (void) FormatLocaleFile(p->file,"</log>\n");
746 (void) fclose(p->file);
747 p->file=(FILE *) NULL;
749 if (p->format != (char *) NULL)
750 p->format=DestroyString(p->format);
751 if (p->path != (char *) NULL)
752 p->path=DestroyString(p->path);
753 if (p->filename != (char *) NULL)
754 p->filename=DestroyString(p->filename);
755 p=(LogInfo *) RelinquishMagickMemory(p);
756 return((void *) NULL);
759 MagickPrivate void LogComponentTerminus(void)
761 if (log_semaphore == (SemaphoreInfo *) NULL)
762 AcquireSemaphoreInfo(&log_semaphore);
763 LockSemaphoreInfo(log_semaphore);
764 if (log_list != (LinkedListInfo *) NULL)
765 log_list=DestroyLinkedList(log_list,DestroyLogElement);
766 instantiate_log=MagickFalse;
767 UnlockSemaphoreInfo(log_semaphore);
768 DestroySemaphoreInfo(&log_semaphore);
772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 % L o g M a g i c k E v e n t %
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782 % LogMagickEvent() logs an event as determined by the log configuration file.
783 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
785 % The format of the LogMagickEvent method is:
787 % MagickBooleanType LogMagickEvent(const LogEventType type,
788 % const char *module,const char *function,const size_t line,
789 % const char *format,...)
791 % A description of each parameter follows:
793 % o type: the event type.
795 % o filename: the source module filename.
797 % o function: the function name.
799 % o line: the line number of the source module.
801 % o format: the output format.
804 static char *TranslateEvent(const LogEventType magick_unused(type),
805 const char *module,const char *function,const size_t line,
806 const char *domain,const char *event)
833 exception=AcquireExceptionInfo();
834 log_info=(LogInfo *) GetLogInfo("*",exception);
835 exception=DestroyExceptionInfo(exception);
836 seconds=time((time_t *) NULL);
837 elapsed_time=GetElapsedTime(&log_info->timer);
838 user_time=GetUserTime(&log_info->timer);
839 text=AcquireString(event);
840 if (log_info->format == (char *) NULL)
842 extent=strlen(event)+MaxTextExtent;
843 if (LocaleCompare(log_info->format,"xml") == 0)
846 timestamp[MaxTextExtent];
849 Translate event in "XML" format.
851 (void) FormatMagickTime(seconds,extent,timestamp);
852 (void) FormatLocaleString(text,extent,
854 " <timestamp>%s</timestamp>\n"
855 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
856 " <user-time>%0.3f</user-time>\n"
857 " <process-id>%.20g</process-id>\n"
858 " <thread-id>%.20g</thread-id>\n"
859 " <module>%s</module>\n"
860 " <function>%s</function>\n"
861 " <line>%.20g</line>\n"
862 " <domain>%s</domain>\n"
863 " <event>%s</event>\n"
864 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
865 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
866 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
867 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
868 (double) line,domain,event);
872 Translate event in "human readable" format.
875 for (p=log_info->format; *p != '\0'; p++)
878 if ((size_t) (q-text+MaxTextExtent) >= extent)
880 extent+=MaxTextExtent;
881 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
883 if (text == (char *) NULL)
884 return((char *) NULL);
888 The format of the log is defined by embedding special format characters:
907 if ((*p == '\\') && (*(p+1) == 'r'))
913 if ((*p == '\\') && (*(p+1) == 'n'))
929 q+=CopyMagickString(q,GetClientName(),extent);
934 q+=CopyMagickString(q,domain,extent);
939 q+=CopyMagickString(q,event,extent);
944 q+=CopyMagickString(q,function,extent);
949 if (log_info->generations == 0)
951 (void) CopyMagickString(q,"0",extent);
955 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
956 log_info->generations));
961 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
969 for (p=module+strlen(module)-1; p > module; p--)
970 if (*p == *DirectorySeparator)
975 q+=CopyMagickString(q,p,extent);
980 q+=CopyMagickString(q,GetLogName(),extent);
985 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
990 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
991 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
992 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
997 q+=FormatMagickTime(seconds,extent,q);
1002 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
1007 q+=CopyMagickString(q,MagickLibVersionText,extent);
1027 static char *TranslateFilename(const LogInfo *log_info)
1042 Translate event in "human readable" format.
1044 assert(log_info != (LogInfo *) NULL);
1045 assert(log_info->filename != (char *) NULL);
1046 filename=AcquireString((char *) NULL);
1047 extent=MaxTextExtent;
1049 for (p=log_info->filename; *p != '\0'; p++)
1052 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1054 extent+=MaxTextExtent;
1055 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1057 if (filename == (char *) NULL)
1058 return((char *) NULL);
1059 q=filename+strlen(filename);
1062 The format of the filename is defined by embedding special format
1081 q+=CopyMagickString(q,GetClientName(),extent);
1086 if (log_info->generations == 0)
1088 (void) CopyMagickString(q,"0",extent);
1092 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1093 log_info->generations));
1098 q+=CopyMagickString(q,GetLogName(),extent);
1103 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1108 q+=CopyMagickString(q,MagickLibVersionText,extent);
1128 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1129 const char *function,const size_t line,const char *format,
1133 event[MaxTextExtent],
1148 if (IsEventLogging() == MagickFalse)
1149 return(MagickFalse);
1150 exception=AcquireExceptionInfo();
1151 log_info=(LogInfo *) GetLogInfo("*",exception);
1152 exception=DestroyExceptionInfo(exception);
1153 LockSemaphoreInfo(log_semaphore);
1154 if ((log_info->event_mask & type) == 0)
1156 UnlockSemaphoreInfo(log_semaphore);
1159 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1160 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1161 n=vsnprintf(event,MaxTextExtent,format,operands);
1163 n=vsprintf(event,format,operands);
1166 event[MaxTextExtent-1]='\0';
1167 text=TranslateEvent(type,module,function,line,domain,event);
1168 if (text == (char *) NULL)
1170 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1171 UnlockSemaphoreInfo(log_semaphore);
1172 return(MagickFalse);
1174 if ((log_info->handler_mask & ConsoleHandler) != 0)
1176 (void) FormatLocaleFile(stderr,"%s\n",text);
1177 (void) fflush(stderr);
1179 if ((log_info->handler_mask & DebugHandler) != 0)
1181 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1182 OutputDebugString(text);
1183 OutputDebugString("\n");
1186 if ((log_info->handler_mask & EventHandler) != 0)
1188 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1189 (void) NTReportEvent(text,MagickFalse);
1192 if ((log_info->handler_mask & FileHandler) != 0)
1197 file_info.st_size=0;
1198 if (log_info->file != (FILE *) NULL)
1199 (void) fstat(fileno(log_info->file),&file_info);
1200 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
1202 (void) FormatLocaleFile(log_info->file,"</log>\n");
1203 (void) fclose(log_info->file);
1204 log_info->file=(FILE *) NULL;
1206 if (log_info->file == (FILE *) NULL)
1211 filename=TranslateFilename(log_info);
1212 if (filename == (char *) NULL)
1214 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1215 UnlockSemaphoreInfo(log_semaphore);
1216 return(MagickFalse);
1218 log_info->append=IsPathAccessible(filename);
1219 log_info->file=fopen_utf8(filename,"ab");
1220 filename=(char *) RelinquishMagickMemory(filename);
1221 if (log_info->file == (FILE *) NULL)
1223 UnlockSemaphoreInfo(log_semaphore);
1224 return(MagickFalse);
1226 log_info->generation++;
1227 if (log_info->append == MagickFalse)
1228 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1229 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1230 (void) FormatLocaleFile(log_info->file,"<log>\n");
1232 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
1233 (void) fflush(log_info->file);
1235 if ((log_info->handler_mask & MethodHandler) != 0)
1237 if (log_info->method != (MagickLogMethod) NULL)
1238 log_info->method(type,text);
1240 if ((log_info->handler_mask & StdoutHandler) != 0)
1242 (void) FormatLocaleFile(stdout,"%s\n",text);
1243 (void) fflush(stdout);
1245 if ((log_info->handler_mask & StderrHandler) != 0)
1247 (void) FormatLocaleFile(stderr,"%s\n",text);
1248 (void) fflush(stderr);
1250 text=(char *) RelinquishMagickMemory(text);
1251 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1252 UnlockSemaphoreInfo(log_semaphore);
1256 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1257 const char *function,const size_t line,const char *format,...)
1265 va_start(operands,format);
1266 status=LogMagickEventList(type,module,function,line,format,operands);
1272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1276 + L o a d L o g L i s t %
1280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1282 % LoadLogList() loads the log configuration file which provides a
1283 % mapping between log attributes and log name.
1285 % The format of the LoadLogList method is:
1287 % MagickBooleanType LoadLogList(const char *xml,const char *filename,
1288 % const size_t depth,ExceptionInfo *exception)
1290 % A description of each parameter follows:
1292 % o xml: The log list in XML format.
1294 % o filename: The log list filename.
1296 % o depth: depth of <include /> statements.
1298 % o exception: return any errors or warnings in this structure.
1301 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1302 const size_t depth,ExceptionInfo *exception)
1305 keyword[MaxTextExtent],
1312 *log_info = (LogInfo *) NULL;
1318 Load the log map file.
1320 if (xml == (const char *) NULL)
1321 return(MagickFalse);
1322 if (log_list == (LinkedListInfo *) NULL)
1324 log_list=NewLinkedList(0);
1325 if (log_list == (LinkedListInfo *) NULL)
1327 ThrowFileException(exception,ResourceLimitError,
1328 "MemoryAllocationFailed",filename);
1329 return(MagickFalse);
1333 token=AcquireString((const char *) xml);
1334 for (q=(const char *) xml; *q != '\0'; )
1339 GetMagickToken(q,&q,token);
1342 (void) CopyMagickString(keyword,token,MaxTextExtent);
1343 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1348 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1349 GetMagickToken(q,&q,token);
1352 if (LocaleNCompare(keyword,"<!--",4) == 0)
1357 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1358 GetMagickToken(q,&q,token);
1361 if (LocaleCompare(keyword,"<include") == 0)
1366 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1368 (void) CopyMagickString(keyword,token,MaxTextExtent);
1369 GetMagickToken(q,&q,token);
1372 GetMagickToken(q,&q,token);
1373 if (LocaleCompare(keyword,"file") == 0)
1376 (void) ThrowMagickException(exception,GetMagickModule(),
1377 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1381 path[MaxTextExtent],
1384 GetPathComponent(filename,HeadPath,path);
1386 (void) ConcatenateMagickString(path,DirectorySeparator,
1388 if (*token == *DirectorySeparator)
1389 (void) CopyMagickString(path,token,MaxTextExtent);
1391 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1392 xml=FileToString(path,~0UL,exception);
1393 if (xml != (char *) NULL)
1395 status&=LoadLogList(xml,path,depth+1,exception);
1396 xml=DestroyString(xml);
1403 if (LocaleCompare(keyword,"<logmap>") == 0)
1406 Allocate memory for the log list.
1408 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1409 if (log_info == (LogInfo *) NULL)
1410 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1411 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1412 log_info->path=ConstantString(filename);
1413 GetTimerInfo((TimerInfo *) &log_info->timer);
1414 log_info->signature=MagickSignature;
1417 if (log_info == (LogInfo *) NULL)
1419 if (LocaleCompare(keyword,"</logmap>") == 0)
1421 status=AppendValueToLinkedList(log_list,log_info);
1422 if (status == MagickFalse)
1423 (void) ThrowMagickException(exception,GetMagickModule(),
1424 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1425 log_info=(LogInfo *) NULL;
1427 GetMagickToken(q,(const char **) NULL,token);
1430 GetMagickToken(q,&q,token);
1431 GetMagickToken(q,&q,token);
1437 if (LocaleCompare((char *) keyword,"events") == 0)
1439 log_info->event_mask=(LogEventType) (log_info->event_mask |
1440 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1448 if (LocaleCompare((char *) keyword,"filename") == 0)
1450 if (log_info->filename != (char *) NULL)
1451 log_info->filename=(char *)
1452 RelinquishMagickMemory(log_info->filename);
1453 log_info->filename=ConstantString(token);
1456 if (LocaleCompare((char *) keyword,"format") == 0)
1458 if (log_info->format != (char *) NULL)
1459 log_info->format=(char *)
1460 RelinquishMagickMemory(log_info->format);
1461 log_info->format=ConstantString(token);
1469 if (LocaleCompare((char *) keyword,"generations") == 0)
1471 if (LocaleCompare(token,"unlimited") == 0)
1473 log_info->generations=(~0UL);
1476 log_info->generations=StringToUnsignedLong(token);
1484 if (LocaleCompare((char *) keyword,"limit") == 0)
1486 if (LocaleCompare(token,"unlimited") == 0)
1488 log_info->limit=(~0UL);
1491 log_info->limit=StringToUnsignedLong(token);
1499 if (LocaleCompare((char *) keyword,"output") == 0)
1501 log_info->handler_mask=(LogHandlerType)
1502 (log_info->handler_mask | ParseLogHandlers(token));
1511 token=DestroyString(token);
1512 if (log_list == (LinkedListInfo *) NULL)
1513 return(MagickFalse);
1514 return(status != 0 ? MagickTrue : MagickFalse);
1518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1522 % L o a d L o g L i s t s %
1526 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1528 % LoadLogLists() loads one or more log configuration file which provides a
1529 % mapping between log attributes and log name.
1531 % The format of the LoadLogLists method is:
1533 % MagickBooleanType LoadLogLists(const char *filename,
1534 % ExceptionInfo *exception)
1536 % A description of each parameter follows:
1538 % o filename: the log configuration filename.
1540 % o exception: return any errors or warnings in this structure.
1543 static MagickBooleanType LoadLogLists(const char *filename,
1544 ExceptionInfo *exception)
1559 Load external log map.
1561 if (log_list == (LinkedListInfo *) NULL)
1563 log_list=NewLinkedList(0);
1564 if (log_list == (LinkedListInfo *) NULL)
1566 ThrowFileException(exception,ResourceLimitError,
1567 "MemoryAllocationFailed",filename);
1568 return(MagickFalse);
1572 options=GetConfigureOptions(filename,exception);
1573 option=(const StringInfo *) GetNextValueInLinkedList(options);
1574 while (option != (const StringInfo *) NULL)
1576 status&=LoadLogList((const char *) GetStringInfoDatum(option),
1577 GetStringInfoPath(option),0,exception);
1578 option=(const StringInfo *) GetNextValueInLinkedList(options);
1580 options=DestroyConfigureOptions(options);
1582 Load built-in log map.
1584 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1589 register const LogMapInfo
1593 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1594 if (log_info == (LogInfo *) NULL)
1596 (void) ThrowMagickException(exception,GetMagickModule(),
1597 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1600 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1601 log_info->path=ConstantString("[built-in]");
1602 GetTimerInfo((TimerInfo *) &log_info->timer);
1603 log_info->event_mask=p->event_mask;
1604 log_info->handler_mask=p->handler_mask;
1605 log_info->filename=ConstantString(p->filename);
1606 log_info->format=ConstantString(p->format);
1607 log_info->signature=MagickSignature;
1608 status&=AppendValueToLinkedList(log_list,log_info);
1609 if (status == MagickFalse)
1610 (void) ThrowMagickException(exception,GetMagickModule(),
1611 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1613 return(status != 0 ? MagickTrue : MagickFalse);
1617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 + P a r s e L o g H a n d l e r s %
1625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1627 % ParseLogHandlers() parses a string defining which handlers takes a log
1628 % message and exports them.
1630 % The format of the ParseLogHandlers method is:
1632 % LogHandlerType ParseLogHandlers(const char *handlers)
1634 % A description of each parameter follows:
1636 % o handlers: one or more handlers separated by commas.
1639 static LogHandlerType ParseLogHandlers(const char *handlers)
1653 handler_mask=NoHandler;
1654 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1656 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1659 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1661 length=strlen(LogHandlers[i].name);
1662 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1664 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1668 if (LogHandlers[i].name == (char *) NULL)
1669 return(UndefinedHandler);
1671 return(handler_mask);
1675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1679 % S e t L o g E v e n t M a s k %
1683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1685 % SetLogEventMask() accepts a list that determines which events to log. All
1686 % other events are ignored. By default, no debug is enabled. This method
1687 % returns the previous log event mask.
1689 % The format of the SetLogEventMask method is:
1691 % LogEventType SetLogEventMask(const char *events)
1693 % A description of each parameter follows:
1695 % o events: log these events.
1698 MagickExport LogEventType SetLogEventMask(const char *events)
1709 exception=AcquireExceptionInfo();
1710 log_info=(LogInfo *) GetLogInfo("*",exception);
1711 exception=DestroyExceptionInfo(exception);
1712 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1713 LockSemaphoreInfo(log_semaphore);
1714 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1715 log_info->event_mask=(LogEventType) option;
1717 log_info->event_mask=UndefinedEvents;
1718 UnlockSemaphoreInfo(log_semaphore);
1719 return(log_info->event_mask);
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % S e t L o g F o r m a t %
1731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733 % SetLogFormat() sets the format for the "human readable" log record.
1735 % The format of the LogMagickFormat method is:
1737 % SetLogFormat(const char *format)
1739 % A description of each parameter follows:
1741 % o format: the log record format.
1744 MagickExport void SetLogFormat(const char *format)
1752 exception=AcquireExceptionInfo();
1753 log_info=(LogInfo *) GetLogInfo("*",exception);
1754 exception=DestroyExceptionInfo(exception);
1755 LockSemaphoreInfo(log_semaphore);
1756 if (log_info->format != (char *) NULL)
1757 log_info->format=DestroyString(log_info->format);
1758 log_info->format=ConstantString(format);
1759 UnlockSemaphoreInfo(log_semaphore);
1763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1767 % S e t L o g M e t h o d %
1771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1773 % SetLogMethod() sets the method that will be called when an event is logged.
1775 % The format of the SetLogMethod method is:
1777 % void SetLogMethod(MagickLogMethod method)
1779 % A description of each parameter follows:
1781 % o method: pointer to a method that will be called when LogMagickEvent is
1785 MagickExport void SetLogMethod(MagickLogMethod method)
1793 exception=AcquireExceptionInfo();
1794 log_info=(LogInfo *) GetLogInfo("*",exception);
1795 exception=DestroyExceptionInfo(exception);
1796 LockSemaphoreInfo(log_semaphore);
1797 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1798 log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1800 log_info->method=method;
1801 UnlockSemaphoreInfo(log_semaphore);
1805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1809 % S e t L o g N a m e %
1813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1815 % SetLogName() sets the log name and returns it.
1817 % The format of the SetLogName method is:
1819 % const char *SetLogName(const char *name)
1821 % A description of each parameter follows:
1823 % o log_name: SetLogName() returns the current client name.
1825 % o name: Specifies the new client name.
1828 MagickExport const char *SetLogName(const char *name)
1830 if ((name != (char *) NULL) && (*name != '\0'))
1831 (void) CopyMagickString(log_name,name,MaxTextExtent);