2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Log Events %
20 % Copyright 1999-2011 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/client.h"
45 #include "magick/configure.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/hashmap.h"
49 #include "magick/log.h"
50 #include "magick/memory_.h"
51 #include "magick/option.h"
52 #include "magick/semaphore.h"
53 #include "magick/timer.h"
54 #include "magick/string_.h"
55 #include "magick/string-private.h"
56 #include "magick/token.h"
57 #include "magick/thread_.h"
58 #include "magick/thread-private.h"
59 #include "magick/utility.h"
60 #include "magick/version.h"
61 #include "magick/xml-tree.h"
66 #define LogFilename "log.xml"
73 UndefinedHandler = 0x0000,
75 ConsoleHandler = 0x0001,
76 StdoutHandler = 0x0002,
77 StderrHandler = 0x0004,
79 DebugHandler = 0x0010,
83 typedef struct _EventInfo
92 typedef struct _HandlerInfo
137 typedef struct _LogMapInfo
153 static const HandlerInfo
156 { "console", ConsoleHandler },
157 { "debug", DebugHandler },
158 { "event", EventHandler },
159 { "file", FileHandler },
160 { "none", NoHandler },
161 { "stderr", StderrHandler },
162 { "stdout", StdoutHandler },
163 { (char *) NULL, UndefinedHandler }
166 static const LogMapInfo
169 { NoEvents, ConsoleHandler, "Magick-%d.log",
170 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\n %e" }
174 log_name[MaxTextExtent] = "Magick";
176 static LinkedListInfo
177 *log_list = (LinkedListInfo *) NULL;
180 *log_semaphore = (SemaphoreInfo *) NULL;
182 static volatile MagickBooleanType
183 instantiate_log = MagickFalse;
186 Forward declarations.
188 static LogHandlerType
189 ParseLogHandlers(const char *);
192 *GetLogInfo(const char *,ExceptionInfo *);
194 static MagickBooleanType
195 InitializeLogList(ExceptionInfo *),
196 LoadLogLists(const char *,ExceptionInfo *);
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % C l o s e M a g i c k L o g %
207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % CloseMagickLog() closes the Magick log.
211 % The format of the CloseMagickLog method is:
213 % CloseMagickLog(void)
216 MagickExport void CloseMagickLog(void)
224 if (IsEventLogging() == MagickFalse)
226 exception=AcquireExceptionInfo();
227 log_info=GetLogInfo("*",exception);
228 exception=DestroyExceptionInfo(exception);
229 LockSemaphoreInfo(log_semaphore);
230 if (log_info->file != (FILE *) NULL)
232 if (log_info->append == MagickFalse)
233 (void) FormatLocaleFile(log_info->file,"</log>\n");
234 (void) fclose(log_info->file);
235 log_info->file=(FILE *) NULL;
237 UnlockSemaphoreInfo(log_semaphore);
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 + G e t L o g I n f o %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % GetLogInfo() searches the log list for the specified name and if found
252 % returns attributes for that log.
254 % The format of the GetLogInfo method is:
256 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
258 % A description of each parameter follows:
260 % o name: the log name.
262 % o exception: return any errors or warnings in this structure.
265 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
270 assert(exception != (ExceptionInfo *) NULL);
271 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
272 if (InitializeLogList(exception) == MagickFalse)
273 return((LogInfo *) NULL);
274 if ((log_list == (LinkedListInfo *) NULL) ||
275 (IsLinkedListEmpty(log_list) != MagickFalse))
276 return((LogInfo *) NULL);
277 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
278 return((LogInfo *) GetValueFromLinkedList(log_list,0));
282 LockSemaphoreInfo(log_semaphore);
283 ResetLinkedListIterator(log_list);
284 p=(LogInfo *) GetNextValueInLinkedList(log_list);
285 while (p != (LogInfo *) NULL)
287 if (LocaleCompare(name,p->name) == 0)
289 p=(LogInfo *) GetNextValueInLinkedList(log_list);
291 if (p != (LogInfo *) NULL)
292 (void) InsertValueInLinkedList(log_list,0,
293 RemoveElementByValueFromLinkedList(log_list,p));
294 UnlockSemaphoreInfo(log_semaphore);
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % G e t L o g I n f o L i s t %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % GetLogInfoList() returns any logs that match the specified pattern.
311 % The format of the GetLogInfoList function is:
313 % const LogInfo **GetLogInfoList(const char *pattern,
314 % size_t *number_preferences,ExceptionInfo *exception)
316 % A description of each parameter follows:
318 % o pattern: Specifies a pointer to a text string containing a pattern.
320 % o number_preferences: This integer returns the number of logs in the list.
322 % o exception: return any errors or warnings in this structure.
325 #if defined(__cplusplus) || defined(c_plusplus)
329 static int LogInfoCompare(const void *x,const void *y)
335 p=(const LogInfo **) x,
336 q=(const LogInfo **) y;
337 if (LocaleCompare((*p)->path,(*q)->path) == 0)
338 return(LocaleCompare((*p)->name,(*q)->name));
339 return(LocaleCompare((*p)->path,(*q)->path));
342 #if defined(__cplusplus) || defined(c_plusplus)
346 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
347 size_t *number_preferences,ExceptionInfo *exception)
352 register const LogInfo
361 assert(pattern != (char *) NULL);
362 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
363 assert(number_preferences != (size_t *) NULL);
364 *number_preferences=0;
365 p=GetLogInfo("*",exception);
366 if (p == (const LogInfo *) NULL)
367 return((const LogInfo **) NULL);
368 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
369 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
370 if (preferences == (const LogInfo **) NULL)
371 return((const LogInfo **) NULL);
375 LockSemaphoreInfo(log_semaphore);
376 ResetLinkedListIterator(log_list);
377 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
378 for (i=0; p != (const LogInfo *) NULL; )
380 if ((p->stealth == MagickFalse) &&
381 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
383 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
385 UnlockSemaphoreInfo(log_semaphore);
386 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
387 preferences[i]=(LogInfo *) NULL;
388 *number_preferences=(size_t) i;
393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 % G e t L o g L i s t %
401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 % GetLogList() returns any logs that match the specified pattern.
405 % The format of the GetLogList function is:
407 % char **GetLogList(const char *pattern,size_t *number_preferences,
408 % ExceptionInfo *exception)
410 % A description of each parameter follows:
412 % o pattern: Specifies a pointer to a text string containing a pattern.
414 % o number_preferences: This integer returns the number of logs in the list.
416 % o exception: return any errors or warnings in this structure.
420 #if defined(__cplusplus) || defined(c_plusplus)
424 static int LogCompare(const void *x,const void *y)
432 return(LocaleCompare(*p,*q));
435 #if defined(__cplusplus) || defined(c_plusplus)
439 MagickExport char **GetLogList(const char *pattern,
440 size_t *number_preferences,ExceptionInfo *exception)
445 register const LogInfo
454 assert(pattern != (char *) NULL);
455 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
456 assert(number_preferences != (size_t *) NULL);
457 *number_preferences=0;
458 p=GetLogInfo("*",exception);
459 if (p == (const LogInfo *) NULL)
460 return((char **) NULL);
461 preferences=(char **) AcquireQuantumMemory((size_t)
462 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
463 if (preferences == (char **) NULL)
464 return((char **) NULL);
468 LockSemaphoreInfo(log_semaphore);
469 ResetLinkedListIterator(log_list);
470 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
471 for (i=0; p != (const LogInfo *) NULL; )
473 if ((p->stealth == MagickFalse) &&
474 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
475 preferences[i++]=ConstantString(p->name);
476 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
478 UnlockSemaphoreInfo(log_semaphore);
479 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
480 preferences[i]=(char *) NULL;
481 *number_preferences=(size_t) i;
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % G e t L o g N a m e %
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 % GetLogName() returns the current log name.
498 % The format of the GetLogName method is:
500 % const char *GetLogName(void)
503 MagickExport const char *GetLogName(void)
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 + I n i t i a l i z e L o g L i s t %
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % InitializeLogList() initialize the log list.
521 % The format of the InitializeLogList method is:
523 % MagickBooleanType InitializeLogList(ExceptionInfo *exception)
525 % A description of each parameter follows.
527 % o exception: return any errors or warnings in this structure.
530 static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
532 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
534 if (log_semaphore == (SemaphoreInfo *) NULL)
535 AcquireSemaphoreInfo(&log_semaphore);
536 LockSemaphoreInfo(log_semaphore);
537 if ((log_list == (LinkedListInfo *) NULL) &&
538 (instantiate_log == MagickFalse))
540 (void) LoadLogLists(LogFilename,exception);
541 instantiate_log=MagickTrue;
543 UnlockSemaphoreInfo(log_semaphore);
545 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % I s E v e n t L o g g i n g %
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
562 % The format of the IsEventLogging method is:
564 % MagickBooleanType IsEventLogging(void)
567 MagickExport MagickBooleanType IsEventLogging(void)
575 if ((log_list == (LinkedListInfo *) NULL) ||
576 (IsLinkedListEmpty(log_list) != MagickFalse))
578 exception=AcquireExceptionInfo();
579 log_info=GetLogInfo("*",exception);
580 exception=DestroyExceptionInfo(exception);
581 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588 % L i s t L o g I n f o %
592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
594 % ListLogInfo() lists the log info to a file.
596 % The format of the ListLogInfo method is:
598 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
600 % A description of each parameter follows.
602 % o file: An pointer to a FILE.
604 % o exception: return any errors or warnings in this structure.
607 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
609 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
612 limit[MaxTextExtent];
629 if (file == (const FILE *) NULL)
631 log_info=GetLogInfoList("*",&number_aliases,exception);
632 if (log_info == (const LogInfo **) NULL)
635 path=(const char *) NULL;
636 for (i=0; i < (ssize_t) number_aliases; i++)
638 if (log_info[i]->stealth != MagickFalse)
640 if ((path == (const char *) NULL) ||
641 (LocaleCompare(path,log_info[i]->path) != 0))
643 if (log_info[i]->path != (char *) NULL)
644 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
645 (void) FormatLocaleFile(file,
646 "Filename Generations Limit Format\n");
647 (void) FormatLocaleFile(file,
648 "-------------------------------------------------"
649 "------------------------------\n");
651 path=log_info[i]->path;
652 if (log_info[i]->filename != (char *) NULL)
654 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
655 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
656 (void) FormatLocaleFile(file," ");
658 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
659 (void) FormatMagickSize(MegabytesToBytes(log_info[i]->limit),MagickFalse,
661 (void) FormatLocaleFile(file,"%8sB ",limit);
662 if (log_info[i]->format != (char *) NULL)
663 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
664 (void) FormatLocaleFile(file,"\n");
667 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676 + L o g C o m p o n e n t G e n e s i s %
680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
682 % LogComponentGenesis() instantiates the log component.
684 % The format of the LogComponentGenesis method is:
686 % MagickBooleanType LogComponentGenesis(void)
689 MagickExport MagickBooleanType LogComponentGenesis(void)
691 AcquireSemaphoreInfo(&log_semaphore);
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700 + L o g C o m p o n e n t T e r m i n u s %
704 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
706 % LogComponentTerminus() destroys the logging component.
708 % The format of the LogComponentTerminus method is:
710 % LogComponentTerminus(void)
714 static void *DestroyLogElement(void *log_info)
719 p=(LogInfo *) log_info;
720 if (p->file != (FILE *) NULL)
722 if (p->append == MagickFalse)
723 (void) FormatLocaleFile(p->file,"</log>\n");
724 (void) fclose(p->file);
725 p->file=(FILE *) NULL;
727 if (p->exempt == MagickFalse)
729 if (p->format != (char *) NULL)
730 p->format=DestroyString(p->format);
731 if (p->path != (char *) NULL)
732 p->path=DestroyString(p->path);
733 if (p->filename != (char *) NULL)
734 p->filename=DestroyString(p->filename);
736 p=(LogInfo *) RelinquishMagickMemory(p);
737 return((void *) NULL);
740 MagickExport void LogComponentTerminus(void)
742 if (log_semaphore == (SemaphoreInfo *) NULL)
743 AcquireSemaphoreInfo(&log_semaphore);
744 LockSemaphoreInfo(log_semaphore);
745 if (log_list != (LinkedListInfo *) NULL)
746 log_list=DestroyLinkedList(log_list,DestroyLogElement);
747 instantiate_log=MagickFalse;
748 UnlockSemaphoreInfo(log_semaphore);
749 DestroySemaphoreInfo(&log_semaphore);
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 % L o g M a g i c k E v e n t %
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % LogMagickEvent() logs an event as determined by the log configuration file.
764 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
766 % The format of the LogMagickEvent method is:
768 % MagickBooleanType LogMagickEvent(const LogEventType type,
769 % const char *module,const char *function,const size_t line,
770 % const char *format,...)
772 % A description of each parameter follows:
774 % o type: the event type.
776 % o filename: the source module filename.
778 % o function: the function name.
780 % o line: the line number of the source module.
782 % o format: the output format.
785 static char *TranslateEvent(const LogEventType magick_unused(type),
786 const char *module,const char *function,const size_t line,
787 const char *domain,const char *event)
814 exception=AcquireExceptionInfo();
815 log_info=(LogInfo *) GetLogInfo("*",exception);
816 exception=DestroyExceptionInfo(exception);
817 seconds=time((time_t *) NULL);
818 elapsed_time=GetElapsedTime(&log_info->timer);
819 user_time=GetUserTime(&log_info->timer);
820 text=AcquireString(event);
821 if (log_info->format == (char *) NULL)
823 extent=strlen(event)+MaxTextExtent;
824 if (LocaleCompare(log_info->format,"xml") == 0)
827 timestamp[MaxTextExtent];
830 Translate event in "XML" format.
832 (void) FormatMagickTime(seconds,extent,timestamp);
833 (void) FormatLocaleString(text,extent,
835 " <timestamp>%s</timestamp>\n"
836 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
837 " <user-time>%0.3f</user-time>\n"
838 " <process-id>%.20g</process-id>\n"
839 " <thread-id>%.20g</thread-id>\n"
840 " <module>%s</module>\n"
841 " <function>%s</function>\n"
842 " <line>%.20g</line>\n"
843 " <domain>%s</domain>\n"
844 " <event>%s</event>\n"
845 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
846 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
847 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
848 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
849 (double) line,domain,event);
853 Translate event in "human readable" format.
856 for (p=log_info->format; *p != '\0'; p++)
859 if ((size_t) (q-text+MaxTextExtent) >= extent)
861 extent+=MaxTextExtent;
862 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
864 if (text == (char *) NULL)
865 return((char *) NULL);
869 The format of the log is defined by embedding special format characters:
888 if ((*p == '\\') && (*(p+1) == 'r'))
894 if ((*p == '\\') && (*(p+1) == 'n'))
910 q+=CopyMagickString(q,GetClientName(),extent);
915 q+=CopyMagickString(q,domain,extent);
920 q+=CopyMagickString(q,event,extent);
925 q+=CopyMagickString(q,function,extent);
930 if (log_info->generations == 0)
932 (void) CopyMagickString(q,"0",extent);
936 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
937 log_info->generations));
942 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
950 for (p=module+strlen(module)-1; p > module; p--)
951 if (*p == *DirectorySeparator)
956 q+=CopyMagickString(q,p,extent);
961 q+=CopyMagickString(q,GetLogName(),extent);
966 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
971 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
972 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
973 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
978 q+=FormatMagickTime(seconds,extent,q);
983 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
988 q+=CopyMagickString(q,MagickLibVersionText,extent);
1008 static char *TranslateFilename(const LogInfo *log_info)
1023 Translate event in "human readable" format.
1025 assert(log_info != (LogInfo *) NULL);
1026 assert(log_info->filename != (char *) NULL);
1027 filename=AcquireString((char *) NULL);
1028 extent=MaxTextExtent;
1030 for (p=log_info->filename; *p != '\0'; p++)
1033 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1035 extent+=MaxTextExtent;
1036 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1038 if (filename == (char *) NULL)
1039 return((char *) NULL);
1040 q=filename+strlen(filename);
1043 The format of the filename is defined by embedding special format
1062 q+=CopyMagickString(q,GetClientName(),extent);
1067 if (log_info->generations == 0)
1069 (void) CopyMagickString(q,"0",extent);
1073 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1074 log_info->generations));
1079 q+=CopyMagickString(q,GetLogName(),extent);
1084 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1089 q+=CopyMagickString(q,MagickLibVersionText,extent);
1109 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1110 const char *function,const size_t line,const char *format,
1114 event[MaxTextExtent],
1129 if (IsEventLogging() == MagickFalse)
1130 return(MagickFalse);
1131 exception=AcquireExceptionInfo();
1132 log_info=(LogInfo *) GetLogInfo("*",exception);
1133 exception=DestroyExceptionInfo(exception);
1134 LockSemaphoreInfo(log_semaphore);
1135 if ((log_info->event_mask & type) == 0)
1137 UnlockSemaphoreInfo(log_semaphore);
1140 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1141 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1142 n=vsnprintf(event,MaxTextExtent,format,operands);
1144 n=vsprintf(event,format,operands);
1147 event[MaxTextExtent-1]='\0';
1148 text=TranslateEvent(type,module,function,line,domain,event);
1149 if (text == (char *) NULL)
1151 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1152 UnlockSemaphoreInfo(log_semaphore);
1153 return(MagickFalse);
1155 if ((log_info->handler_mask & ConsoleHandler) != 0)
1157 (void) FormatLocaleFile(stderr,"%s\n",text);
1158 (void) fflush(stderr);
1160 if ((log_info->handler_mask & DebugHandler) != 0)
1162 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1163 OutputDebugString(text);
1166 if ((log_info->handler_mask & EventHandler) != 0)
1168 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1169 (void) NTReportEvent(text,MagickFalse);
1172 if ((log_info->handler_mask & FileHandler) != 0)
1177 file_info.st_size=0;
1178 if (log_info->file != (FILE *) NULL)
1179 (void) fstat(fileno(log_info->file),&file_info);
1180 if (file_info.st_size > (1024*1024*log_info->limit))
1182 (void) FormatLocaleFile(log_info->file,"</log>\n");
1183 (void) fclose(log_info->file);
1184 log_info->file=(FILE *) NULL;
1186 if (log_info->file == (FILE *) NULL)
1191 filename=TranslateFilename(log_info);
1192 if (filename == (char *) NULL)
1194 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1195 UnlockSemaphoreInfo(log_semaphore);
1196 return(MagickFalse);
1198 log_info->append=IsPathAccessible(filename);
1199 log_info->file=OpenMagickStream(filename,"ab");
1200 filename=(char *) RelinquishMagickMemory(filename);
1201 if (log_info->file == (FILE *) NULL)
1203 UnlockSemaphoreInfo(log_semaphore);
1204 return(MagickFalse);
1206 log_info->generation++;
1207 if (log_info->append == MagickFalse)
1209 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1210 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1211 (void) FormatLocaleFile(log_info->file,"<log>\n");
1214 (void) FormatLocaleFile(log_info->file,"%s\n",text);
1215 (void) fflush(log_info->file);
1217 if ((log_info->handler_mask & StdoutHandler) != 0)
1219 (void) FormatLocaleFile(stdout,"%s\n",text);
1220 (void) fflush(stdout);
1222 if ((log_info->handler_mask & StderrHandler) != 0)
1224 (void) FormatLocaleFile(stderr,"%s\n",text);
1225 (void) fflush(stderr);
1227 text=(char *) RelinquishMagickMemory(text);
1228 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1229 UnlockSemaphoreInfo(log_semaphore);
1233 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1234 const char *function,const size_t line,const char *format,...)
1242 va_start(operands,format);
1243 status=LogMagickEventList(type,module,function,line,format,operands);
1249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253 + L o a d L o g L i s t %
1257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1259 % LoadLogList() loads the log configuration file which provides a
1260 % mapping between log attributes and log name.
1262 % The format of the LoadLogList method is:
1264 % MagickBooleanType LoadLogList(const char *xml,const char *filename,
1265 % const size_t depth,ExceptionInfo *exception)
1267 % A description of each parameter follows:
1269 % o xml: The log list in XML format.
1271 % o filename: The log list filename.
1273 % o depth: depth of <include /> statements.
1275 % o exception: return any errors or warnings in this structure.
1278 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1279 const size_t depth,ExceptionInfo *exception)
1282 keyword[MaxTextExtent],
1289 *log_info = (LogInfo *) NULL;
1295 Load the log map file.
1297 if (xml == (const char *) NULL)
1298 return(MagickFalse);
1299 if (log_list == (LinkedListInfo *) NULL)
1301 log_list=NewLinkedList(0);
1302 if (log_list == (LinkedListInfo *) NULL)
1304 ThrowFileException(exception,ResourceLimitError,
1305 "MemoryAllocationFailed",filename);
1306 return(MagickFalse);
1310 token=AcquireString((const char *) xml);
1311 for (q=(const char *) xml; *q != '\0'; )
1316 GetMagickToken(q,&q,token);
1319 (void) CopyMagickString(keyword,token,MaxTextExtent);
1320 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1325 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1326 GetMagickToken(q,&q,token);
1329 if (LocaleNCompare(keyword,"<!--",4) == 0)
1334 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1335 GetMagickToken(q,&q,token);
1338 if (LocaleCompare(keyword,"<include") == 0)
1343 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1345 (void) CopyMagickString(keyword,token,MaxTextExtent);
1346 GetMagickToken(q,&q,token);
1349 GetMagickToken(q,&q,token);
1350 if (LocaleCompare(keyword,"file") == 0)
1353 (void) ThrowMagickException(exception,GetMagickModule(),
1354 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1358 path[MaxTextExtent],
1361 GetPathComponent(filename,HeadPath,path);
1363 (void) ConcatenateMagickString(path,DirectorySeparator,
1365 if (*token == *DirectorySeparator)
1366 (void) CopyMagickString(path,token,MaxTextExtent);
1368 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1369 xml=FileToString(path,~0,exception);
1370 if (xml != (char *) NULL)
1372 status|=LoadLogList(xml,path,depth+1,exception);
1373 xml=DestroyString(xml);
1380 if (LocaleCompare(keyword,"<logmap>") == 0)
1383 Allocate memory for the log list.
1385 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1386 if (log_info == (LogInfo *) NULL)
1387 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1388 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1389 log_info->path=ConstantString(filename);
1390 GetTimerInfo((TimerInfo *) &log_info->timer);
1391 log_info->exempt=MagickFalse;
1392 log_info->signature=MagickSignature;
1395 if (log_info == (LogInfo *) NULL)
1397 if (LocaleCompare(keyword,"</logmap>") == 0)
1399 status=AppendValueToLinkedList(log_list,log_info);
1400 if (status == MagickFalse)
1401 (void) ThrowMagickException(exception,GetMagickModule(),
1402 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1403 log_info=(LogInfo *) NULL;
1405 GetMagickToken(q,(const char **) NULL,token);
1408 GetMagickToken(q,&q,token);
1409 GetMagickToken(q,&q,token);
1415 if (LocaleCompare((char *) keyword,"events") == 0)
1417 log_info->event_mask=(LogEventType) (log_info->event_mask |
1418 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1426 if (LocaleCompare((char *) keyword,"filename") == 0)
1428 if (log_info->filename != (char *) NULL)
1429 log_info->filename=(char *)
1430 RelinquishMagickMemory(log_info->filename);
1431 log_info->filename=ConstantString(token);
1434 if (LocaleCompare((char *) keyword,"format") == 0)
1436 if (log_info->format != (char *) NULL)
1437 log_info->format=(char *)
1438 RelinquishMagickMemory(log_info->format);
1439 log_info->format=ConstantString(token);
1447 if (LocaleCompare((char *) keyword,"generations") == 0)
1449 if (LocaleCompare(token,"unlimited") == 0)
1451 log_info->generations=(~0UL);
1454 log_info->generations=StringToUnsignedLong(token);
1462 if (LocaleCompare((char *) keyword,"limit") == 0)
1464 if (LocaleCompare(token,"unlimited") == 0)
1466 log_info->limit=(~0UL);
1469 log_info->limit=StringToUnsignedLong(token);
1477 if (LocaleCompare((char *) keyword,"output") == 0)
1479 log_info->handler_mask=(LogHandlerType)
1480 (log_info->handler_mask | ParseLogHandlers(token));
1489 token=DestroyString(token);
1490 if (log_list == (LinkedListInfo *) NULL)
1491 return(MagickFalse);
1492 return(status != 0 ? MagickTrue : MagickFalse);
1496 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1500 % L o a d L o g L i s t s %
1504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1506 % LoadLogLists() loads one or more log configuration file which provides a
1507 % mapping between log attributes and log name.
1509 % The format of the LoadLogLists method is:
1511 % MagickBooleanType LoadLogLists(const char *filename,
1512 % ExceptionInfo *exception)
1514 % A description of each parameter follows:
1516 % o filename: the log configuration filename.
1518 % o exception: return any errors or warnings in this structure.
1521 static MagickBooleanType LoadLogLists(const char *filename,
1522 ExceptionInfo *exception)
1537 Load built-in log map.
1540 if (log_list == (LinkedListInfo *) NULL)
1542 log_list=NewLinkedList(0);
1543 if (log_list == (LinkedListInfo *) NULL)
1545 ThrowFileException(exception,ResourceLimitError,
1546 "MemoryAllocationFailed",filename);
1547 return(MagickFalse);
1550 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1555 register const LogMapInfo
1559 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1560 if (log_info == (LogInfo *) NULL)
1562 (void) ThrowMagickException(exception,GetMagickModule(),
1563 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1566 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1567 log_info->path=(char *) "[built-in]";
1568 GetTimerInfo((TimerInfo *) &log_info->timer);
1569 log_info->event_mask=p->event_mask;
1570 log_info->handler_mask=p->handler_mask;
1571 log_info->filename=ConstantString(p->filename);
1572 log_info->format=ConstantString(p->format);
1573 log_info->exempt=MagickTrue;
1574 log_info->signature=MagickSignature;
1575 status=AppendValueToLinkedList(log_list,log_info);
1576 if (status == MagickFalse)
1577 (void) ThrowMagickException(exception,GetMagickModule(),
1578 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1581 Load external log map.
1583 options=GetConfigureOptions(filename,exception);
1584 option=(const StringInfo *) GetNextValueInLinkedList(options);
1585 while (option != (const StringInfo *) NULL)
1587 status|=LoadLogList((const char *) GetStringInfoDatum(option),
1588 GetStringInfoPath(option),0,exception);
1589 option=(const StringInfo *) GetNextValueInLinkedList(options);
1591 options=DestroyConfigureOptions(options);
1592 return(status != 0 ? MagickTrue : MagickFalse);
1596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 + P a r s e L o g H a n d l e r s %
1604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1606 % ParseLogHandlers() parses a string defining which handlers takes a log
1607 % message and exports them.
1609 % The format of the ParseLogHandlers method is:
1611 % LogHandlerType ParseLogHandlers(const char *handlers)
1613 % A description of each parameter follows:
1615 % o handlers: one or more handlers separated by commas.
1618 static LogHandlerType ParseLogHandlers(const char *handlers)
1632 handler_mask=NoHandler;
1633 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1635 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1638 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1640 length=strlen(LogHandlers[i].name);
1641 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1643 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1647 if (LogHandlers[i].name == (char *) NULL)
1648 return(UndefinedHandler);
1650 return(handler_mask);
1654 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1658 % S e t L o g E v e n t M a s k %
1662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1664 % SetLogEventMask() accepts a list that determines which events to log. All
1665 % other events are ignored. By default, no debug is enabled. This method
1666 % returns the previous log event mask.
1668 % The format of the SetLogEventMask method is:
1670 % LogEventType SetLogEventMask(const char *events)
1672 % A description of each parameter follows:
1674 % o events: log these events.
1677 MagickExport LogEventType SetLogEventMask(const char *events)
1688 exception=AcquireExceptionInfo();
1689 log_info=(LogInfo *) GetLogInfo("*",exception);
1690 exception=DestroyExceptionInfo(exception);
1691 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1692 LockSemaphoreInfo(log_semaphore);
1693 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1694 log_info->event_mask=(LogEventType) option;
1696 log_info->event_mask=UndefinedEvents;
1697 UnlockSemaphoreInfo(log_semaphore);
1698 return(log_info->event_mask);
1702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1706 % S e t L o g F o r m a t %
1710 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1712 % SetLogFormat() sets the format for the "human readable" log record.
1714 % The format of the LogMagickFormat method is:
1716 % SetLogFormat(const char *format)
1718 % A description of each parameter follows:
1720 % o format: the log record format.
1723 MagickExport void SetLogFormat(const char *format)
1731 exception=AcquireExceptionInfo();
1732 log_info=(LogInfo *) GetLogInfo("*",exception);
1733 exception=DestroyExceptionInfo(exception);
1734 LockSemaphoreInfo(log_semaphore);
1735 if (log_info->format != (char *) NULL)
1736 log_info->format=DestroyString(log_info->format);
1737 log_info->format=ConstantString(format);
1738 UnlockSemaphoreInfo(log_semaphore);
1742 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1746 % S e t L o g N a m e %
1750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1752 % SetLogName() sets the log name and returns it.
1754 % The format of the SetLogName method is:
1756 % const char *SetLogName(const char *name)
1758 % A description of each parameter follows:
1760 % o log_name: SetLogName() returns the current client name.
1762 % o name: Specifies the new client name.
1765 MagickExport const char *SetLogName(const char *name)
1767 if ((name != (char *) NULL) && (*name != '\0'))
1768 (void) CopyMagickString(log_name,name,MaxTextExtent);