2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Log Events %
20 % Copyright 1999-2013 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,
86 typedef struct _EventInfo
95 typedef struct _HandlerInfo
140 typedef struct _LogMapInfo
156 static const HandlerInfo
159 { "Console", ConsoleHandler },
160 { "Debug", DebugHandler },
161 { "Event", EventHandler },
162 { "File", FileHandler },
163 { "None", NoHandler },
164 { "Stderr", StderrHandler },
165 { "Stdout", StdoutHandler },
166 { (char *) NULL, UndefinedHandler }
169 static const LogMapInfo
172 { NoEvents, ConsoleHandler, "Magick-%g.log",
173 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n %e" }
177 log_name[MaxTextExtent] = "Magick";
179 static LinkedListInfo
180 *log_list = (LinkedListInfo *) NULL;
183 *log_semaphore = (SemaphoreInfo *) NULL;
185 static volatile MagickBooleanType
186 instantiate_log = MagickFalse;
189 Forward declarations.
191 static LogHandlerType
192 ParseLogHandlers(const char *);
195 *GetLogInfo(const char *,ExceptionInfo *);
197 static MagickBooleanType
198 InitializeLogList(ExceptionInfo *),
199 LoadLogLists(const char *,ExceptionInfo *);
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 % C l o s e M a g i c k L o g %
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 % CloseMagickLog() closes the Magick log.
214 % The format of the CloseMagickLog method is:
216 % CloseMagickLog(void)
219 MagickExport void CloseMagickLog(void)
227 if (IsEventLogging() == MagickFalse)
229 exception=AcquireExceptionInfo();
230 log_info=GetLogInfo("*",exception);
231 exception=DestroyExceptionInfo(exception);
232 LockSemaphoreInfo(log_semaphore);
233 if (log_info->file != (FILE *) NULL)
235 (void) FormatLocaleFile(log_info->file,"</log>\n");
236 (void) fclose(log_info->file);
237 log_info->file=(FILE *) NULL;
239 UnlockSemaphoreInfo(log_semaphore);
243 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 + G e t L o g I n f o %
251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253 % GetLogInfo() searches the log list for the specified name and if found
254 % returns attributes for that log.
256 % The format of the GetLogInfo method is:
258 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
260 % A description of each parameter follows:
262 % o name: the log name.
264 % o exception: return any errors or warnings in this structure.
267 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
272 assert(exception != (ExceptionInfo *) NULL);
273 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
274 if (InitializeLogList(exception) == MagickFalse)
275 return((LogInfo *) NULL);
276 if ((log_list == (LinkedListInfo *) NULL) ||
277 (IsLinkedListEmpty(log_list) != MagickFalse))
278 return((LogInfo *) NULL);
279 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
280 return((LogInfo *) GetValueFromLinkedList(log_list,0));
284 LockSemaphoreInfo(log_semaphore);
285 ResetLinkedListIterator(log_list);
286 p=(LogInfo *) GetNextValueInLinkedList(log_list);
287 while (p != (LogInfo *) NULL)
289 if (LocaleCompare(name,p->name) == 0)
291 p=(LogInfo *) GetNextValueInLinkedList(log_list);
293 if (p != (LogInfo *) NULL)
294 (void) InsertValueInLinkedList(log_list,0,
295 RemoveElementByValueFromLinkedList(log_list,p));
296 UnlockSemaphoreInfo(log_semaphore);
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 % G e t L o g I n f o L i s t %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 % GetLogInfoList() returns any logs that match the specified pattern.
313 % The format of the GetLogInfoList function is:
315 % const LogInfo **GetLogInfoList(const char *pattern,
316 % size_t *number_preferences,ExceptionInfo *exception)
318 % A description of each parameter follows:
320 % o pattern: Specifies a pointer to a text string containing a pattern.
322 % o number_preferences: This integer returns the number of logs in the list.
324 % o exception: return any errors or warnings in this structure.
327 #if defined(__cplusplus) || defined(c_plusplus)
331 static int LogInfoCompare(const void *x,const void *y)
337 p=(const LogInfo **) x,
338 q=(const LogInfo **) y;
339 if (LocaleCompare((*p)->path,(*q)->path) == 0)
340 return(LocaleCompare((*p)->name,(*q)->name));
341 return(LocaleCompare((*p)->path,(*q)->path));
344 #if defined(__cplusplus) || defined(c_plusplus)
348 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
349 size_t *number_preferences,ExceptionInfo *exception)
354 register const LogInfo
363 assert(pattern != (char *) NULL);
364 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
365 assert(number_preferences != (size_t *) NULL);
366 *number_preferences=0;
367 p=GetLogInfo("*",exception);
368 if (p == (const LogInfo *) NULL)
369 return((const LogInfo **) NULL);
370 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
371 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
372 if (preferences == (const LogInfo **) NULL)
373 return((const LogInfo **) NULL);
377 LockSemaphoreInfo(log_semaphore);
378 ResetLinkedListIterator(log_list);
379 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
380 for (i=0; p != (const LogInfo *) NULL; )
382 if ((p->stealth == MagickFalse) &&
383 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
385 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
387 UnlockSemaphoreInfo(log_semaphore);
388 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
389 preferences[i]=(LogInfo *) NULL;
390 *number_preferences=(size_t) i;
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 % G e t L o g L i s t %
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % GetLogList() returns any logs that match the specified pattern.
407 % The format of the GetLogList function is:
409 % char **GetLogList(const char *pattern,size_t *number_preferences,
410 % ExceptionInfo *exception)
412 % A description of each parameter follows:
414 % o pattern: Specifies a pointer to a text string containing a pattern.
416 % o number_preferences: This integer returns the number of logs in the list.
418 % o exception: return any errors or warnings in this structure.
422 #if defined(__cplusplus) || defined(c_plusplus)
426 static int LogCompare(const void *x,const void *y)
434 return(LocaleCompare(*p,*q));
437 #if defined(__cplusplus) || defined(c_plusplus)
441 MagickExport char **GetLogList(const char *pattern,
442 size_t *number_preferences,ExceptionInfo *exception)
447 register const LogInfo
456 assert(pattern != (char *) NULL);
457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
458 assert(number_preferences != (size_t *) NULL);
459 *number_preferences=0;
460 p=GetLogInfo("*",exception);
461 if (p == (const LogInfo *) NULL)
462 return((char **) NULL);
463 preferences=(char **) AcquireQuantumMemory((size_t)
464 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
465 if (preferences == (char **) NULL)
466 return((char **) NULL);
470 LockSemaphoreInfo(log_semaphore);
471 ResetLinkedListIterator(log_list);
472 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
473 for (i=0; p != (const LogInfo *) NULL; )
475 if ((p->stealth == MagickFalse) &&
476 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
477 preferences[i++]=ConstantString(p->name);
478 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
480 UnlockSemaphoreInfo(log_semaphore);
481 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
482 preferences[i]=(char *) NULL;
483 *number_preferences=(size_t) i;
488 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 % G e t L o g N a m e %
496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 % GetLogName() returns the current log name.
500 % The format of the GetLogName method is:
502 % const char *GetLogName(void)
505 MagickExport const char *GetLogName(void)
511 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515 + I n i t i a l i z e L o g L i s t %
519 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521 % InitializeLogList() initialize the log list.
523 % The format of the InitializeLogList method is:
525 % MagickBooleanType InitializeLogList(ExceptionInfo *exception)
527 % A description of each parameter follows.
529 % o exception: return any errors or warnings in this structure.
532 static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
534 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
536 if (log_semaphore == (SemaphoreInfo *) NULL)
537 AcquireSemaphoreInfo(&log_semaphore);
538 LockSemaphoreInfo(log_semaphore);
539 if ((log_list == (LinkedListInfo *) NULL) &&
540 (instantiate_log == MagickFalse))
542 (void) LoadLogLists(LogFilename,exception);
543 instantiate_log=MagickTrue;
545 UnlockSemaphoreInfo(log_semaphore);
547 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555 % I s E v e n t L o g g i n g %
559 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
564 % The format of the IsEventLogging method is:
566 % MagickBooleanType IsEventLogging(void)
569 MagickExport MagickBooleanType IsEventLogging(void)
577 if ((log_list == (LinkedListInfo *) NULL) ||
578 (IsLinkedListEmpty(log_list) != MagickFalse))
580 exception=AcquireExceptionInfo();
581 log_info=GetLogInfo("*",exception);
582 exception=DestroyExceptionInfo(exception);
583 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590 % L i s t L o g I n f o %
594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596 % ListLogInfo() lists the log info to a file.
598 % The format of the ListLogInfo method is:
600 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
602 % A description of each parameter follows.
604 % o file: An pointer to a FILE.
606 % o exception: return any errors or warnings in this structure.
609 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
611 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
628 if (file == (const FILE *) NULL)
630 log_info=GetLogInfoList("*",&number_aliases,exception);
631 if (log_info == (const LogInfo **) NULL)
634 path=(const char *) NULL;
635 for (i=0; i < (ssize_t) number_aliases; i++)
637 if (log_info[i]->stealth != MagickFalse)
639 if ((path == (const char *) NULL) ||
640 (LocaleCompare(path,log_info[i]->path) != 0))
645 if (log_info[i]->path != (char *) NULL)
646 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
648 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
655 if ((log_info[i]->handler_mask & mask) != 0)
657 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
658 length+=strlen(LogHandlers[j].name);
661 for (j=(ssize_t) length; j <= 12; j++)
662 (void) FormatLocaleFile(file," ");
663 (void) FormatLocaleFile(file," Generations Limit Format\n");
664 (void) FormatLocaleFile(file,"-----------------------------------------"
665 "--------------------------------------\n");
667 path=log_info[i]->path;
668 if (log_info[i]->filename != (char *) NULL)
670 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
671 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
672 (void) FormatLocaleFile(file," ");
674 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
675 (void) FormatLocaleFile(file,"%8g ",(double) log_info[i]->limit);
676 if (log_info[i]->format != (char *) NULL)
677 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
678 (void) FormatLocaleFile(file,"\n");
681 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690 + L o g C o m p o n e n t G e n e s i s %
694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696 % LogComponentGenesis() instantiates the log component.
698 % The format of the LogComponentGenesis method is:
700 % MagickBooleanType LogComponentGenesis(void)
703 MagickPrivate MagickBooleanType LogComponentGenesis(void)
708 AcquireSemaphoreInfo(&log_semaphore);
709 exception=AcquireExceptionInfo();
710 (void) GetLogInfo("*",exception);
711 exception=DestroyExceptionInfo(exception);
716 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 + L o g C o m p o n e n t T e r m i n u s %
724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
726 % LogComponentTerminus() destroys the logging component.
728 % The format of the LogComponentTerminus method is:
730 % LogComponentTerminus(void)
734 static void *DestroyLogElement(void *log_info)
739 p=(LogInfo *) log_info;
740 if (p->file != (FILE *) NULL)
742 (void) FormatLocaleFile(p->file,"</log>\n");
743 (void) fclose(p->file);
744 p->file=(FILE *) NULL;
746 if (p->exempt == MagickFalse)
748 if (p->format != (char *) NULL)
749 p->format=DestroyString(p->format);
750 if (p->path != (char *) NULL)
751 p->path=DestroyString(p->path);
752 if (p->filename != (char *) NULL)
753 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 & StdoutHandler) != 0)
1237 (void) FormatLocaleFile(stdout,"%s\n",text);
1238 (void) fflush(stdout);
1240 if ((log_info->handler_mask & StderrHandler) != 0)
1242 (void) FormatLocaleFile(stderr,"%s\n",text);
1243 (void) fflush(stderr);
1245 text=(char *) RelinquishMagickMemory(text);
1246 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1247 UnlockSemaphoreInfo(log_semaphore);
1251 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1252 const char *function,const size_t line,const char *format,...)
1260 va_start(operands,format);
1261 status=LogMagickEventList(type,module,function,line,format,operands);
1267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1271 + L o a d L o g L i s t %
1275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1277 % LoadLogList() loads the log configuration file which provides a
1278 % mapping between log attributes and log name.
1280 % The format of the LoadLogList method is:
1282 % MagickBooleanType LoadLogList(const char *xml,const char *filename,
1283 % const size_t depth,ExceptionInfo *exception)
1285 % A description of each parameter follows:
1287 % o xml: The log list in XML format.
1289 % o filename: The log list filename.
1291 % o depth: depth of <include /> statements.
1293 % o exception: return any errors or warnings in this structure.
1296 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1297 const size_t depth,ExceptionInfo *exception)
1300 keyword[MaxTextExtent],
1307 *log_info = (LogInfo *) NULL;
1313 Load the log map file.
1315 if (xml == (const char *) NULL)
1316 return(MagickFalse);
1317 if (log_list == (LinkedListInfo *) NULL)
1319 log_list=NewLinkedList(0);
1320 if (log_list == (LinkedListInfo *) NULL)
1322 ThrowFileException(exception,ResourceLimitError,
1323 "MemoryAllocationFailed",filename);
1324 return(MagickFalse);
1328 token=AcquireString((const char *) xml);
1329 for (q=(const char *) xml; *q != '\0'; )
1334 GetMagickToken(q,&q,token);
1337 (void) CopyMagickString(keyword,token,MaxTextExtent);
1338 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1343 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1344 GetMagickToken(q,&q,token);
1347 if (LocaleNCompare(keyword,"<!--",4) == 0)
1352 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1353 GetMagickToken(q,&q,token);
1356 if (LocaleCompare(keyword,"<include") == 0)
1361 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1363 (void) CopyMagickString(keyword,token,MaxTextExtent);
1364 GetMagickToken(q,&q,token);
1367 GetMagickToken(q,&q,token);
1368 if (LocaleCompare(keyword,"file") == 0)
1371 (void) ThrowMagickException(exception,GetMagickModule(),
1372 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1376 path[MaxTextExtent],
1379 GetPathComponent(filename,HeadPath,path);
1381 (void) ConcatenateMagickString(path,DirectorySeparator,
1383 if (*token == *DirectorySeparator)
1384 (void) CopyMagickString(path,token,MaxTextExtent);
1386 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1387 xml=FileToString(path,~0,exception);
1388 if (xml != (char *) NULL)
1390 status&=LoadLogList(xml,path,depth+1,exception);
1391 xml=DestroyString(xml);
1398 if (LocaleCompare(keyword,"<logmap>") == 0)
1401 Allocate memory for the log list.
1403 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1404 if (log_info == (LogInfo *) NULL)
1405 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1406 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1407 log_info->path=ConstantString(filename);
1408 GetTimerInfo((TimerInfo *) &log_info->timer);
1409 log_info->exempt=MagickFalse;
1410 log_info->signature=MagickSignature;
1413 if (log_info == (LogInfo *) NULL)
1415 if (LocaleCompare(keyword,"</logmap>") == 0)
1417 status=AppendValueToLinkedList(log_list,log_info);
1418 if (status == MagickFalse)
1419 (void) ThrowMagickException(exception,GetMagickModule(),
1420 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1421 log_info=(LogInfo *) NULL;
1423 GetMagickToken(q,(const char **) NULL,token);
1426 GetMagickToken(q,&q,token);
1427 GetMagickToken(q,&q,token);
1433 if (LocaleCompare((char *) keyword,"events") == 0)
1435 log_info->event_mask=(LogEventType) (log_info->event_mask |
1436 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1444 if (LocaleCompare((char *) keyword,"filename") == 0)
1446 if (log_info->filename != (char *) NULL)
1447 log_info->filename=(char *)
1448 RelinquishMagickMemory(log_info->filename);
1449 log_info->filename=ConstantString(token);
1452 if (LocaleCompare((char *) keyword,"format") == 0)
1454 if (log_info->format != (char *) NULL)
1455 log_info->format=(char *)
1456 RelinquishMagickMemory(log_info->format);
1457 log_info->format=ConstantString(token);
1465 if (LocaleCompare((char *) keyword,"generations") == 0)
1467 if (LocaleCompare(token,"unlimited") == 0)
1469 log_info->generations=(~0UL);
1472 log_info->generations=StringToUnsignedLong(token);
1480 if (LocaleCompare((char *) keyword,"limit") == 0)
1482 if (LocaleCompare(token,"unlimited") == 0)
1484 log_info->limit=(~0UL);
1487 log_info->limit=StringToUnsignedLong(token);
1495 if (LocaleCompare((char *) keyword,"output") == 0)
1497 log_info->handler_mask=(LogHandlerType)
1498 (log_info->handler_mask | ParseLogHandlers(token));
1507 token=DestroyString(token);
1508 if (log_list == (LinkedListInfo *) NULL)
1509 return(MagickFalse);
1510 return(status != 0 ? MagickTrue : MagickFalse);
1514 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1518 % L o a d L o g L i s t s %
1522 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1524 % LoadLogLists() loads one or more log configuration file which provides a
1525 % mapping between log attributes and log name.
1527 % The format of the LoadLogLists method is:
1529 % MagickBooleanType LoadLogLists(const char *filename,
1530 % ExceptionInfo *exception)
1532 % A description of each parameter follows:
1534 % o filename: the log configuration filename.
1536 % o exception: return any errors or warnings in this structure.
1539 static MagickBooleanType LoadLogLists(const char *filename,
1540 ExceptionInfo *exception)
1555 Load external log map.
1557 if (log_list == (LinkedListInfo *) NULL)
1559 log_list=NewLinkedList(0);
1560 if (log_list == (LinkedListInfo *) NULL)
1562 ThrowFileException(exception,ResourceLimitError,
1563 "MemoryAllocationFailed",filename);
1564 return(MagickFalse);
1568 options=GetConfigureOptions(filename,exception);
1569 option=(const StringInfo *) GetNextValueInLinkedList(options);
1570 while (option != (const StringInfo *) NULL)
1572 status&=LoadLogList((const char *) GetStringInfoDatum(option),
1573 GetStringInfoPath(option),0,exception);
1574 option=(const StringInfo *) GetNextValueInLinkedList(options);
1576 options=DestroyConfigureOptions(options);
1578 Load built-in log map.
1580 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1585 register const LogMapInfo
1589 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1590 if (log_info == (LogInfo *) NULL)
1592 (void) ThrowMagickException(exception,GetMagickModule(),
1593 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1596 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1597 log_info->path=(char *) "[built-in]";
1598 GetTimerInfo((TimerInfo *) &log_info->timer);
1599 log_info->event_mask=p->event_mask;
1600 log_info->handler_mask=p->handler_mask;
1601 log_info->filename=ConstantString(p->filename);
1602 log_info->format=ConstantString(p->format);
1603 log_info->exempt=MagickTrue;
1604 log_info->signature=MagickSignature;
1605 status&=AppendValueToLinkedList(log_list,log_info);
1606 if (status == MagickFalse)
1607 (void) ThrowMagickException(exception,GetMagickModule(),
1608 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1610 return(status != 0 ? MagickTrue : MagickFalse);
1614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1618 + P a r s e L o g H a n d l e r s %
1622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1624 % ParseLogHandlers() parses a string defining which handlers takes a log
1625 % message and exports them.
1627 % The format of the ParseLogHandlers method is:
1629 % LogHandlerType ParseLogHandlers(const char *handlers)
1631 % A description of each parameter follows:
1633 % o handlers: one or more handlers separated by commas.
1636 static LogHandlerType ParseLogHandlers(const char *handlers)
1650 handler_mask=NoHandler;
1651 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1653 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1656 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1658 length=strlen(LogHandlers[i].name);
1659 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1661 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1665 if (LogHandlers[i].name == (char *) NULL)
1666 return(UndefinedHandler);
1668 return(handler_mask);
1672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1676 % S e t L o g E v e n t M a s k %
1680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1682 % SetLogEventMask() accepts a list that determines which events to log. All
1683 % other events are ignored. By default, no debug is enabled. This method
1684 % returns the previous log event mask.
1686 % The format of the SetLogEventMask method is:
1688 % LogEventType SetLogEventMask(const char *events)
1690 % A description of each parameter follows:
1692 % o events: log these events.
1695 MagickExport LogEventType SetLogEventMask(const char *events)
1706 exception=AcquireExceptionInfo();
1707 log_info=(LogInfo *) GetLogInfo("*",exception);
1708 exception=DestroyExceptionInfo(exception);
1709 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1710 LockSemaphoreInfo(log_semaphore);
1711 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1712 log_info->event_mask=(LogEventType) option;
1714 log_info->event_mask=UndefinedEvents;
1715 UnlockSemaphoreInfo(log_semaphore);
1716 return(log_info->event_mask);
1720 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1724 % S e t L o g F o r m a t %
1728 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1730 % SetLogFormat() sets the format for the "human readable" log record.
1732 % The format of the LogMagickFormat method is:
1734 % SetLogFormat(const char *format)
1736 % A description of each parameter follows:
1738 % o format: the log record format.
1741 MagickExport void SetLogFormat(const char *format)
1749 exception=AcquireExceptionInfo();
1750 log_info=(LogInfo *) GetLogInfo("*",exception);
1751 exception=DestroyExceptionInfo(exception);
1752 LockSemaphoreInfo(log_semaphore);
1753 if (log_info->format != (char *) NULL)
1754 log_info->format=DestroyString(log_info->format);
1755 log_info->format=ConstantString(format);
1756 UnlockSemaphoreInfo(log_semaphore);
1760 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1764 % S e t L o g N a m e %
1768 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1770 % SetLogName() sets the log name and returns it.
1772 % The format of the SetLogName method is:
1774 % const char *SetLogName(const char *name)
1776 % A description of each parameter follows:
1778 % o log_name: SetLogName() returns the current client name.
1780 % o name: Specifies the new client name.
1783 MagickExport const char *SetLogName(const char *name)
1785 if ((name != (char *) NULL) && (*name != '\0'))
1786 (void) CopyMagickString(log_name,name,MaxTextExtent);