2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Log Events %
20 % Copyright 1999-2010 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) fprintf(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) fprintf(file,"\nPath: %s\n\n",log_info[i]->path);
645 (void) fprintf(file,"Filename Generations Limit Format\n");
646 (void) fprintf(file,"-------------------------------------------------"
647 "------------------------------\n");
649 path=log_info[i]->path;
650 if (log_info[i]->filename != (char *) NULL)
652 (void) fprintf(file,"%s",log_info[i]->filename);
653 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
654 (void) fprintf(file," ");
656 (void) fprintf(file,"%9g ",(double) log_info[i]->generations);
657 (void) FormatMagickSize(MegabytesToBytes(log_info[i]->limit),MagickFalse,
659 (void) fprintf(file,"%8sB ",limit);
660 if (log_info[i]->format != (char *) NULL)
661 (void) fprintf(file,"%s",log_info[i]->format);
662 (void) fprintf(file,"\n");
665 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 + L o g C o m p o n e n t G e n e s i s %
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % LogComponentGenesis() instantiates the log component.
682 % The format of the LogComponentGenesis method is:
684 % MagickBooleanType LogComponentGenesis(void)
687 MagickExport MagickBooleanType LogComponentGenesis(void)
689 AcquireSemaphoreInfo(&log_semaphore);
694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 + L o g C o m p o n e n t T e r m i n u s %
702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704 % LogComponentTerminus() destroys the logging component.
706 % The format of the LogComponentTerminus method is:
708 % LogComponentTerminus(void)
712 static void *DestroyLogElement(void *log_info)
717 p=(LogInfo *) log_info;
718 if (p->file != (FILE *) NULL)
720 if (p->append == MagickFalse)
721 (void) fprintf(p->file,"</log>\n");
722 (void) fclose(p->file);
723 p->file=(FILE *) NULL;
725 if (p->exempt == MagickFalse)
727 if (p->format != (char *) NULL)
728 p->format=DestroyString(p->format);
729 if (p->path != (char *) NULL)
730 p->path=DestroyString(p->path);
731 if (p->filename != (char *) NULL)
732 p->filename=DestroyString(p->filename);
734 p=(LogInfo *) RelinquishMagickMemory(p);
735 return((void *) NULL);
738 MagickExport void LogComponentTerminus(void)
740 if (log_semaphore == (SemaphoreInfo *) NULL)
741 AcquireSemaphoreInfo(&log_semaphore);
742 LockSemaphoreInfo(log_semaphore);
743 if (log_list != (LinkedListInfo *) NULL)
744 log_list=DestroyLinkedList(log_list,DestroyLogElement);
745 instantiate_log=MagickFalse;
746 UnlockSemaphoreInfo(log_semaphore);
747 DestroySemaphoreInfo(&log_semaphore);
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 % L o g M a g i c k E v e n t %
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 % LogMagickEvent() logs an event as determined by the log configuration file.
762 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
764 % The format of the LogMagickEvent method is:
766 % MagickBooleanType LogMagickEvent(const LogEventType type,
767 % const char *module,const char *function,const size_t line,
768 % const char *format,...)
770 % A description of each parameter follows:
772 % o type: the event type.
774 % o filename: the source module filename.
776 % o function: the function name.
778 % o line: the line number of the source module.
780 % o format: the output format.
783 static char *TranslateEvent(const LogEventType magick_unused(type),
784 const char *module,const char *function,const size_t line,
785 const char *domain,const char *event)
812 exception=AcquireExceptionInfo();
813 log_info=(LogInfo *) GetLogInfo("*",exception);
814 exception=DestroyExceptionInfo(exception);
815 seconds=time((time_t *) NULL);
816 elapsed_time=GetElapsedTime(&log_info->timer);
817 user_time=GetUserTime(&log_info->timer);
818 text=AcquireString(event);
819 if (log_info->format == (char *) NULL)
821 extent=strlen(event)+MaxTextExtent;
822 if (LocaleCompare(log_info->format,"xml") == 0)
825 timestamp[MaxTextExtent];
828 Translate event in "XML" format.
830 (void) FormatMagickTime(seconds,extent,timestamp);
831 (void) FormatMagickString(text,extent,
833 " <timestamp>%s</timestamp>\n"
834 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
835 " <user-time>%0.3f</user-time>\n"
836 " <process-id>%.20g</process-id>\n"
837 " <thread-id>%.20g</thread-id>\n"
838 " <module>%s</module>\n"
839 " <function>%s</function>\n"
840 " <line>%.20g</line>\n"
841 " <domain>%s</domain>\n"
842 " <event>%s</event>\n"
843 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
844 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
845 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
846 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
847 (double) line,domain,event);
851 Translate event in "human readable" format.
854 for (p=log_info->format; *p != '\0'; p++)
857 if ((size_t) (q-text+MaxTextExtent) >= extent)
859 extent+=MaxTextExtent;
860 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
862 if (text == (char *) NULL)
863 return((char *) NULL);
867 The format of the log is defined by embedding special format characters:
886 if ((*p == '\\') && (*(p+1) == 'r'))
892 if ((*p == '\\') && (*(p+1) == 'n'))
908 q+=CopyMagickString(q,GetClientName(),extent);
913 q+=CopyMagickString(q,domain,extent);
918 q+=CopyMagickString(q,event,extent);
923 q+=CopyMagickString(q,function,extent);
928 if (log_info->generations == 0)
930 (void) CopyMagickString(q,"0",extent);
934 q+=FormatMagickString(q,extent,"%.20g",(double) (log_info->generation %
935 log_info->generations));
940 q+=FormatMagickString(q,extent,"%.20g",(double) line);
948 for (p=module+strlen(module)-1; p > module; p--)
949 if (*p == *DirectorySeparator)
954 q+=CopyMagickString(q,p,extent);
959 q+=CopyMagickString(q,GetLogName(),extent);
964 q+=FormatMagickString(q,extent,"%.20g",(double) getpid());
969 q+=FormatMagickString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
970 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
971 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
976 q+=FormatMagickTime(seconds,extent,q);
981 q+=FormatMagickString(q,extent,"%0.3fu",user_time);
986 q+=CopyMagickString(q,MagickLibVersionText,extent);
1006 static char *TranslateFilename(const LogInfo *log_info)
1021 Translate event in "human readable" format.
1023 assert(log_info != (LogInfo *) NULL);
1024 assert(log_info->filename != (char *) NULL);
1025 filename=AcquireString((char *) NULL);
1026 extent=MaxTextExtent;
1028 for (p=log_info->filename; *p != '\0'; p++)
1031 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1033 extent+=MaxTextExtent;
1034 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1036 if (filename == (char *) NULL)
1037 return((char *) NULL);
1038 q=filename+strlen(filename);
1041 The format of the filename is defined by embedding special format
1060 q+=CopyMagickString(q,GetClientName(),extent);
1065 if (log_info->generations == 0)
1067 (void) CopyMagickString(q,"0",extent);
1071 q+=FormatMagickString(q,extent,"%.20g",(double) (log_info->generation %
1072 log_info->generations));
1077 q+=CopyMagickString(q,GetLogName(),extent);
1082 q+=FormatMagickString(q,extent,"%.20g",(double) getpid());
1087 q+=CopyMagickString(q,MagickLibVersionText,extent);
1107 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1108 const char *function,const size_t line,const char *format,
1112 event[MaxTextExtent],
1127 if (IsEventLogging() == MagickFalse)
1128 return(MagickFalse);
1129 exception=AcquireExceptionInfo();
1130 log_info=(LogInfo *) GetLogInfo("*",exception);
1131 exception=DestroyExceptionInfo(exception);
1132 LockSemaphoreInfo(log_semaphore);
1133 if ((log_info->event_mask & type) == 0)
1135 UnlockSemaphoreInfo(log_semaphore);
1138 domain=MagickOptionToMnemonic(MagickLogEventOptions,type);
1139 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1140 n=vsnprintf(event,MaxTextExtent,format,operands);
1142 n=vsprintf(event,format,operands);
1145 event[MaxTextExtent-1]='\0';
1146 text=TranslateEvent(type,module,function,line,domain,event);
1147 if (text == (char *) NULL)
1149 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1150 UnlockSemaphoreInfo(log_semaphore);
1151 return(MagickFalse);
1153 if ((log_info->handler_mask & ConsoleHandler) != 0)
1155 (void) fprintf(stderr,"%s\n",text);
1156 (void) fflush(stderr);
1158 if ((log_info->handler_mask & DebugHandler) != 0)
1160 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1161 OutputDebugString(text);
1164 if ((log_info->handler_mask & EventHandler) != 0)
1166 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1167 (void) NTReportEvent(text,MagickFalse);
1170 if ((log_info->handler_mask & FileHandler) != 0)
1175 file_info.st_size=0;
1176 if (log_info->file != (FILE *) NULL)
1177 (void) fstat(fileno(log_info->file),&file_info);
1178 if (file_info.st_size > (off_t) (1024*1024*log_info->limit))
1180 (void) fprintf(log_info->file,"</log>\n");
1181 (void) fclose(log_info->file);
1182 log_info->file=(FILE *) NULL;
1184 if (log_info->file == (FILE *) NULL)
1189 filename=TranslateFilename(log_info);
1190 if (filename == (char *) NULL)
1192 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1193 UnlockSemaphoreInfo(log_semaphore);
1194 return(MagickFalse);
1196 log_info->append=IsPathAccessible(filename);
1197 log_info->file=OpenMagickStream(filename,"ab");
1198 filename=(char *) RelinquishMagickMemory(filename);
1199 if (log_info->file == (FILE *) NULL)
1201 UnlockSemaphoreInfo(log_semaphore);
1202 return(MagickFalse);
1204 log_info->generation++;
1205 if (log_info->append == MagickFalse)
1207 (void) fprintf(log_info->file,"<?xml version=\"1.0\" "
1208 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1209 (void) fprintf(log_info->file,"<log>\n");
1212 (void) fprintf(log_info->file,"%s\n",text);
1213 (void) fflush(log_info->file);
1215 if ((log_info->handler_mask & StdoutHandler) != 0)
1217 (void) fprintf(stdout,"%s\n",text);
1218 (void) fflush(stdout);
1220 if ((log_info->handler_mask & StderrHandler) != 0)
1222 (void) fprintf(stderr,"%s\n",text);
1223 (void) fflush(stderr);
1225 text=(char *) RelinquishMagickMemory(text);
1226 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1227 UnlockSemaphoreInfo(log_semaphore);
1231 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1232 const char *function,const size_t line,const char *format,...)
1240 va_start(operands,format);
1241 status=LogMagickEventList(type,module,function,line,format,operands);
1247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1251 + L o a d L o g L i s t %
1255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1257 % LoadLogList() loads the log configuration file which provides a
1258 % mapping between log attributes and log name.
1260 % The format of the LoadLogList method is:
1262 % MagickBooleanType LoadLogList(const char *xml,const char *filename,
1263 % const size_t depth,ExceptionInfo *exception)
1265 % A description of each parameter follows:
1267 % o xml: The log list in XML format.
1269 % o filename: The log list filename.
1271 % o depth: depth of <include /> statements.
1273 % o exception: return any errors or warnings in this structure.
1276 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1277 const size_t depth,ExceptionInfo *exception)
1280 keyword[MaxTextExtent],
1287 *log_info = (LogInfo *) NULL;
1293 Load the log map file.
1295 if (xml == (const char *) NULL)
1296 return(MagickFalse);
1297 if (log_list == (LinkedListInfo *) NULL)
1299 log_list=NewLinkedList(0);
1300 if (log_list == (LinkedListInfo *) NULL)
1302 ThrowFileException(exception,ResourceLimitError,
1303 "MemoryAllocationFailed",filename);
1304 return(MagickFalse);
1308 token=AcquireString((const char *) xml);
1309 for (q=(const char *) xml; *q != '\0'; )
1314 GetMagickToken(q,&q,token);
1317 (void) CopyMagickString(keyword,token,MaxTextExtent);
1318 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1323 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1324 GetMagickToken(q,&q,token);
1327 if (LocaleNCompare(keyword,"<!--",4) == 0)
1332 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1333 GetMagickToken(q,&q,token);
1336 if (LocaleCompare(keyword,"<include") == 0)
1341 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1343 (void) CopyMagickString(keyword,token,MaxTextExtent);
1344 GetMagickToken(q,&q,token);
1347 GetMagickToken(q,&q,token);
1348 if (LocaleCompare(keyword,"file") == 0)
1351 (void) ThrowMagickException(exception,GetMagickModule(),
1352 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1356 path[MaxTextExtent],
1359 GetPathComponent(filename,HeadPath,path);
1361 (void) ConcatenateMagickString(path,DirectorySeparator,
1363 if (*token == *DirectorySeparator)
1364 (void) CopyMagickString(path,token,MaxTextExtent);
1366 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1367 xml=FileToString(path,~0,exception);
1368 if (xml != (char *) NULL)
1370 status|=LoadLogList(xml,path,depth+1,exception);
1371 xml=DestroyString(xml);
1378 if (LocaleCompare(keyword,"<logmap>") == 0)
1381 Allocate memory for the log list.
1383 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1384 if (log_info == (LogInfo *) NULL)
1385 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1386 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1387 log_info->path=ConstantString(filename);
1388 GetTimerInfo((TimerInfo *) &log_info->timer);
1389 log_info->exempt=MagickFalse;
1390 log_info->signature=MagickSignature;
1393 if (log_info == (LogInfo *) NULL)
1395 if (LocaleCompare(keyword,"</logmap>") == 0)
1397 status=AppendValueToLinkedList(log_list,log_info);
1398 if (status == MagickFalse)
1399 (void) ThrowMagickException(exception,GetMagickModule(),
1400 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1401 log_info=(LogInfo *) NULL;
1403 GetMagickToken(q,(const char **) NULL,token);
1406 GetMagickToken(q,&q,token);
1407 GetMagickToken(q,&q,token);
1413 if (LocaleCompare((char *) keyword,"events") == 0)
1415 log_info->event_mask=(LogEventType) (log_info->event_mask |
1416 ParseMagickOption(MagickLogEventOptions,MagickTrue,token));
1424 if (LocaleCompare((char *) keyword,"filename") == 0)
1426 if (log_info->filename != (char *) NULL)
1427 log_info->filename=(char *)
1428 RelinquishMagickMemory(log_info->filename);
1429 log_info->filename=ConstantString(token);
1432 if (LocaleCompare((char *) keyword,"format") == 0)
1434 if (log_info->format != (char *) NULL)
1435 log_info->format=(char *)
1436 RelinquishMagickMemory(log_info->format);
1437 log_info->format=ConstantString(token);
1445 if (LocaleCompare((char *) keyword,"generations") == 0)
1447 if (LocaleCompare(token,"unlimited") == 0)
1449 log_info->generations=(~0UL);
1452 log_info->generations=StringToUnsignedLong(token);
1460 if (LocaleCompare((char *) keyword,"limit") == 0)
1462 if (LocaleCompare(token,"unlimited") == 0)
1464 log_info->limit=(~0UL);
1467 log_info->limit=StringToUnsignedLong(token);
1475 if (LocaleCompare((char *) keyword,"output") == 0)
1477 log_info->handler_mask=(LogHandlerType)
1478 (log_info->handler_mask | ParseLogHandlers(token));
1487 token=DestroyString(token);
1488 if (log_list == (LinkedListInfo *) NULL)
1489 return(MagickFalse);
1490 return(status != 0 ? MagickTrue : MagickFalse);
1494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498 % L o a d L o g L i s t s %
1502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1504 % LoadLogLists() loads one or more log configuration file which provides a
1505 % mapping between log attributes and log name.
1507 % The format of the LoadLogLists method is:
1509 % MagickBooleanType LoadLogLists(const char *filename,
1510 % ExceptionInfo *exception)
1512 % A description of each parameter follows:
1514 % o filename: the log configuration filename.
1516 % o exception: return any errors or warnings in this structure.
1519 static MagickBooleanType LoadLogLists(const char *filename,
1520 ExceptionInfo *exception)
1535 Load built-in log map.
1538 if (log_list == (LinkedListInfo *) NULL)
1540 log_list=NewLinkedList(0);
1541 if (log_list == (LinkedListInfo *) NULL)
1543 ThrowFileException(exception,ResourceLimitError,
1544 "MemoryAllocationFailed",filename);
1545 return(MagickFalse);
1548 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1553 register const LogMapInfo
1557 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1558 if (log_info == (LogInfo *) NULL)
1560 (void) ThrowMagickException(exception,GetMagickModule(),
1561 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1564 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1565 log_info->path=(char *) "[built-in]";
1566 GetTimerInfo((TimerInfo *) &log_info->timer);
1567 log_info->event_mask=p->event_mask;
1568 log_info->handler_mask=p->handler_mask;
1569 log_info->filename=ConstantString(p->filename);
1570 log_info->format=ConstantString(p->format);
1571 log_info->exempt=MagickTrue;
1572 log_info->signature=MagickSignature;
1573 status=AppendValueToLinkedList(log_list,log_info);
1574 if (status == MagickFalse)
1575 (void) ThrowMagickException(exception,GetMagickModule(),
1576 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1579 Load external log map.
1581 options=GetConfigureOptions(filename,exception);
1582 option=(const StringInfo *) GetNextValueInLinkedList(options);
1583 while (option != (const StringInfo *) NULL)
1585 status|=LoadLogList((const char *) GetStringInfoDatum(option),
1586 GetStringInfoPath(option),0,exception);
1587 option=(const StringInfo *) GetNextValueInLinkedList(options);
1589 options=DestroyConfigureOptions(options);
1590 return(status != 0 ? MagickTrue : MagickFalse);
1594 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1598 + P a r s e L o g H a n d l e r s %
1602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1604 % ParseLogHandlers() parses a string defining which handlers takes a log
1605 % message and exports them.
1607 % The format of the ParseLogHandlers method is:
1609 % LogHandlerType ParseLogHandlers(const char *handlers)
1611 % A description of each parameter follows:
1613 % o handlers: one or more handlers separated by commas.
1616 static LogHandlerType ParseLogHandlers(const char *handlers)
1630 handler_mask=NoHandler;
1631 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1633 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1636 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1638 length=strlen(LogHandlers[i].name);
1639 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1641 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1645 if (LogHandlers[i].name == (char *) NULL)
1646 return(UndefinedHandler);
1648 return(handler_mask);
1652 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1656 % S e t L o g E v e n t M a s k %
1660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1662 % SetLogEventMask() accepts a list that determines which events to log. All
1663 % other events are ignored. By default, no debug is enabled. This method
1664 % returns the previous log event mask.
1666 % The format of the SetLogEventMask method is:
1668 % LogEventType SetLogEventMask(const char *events)
1670 % A description of each parameter follows:
1672 % o events: log these events.
1675 MagickExport LogEventType SetLogEventMask(const char *events)
1686 exception=AcquireExceptionInfo();
1687 log_info=(LogInfo *) GetLogInfo("*",exception);
1688 exception=DestroyExceptionInfo(exception);
1689 option=ParseMagickOption(MagickLogEventOptions,MagickTrue,events);
1690 LockSemaphoreInfo(log_semaphore);
1691 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1692 log_info->event_mask=(LogEventType) option;
1694 log_info->event_mask=UndefinedEvents;
1695 UnlockSemaphoreInfo(log_semaphore);
1696 return(log_info->event_mask);
1700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1704 % S e t L o g F o r m a t %
1708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 % SetLogFormat() sets the format for the "human readable" log record.
1712 % The format of the LogMagickFormat method is:
1714 % SetLogFormat(const char *format)
1716 % A description of each parameter follows:
1718 % o format: the log record format.
1721 MagickExport void SetLogFormat(const char *format)
1729 exception=AcquireExceptionInfo();
1730 log_info=(LogInfo *) GetLogInfo("*",exception);
1731 exception=DestroyExceptionInfo(exception);
1732 LockSemaphoreInfo(log_semaphore);
1733 if (log_info->format != (char *) NULL)
1734 log_info->format=DestroyString(log_info->format);
1735 log_info->format=ConstantString(format);
1736 UnlockSemaphoreInfo(log_semaphore);
1740 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1744 % S e t L o g N a m e %
1748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1750 % SetLogName() sets the log name and returns it.
1752 % The format of the SetLogName method is:
1754 % const char *SetLogName(const char *name)
1756 % A description of each parameter follows:
1758 % o log_name: SetLogName() returns the current client name.
1760 % o name: Specifies the new client name.
1763 MagickExport const char *SetLogName(const char *name)
1765 if ((name != (char *) NULL) && (*name != '\0'))
1766 (void) CopyMagickString(log_name,name,MaxTextExtent);