2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Log Events %
20 % Copyright 1999-2014 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/nt-base-private.h"
54 #include "MagickCore/option.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/timer.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/string-private.h"
59 #include "MagickCore/token.h"
60 #include "MagickCore/thread_.h"
61 #include "MagickCore/thread-private.h"
62 #include "MagickCore/utility.h"
63 #include "MagickCore/utility-private.h"
64 #include "MagickCore/version.h"
65 #include "MagickCore/xml-tree.h"
66 #include "MagickCore/xml-tree-private.h"
71 #define LogFilename "log.xml"
78 UndefinedHandler = 0x0000,
80 ConsoleHandler = 0x0001,
81 StdoutHandler = 0x0002,
82 StderrHandler = 0x0004,
84 DebugHandler = 0x0010,
85 EventHandler = 0x0020,
86 MethodHandler = 0x0040
89 typedef struct _EventInfo
98 typedef struct _HandlerInfo
145 typedef struct _LogMapInfo
161 static const HandlerInfo
164 { "Console", ConsoleHandler },
165 { "Debug", DebugHandler },
166 { "Event", EventHandler },
167 { "File", FileHandler },
168 { "None", NoHandler },
169 { "Stderr", StderrHandler },
170 { "Stdout", StdoutHandler },
171 { (char *) NULL, UndefinedHandler },
172 { (char *) NULL, UndefinedHandler },
173 { (char *) NULL, UndefinedHandler },
174 { (char *) NULL, UndefinedHandler },
175 { (char *) NULL, UndefinedHandler },
176 { (char *) NULL, UndefinedHandler },
177 { (char *) NULL, UndefinedHandler },
178 { (char *) NULL, UndefinedHandler },
179 { (char *) NULL, UndefinedHandler },
180 { (char *) NULL, UndefinedHandler },
181 { (char *) NULL, UndefinedHandler },
182 { (char *) NULL, UndefinedHandler },
183 { (char *) NULL, UndefinedHandler },
184 { (char *) NULL, UndefinedHandler },
185 { (char *) NULL, UndefinedHandler },
186 { (char *) NULL, UndefinedHandler },
187 { (char *) NULL, UndefinedHandler },
188 { (char *) NULL, UndefinedHandler },
189 { (char *) NULL, UndefinedHandler },
190 { (char *) NULL, UndefinedHandler },
191 { (char *) NULL, UndefinedHandler },
192 { (char *) NULL, UndefinedHandler },
193 { (char *) NULL, UndefinedHandler },
194 { (char *) NULL, UndefinedHandler },
195 { (char *) NULL, UndefinedHandler }
198 static const LogMapInfo
201 { NoEvents, ConsoleHandler, "Magick-%g.log",
202 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n %e" }
206 log_name[MaxTextExtent] = "Magick";
208 static LinkedListInfo
209 *log_cache = (LinkedListInfo *) NULL;
212 *event_semaphore = (SemaphoreInfo *) NULL,
213 *log_semaphore = (SemaphoreInfo *) NULL;
216 Forward declarations.
218 static LogHandlerType
219 ParseLogHandlers(const char *);
222 *GetLogInfo(const char *,ExceptionInfo *);
224 static MagickBooleanType
225 IsLogCacheInstantiated(ExceptionInfo *),
226 LoadLogCache(LinkedListInfo *,const char *,const char *,const size_t,
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % A c q u i r e L o g C a c h e %
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % AcquireLogCache() caches one or more log configurations which provides a
241 % mapping between log attributes and log name.
243 % The format of the AcquireLogCache method is:
245 % LinkedListInfo *AcquireLogCache(const char *filename,
246 % ExceptionInfo *exception)
248 % A description of each parameter follows:
250 % o filename: the log configuration filename.
252 % o exception: return any errors or warnings in this structure.
255 static LinkedListInfo *AcquireLogCache(const char *filename,
256 ExceptionInfo *exception)
272 Load external log map.
274 log_cache=NewLinkedList(0);
275 if (log_cache == (LinkedListInfo *) NULL)
276 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
278 options=GetConfigureOptions(filename,exception);
279 option=(const StringInfo *) GetNextValueInLinkedList(options);
280 while (option != (const StringInfo *) NULL)
282 status&=LoadLogCache(log_cache,(const char *) GetStringInfoDatum(option),
283 GetStringInfoPath(option),0,exception);
284 option=(const StringInfo *) GetNextValueInLinkedList(options);
286 options=DestroyConfigureOptions(options);
288 Load built-in log map.
290 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
295 register const LogMapInfo
299 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
300 if (log_info == (LogInfo *) NULL)
302 (void) ThrowMagickException(exception,GetMagickModule(),
303 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->filename);
306 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
307 log_info->path=ConstantString("[built-in]");
308 GetTimerInfo((TimerInfo *) &log_info->timer);
309 log_info->event_mask=p->event_mask;
310 log_info->handler_mask=p->handler_mask;
311 log_info->filename=ConstantString(p->filename);
312 log_info->format=ConstantString(p->format);
313 log_info->signature=MagickSignature;
314 status&=AppendValueToLinkedList(log_cache,log_info);
315 if (status == MagickFalse)
316 (void) ThrowMagickException(exception,GetMagickModule(),
317 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % C l o s e M a g i c k L o g %
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 % CloseMagickLog() closes the Magick log.
335 % The format of the CloseMagickLog method is:
337 % CloseMagickLog(void)
340 MagickExport void CloseMagickLog(void)
348 if (IsEventLogging() == MagickFalse)
350 exception=AcquireExceptionInfo();
351 log_info=GetLogInfo("*",exception);
352 exception=DestroyExceptionInfo(exception);
353 LockSemaphoreInfo(log_semaphore);
354 if (log_info->file != (FILE *) NULL)
356 (void) FormatLocaleFile(log_info->file,"</log>\n");
357 (void) fclose(log_info->file);
358 log_info->file=(FILE *) NULL;
360 UnlockSemaphoreInfo(log_semaphore);
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368 + G e t L o g I n f o %
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 % GetLogInfo() searches the log list for the specified name and if found
375 % returns attributes for that log.
377 % The format of the GetLogInfo method is:
379 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
381 % A description of each parameter follows:
383 % o name: the log name.
385 % o exception: return any errors or warnings in this structure.
388 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
393 assert(exception != (ExceptionInfo *) NULL);
394 if (IsLogCacheInstantiated(exception) == MagickFalse)
395 return((LogInfo *) NULL);
399 LockSemaphoreInfo(log_semaphore);
400 ResetLinkedListIterator(log_cache);
401 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
402 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
404 UnlockSemaphoreInfo(log_semaphore);
407 while (p != (LogInfo *) NULL)
409 if (LocaleCompare(name,p->name) == 0)
411 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
413 if (p != (LogInfo *) NULL)
414 (void) InsertValueInLinkedList(log_cache,0,
415 RemoveElementByValueFromLinkedList(log_cache,p));
416 UnlockSemaphoreInfo(log_semaphore);
421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 % G e t L o g I n f o L i s t %
429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % GetLogInfoList() returns any logs that match the specified pattern.
433 % The format of the GetLogInfoList function is:
435 % const LogInfo **GetLogInfoList(const char *pattern,
436 % size_t *number_preferences,ExceptionInfo *exception)
438 % A description of each parameter follows:
440 % o pattern: Specifies a pointer to a text string containing a pattern.
442 % o number_preferences: This integer returns the number of logs in the list.
444 % o exception: return any errors or warnings in this structure.
447 #if defined(__cplusplus) || defined(c_plusplus)
451 static int LogInfoCompare(const void *x,const void *y)
457 p=(const LogInfo **) x,
458 q=(const LogInfo **) y;
459 if (LocaleCompare((*p)->path,(*q)->path) == 0)
460 return(LocaleCompare((*p)->name,(*q)->name));
461 return(LocaleCompare((*p)->path,(*q)->path));
464 #if defined(__cplusplus) || defined(c_plusplus)
468 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
469 size_t *number_preferences,ExceptionInfo *exception)
474 register const LogInfo
483 assert(pattern != (char *) NULL);
484 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
485 assert(number_preferences != (size_t *) NULL);
486 *number_preferences=0;
487 p=GetLogInfo("*",exception);
488 if (p == (const LogInfo *) NULL)
489 return((const LogInfo **) NULL);
490 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
491 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
492 if (preferences == (const LogInfo **) NULL)
493 return((const LogInfo **) NULL);
497 LockSemaphoreInfo(log_semaphore);
498 ResetLinkedListIterator(log_cache);
499 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
500 for (i=0; p != (const LogInfo *) NULL; )
502 if ((p->stealth == MagickFalse) &&
503 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
505 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
507 UnlockSemaphoreInfo(log_semaphore);
508 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
509 preferences[i]=(LogInfo *) NULL;
510 *number_preferences=(size_t) i;
515 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % G e t L o g L i s t %
523 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525 % GetLogList() returns any logs that match the specified pattern.
527 % The format of the GetLogList function is:
529 % char **GetLogList(const char *pattern,size_t *number_preferences,
530 % ExceptionInfo *exception)
532 % A description of each parameter follows:
534 % o pattern: Specifies a pointer to a text string containing a pattern.
536 % o number_preferences: This integer returns the number of logs in the list.
538 % o exception: return any errors or warnings in this structure.
542 #if defined(__cplusplus) || defined(c_plusplus)
546 static int LogCompare(const void *x,const void *y)
554 return(LocaleCompare(*p,*q));
557 #if defined(__cplusplus) || defined(c_plusplus)
561 MagickExport char **GetLogList(const char *pattern,
562 size_t *number_preferences,ExceptionInfo *exception)
567 register const LogInfo
576 assert(pattern != (char *) NULL);
577 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
578 assert(number_preferences != (size_t *) NULL);
579 *number_preferences=0;
580 p=GetLogInfo("*",exception);
581 if (p == (const LogInfo *) NULL)
582 return((char **) NULL);
583 preferences=(char **) AcquireQuantumMemory((size_t)
584 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
585 if (preferences == (char **) NULL)
586 return((char **) NULL);
590 LockSemaphoreInfo(log_semaphore);
591 ResetLinkedListIterator(log_cache);
592 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
593 for (i=0; p != (const LogInfo *) NULL; )
595 if ((p->stealth == MagickFalse) &&
596 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
597 preferences[i++]=ConstantString(p->name);
598 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
600 UnlockSemaphoreInfo(log_semaphore);
601 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
602 preferences[i]=(char *) NULL;
603 *number_preferences=(size_t) i;
608 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
612 % G e t L o g N a m e %
616 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618 % GetLogName() returns the current log name.
620 % The format of the GetLogName method is:
622 % const char *GetLogName(void)
625 MagickExport const char *GetLogName(void)
631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 + I s L o g C a c h e I n s t a n t i a t e d %
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 % IsLogCacheInstantiated() determines if the log list is instantiated. If
642 % not, it instantiates the list and returns it.
644 % The format of the IsLogInstantiated method is:
646 % MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
648 % A description of each parameter follows.
650 % o exception: return any errors or warnings in this structure.
653 static MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
655 if (log_cache == (LinkedListInfo *) NULL)
657 if (log_semaphore == (SemaphoreInfo *) NULL)
658 ActivateSemaphoreInfo(&log_semaphore);
659 LockSemaphoreInfo(log_semaphore);
660 if (log_cache == (LinkedListInfo *) NULL)
661 log_cache=AcquireLogCache(LogFilename,exception);
662 UnlockSemaphoreInfo(log_semaphore);
664 return(log_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
672 % I s E v e n t L o g g i n g %
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
681 % The format of the IsEventLogging method is:
683 % MagickBooleanType IsEventLogging(void)
686 MagickExport MagickBooleanType IsEventLogging(void)
694 if ((log_cache == (LinkedListInfo *) NULL) ||
695 (IsLinkedListEmpty(log_cache) != MagickFalse))
697 exception=AcquireExceptionInfo();
698 log_info=GetLogInfo("*",exception);
699 exception=DestroyExceptionInfo(exception);
700 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707 % L i s t L o g I n f o %
711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % ListLogInfo() lists the log info to a file.
715 % The format of the ListLogInfo method is:
717 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
719 % A description of each parameter follows.
721 % o file: An pointer to a FILE.
723 % o exception: return any errors or warnings in this structure.
726 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
728 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
745 if (file == (const FILE *) NULL)
747 log_info=GetLogInfoList("*",&number_aliases,exception);
748 if (log_info == (const LogInfo **) NULL)
751 path=(const char *) NULL;
752 for (i=0; i < (ssize_t) number_aliases; i++)
754 if (log_info[i]->stealth != MagickFalse)
756 if ((path == (const char *) NULL) ||
757 (LocaleCompare(path,log_info[i]->path) != 0))
762 if (log_info[i]->path != (char *) NULL)
763 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
765 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
770 if (LogHandlers[j].name == (const char *) NULL)
774 if ((log_info[i]->handler_mask & mask) != 0)
776 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
777 length+=strlen(LogHandlers[j].name);
780 for (j=(ssize_t) length; j <= 12; j++)
781 (void) FormatLocaleFile(file," ");
782 (void) FormatLocaleFile(file," Generations Limit Format\n");
783 (void) FormatLocaleFile(file,"-----------------------------------------"
784 "--------------------------------------\n");
786 path=log_info[i]->path;
787 if (log_info[i]->filename != (char *) NULL)
789 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
790 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
791 (void) FormatLocaleFile(file," ");
793 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
794 (void) FormatLocaleFile(file,"%8g ",(double) log_info[i]->limit);
795 if (log_info[i]->format != (char *) NULL)
796 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
797 (void) FormatLocaleFile(file,"\n");
800 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 + L o g C o m p o n e n t G e n e s i s %
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 % LogComponentGenesis() instantiates the log component.
817 % The format of the LogComponentGenesis method is:
819 % MagickBooleanType LogComponentGenesis(void)
822 MagickPrivate MagickBooleanType LogComponentGenesis(void)
827 if (log_semaphore == (SemaphoreInfo *) NULL)
828 log_semaphore=AcquireSemaphoreInfo();
829 exception=AcquireExceptionInfo();
830 (void) GetLogInfo("*",exception);
831 exception=DestroyExceptionInfo(exception);
832 event_semaphore=AcquireSemaphoreInfo();
837 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841 + L o g C o m p o n e n t T e r m i n u s %
845 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847 % LogComponentTerminus() destroys the logging component.
849 % The format of the LogComponentTerminus method is:
851 % LogComponentTerminus(void)
855 static void *DestroyLogElement(void *log_info)
860 p=(LogInfo *) log_info;
861 if (p->file != (FILE *) NULL)
863 (void) FormatLocaleFile(p->file,"</log>\n");
864 (void) fclose(p->file);
865 p->file=(FILE *) NULL;
867 if (p->format != (char *) NULL)
868 p->format=DestroyString(p->format);
869 if (p->path != (char *) NULL)
870 p->path=DestroyString(p->path);
871 if (p->filename != (char *) NULL)
872 p->filename=DestroyString(p->filename);
873 p=(LogInfo *) RelinquishMagickMemory(p);
874 return((void *) NULL);
877 MagickPrivate void LogComponentTerminus(void)
879 if (event_semaphore == (SemaphoreInfo *) NULL)
880 ActivateSemaphoreInfo(&event_semaphore);
881 LockSemaphoreInfo(event_semaphore);
882 UnlockSemaphoreInfo(event_semaphore);
883 RelinquishSemaphoreInfo(&event_semaphore);
884 if (log_semaphore == (SemaphoreInfo *) NULL)
885 ActivateSemaphoreInfo(&log_semaphore);
886 LockSemaphoreInfo(log_semaphore);
887 if (log_cache != (LinkedListInfo *) NULL)
888 log_cache=DestroyLinkedList(log_cache,DestroyLogElement);
889 UnlockSemaphoreInfo(log_semaphore);
890 RelinquishSemaphoreInfo(&log_semaphore);
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % L o g M a g i c k E v e n t %
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 % LogMagickEvent() logs an event as determined by the log configuration file.
905 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
907 % The format of the LogMagickEvent method is:
909 % MagickBooleanType LogMagickEvent(const LogEventType type,
910 % const char *module,const char *function,const size_t line,
911 % const char *format,...)
913 % A description of each parameter follows:
915 % o type: the event type.
917 % o filename: the source module filename.
919 % o function: the function name.
921 % o line: the line number of the source module.
923 % o format: the output format.
926 static char *TranslateEvent(const LogEventType magick_unused(type),
927 const char *module,const char *function,const size_t line,
928 const char *domain,const char *event)
955 exception=AcquireExceptionInfo();
956 log_info=(LogInfo *) GetLogInfo("*",exception);
957 exception=DestroyExceptionInfo(exception);
958 seconds=time((time_t *) NULL);
959 elapsed_time=GetElapsedTime(&log_info->timer);
960 user_time=GetUserTime(&log_info->timer);
961 text=AcquireString(event);
962 if (log_info->format == (char *) NULL)
964 extent=strlen(event)+MaxTextExtent;
965 if (LocaleCompare(log_info->format,"xml") == 0)
968 timestamp[MaxTextExtent];
971 Translate event in "XML" format.
973 (void) FormatMagickTime(seconds,extent,timestamp);
974 (void) FormatLocaleString(text,extent,
976 " <timestamp>%s</timestamp>\n"
977 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
978 " <user-time>%0.3f</user-time>\n"
979 " <process-id>%.20g</process-id>\n"
980 " <thread-id>%.20g</thread-id>\n"
981 " <module>%s</module>\n"
982 " <function>%s</function>\n"
983 " <line>%.20g</line>\n"
984 " <domain>%s</domain>\n"
985 " <event>%s</event>\n"
986 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
987 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
988 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
989 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
990 (double) line,domain,event);
994 Translate event in "human readable" format.
997 for (p=log_info->format; *p != '\0'; p++)
1000 if ((size_t) (q-text+MaxTextExtent) >= extent)
1002 extent+=MaxTextExtent;
1003 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
1005 if (text == (char *) NULL)
1006 return((char *) NULL);
1007 q=text+strlen(text);
1010 The format of the log is defined by embedding special format characters:
1029 if ((*p == '\\') && (*(p+1) == 'r'))
1035 if ((*p == '\\') && (*(p+1) == 'n'))
1051 q+=CopyMagickString(q,GetClientName(),extent);
1056 q+=CopyMagickString(q,domain,extent);
1061 q+=CopyMagickString(q,event,extent);
1066 q+=CopyMagickString(q,function,extent);
1071 if (log_info->generations == 0)
1073 (void) CopyMagickString(q,"0",extent);
1077 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1078 log_info->generations));
1083 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
1091 for (p=module+strlen(module)-1; p > module; p--)
1092 if (*p == *DirectorySeparator)
1097 q+=CopyMagickString(q,p,extent);
1102 q+=CopyMagickString(q,GetLogName(),extent);
1107 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1112 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
1113 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
1114 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
1119 q+=FormatMagickTime(seconds,extent,q);
1124 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
1129 q+=CopyMagickString(q,MagickLibVersionText,extent);
1149 static char *TranslateFilename(const LogInfo *log_info)
1164 Translate event in "human readable" format.
1166 assert(log_info != (LogInfo *) NULL);
1167 assert(log_info->filename != (char *) NULL);
1168 filename=AcquireString((char *) NULL);
1169 extent=MaxTextExtent;
1171 for (p=log_info->filename; *p != '\0'; p++)
1174 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1176 extent+=MaxTextExtent;
1177 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1179 if (filename == (char *) NULL)
1180 return((char *) NULL);
1181 q=filename+strlen(filename);
1184 The format of the filename is defined by embedding special format
1203 q+=CopyMagickString(q,GetClientName(),extent);
1208 if (log_info->generations == 0)
1210 (void) CopyMagickString(q,"0",extent);
1214 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1215 log_info->generations));
1220 q+=CopyMagickString(q,GetLogName(),extent);
1225 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1230 q+=CopyMagickString(q,MagickLibVersionText,extent);
1250 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1251 const char *function,const size_t line,const char *format,
1255 event[MaxTextExtent],
1270 if (IsEventLogging() == MagickFalse)
1271 return(MagickFalse);
1272 exception=AcquireExceptionInfo();
1273 log_info=(LogInfo *) GetLogInfo("*",exception);
1274 exception=DestroyExceptionInfo(exception);
1275 if (event_semaphore == (SemaphoreInfo *) NULL)
1276 ActivateSemaphoreInfo(&event_semaphore);
1277 LockSemaphoreInfo(event_semaphore);
1278 if ((log_info->event_mask & type) == 0)
1280 UnlockSemaphoreInfo(event_semaphore);
1283 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1284 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1285 n=vsnprintf(event,MaxTextExtent,format,operands);
1287 n=vsprintf(event,format,operands);
1290 event[MaxTextExtent-1]='\0';
1291 text=TranslateEvent(type,module,function,line,domain,event);
1292 if (text == (char *) NULL)
1294 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1295 UnlockSemaphoreInfo(event_semaphore);
1296 return(MagickFalse);
1298 if ((log_info->handler_mask & ConsoleHandler) != 0)
1300 (void) FormatLocaleFile(stderr,"%s\n",text);
1301 (void) fflush(stderr);
1303 if ((log_info->handler_mask & DebugHandler) != 0)
1305 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1306 OutputDebugString(text);
1307 OutputDebugString("\n");
1310 if ((log_info->handler_mask & EventHandler) != 0)
1312 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1313 (void) NTReportEvent(text,MagickFalse);
1316 if ((log_info->handler_mask & FileHandler) != 0)
1321 file_info.st_size=0;
1322 if (log_info->file != (FILE *) NULL)
1323 (void) fstat(fileno(log_info->file),&file_info);
1324 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
1326 (void) FormatLocaleFile(log_info->file,"</log>\n");
1327 (void) fclose(log_info->file);
1328 log_info->file=(FILE *) NULL;
1330 if (log_info->file == (FILE *) NULL)
1335 filename=TranslateFilename(log_info);
1336 if (filename == (char *) NULL)
1338 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1339 UnlockSemaphoreInfo(event_semaphore);
1340 return(MagickFalse);
1342 log_info->append=IsPathAccessible(filename);
1343 log_info->file=fopen_utf8(filename,"ab");
1344 filename=(char *) RelinquishMagickMemory(filename);
1345 if (log_info->file == (FILE *) NULL)
1347 UnlockSemaphoreInfo(event_semaphore);
1348 return(MagickFalse);
1350 log_info->generation++;
1351 if (log_info->append == MagickFalse)
1352 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1353 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1354 (void) FormatLocaleFile(log_info->file,"<log>\n");
1356 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
1357 (void) fflush(log_info->file);
1359 if ((log_info->handler_mask & MethodHandler) != 0)
1361 if (log_info->method != (MagickLogMethod) NULL)
1362 log_info->method(type,text);
1364 if ((log_info->handler_mask & StdoutHandler) != 0)
1366 (void) FormatLocaleFile(stdout,"%s\n",text);
1367 (void) fflush(stdout);
1369 if ((log_info->handler_mask & StderrHandler) != 0)
1371 (void) FormatLocaleFile(stderr,"%s\n",text);
1372 (void) fflush(stderr);
1374 text=(char *) RelinquishMagickMemory(text);
1375 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1376 UnlockSemaphoreInfo(event_semaphore);
1380 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1381 const char *function,const size_t line,const char *format,...)
1389 va_start(operands,format);
1390 status=LogMagickEventList(type,module,function,line,format,operands);
1396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1400 + L o a d L o g L i s t %
1404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1406 % LoadLogCache() loads the log configurations which provides a
1407 % mapping between log attributes and log name.
1409 % The format of the LoadLogCache method is:
1411 % MagickBooleanType LoadLogCache(LinkedListInfo *log_cache,const char *xml,
1412 % const char *filename,const size_t depth,ExceptionInfo *exception)
1414 % A description of each parameter follows:
1416 % o xml: The log list in XML format.
1418 % o filename: The log list filename.
1420 % o depth: depth of <include /> statements.
1422 % o exception: return any errors or warnings in this structure.
1425 static MagickBooleanType LoadLogCache(LinkedListInfo *log_cache,const char *xml,
1426 const char *filename,const size_t depth,ExceptionInfo *exception)
1429 keyword[MaxTextExtent],
1436 *log_info = (LogInfo *) NULL;
1442 Load the log map file.
1444 if (xml == (const char *) NULL)
1445 return(MagickFalse);
1447 token=AcquireString((const char *) xml);
1448 for (q=(const char *) xml; *q != '\0'; )
1453 GetMagickToken(q,&q,token);
1456 (void) CopyMagickString(keyword,token,MaxTextExtent);
1457 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1462 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1463 GetMagickToken(q,&q,token);
1466 if (LocaleNCompare(keyword,"<!--",4) == 0)
1471 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1472 GetMagickToken(q,&q,token);
1475 if (LocaleCompare(keyword,"<include") == 0)
1480 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1482 (void) CopyMagickString(keyword,token,MaxTextExtent);
1483 GetMagickToken(q,&q,token);
1486 GetMagickToken(q,&q,token);
1487 if (LocaleCompare(keyword,"file") == 0)
1490 (void) ThrowMagickException(exception,GetMagickModule(),
1491 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1495 path[MaxTextExtent],
1498 GetPathComponent(filename,HeadPath,path);
1500 (void) ConcatenateMagickString(path,DirectorySeparator,
1502 if (*token == *DirectorySeparator)
1503 (void) CopyMagickString(path,token,MaxTextExtent);
1505 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1506 xml=FileToXML(path,~0UL);
1507 if (xml != (char *) NULL)
1509 status&=LoadLogCache(log_cache,xml,path,depth+1,
1511 xml=DestroyString(xml);
1518 if (LocaleCompare(keyword,"<logmap>") == 0)
1521 Allocate memory for the log list.
1523 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1524 if (log_info == (LogInfo *) NULL)
1525 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1526 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1527 log_info->path=ConstantString(filename);
1528 GetTimerInfo((TimerInfo *) &log_info->timer);
1529 log_info->signature=MagickSignature;
1532 if (log_info == (LogInfo *) NULL)
1534 if (LocaleCompare(keyword,"</logmap>") == 0)
1536 status=AppendValueToLinkedList(log_cache,log_info);
1537 if (status == MagickFalse)
1538 (void) ThrowMagickException(exception,GetMagickModule(),
1539 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1540 log_info=(LogInfo *) NULL;
1543 GetMagickToken(q,(const char **) NULL,token);
1546 GetMagickToken(q,&q,token);
1547 GetMagickToken(q,&q,token);
1553 if (LocaleCompare((char *) keyword,"events") == 0)
1555 log_info->event_mask=(LogEventType) (log_info->event_mask |
1556 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1564 if (LocaleCompare((char *) keyword,"filename") == 0)
1566 if (log_info->filename != (char *) NULL)
1567 log_info->filename=(char *)
1568 RelinquishMagickMemory(log_info->filename);
1569 log_info->filename=ConstantString(token);
1572 if (LocaleCompare((char *) keyword,"format") == 0)
1574 if (log_info->format != (char *) NULL)
1575 log_info->format=(char *)
1576 RelinquishMagickMemory(log_info->format);
1577 log_info->format=ConstantString(token);
1585 if (LocaleCompare((char *) keyword,"generations") == 0)
1587 if (LocaleCompare(token,"unlimited") == 0)
1589 log_info->generations=(~0UL);
1592 log_info->generations=StringToUnsignedLong(token);
1600 if (LocaleCompare((char *) keyword,"limit") == 0)
1602 if (LocaleCompare(token,"unlimited") == 0)
1604 log_info->limit=(~0UL);
1607 log_info->limit=StringToUnsignedLong(token);
1615 if (LocaleCompare((char *) keyword,"output") == 0)
1617 log_info->handler_mask=(LogHandlerType)
1618 (log_info->handler_mask | ParseLogHandlers(token));
1627 token=DestroyString(token);
1628 if (log_cache == (LinkedListInfo *) NULL)
1629 return(MagickFalse);
1630 return(status != 0 ? MagickTrue : MagickFalse);
1634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1638 + P a r s e L o g H a n d l e r s %
1642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1644 % ParseLogHandlers() parses a string defining which handlers takes a log
1645 % message and exports them.
1647 % The format of the ParseLogHandlers method is:
1649 % LogHandlerType ParseLogHandlers(const char *handlers)
1651 % A description of each parameter follows:
1653 % o handlers: one or more handlers separated by commas.
1656 static LogHandlerType ParseLogHandlers(const char *handlers)
1670 handler_mask=NoHandler;
1671 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1673 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1676 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1678 length=strlen(LogHandlers[i].name);
1679 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1681 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1685 if (LogHandlers[i].name == (char *) NULL)
1686 return(UndefinedHandler);
1688 return(handler_mask);
1692 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1696 % S e t L o g E v e n t M a s k %
1700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1702 % SetLogEventMask() accepts a list that determines which events to log. All
1703 % other events are ignored. By default, no debug is enabled. This method
1704 % returns the previous log event mask.
1706 % The format of the SetLogEventMask method is:
1708 % LogEventType SetLogEventMask(const char *events)
1710 % A description of each parameter follows:
1712 % o events: log these events.
1715 MagickExport LogEventType SetLogEventMask(const char *events)
1726 exception=AcquireExceptionInfo();
1727 log_info=(LogInfo *) GetLogInfo("*",exception);
1728 exception=DestroyExceptionInfo(exception);
1729 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1730 LockSemaphoreInfo(log_semaphore);
1731 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1732 log_info->event_mask=(LogEventType) option;
1734 log_info->event_mask=UndefinedEvents;
1735 UnlockSemaphoreInfo(log_semaphore);
1736 return(log_info->event_mask);
1740 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1744 % S e t L o g F o r m a t %
1748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1750 % SetLogFormat() sets the format for the "human readable" log record.
1752 % The format of the LogMagickFormat method is:
1754 % SetLogFormat(const char *format)
1756 % A description of each parameter follows:
1758 % o format: the log record format.
1761 MagickExport void SetLogFormat(const char *format)
1769 exception=AcquireExceptionInfo();
1770 log_info=(LogInfo *) GetLogInfo("*",exception);
1771 exception=DestroyExceptionInfo(exception);
1772 LockSemaphoreInfo(log_semaphore);
1773 if (log_info->format != (char *) NULL)
1774 log_info->format=DestroyString(log_info->format);
1775 log_info->format=ConstantString(format);
1776 UnlockSemaphoreInfo(log_semaphore);
1780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1784 % S e t L o g M e t h o d %
1788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1790 % SetLogMethod() sets the method that will be called when an event is logged.
1792 % The format of the SetLogMethod method is:
1794 % void SetLogMethod(MagickLogMethod method)
1796 % A description of each parameter follows:
1798 % o method: pointer to a method that will be called when LogMagickEvent is
1802 MagickExport void SetLogMethod(MagickLogMethod method)
1810 exception=AcquireExceptionInfo();
1811 log_info=(LogInfo *) GetLogInfo("*",exception);
1812 exception=DestroyExceptionInfo(exception);
1813 LockSemaphoreInfo(log_semaphore);
1814 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1815 log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1817 log_info->method=method;
1818 UnlockSemaphoreInfo(log_semaphore);
1822 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826 % S e t L o g N a m e %
1830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1832 % SetLogName() sets the log name and returns it.
1834 % The format of the SetLogName method is:
1836 % const char *SetLogName(const char *name)
1838 % A description of each parameter follows:
1840 % o log_name: SetLogName() returns the current client name.
1842 % o name: Specifies the new client name.
1845 MagickExport const char *SetLogName(const char *name)
1847 if ((name != (char *) NULL) && (*name != '\0'))
1848 (void) CopyMagickString(log_name,name,MaxTextExtent);