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 "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/configure.h"
46 #include "MagickCore/configure-private.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/hashmap.h"
50 #include "MagickCore/log.h"
51 #include "MagickCore/log-private.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/option.h"
54 #include "MagickCore/semaphore.h"
55 #include "MagickCore/timer.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/string-private.h"
58 #include "MagickCore/token.h"
59 #include "MagickCore/thread_.h"
60 #include "MagickCore/thread-private.h"
61 #include "MagickCore/utility.h"
62 #include "MagickCore/utility-private.h"
63 #include "MagickCore/version.h"
64 #include "MagickCore/xml-tree.h"
69 #define LogFilename "log.xml"
76 UndefinedHandler = 0x0000,
78 ConsoleHandler = 0x0001,
79 StdoutHandler = 0x0002,
80 StderrHandler = 0x0004,
82 DebugHandler = 0x0010,
86 typedef struct _EventInfo
95 typedef struct _HandlerInfo
140 typedef struct _LogMapInfo
156 static const HandlerInfo
159 { "console", ConsoleHandler },
160 { "debug", DebugHandler },
161 { "event", EventHandler },
162 { "file", FileHandler },
163 { "none", NoHandler },
164 { "stderr", StderrHandler },
165 { "stdout", StdoutHandler },
166 { (char *) NULL, UndefinedHandler }
169 static const LogMapInfo
172 { NoEvents, ConsoleHandler, "Magick-%d.log",
173 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\n %e" }
177 log_name[MaxTextExtent] = "Magick";
179 static LinkedListInfo
180 *log_list = (LinkedListInfo *) NULL;
183 *log_semaphore = (SemaphoreInfo *) NULL;
185 static volatile MagickBooleanType
186 instantiate_log = MagickFalse;
189 Forward declarations.
191 static LogHandlerType
192 ParseLogHandlers(const char *);
195 *GetLogInfo(const char *,ExceptionInfo *);
197 static MagickBooleanType
198 InitializeLogList(ExceptionInfo *),
199 LoadLogLists(const char *,ExceptionInfo *);
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206 % C l o s e M a g i c k L o g %
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 % CloseMagickLog() closes the Magick log.
214 % The format of the CloseMagickLog method is:
216 % CloseMagickLog(void)
219 MagickExport void CloseMagickLog(void)
227 if (IsEventLogging() == MagickFalse)
229 exception=AcquireExceptionInfo();
230 log_info=GetLogInfo("*",exception);
231 exception=DestroyExceptionInfo(exception);
232 LockSemaphoreInfo(log_semaphore);
233 if (log_info->file != (FILE *) NULL)
235 if (log_info->append == MagickFalse)
236 (void) FormatLocaleFile(log_info->file,"</log>\n");
237 (void) fclose(log_info->file);
238 log_info->file=(FILE *) NULL;
240 UnlockSemaphoreInfo(log_semaphore);
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 + G e t L o g I n f o %
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 % GetLogInfo() searches the log list for the specified name and if found
255 % returns attributes for that log.
257 % The format of the GetLogInfo method is:
259 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
261 % A description of each parameter follows:
263 % o name: the log name.
265 % o exception: return any errors or warnings in this structure.
268 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
273 assert(exception != (ExceptionInfo *) NULL);
274 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
275 if (InitializeLogList(exception) == MagickFalse)
276 return((LogInfo *) NULL);
277 if ((log_list == (LinkedListInfo *) NULL) ||
278 (IsLinkedListEmpty(log_list) != MagickFalse))
279 return((LogInfo *) NULL);
280 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
281 return((LogInfo *) GetValueFromLinkedList(log_list,0));
285 LockSemaphoreInfo(log_semaphore);
286 ResetLinkedListIterator(log_list);
287 p=(LogInfo *) GetNextValueInLinkedList(log_list);
288 while (p != (LogInfo *) NULL)
290 if (LocaleCompare(name,p->name) == 0)
292 p=(LogInfo *) GetNextValueInLinkedList(log_list);
294 if (p != (LogInfo *) NULL)
295 (void) InsertValueInLinkedList(log_list,0,
296 RemoveElementByValueFromLinkedList(log_list,p));
297 UnlockSemaphoreInfo(log_semaphore);
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 % G e t L o g I n f o L i s t %
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % GetLogInfoList() returns any logs that match the specified pattern.
314 % The format of the GetLogInfoList function is:
316 % const LogInfo **GetLogInfoList(const char *pattern,
317 % size_t *number_preferences,ExceptionInfo *exception)
319 % A description of each parameter follows:
321 % o pattern: Specifies a pointer to a text string containing a pattern.
323 % o number_preferences: This integer returns the number of logs in the list.
325 % o exception: return any errors or warnings in this structure.
328 #if defined(__cplusplus) || defined(c_plusplus)
332 static int LogInfoCompare(const void *x,const void *y)
338 p=(const LogInfo **) x,
339 q=(const LogInfo **) y;
340 if (LocaleCompare((*p)->path,(*q)->path) == 0)
341 return(LocaleCompare((*p)->name,(*q)->name));
342 return(LocaleCompare((*p)->path,(*q)->path));
345 #if defined(__cplusplus) || defined(c_plusplus)
349 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
350 size_t *number_preferences,ExceptionInfo *exception)
355 register const LogInfo
364 assert(pattern != (char *) NULL);
365 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
366 assert(number_preferences != (size_t *) NULL);
367 *number_preferences=0;
368 p=GetLogInfo("*",exception);
369 if (p == (const LogInfo *) NULL)
370 return((const LogInfo **) NULL);
371 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
372 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
373 if (preferences == (const LogInfo **) NULL)
374 return((const LogInfo **) NULL);
378 LockSemaphoreInfo(log_semaphore);
379 ResetLinkedListIterator(log_list);
380 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
381 for (i=0; p != (const LogInfo *) NULL; )
383 if ((p->stealth == MagickFalse) &&
384 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
386 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
388 UnlockSemaphoreInfo(log_semaphore);
389 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
390 preferences[i]=(LogInfo *) NULL;
391 *number_preferences=(size_t) i;
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % G e t L o g L i s t %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % GetLogList() returns any logs that match the specified pattern.
408 % The format of the GetLogList function is:
410 % char **GetLogList(const char *pattern,size_t *number_preferences,
411 % ExceptionInfo *exception)
413 % A description of each parameter follows:
415 % o pattern: Specifies a pointer to a text string containing a pattern.
417 % o number_preferences: This integer returns the number of logs in the list.
419 % o exception: return any errors or warnings in this structure.
423 #if defined(__cplusplus) || defined(c_plusplus)
427 static int LogCompare(const void *x,const void *y)
435 return(LocaleCompare(*p,*q));
438 #if defined(__cplusplus) || defined(c_plusplus)
442 MagickExport char **GetLogList(const char *pattern,
443 size_t *number_preferences,ExceptionInfo *exception)
448 register const LogInfo
457 assert(pattern != (char *) NULL);
458 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
459 assert(number_preferences != (size_t *) NULL);
460 *number_preferences=0;
461 p=GetLogInfo("*",exception);
462 if (p == (const LogInfo *) NULL)
463 return((char **) NULL);
464 preferences=(char **) AcquireQuantumMemory((size_t)
465 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
466 if (preferences == (char **) NULL)
467 return((char **) NULL);
471 LockSemaphoreInfo(log_semaphore);
472 ResetLinkedListIterator(log_list);
473 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
474 for (i=0; p != (const LogInfo *) NULL; )
476 if ((p->stealth == MagickFalse) &&
477 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
478 preferences[i++]=ConstantString(p->name);
479 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
481 UnlockSemaphoreInfo(log_semaphore);
482 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
483 preferences[i]=(char *) NULL;
484 *number_preferences=(size_t) i;
489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493 % G e t L o g N a m e %
497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 % GetLogName() returns the current log name.
501 % The format of the GetLogName method is:
503 % const char *GetLogName(void)
506 MagickExport const char *GetLogName(void)
512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 + I n i t i a l i z e L o g L i s t %
520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
522 % InitializeLogList() initialize the log list.
524 % The format of the InitializeLogList method is:
526 % MagickBooleanType InitializeLogList(ExceptionInfo *exception)
528 % A description of each parameter follows.
530 % o exception: return any errors or warnings in this structure.
533 static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
535 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
537 if (log_semaphore == (SemaphoreInfo *) NULL)
538 AcquireSemaphoreInfo(&log_semaphore);
539 LockSemaphoreInfo(log_semaphore);
540 if ((log_list == (LinkedListInfo *) NULL) &&
541 (instantiate_log == MagickFalse))
543 (void) LoadLogLists(LogFilename,exception);
544 instantiate_log=MagickTrue;
546 UnlockSemaphoreInfo(log_semaphore);
548 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
552 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
556 % I s E v e n t L o g g i n g %
560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
562 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
565 % The format of the IsEventLogging method is:
567 % MagickBooleanType IsEventLogging(void)
570 MagickExport MagickBooleanType IsEventLogging(void)
578 if ((log_list == (LinkedListInfo *) NULL) ||
579 (IsLinkedListEmpty(log_list) != MagickFalse))
581 exception=AcquireExceptionInfo();
582 log_info=GetLogInfo("*",exception);
583 exception=DestroyExceptionInfo(exception);
584 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591 % L i s t L o g I n f o %
595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
597 % ListLogInfo() lists the log info to a file.
599 % The format of the ListLogInfo method is:
601 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
603 % A description of each parameter follows.
605 % o file: An pointer to a FILE.
607 % o exception: return any errors or warnings in this structure.
610 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
612 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
615 limit[MaxTextExtent];
632 if (file == (const FILE *) NULL)
634 log_info=GetLogInfoList("*",&number_aliases,exception);
635 if (log_info == (const LogInfo **) NULL)
638 path=(const char *) NULL;
639 for (i=0; i < (ssize_t) number_aliases; i++)
641 if (log_info[i]->stealth != MagickFalse)
643 if ((path == (const char *) NULL) ||
644 (LocaleCompare(path,log_info[i]->path) != 0))
646 if (log_info[i]->path != (char *) NULL)
647 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
648 (void) FormatLocaleFile(file,
649 "Filename Generations Limit Format\n");
650 (void) FormatLocaleFile(file,
651 "-------------------------------------------------"
652 "------------------------------\n");
654 path=log_info[i]->path;
655 if (log_info[i]->filename != (char *) NULL)
657 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
658 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
659 (void) FormatLocaleFile(file," ");
661 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
662 (void) FormatMagickSize(MegabytesToBytes(log_info[i]->limit),MagickFalse,
664 (void) FormatLocaleFile(file,"%8sB ",limit);
665 if (log_info[i]->format != (char *) NULL)
666 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
667 (void) FormatLocaleFile(file,"\n");
670 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 + L o g C o m p o n e n t G e n e s i s %
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % LogComponentGenesis() instantiates the log component.
687 % The format of the LogComponentGenesis method is:
689 % MagickBooleanType LogComponentGenesis(void)
692 MagickPrivate MagickBooleanType LogComponentGenesis(void)
694 AcquireSemaphoreInfo(&log_semaphore);
699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
703 + L o g C o m p o n e n t T e r m i n u s %
707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
709 % LogComponentTerminus() destroys the logging component.
711 % The format of the LogComponentTerminus method is:
713 % LogComponentTerminus(void)
717 static void *DestroyLogElement(void *log_info)
722 p=(LogInfo *) log_info;
723 if (p->file != (FILE *) NULL)
725 if (p->append == MagickFalse)
726 (void) FormatLocaleFile(p->file,"</log>\n");
727 (void) fclose(p->file);
728 p->file=(FILE *) NULL;
730 if (p->exempt == MagickFalse)
732 if (p->format != (char *) NULL)
733 p->format=DestroyString(p->format);
734 if (p->path != (char *) NULL)
735 p->path=DestroyString(p->path);
736 if (p->filename != (char *) NULL)
737 p->filename=DestroyString(p->filename);
739 p=(LogInfo *) RelinquishMagickMemory(p);
740 return((void *) NULL);
743 MagickPrivate void LogComponentTerminus(void)
745 if (log_semaphore == (SemaphoreInfo *) NULL)
746 AcquireSemaphoreInfo(&log_semaphore);
747 LockSemaphoreInfo(log_semaphore);
748 if (log_list != (LinkedListInfo *) NULL)
749 log_list=DestroyLinkedList(log_list,DestroyLogElement);
750 instantiate_log=MagickFalse;
751 UnlockSemaphoreInfo(log_semaphore);
752 DestroySemaphoreInfo(&log_semaphore);
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % L o g M a g i c k E v e n t %
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % LogMagickEvent() logs an event as determined by the log configuration file.
767 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
769 % The format of the LogMagickEvent method is:
771 % MagickBooleanType LogMagickEvent(const LogEventType type,
772 % const char *module,const char *function,const size_t line,
773 % const char *format,...)
775 % A description of each parameter follows:
777 % o type: the event type.
779 % o filename: the source module filename.
781 % o function: the function name.
783 % o line: the line number of the source module.
785 % o format: the output format.
788 static char *TranslateEvent(const LogEventType magick_unused(type),
789 const char *module,const char *function,const size_t line,
790 const char *domain,const char *event)
817 exception=AcquireExceptionInfo();
818 log_info=(LogInfo *) GetLogInfo("*",exception);
819 exception=DestroyExceptionInfo(exception);
820 seconds=time((time_t *) NULL);
821 elapsed_time=GetElapsedTime(&log_info->timer);
822 user_time=GetUserTime(&log_info->timer);
823 text=AcquireString(event);
824 if (log_info->format == (char *) NULL)
826 extent=strlen(event)+MaxTextExtent;
827 if (LocaleCompare(log_info->format,"xml") == 0)
830 timestamp[MaxTextExtent];
833 Translate event in "XML" format.
835 (void) FormatMagickTime(seconds,extent,timestamp);
836 (void) FormatLocaleString(text,extent,
838 " <timestamp>%s</timestamp>\n"
839 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
840 " <user-time>%0.3f</user-time>\n"
841 " <process-id>%.20g</process-id>\n"
842 " <thread-id>%.20g</thread-id>\n"
843 " <module>%s</module>\n"
844 " <function>%s</function>\n"
845 " <line>%.20g</line>\n"
846 " <domain>%s</domain>\n"
847 " <event>%s</event>\n"
848 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
849 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
850 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
851 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
852 (double) line,domain,event);
856 Translate event in "human readable" format.
859 for (p=log_info->format; *p != '\0'; p++)
862 if ((size_t) (q-text+MaxTextExtent) >= extent)
864 extent+=MaxTextExtent;
865 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
867 if (text == (char *) NULL)
868 return((char *) NULL);
872 The format of the log is defined by embedding special format characters:
891 if ((*p == '\\') && (*(p+1) == 'r'))
897 if ((*p == '\\') && (*(p+1) == 'n'))
913 q+=CopyMagickString(q,GetClientName(),extent);
918 q+=CopyMagickString(q,domain,extent);
923 q+=CopyMagickString(q,event,extent);
928 q+=CopyMagickString(q,function,extent);
933 if (log_info->generations == 0)
935 (void) CopyMagickString(q,"0",extent);
939 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
940 log_info->generations));
945 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
953 for (p=module+strlen(module)-1; p > module; p--)
954 if (*p == *DirectorySeparator)
959 q+=CopyMagickString(q,p,extent);
964 q+=CopyMagickString(q,GetLogName(),extent);
969 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
974 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
975 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
976 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
981 q+=FormatMagickTime(seconds,extent,q);
986 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
991 q+=CopyMagickString(q,MagickLibVersionText,extent);
1011 static char *TranslateFilename(const LogInfo *log_info)
1026 Translate event in "human readable" format.
1028 assert(log_info != (LogInfo *) NULL);
1029 assert(log_info->filename != (char *) NULL);
1030 filename=AcquireString((char *) NULL);
1031 extent=MaxTextExtent;
1033 for (p=log_info->filename; *p != '\0'; p++)
1036 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1038 extent+=MaxTextExtent;
1039 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1041 if (filename == (char *) NULL)
1042 return((char *) NULL);
1043 q=filename+strlen(filename);
1046 The format of the filename is defined by embedding special format
1065 q+=CopyMagickString(q,GetClientName(),extent);
1070 if (log_info->generations == 0)
1072 (void) CopyMagickString(q,"0",extent);
1076 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1077 log_info->generations));
1082 q+=CopyMagickString(q,GetLogName(),extent);
1087 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1092 q+=CopyMagickString(q,MagickLibVersionText,extent);
1112 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1113 const char *function,const size_t line,const char *format,
1117 event[MaxTextExtent],
1132 if (IsEventLogging() == MagickFalse)
1133 return(MagickFalse);
1134 exception=AcquireExceptionInfo();
1135 log_info=(LogInfo *) GetLogInfo("*",exception);
1136 exception=DestroyExceptionInfo(exception);
1137 LockSemaphoreInfo(log_semaphore);
1138 if ((log_info->event_mask & type) == 0)
1140 UnlockSemaphoreInfo(log_semaphore);
1143 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1144 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1145 n=vsnprintf(event,MaxTextExtent,format,operands);
1147 n=vsprintf(event,format,operands);
1150 event[MaxTextExtent-1]='\0';
1151 text=TranslateEvent(type,module,function,line,domain,event);
1152 if (text == (char *) NULL)
1154 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1155 UnlockSemaphoreInfo(log_semaphore);
1156 return(MagickFalse);
1158 if ((log_info->handler_mask & ConsoleHandler) != 0)
1160 (void) FormatLocaleFile(stderr,"%s\n",text);
1161 (void) fflush(stderr);
1163 if ((log_info->handler_mask & DebugHandler) != 0)
1165 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1166 OutputDebugString(text);
1169 if ((log_info->handler_mask & EventHandler) != 0)
1171 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1172 (void) NTReportEvent(text,MagickFalse);
1175 if ((log_info->handler_mask & FileHandler) != 0)
1180 file_info.st_size=0;
1181 if (log_info->file != (FILE *) NULL)
1182 (void) fstat(fileno(log_info->file),&file_info);
1183 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
1185 (void) FormatLocaleFile(log_info->file,"</log>\n");
1186 (void) fclose(log_info->file);
1187 log_info->file=(FILE *) NULL;
1189 if (log_info->file == (FILE *) NULL)
1194 filename=TranslateFilename(log_info);
1195 if (filename == (char *) NULL)
1197 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1198 UnlockSemaphoreInfo(log_semaphore);
1199 return(MagickFalse);
1201 log_info->append=IsPathAccessible(filename);
1202 log_info->file=fopen_utf8(filename,"ab");
1203 filename=(char *) RelinquishMagickMemory(filename);
1204 if (log_info->file == (FILE *) NULL)
1206 UnlockSemaphoreInfo(log_semaphore);
1207 return(MagickFalse);
1209 log_info->generation++;
1210 if (log_info->append == MagickFalse)
1212 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1213 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1214 (void) FormatLocaleFile(log_info->file,"<log>\n");
1217 (void) FormatLocaleFile(log_info->file,"%s\n",text);
1218 (void) fflush(log_info->file);
1220 if ((log_info->handler_mask & StdoutHandler) != 0)
1222 (void) FormatLocaleFile(stdout,"%s\n",text);
1223 (void) fflush(stdout);
1225 if ((log_info->handler_mask & StderrHandler) != 0)
1227 (void) FormatLocaleFile(stderr,"%s\n",text);
1228 (void) fflush(stderr);
1230 text=(char *) RelinquishMagickMemory(text);
1231 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1232 UnlockSemaphoreInfo(log_semaphore);
1236 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1237 const char *function,const size_t line,const char *format,...)
1245 va_start(operands,format);
1246 status=LogMagickEventList(type,module,function,line,format,operands);
1252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 + L o a d L o g L i s t %
1260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1262 % LoadLogList() loads the log configuration file which provides a
1263 % mapping between log attributes and log name.
1265 % The format of the LoadLogList method is:
1267 % MagickBooleanType LoadLogList(const char *xml,const char *filename,
1268 % const size_t depth,ExceptionInfo *exception)
1270 % A description of each parameter follows:
1272 % o xml: The log list in XML format.
1274 % o filename: The log list filename.
1276 % o depth: depth of <include /> statements.
1278 % o exception: return any errors or warnings in this structure.
1281 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1282 const size_t depth,ExceptionInfo *exception)
1285 keyword[MaxTextExtent],
1292 *log_info = (LogInfo *) NULL;
1298 Load the log map file.
1300 if (xml == (const char *) NULL)
1301 return(MagickFalse);
1302 if (log_list == (LinkedListInfo *) NULL)
1304 log_list=NewLinkedList(0);
1305 if (log_list == (LinkedListInfo *) NULL)
1307 ThrowFileException(exception,ResourceLimitError,
1308 "MemoryAllocationFailed",filename);
1309 return(MagickFalse);
1313 token=AcquireString((const char *) xml);
1314 for (q=(const char *) xml; *q != '\0'; )
1319 GetMagickToken(q,&q,token);
1322 (void) CopyMagickString(keyword,token,MaxTextExtent);
1323 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1328 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1329 GetMagickToken(q,&q,token);
1332 if (LocaleNCompare(keyword,"<!--",4) == 0)
1337 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1338 GetMagickToken(q,&q,token);
1341 if (LocaleCompare(keyword,"<include") == 0)
1346 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1348 (void) CopyMagickString(keyword,token,MaxTextExtent);
1349 GetMagickToken(q,&q,token);
1352 GetMagickToken(q,&q,token);
1353 if (LocaleCompare(keyword,"file") == 0)
1356 (void) ThrowMagickException(exception,GetMagickModule(),
1357 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1361 path[MaxTextExtent],
1364 GetPathComponent(filename,HeadPath,path);
1366 (void) ConcatenateMagickString(path,DirectorySeparator,
1368 if (*token == *DirectorySeparator)
1369 (void) CopyMagickString(path,token,MaxTextExtent);
1371 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1372 xml=FileToString(path,~0,exception);
1373 if (xml != (char *) NULL)
1375 status|=LoadLogList(xml,path,depth+1,exception);
1376 xml=DestroyString(xml);
1383 if (LocaleCompare(keyword,"<logmap>") == 0)
1386 Allocate memory for the log list.
1388 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1389 if (log_info == (LogInfo *) NULL)
1390 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1391 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1392 log_info->path=ConstantString(filename);
1393 GetTimerInfo((TimerInfo *) &log_info->timer);
1394 log_info->exempt=MagickFalse;
1395 log_info->signature=MagickSignature;
1398 if (log_info == (LogInfo *) NULL)
1400 if (LocaleCompare(keyword,"</logmap>") == 0)
1402 status=AppendValueToLinkedList(log_list,log_info);
1403 if (status == MagickFalse)
1404 (void) ThrowMagickException(exception,GetMagickModule(),
1405 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1406 log_info=(LogInfo *) NULL;
1408 GetMagickToken(q,(const char **) NULL,token);
1411 GetMagickToken(q,&q,token);
1412 GetMagickToken(q,&q,token);
1418 if (LocaleCompare((char *) keyword,"events") == 0)
1420 log_info->event_mask=(LogEventType) (log_info->event_mask |
1421 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1429 if (LocaleCompare((char *) keyword,"filename") == 0)
1431 if (log_info->filename != (char *) NULL)
1432 log_info->filename=(char *)
1433 RelinquishMagickMemory(log_info->filename);
1434 log_info->filename=ConstantString(token);
1437 if (LocaleCompare((char *) keyword,"format") == 0)
1439 if (log_info->format != (char *) NULL)
1440 log_info->format=(char *)
1441 RelinquishMagickMemory(log_info->format);
1442 log_info->format=ConstantString(token);
1450 if (LocaleCompare((char *) keyword,"generations") == 0)
1452 if (LocaleCompare(token,"unlimited") == 0)
1454 log_info->generations=(~0UL);
1457 log_info->generations=StringToUnsignedLong(token);
1465 if (LocaleCompare((char *) keyword,"limit") == 0)
1467 if (LocaleCompare(token,"unlimited") == 0)
1469 log_info->limit=(~0UL);
1472 log_info->limit=StringToUnsignedLong(token);
1480 if (LocaleCompare((char *) keyword,"output") == 0)
1482 log_info->handler_mask=(LogHandlerType)
1483 (log_info->handler_mask | ParseLogHandlers(token));
1492 token=DestroyString(token);
1493 if (log_list == (LinkedListInfo *) NULL)
1494 return(MagickFalse);
1495 return(status != 0 ? MagickTrue : MagickFalse);
1499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 % L o a d L o g L i s t s %
1507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1509 % LoadLogLists() loads one or more log configuration file which provides a
1510 % mapping between log attributes and log name.
1512 % The format of the LoadLogLists method is:
1514 % MagickBooleanType LoadLogLists(const char *filename,
1515 % ExceptionInfo *exception)
1517 % A description of each parameter follows:
1519 % o filename: the log configuration filename.
1521 % o exception: return any errors or warnings in this structure.
1524 static MagickBooleanType LoadLogLists(const char *filename,
1525 ExceptionInfo *exception)
1540 Load built-in log map.
1543 if (log_list == (LinkedListInfo *) NULL)
1545 log_list=NewLinkedList(0);
1546 if (log_list == (LinkedListInfo *) NULL)
1548 ThrowFileException(exception,ResourceLimitError,
1549 "MemoryAllocationFailed",filename);
1550 return(MagickFalse);
1553 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1558 register const LogMapInfo
1562 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1563 if (log_info == (LogInfo *) NULL)
1565 (void) ThrowMagickException(exception,GetMagickModule(),
1566 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1569 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1570 log_info->path=(char *) "[built-in]";
1571 GetTimerInfo((TimerInfo *) &log_info->timer);
1572 log_info->event_mask=p->event_mask;
1573 log_info->handler_mask=p->handler_mask;
1574 log_info->filename=ConstantString(p->filename);
1575 log_info->format=ConstantString(p->format);
1576 log_info->exempt=MagickTrue;
1577 log_info->signature=MagickSignature;
1578 status=AppendValueToLinkedList(log_list,log_info);
1579 if (status == MagickFalse)
1580 (void) ThrowMagickException(exception,GetMagickModule(),
1581 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1584 Load external log map.
1586 options=GetConfigureOptions(filename,exception);
1587 option=(const StringInfo *) GetNextValueInLinkedList(options);
1588 while (option != (const StringInfo *) NULL)
1590 status|=LoadLogList((const char *) GetStringInfoDatum(option),
1591 GetStringInfoPath(option),0,exception);
1592 option=(const StringInfo *) GetNextValueInLinkedList(options);
1594 options=DestroyConfigureOptions(options);
1595 return(status != 0 ? MagickTrue : MagickFalse);
1599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603 + P a r s e L o g H a n d l e r s %
1607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609 % ParseLogHandlers() parses a string defining which handlers takes a log
1610 % message and exports them.
1612 % The format of the ParseLogHandlers method is:
1614 % LogHandlerType ParseLogHandlers(const char *handlers)
1616 % A description of each parameter follows:
1618 % o handlers: one or more handlers separated by commas.
1621 static LogHandlerType ParseLogHandlers(const char *handlers)
1635 handler_mask=NoHandler;
1636 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1638 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1641 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1643 length=strlen(LogHandlers[i].name);
1644 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1646 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1650 if (LogHandlers[i].name == (char *) NULL)
1651 return(UndefinedHandler);
1653 return(handler_mask);
1657 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1661 % S e t L o g E v e n t M a s k %
1665 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1667 % SetLogEventMask() accepts a list that determines which events to log. All
1668 % other events are ignored. By default, no debug is enabled. This method
1669 % returns the previous log event mask.
1671 % The format of the SetLogEventMask method is:
1673 % LogEventType SetLogEventMask(const char *events)
1675 % A description of each parameter follows:
1677 % o events: log these events.
1680 MagickExport LogEventType SetLogEventMask(const char *events)
1691 exception=AcquireExceptionInfo();
1692 log_info=(LogInfo *) GetLogInfo("*",exception);
1693 exception=DestroyExceptionInfo(exception);
1694 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1695 LockSemaphoreInfo(log_semaphore);
1696 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1697 log_info->event_mask=(LogEventType) option;
1699 log_info->event_mask=UndefinedEvents;
1700 UnlockSemaphoreInfo(log_semaphore);
1701 return(log_info->event_mask);
1705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1709 % S e t L o g F o r m a t %
1713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715 % SetLogFormat() sets the format for the "human readable" log record.
1717 % The format of the LogMagickFormat method is:
1719 % SetLogFormat(const char *format)
1721 % A description of each parameter follows:
1723 % o format: the log record format.
1726 MagickExport void SetLogFormat(const char *format)
1734 exception=AcquireExceptionInfo();
1735 log_info=(LogInfo *) GetLogInfo("*",exception);
1736 exception=DestroyExceptionInfo(exception);
1737 LockSemaphoreInfo(log_semaphore);
1738 if (log_info->format != (char *) NULL)
1739 log_info->format=DestroyString(log_info->format);
1740 log_info->format=ConstantString(format);
1741 UnlockSemaphoreInfo(log_semaphore);
1745 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1749 % S e t L o g N a m e %
1753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1755 % SetLogName() sets the log name and returns it.
1757 % The format of the SetLogName method is:
1759 % const char *SetLogName(const char *name)
1761 % A description of each parameter follows:
1763 % o log_name: SetLogName() returns the current client name.
1765 % o name: Specifies the new client name.
1768 MagickExport const char *SetLogName(const char *name)
1770 if ((name != (char *) NULL) && (*name != '\0'))
1771 (void) CopyMagickString(log_name,name,MaxTextExtent);