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/token.h"
56 #include "magick/thread_.h"
57 #include "magick/thread-private.h"
58 #include "magick/utility.h"
59 #include "magick/version.h"
60 #include "magick/xml-tree.h"
65 #define LogFilename "log.xml"
72 UndefinedHandler = 0x0000,
74 ConsoleHandler = 0x0001,
75 StdoutHandler = 0x0002,
76 StderrHandler = 0x0004,
78 DebugHandler = 0x0010,
82 typedef struct _EventInfo
91 typedef struct _HandlerInfo
136 typedef struct _LogMapInfo
152 static const HandlerInfo
155 { "console", ConsoleHandler },
156 { "debug", DebugHandler },
157 { "event", EventHandler },
158 { "file", FileHandler },
159 { "none", NoHandler },
160 { "stderr", StderrHandler },
161 { "stdout", StdoutHandler },
162 { (char *) NULL, UndefinedHandler }
165 static const LogMapInfo
168 { NoEvents, ConsoleHandler, "Magick-%d.log",
169 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\n %e" }
173 log_name[MaxTextExtent] = "Magick";
175 static LinkedListInfo
176 *log_list = (LinkedListInfo *) NULL;
179 *log_semaphore = (SemaphoreInfo *) NULL;
181 static volatile MagickBooleanType
182 instantiate_log = MagickFalse;
185 Forward declarations.
187 static LogHandlerType
188 ParseLogHandlers(const char *);
191 *GetLogInfo(const char *,ExceptionInfo *);
193 static MagickBooleanType
194 InitializeLogList(ExceptionInfo *),
195 LoadLogLists(const char *,ExceptionInfo *);
198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 % C l o s e M a g i c k L o g %
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 % CloseMagickLog() closes the Magick log.
210 % The format of the CloseMagickLog method is:
212 % CloseMagickLog(void)
215 MagickExport void CloseMagickLog(void)
223 if (IsEventLogging() == MagickFalse)
225 exception=AcquireExceptionInfo();
226 log_info=GetLogInfo("*",exception);
227 exception=DestroyExceptionInfo(exception);
228 (void) LockSemaphoreInfo(log_semaphore);
229 if (log_info->file != (FILE *) NULL)
231 if (log_info->append == MagickFalse)
232 (void) fprintf(log_info->file,"</log>\n");
233 (void) fclose(log_info->file);
234 log_info->file=(FILE *) NULL;
236 (void) UnlockSemaphoreInfo(log_semaphore);
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 + G e t L o g I n f o %
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % GetLogInfo() searches the log list for the specified name and if found
251 % returns attributes for that log.
253 % The format of the GetLogInfo method is:
255 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
257 % A description of each parameter follows:
259 % o name: the log name.
261 % o exception: return any errors or warnings in this structure.
264 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
269 assert(exception != (ExceptionInfo *) NULL);
270 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
271 if (InitializeLogList(exception) == MagickFalse)
272 return((LogInfo *) NULL);
273 if ((log_list == (LinkedListInfo *) NULL) ||
274 (IsLinkedListEmpty(log_list) != MagickFalse))
275 return((LogInfo *) NULL);
276 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
277 return((LogInfo *) GetValueFromLinkedList(log_list,0));
281 (void) LockSemaphoreInfo(log_semaphore);
282 ResetLinkedListIterator(log_list);
283 p=(LogInfo *) GetNextValueInLinkedList(log_list);
284 while (p != (LogInfo *) NULL)
286 if (LocaleCompare(name,p->name) == 0)
288 p=(LogInfo *) GetNextValueInLinkedList(log_list);
290 if (p != (LogInfo *) NULL)
291 (void) InsertValueInLinkedList(log_list,0,
292 RemoveElementByValueFromLinkedList(log_list,p));
293 (void) UnlockSemaphoreInfo(log_semaphore);
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % G e t L o g I n f o L i s t %
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 % GetLogInfoList() returns any logs that match the specified pattern.
310 % The format of the GetLogInfoList function is:
312 % const LogInfo **GetLogInfoList(const char *pattern,
313 % unsigned long *number_preferences,ExceptionInfo *exception)
315 % A description of each parameter follows:
317 % o pattern: Specifies a pointer to a text string containing a pattern.
319 % o number_preferences: This integer returns the number of logs in the list.
321 % o exception: return any errors or warnings in this structure.
324 #if defined(__cplusplus) || defined(c_plusplus)
328 static int LogInfoCompare(const void *x,const void *y)
334 p=(const LogInfo **) x,
335 q=(const LogInfo **) y;
336 if (LocaleCompare((*p)->path,(*q)->path) == 0)
337 return(LocaleCompare((*p)->name,(*q)->name));
338 return(LocaleCompare((*p)->path,(*q)->path));
341 #if defined(__cplusplus) || defined(c_plusplus)
345 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
346 unsigned long *number_preferences,ExceptionInfo *exception)
351 register const LogInfo
360 assert(pattern != (char *) NULL);
361 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
362 assert(number_preferences != (unsigned long *) NULL);
363 *number_preferences=0;
364 p=GetLogInfo("*",exception);
365 if (p == (const LogInfo *) NULL)
366 return((const LogInfo **) NULL);
367 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
368 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
369 if (preferences == (const LogInfo **) NULL)
370 return((const LogInfo **) NULL);
374 (void) LockSemaphoreInfo(log_semaphore);
375 ResetLinkedListIterator(log_list);
376 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
377 for (i=0; p != (const LogInfo *) NULL; )
379 if ((p->stealth == MagickFalse) &&
380 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
382 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
384 (void) UnlockSemaphoreInfo(log_semaphore);
385 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
386 preferences[i]=(LogInfo *) NULL;
387 *number_preferences=(unsigned long) i;
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 % G e t L o g L i s t %
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 % GetLogList() returns any logs that match the specified pattern.
404 % The format of the GetLogList function is:
406 % char **GetLogList(const char *pattern,unsigned long *number_preferences,
407 % ExceptionInfo *exception)
409 % A description of each parameter follows:
411 % o pattern: Specifies a pointer to a text string containing a pattern.
413 % o number_preferences: This integer returns the number of logs in the list.
415 % o exception: return any errors or warnings in this structure.
419 #if defined(__cplusplus) || defined(c_plusplus)
423 static int LogCompare(const void *x,const void *y)
431 return(LocaleCompare(*p,*q));
434 #if defined(__cplusplus) || defined(c_plusplus)
438 MagickExport char **GetLogList(const char *pattern,
439 unsigned long *number_preferences,ExceptionInfo *exception)
444 register const LogInfo
453 assert(pattern != (char *) NULL);
454 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
455 assert(number_preferences != (unsigned long *) NULL);
456 *number_preferences=0;
457 p=GetLogInfo("*",exception);
458 if (p == (const LogInfo *) NULL)
459 return((char **) NULL);
460 preferences=(char **) AcquireQuantumMemory((size_t)
461 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
462 if (preferences == (char **) NULL)
463 return((char **) NULL);
467 (void) LockSemaphoreInfo(log_semaphore);
468 ResetLinkedListIterator(log_list);
469 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
470 for (i=0; p != (const LogInfo *) NULL; )
472 if ((p->stealth == MagickFalse) &&
473 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
474 preferences[i++]=ConstantString(p->name);
475 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
477 (void) UnlockSemaphoreInfo(log_semaphore);
478 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
479 preferences[i]=(char *) NULL;
480 *number_preferences=(unsigned long) i;
485 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
489 % G e t L o g N a m e %
493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495 % GetLogName() returns the current log name.
497 % The format of the GetLogName method is:
499 % const char *GetLogName(void)
502 MagickExport const char *GetLogName(void)
508 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512 + I n i t i a l i z e L o g L i s t %
516 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518 % InitializeLogList() initialize the log list.
520 % The format of the InitializeLogList method is:
522 % MagickBooleanType InitializeLogList(ExceptionInfo *exception)
524 % A description of each parameter follows.
526 % o exception: return any errors or warnings in this structure.
529 static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
531 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
533 if (log_semaphore == (SemaphoreInfo *) NULL)
534 AcquireSemaphoreInfo(&log_semaphore);
535 (void) LockSemaphoreInfo(log_semaphore);
536 if ((log_list == (LinkedListInfo *) NULL) &&
537 (instantiate_log == MagickFalse))
539 (void) LoadLogLists(LogFilename,exception);
540 instantiate_log=MagickTrue;
542 (void) UnlockSemaphoreInfo(log_semaphore);
544 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552 % I s E v e n t L o g g i n g %
556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
561 % The format of the IsEventLogging method is:
563 % MagickBooleanType IsEventLogging(void)
566 MagickExport MagickBooleanType IsEventLogging(void)
574 if ((log_list == (LinkedListInfo *) NULL) ||
575 (IsLinkedListEmpty(log_list) != MagickFalse))
577 exception=AcquireExceptionInfo();
578 log_info=GetLogInfo("*",exception);
579 exception=DestroyExceptionInfo(exception);
580 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587 % L i s t L o g I n f o %
591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
593 % ListLogInfo() lists the log info to a file.
595 % The format of the ListLogInfo method is:
597 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
599 % A description of each parameter follows.
601 % o file: An pointer to a FILE.
603 % o exception: return any errors or warnings in this structure.
606 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
608 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
611 limit[MaxTextExtent];
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 < (long) number_aliases; i++)
637 if (log_info[i]->stealth != MagickFalse)
639 if ((path == (const char *) NULL) ||
640 (LocaleCompare(path,log_info[i]->path) != 0))
642 if (log_info[i]->path != (char *) NULL)
643 (void) fprintf(file,"\nPath: %s\n\n",log_info[i]->path);
644 (void) fprintf(file,"Filename Generations Limit Format\n");
645 (void) fprintf(file,"-------------------------------------------------"
646 "------------------------------\n");
648 path=log_info[i]->path;
649 if (log_info[i]->filename != (char *) NULL)
651 (void) fprintf(file,"%s",log_info[i]->filename);
652 for (j=(long) strlen(log_info[i]->filename); j <= 16; j++)
653 (void) fprintf(file," ");
655 (void) fprintf(file,"%9lu ",log_info[i]->generations);
656 (void) FormatMagickSize(MegabytesToBytes(log_info[i]->limit),MagickFalse,
658 (void) fprintf(file,"%8s ",limit);
659 if (log_info[i]->format != (char *) NULL)
660 (void) fprintf(file,"%s",log_info[i]->format);
661 (void) fprintf(file,"\n");
664 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673 + L o g C o m p o n e n t G e n e s i s %
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 % LogComponentGenesis() instantiates the log component.
681 % The format of the LogComponentGenesis method is:
683 % MagickBooleanType LogComponentGenesis(void)
686 MagickExport MagickBooleanType LogComponentGenesis(void)
688 AcquireSemaphoreInfo(&log_semaphore);
693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
697 + L o g C o m p o n e n t T e r m i n u s %
701 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703 % LogComponentTerminus() destroys the logging component.
705 % The format of the LogComponentTerminus method is:
707 % LogComponentTerminus(void)
711 static void *DestroyLogElement(void *log_info)
716 p=(LogInfo *) log_info;
717 if (p->file != (FILE *) NULL)
719 if (p->append == MagickFalse)
720 (void) fprintf(p->file,"</log>\n");
721 (void) fclose(p->file);
722 p->file=(FILE *) NULL;
724 if (p->exempt == MagickFalse)
726 if (p->format != (char *) NULL)
727 p->format=DestroyString(p->format);
728 if (p->path != (char *) NULL)
729 p->path=DestroyString(p->path);
730 if (p->filename != (char *) NULL)
731 p->filename=DestroyString(p->filename);
733 p=(LogInfo *) RelinquishMagickMemory(p);
734 return((void *) NULL);
737 MagickExport void LogComponentTerminus(void)
739 if (log_semaphore == (SemaphoreInfo *) NULL)
740 AcquireSemaphoreInfo(&log_semaphore);
741 (void) LockSemaphoreInfo(log_semaphore);
742 if (log_list != (LinkedListInfo *) NULL)
743 log_list=DestroyLinkedList(log_list,DestroyLogElement);
744 instantiate_log=MagickFalse;
745 (void) UnlockSemaphoreInfo(log_semaphore);
746 DestroySemaphoreInfo(&log_semaphore);
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 % L o g M a g i c k E v e n t %
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % LogMagickEvent() logs an event as determined by the log configuration file.
761 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
763 % The format of the LogMagickEvent method is:
765 % MagickBooleanType LogMagickEvent(const LogEventType type,
766 % const char *module,const char *function,const unsigned long line,
767 % const char *format,...)
769 % A description of each parameter follows:
771 % o type: the event type.
773 % o filename: the source module filename.
775 % o function: the function name.
777 % o line: the line number of the source module.
779 % o format: the output format.
782 static char *TranslateEvent(const LogEventType magick_unused(type),
783 const char *module,const char *function,const unsigned long line,
784 const char *domain,const char *event)
811 exception=AcquireExceptionInfo();
812 log_info=(LogInfo *) GetLogInfo("*",exception);
813 exception=DestroyExceptionInfo(exception);
814 seconds=time((time_t *) NULL);
815 elapsed_time=GetElapsedTime(&log_info->timer);
816 user_time=GetUserTime(&log_info->timer);
817 text=AcquireString(event);
818 if (log_info->format == (char *) NULL)
820 extent=strlen(event)+MaxTextExtent;
821 if (LocaleCompare(log_info->format,"xml") == 0)
824 timestamp[MaxTextExtent];
827 Translate event in "XML" format.
829 (void) FormatMagickTime(seconds,extent,timestamp);
830 (void) FormatMagickString(text,extent,
832 " <timestamp>%s</timestamp>\n"
833 " <elapsed-time>%ld:%02ld.%03ld</elapsed-time>\n"
834 " <user-time>%0.3f</user-time>\n"
835 " <process-id>%ld</process-id>\n"
836 " <thread-id>%lu</thread-id>\n"
837 " <module>%s</module>\n"
838 " <function>%s</function>\n"
839 " <line>%lu</line>\n"
840 " <domain>%s</domain>\n"
841 " <event>%s</event>\n"
842 "</entry>",timestamp,(long) (elapsed_time/60.0),(long)
843 floor(fmod(elapsed_time,60.0)),(long) (1000.0*(elapsed_time-
844 floor(elapsed_time))+0.5),user_time,(long) getpid(),
845 GetMagickThreadSignature(),module,function,line,domain,event);
849 Translate event in "human readable" format.
852 for (p=log_info->format; *p != '\0'; p++)
855 if ((size_t) (q-text+MaxTextExtent) >= extent)
857 extent+=MaxTextExtent;
858 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
860 if (text == (char *) NULL)
861 return((char *) NULL);
865 The format of the log is defined by embedding special format characters:
884 if ((*p == '\\') && (*(p+1) == 'r'))
890 if ((*p == '\\') && (*(p+1) == 'n'))
906 q+=CopyMagickString(q,GetClientName(),extent);
911 q+=CopyMagickString(q,domain,extent);
916 q+=CopyMagickString(q,event,extent);
921 q+=CopyMagickString(q,function,extent);
926 if (log_info->generations == 0)
928 (void) CopyMagickString(q,"0",extent);
932 q+=FormatMagickString(q,extent,"%lu",log_info->generation %
933 log_info->generations);
938 q+=FormatMagickString(q,extent,"%lu",line);
946 for (p=module+strlen(module)-1; p > module; p--)
947 if (*p == *DirectorySeparator)
952 q+=CopyMagickString(q,p,extent);
957 q+=CopyMagickString(q,GetLogName(),extent);
962 q+=FormatMagickString(q,extent,"%ld",(long) getpid());
967 q+=FormatMagickString(q,extent,"%ld:%02ld.%03ld",(long)
968 (elapsed_time/60.0),(long) floor(fmod(elapsed_time,60.0)),
969 (long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
974 q+=FormatMagickTime(seconds,extent,q);
979 q+=FormatMagickString(q,extent,"%0.3fu",user_time);
984 q+=CopyMagickString(q,MagickLibVersionText,extent);
1004 static char *TranslateFilename(const LogInfo *log_info)
1019 Translate event in "human readable" format.
1021 assert(log_info != (LogInfo *) NULL);
1022 assert(log_info->filename != (char *) NULL);
1023 filename=AcquireString((char *) NULL);
1024 extent=MaxTextExtent;
1026 for (p=log_info->filename; *p != '\0'; p++)
1029 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1031 extent+=MaxTextExtent;
1032 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1034 if (filename == (char *) NULL)
1035 return((char *) NULL);
1036 q=filename+strlen(filename);
1039 The format of the filename is defined by embedding special format
1058 q+=CopyMagickString(q,GetClientName(),extent);
1063 if (log_info->generations == 0)
1065 (void) CopyMagickString(q,"0",extent);
1069 q+=FormatMagickString(q,extent,"%lu",log_info->generation %
1070 log_info->generations);
1075 q+=CopyMagickString(q,GetLogName(),extent);
1080 q+=FormatMagickString(q,extent,"%ld",(long) getpid());
1085 q+=CopyMagickString(q,MagickLibVersionText,extent);
1105 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1106 const char *function,const unsigned long line,const char *format,
1110 event[MaxTextExtent],
1125 if (IsEventLogging() == MagickFalse)
1126 return(MagickFalse);
1127 exception=AcquireExceptionInfo();
1128 log_info=(LogInfo *) GetLogInfo("*",exception);
1129 exception=DestroyExceptionInfo(exception);
1130 (void) LockSemaphoreInfo(log_semaphore);
1131 if ((log_info->event_mask & type) == 0)
1133 (void) UnlockSemaphoreInfo(log_semaphore);
1136 domain=MagickOptionToMnemonic(MagickLogEventOptions,type);
1137 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1138 n=vsnprintf(event,MaxTextExtent,format,operands);
1140 n=vsprintf(event,format,operands);
1143 event[MaxTextExtent-1]='\0';
1144 text=TranslateEvent(type,module,function,line,domain,event);
1145 if (text == (char *) NULL)
1147 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1148 (void) UnlockSemaphoreInfo(log_semaphore);
1149 return(MagickFalse);
1151 if ((log_info->handler_mask & ConsoleHandler) != 0)
1153 (void) fprintf(stderr,"%s\n",text);
1154 (void) fflush(stderr);
1156 if ((log_info->handler_mask & DebugHandler) != 0)
1158 #if defined(__WINDOWS__)
1159 OutputDebugString(text);
1162 if ((log_info->handler_mask & EventHandler) != 0)
1164 #if defined(__WINDOWS__)
1165 (void) NTReportEvent(text,MagickFalse);
1168 if ((log_info->handler_mask & FileHandler) != 0)
1173 file_info.st_size=0;
1174 if (log_info->file != (FILE *) NULL)
1175 (void) fstat(fileno(log_info->file),&file_info);
1176 if (file_info.st_size > (off_t) (1024*1024*log_info->limit))
1178 (void) fprintf(log_info->file,"</log>\n");
1179 (void) fclose(log_info->file);
1180 log_info->file=(FILE *) NULL;
1182 if (log_info->file == (FILE *) NULL)
1187 filename=TranslateFilename(log_info);
1188 if (filename == (char *) NULL)
1190 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1191 (void) UnlockSemaphoreInfo(log_semaphore);
1192 return(MagickFalse);
1194 log_info->append=IsPathAccessible(filename);
1195 log_info->file=OpenMagickStream(filename,"ab");
1196 filename=(char *) RelinquishMagickMemory(filename);
1197 if (log_info->file == (FILE *) NULL)
1199 (void) UnlockSemaphoreInfo(log_semaphore);
1200 return(MagickFalse);
1202 log_info->generation++;
1203 if (log_info->append == MagickFalse)
1205 (void) fprintf(log_info->file,"<?xml version=\"1.0\" "
1206 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1207 (void) fprintf(log_info->file,"<log>\n");
1210 (void) fprintf(log_info->file,"%s\n",text);
1211 (void) fflush(log_info->file);
1213 if ((log_info->handler_mask & StdoutHandler) != 0)
1215 (void) fprintf(stdout,"%s\n",text);
1216 (void) fflush(stdout);
1218 if ((log_info->handler_mask & StderrHandler) != 0)
1220 (void) fprintf(stderr,"%s\n",text);
1221 (void) fflush(stderr);
1223 text=(char *) RelinquishMagickMemory(text);
1224 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1225 (void) UnlockSemaphoreInfo(log_semaphore);
1229 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1230 const char *function,const unsigned long line,const char *format,...)
1238 va_start(operands,format);
1239 status=LogMagickEventList(type,module,function,line,format,operands);
1245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1249 + L o a d L o g L i s t %
1253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1255 % LoadLogList() loads the log configuration file which provides a
1256 % mapping between log attributes and log name.
1258 % The format of the LoadLogList method is:
1260 % MagickBooleanType LoadLogList(const char *xml,const char *filename,
1261 % const unsigned long depth,ExceptionInfo *exception)
1263 % A description of each parameter follows:
1265 % o xml: The log list in XML format.
1267 % o filename: The log list filename.
1269 % o depth: depth of <include /> statements.
1271 % o exception: return any errors or warnings in this structure.
1274 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1275 const unsigned long depth,ExceptionInfo *exception)
1278 keyword[MaxTextExtent],
1285 *log_info = (LogInfo *) NULL;
1291 Load the log map file.
1293 if (xml == (const char *) NULL)
1294 return(MagickFalse);
1295 if (log_list == (LinkedListInfo *) NULL)
1297 log_list=NewLinkedList(0);
1298 if (log_list == (LinkedListInfo *) NULL)
1300 ThrowFileException(exception,ResourceLimitError,
1301 "MemoryAllocationFailed",filename);
1302 return(MagickFalse);
1306 token=AcquireString((const char *) xml);
1307 for (q=(const char *) xml; *q != '\0'; )
1312 GetMagickToken(q,&q,token);
1315 (void) CopyMagickString(keyword,token,MaxTextExtent);
1316 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1321 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1322 GetMagickToken(q,&q,token);
1325 if (LocaleNCompare(keyword,"<!--",4) == 0)
1330 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1331 GetMagickToken(q,&q,token);
1334 if (LocaleCompare(keyword,"<include") == 0)
1339 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1341 (void) CopyMagickString(keyword,token,MaxTextExtent);
1342 GetMagickToken(q,&q,token);
1345 GetMagickToken(q,&q,token);
1346 if (LocaleCompare(keyword,"file") == 0)
1349 (void) ThrowMagickException(exception,GetMagickModule(),
1350 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1354 path[MaxTextExtent],
1357 GetPathComponent(filename,HeadPath,path);
1359 (void) ConcatenateMagickString(path,DirectorySeparator,
1361 if (*token == *DirectorySeparator)
1362 (void) CopyMagickString(path,token,MaxTextExtent);
1364 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1365 xml=FileToString(path,~0,exception);
1366 if (xml != (char *) NULL)
1368 status|=LoadLogList(xml,path,depth+1,exception);
1369 xml=DestroyString(xml);
1376 if (LocaleCompare(keyword,"<logmap>") == 0)
1379 Allocate memory for the log list.
1381 log_info=(LogInfo *) AcquireAlignedMemory(1,sizeof(*log_info));
1382 if (log_info == (LogInfo *) NULL)
1383 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1384 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1385 log_info->path=ConstantString(filename);
1386 GetTimerInfo((TimerInfo *) &log_info->timer);
1387 log_info->exempt=MagickFalse;
1388 log_info->signature=MagickSignature;
1391 if (log_info == (LogInfo *) NULL)
1393 if (LocaleCompare(keyword,"</logmap>") == 0)
1395 status=AppendValueToLinkedList(log_list,log_info);
1396 if (status == MagickFalse)
1397 (void) ThrowMagickException(exception,GetMagickModule(),
1398 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1399 log_info=(LogInfo *) NULL;
1401 GetMagickToken(q,(const char **) NULL,token);
1404 GetMagickToken(q,&q,token);
1405 GetMagickToken(q,&q,token);
1411 if (LocaleCompare((char *) keyword,"events") == 0)
1413 log_info->event_mask=(LogEventType) (log_info->event_mask |
1414 ParseMagickOption(MagickLogEventOptions,MagickTrue,token));
1422 if (LocaleCompare((char *) keyword,"filename") == 0)
1424 if (log_info->filename != (char *) NULL)
1425 log_info->filename=(char *)
1426 RelinquishMagickMemory(log_info->filename);
1427 log_info->filename=ConstantString(token);
1430 if (LocaleCompare((char *) keyword,"format") == 0)
1432 if (log_info->format != (char *) NULL)
1433 log_info->format=(char *)
1434 RelinquishMagickMemory(log_info->format);
1435 log_info->format=ConstantString(token);
1443 if (LocaleCompare((char *) keyword,"generations") == 0)
1445 if (LocaleCompare(token,"unlimited") == 0)
1447 log_info->generations=(~0UL);
1450 log_info->generations=(unsigned long) atol(token);
1458 if (LocaleCompare((char *) keyword,"limit") == 0)
1460 if (LocaleCompare(token,"unlimited") == 0)
1462 log_info->limit=(~0UL);
1465 log_info->limit=(unsigned long) atol(token);
1473 if (LocaleCompare((char *) keyword,"output") == 0)
1475 log_info->handler_mask=(LogHandlerType)
1476 (log_info->handler_mask | ParseLogHandlers(token));
1485 token=DestroyString(token);
1486 if (log_list == (LinkedListInfo *) NULL)
1487 return(MagickFalse);
1488 return(status != 0 ? MagickTrue : MagickFalse);
1492 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1496 % L o a d L o g L i s t s %
1500 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1502 % LoadLogLists() loads one or more log configuration file which provides a
1503 % mapping between log attributes and log name.
1505 % The format of the LoadLogLists method is:
1507 % MagickBooleanType LoadLogLists(const char *filename,
1508 % ExceptionInfo *exception)
1510 % A description of each parameter follows:
1512 % o filename: the log configuration filename.
1514 % o exception: return any errors or warnings in this structure.
1517 static MagickBooleanType LoadLogLists(const char *filename,
1518 ExceptionInfo *exception)
1533 Load built-in log map.
1536 if (log_list == (LinkedListInfo *) NULL)
1538 log_list=NewLinkedList(0);
1539 if (log_list == (LinkedListInfo *) NULL)
1541 ThrowFileException(exception,ResourceLimitError,
1542 "MemoryAllocationFailed",filename);
1543 return(MagickFalse);
1546 for (i=0; i < (long) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1551 register const LogMapInfo
1555 log_info=(LogInfo *) AcquireAlignedMemory(1,sizeof(*log_info));
1556 if (log_info == (LogInfo *) NULL)
1558 (void) ThrowMagickException(exception,GetMagickModule(),
1559 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1562 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1563 log_info->path=(char *) "[built-in]";
1564 GetTimerInfo((TimerInfo *) &log_info->timer);
1565 log_info->event_mask=p->event_mask;
1566 log_info->handler_mask=p->handler_mask;
1567 log_info->filename=ConstantString(p->filename);
1568 log_info->format=ConstantString(p->format);
1569 log_info->exempt=MagickTrue;
1570 log_info->signature=MagickSignature;
1571 status=AppendValueToLinkedList(log_list,log_info);
1572 if (status == MagickFalse)
1573 (void) ThrowMagickException(exception,GetMagickModule(),
1574 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1577 Load external log map.
1579 options=GetConfigureOptions(filename,exception);
1580 option=(const StringInfo *) GetNextValueInLinkedList(options);
1581 while (option != (const StringInfo *) NULL)
1583 status|=LoadLogList((const char *) GetStringInfoDatum(option),
1584 GetStringInfoPath(option),0,exception);
1585 option=(const StringInfo *) GetNextValueInLinkedList(options);
1587 options=DestroyConfigureOptions(options);
1588 return(status != 0 ? MagickTrue : MagickFalse);
1592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1596 + P a r s e L o g H a n d l e r s %
1600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1602 % ParseLogHandlers() parses a string defining which handlers takes a log
1603 % message and exports them.
1605 % The format of the ParseLogHandlers method is:
1607 % LogHandlerType ParseLogHandlers(const char *handlers)
1609 % A description of each parameter follows:
1611 % o handlers: one or more handlers separated by commas.
1614 static LogHandlerType ParseLogHandlers(const char *handlers)
1628 handler_mask=NoHandler;
1629 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1631 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1634 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1636 length=strlen(LogHandlers[i].name);
1637 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1639 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1643 if (LogHandlers[i].name == (char *) NULL)
1644 return(UndefinedHandler);
1646 return(handler_mask);
1650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1654 % S e t L o g E v e n t M a s k %
1658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1660 % SetLogEventMask() accepts a list that determines which events to log. All
1661 % other events are ignored. By default, no debug is enabled. This method
1662 % returns the previous log event mask.
1664 % The format of the SetLogEventMask method is:
1666 % LogEventType SetLogEventMask(const char *events)
1668 % A description of each parameter follows:
1670 % o events: log these events.
1673 MagickExport LogEventType SetLogEventMask(const char *events)
1684 exception=AcquireExceptionInfo();
1685 log_info=(LogInfo *) GetLogInfo("*",exception);
1686 exception=DestroyExceptionInfo(exception);
1687 option=ParseMagickOption(MagickLogEventOptions,MagickTrue,events);
1688 (void) LockSemaphoreInfo(log_semaphore);
1689 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1690 log_info->event_mask=(LogEventType) option;
1692 log_info->event_mask=UndefinedEvents;
1693 (void) UnlockSemaphoreInfo(log_semaphore);
1694 return(log_info->event_mask);
1698 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1702 % S e t L o g F o r m a t %
1706 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1708 % SetLogFormat() sets the format for the "human readable" log record.
1710 % The format of the LogMagickFormat method is:
1712 % SetLogFormat(const char *format)
1714 % A description of each parameter follows:
1716 % o format: the log record format.
1719 MagickExport void SetLogFormat(const char *format)
1727 exception=AcquireExceptionInfo();
1728 log_info=(LogInfo *) GetLogInfo("*",exception);
1729 exception=DestroyExceptionInfo(exception);
1730 (void) LockSemaphoreInfo(log_semaphore);
1731 if (log_info->format != (char *) NULL)
1732 log_info->format=DestroyString(log_info->format);
1733 log_info->format=ConstantString(format);
1734 (void) UnlockSemaphoreInfo(log_semaphore);
1738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1742 % S e t L o g N a m e %
1746 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1748 % SetLogName() sets the log name and returns it.
1750 % The format of the SetLogName method is:
1752 % const char *SetLogName(const char *name)
1754 % A description of each parameter follows:
1756 % o log_name: SetLogName() returns the current client name.
1758 % o name: Specifies the new client name.
1761 MagickExport const char *SetLogName(const char *name)
1763 if ((name != (char *) NULL) && (*name != '\0'))
1764 (void) CopyMagickString(log_name,name,MaxTextExtent);