2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Log Events %
20 % Copyright 1999-2017 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 % https://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/linked-list.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[MagickPathExtent] = "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)
268 Load external log map.
270 cache=NewLinkedList(0);
271 if (cache == (LinkedListInfo *) NULL)
272 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
274 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
282 options=GetConfigureOptions(filename,exception);
283 option=(const StringInfo *) GetNextValueInLinkedList(options);
284 while (option != (const StringInfo *) NULL)
286 status&=LoadLogCache(cache,(const char *) GetStringInfoDatum(option),
287 GetStringInfoPath(option),0,exception);
288 option=(const StringInfo *) GetNextValueInLinkedList(options);
290 options=DestroyConfigureOptions(options);
294 Load built-in log map.
296 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
301 register const LogMapInfo
305 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
306 if (log_info == (LogInfo *) NULL)
308 (void) ThrowMagickException(exception,GetMagickModule(),
309 ResourceLimitError,"MemoryAllocationFailed","`%s'",p->filename);
312 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
313 log_info->path=ConstantString("[built-in]");
314 GetTimerInfo((TimerInfo *) &log_info->timer);
315 log_info->event_mask=p->event_mask;
316 log_info->handler_mask=p->handler_mask;
317 log_info->filename=ConstantString(p->filename);
318 log_info->format=ConstantString(p->format);
319 log_info->signature=MagickCoreSignature;
320 status&=AppendValueToLinkedList(cache,log_info);
321 if (status == MagickFalse)
322 (void) ThrowMagickException(exception,GetMagickModule(),
323 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333 % C l o s e M a g i c k L o g %
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339 % CloseMagickLog() closes the Magick log.
341 % The format of the CloseMagickLog method is:
343 % CloseMagickLog(void)
346 MagickExport void CloseMagickLog(void)
354 if (IsEventLogging() == MagickFalse)
356 exception=AcquireExceptionInfo();
357 log_info=GetLogInfo("*",exception);
358 exception=DestroyExceptionInfo(exception);
359 LockSemaphoreInfo(log_semaphore);
360 if (log_info->file != (FILE *) NULL)
362 (void) FormatLocaleFile(log_info->file,"</log>\n");
363 (void) fclose(log_info->file);
364 log_info->file=(FILE *) NULL;
366 UnlockSemaphoreInfo(log_semaphore);
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 + G e t L o g I n f o %
378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 % GetLogInfo() searches the log list for the specified name and if found
381 % returns attributes for that log.
383 % The format of the GetLogInfo method is:
385 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
387 % A description of each parameter follows:
389 % o name: the log name.
391 % o exception: return any errors or warnings in this structure.
394 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
399 assert(exception != (ExceptionInfo *) NULL);
400 if (IsLogCacheInstantiated(exception) == MagickFalse)
401 return((LogInfo *) NULL);
405 LockSemaphoreInfo(log_semaphore);
406 ResetLinkedListIterator(log_cache);
407 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
408 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
410 UnlockSemaphoreInfo(log_semaphore);
413 while (p != (LogInfo *) NULL)
415 if (LocaleCompare(name,p->name) == 0)
417 p=(LogInfo *) GetNextValueInLinkedList(log_cache);
419 if (p != (LogInfo *) NULL)
420 (void) InsertValueInLinkedList(log_cache,0,
421 RemoveElementByValueFromLinkedList(log_cache,p));
422 UnlockSemaphoreInfo(log_semaphore);
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % G e t L o g I n f o L i s t %
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 % GetLogInfoList() returns any logs that match the specified pattern.
439 % The format of the GetLogInfoList function is:
441 % const LogInfo **GetLogInfoList(const char *pattern,
442 % size_t *number_preferences,ExceptionInfo *exception)
444 % A description of each parameter follows:
446 % o pattern: Specifies a pointer to a text string containing a pattern.
448 % o number_preferences: This integer returns the number of logs in the list.
450 % o exception: return any errors or warnings in this structure.
453 #if defined(__cplusplus) || defined(c_plusplus)
457 static int LogInfoCompare(const void *x,const void *y)
463 p=(const LogInfo **) x,
464 q=(const LogInfo **) y;
465 if (LocaleCompare((*p)->path,(*q)->path) == 0)
466 return(LocaleCompare((*p)->name,(*q)->name));
467 return(LocaleCompare((*p)->path,(*q)->path));
470 #if defined(__cplusplus) || defined(c_plusplus)
474 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
475 size_t *number_preferences,ExceptionInfo *exception)
480 register const LogInfo
489 assert(pattern != (char *) NULL);
490 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
491 assert(number_preferences != (size_t *) NULL);
492 *number_preferences=0;
493 p=GetLogInfo("*",exception);
494 if (p == (const LogInfo *) NULL)
495 return((const LogInfo **) NULL);
496 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
497 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
498 if (preferences == (const LogInfo **) NULL)
499 return((const LogInfo **) NULL);
503 LockSemaphoreInfo(log_semaphore);
504 ResetLinkedListIterator(log_cache);
505 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
506 for (i=0; p != (const LogInfo *) NULL; )
508 if ((p->stealth == MagickFalse) &&
509 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
511 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
513 UnlockSemaphoreInfo(log_semaphore);
514 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
515 preferences[i]=(LogInfo *) NULL;
516 *number_preferences=(size_t) i;
521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525 % G e t L o g L i s t %
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
531 % GetLogList() returns any logs that match the specified pattern.
533 % The format of the GetLogList function is:
535 % char **GetLogList(const char *pattern,size_t *number_preferences,
536 % ExceptionInfo *exception)
538 % A description of each parameter follows:
540 % o pattern: Specifies a pointer to a text string containing a pattern.
542 % o number_preferences: This integer returns the number of logs in the list.
544 % o exception: return any errors or warnings in this structure.
548 #if defined(__cplusplus) || defined(c_plusplus)
552 static int LogCompare(const void *x,const void *y)
560 return(LocaleCompare(*p,*q));
563 #if defined(__cplusplus) || defined(c_plusplus)
567 MagickExport char **GetLogList(const char *pattern,size_t *number_preferences,
568 ExceptionInfo *exception)
573 register const LogInfo
582 assert(pattern != (char *) NULL);
583 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
584 assert(number_preferences != (size_t *) NULL);
585 *number_preferences=0;
586 p=GetLogInfo("*",exception);
587 if (p == (const LogInfo *) NULL)
588 return((char **) NULL);
589 preferences=(char **) AcquireQuantumMemory((size_t)
590 GetNumberOfElementsInLinkedList(log_cache)+1UL,sizeof(*preferences));
591 if (preferences == (char **) NULL)
592 return((char **) NULL);
596 LockSemaphoreInfo(log_semaphore);
597 ResetLinkedListIterator(log_cache);
598 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
599 for (i=0; p != (const LogInfo *) NULL; )
601 if ((p->stealth == MagickFalse) &&
602 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
603 preferences[i++]=ConstantString(p->name);
604 p=(const LogInfo *) GetNextValueInLinkedList(log_cache);
606 UnlockSemaphoreInfo(log_semaphore);
607 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
608 preferences[i]=(char *) NULL;
609 *number_preferences=(size_t) i;
614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618 % G e t L o g N a m e %
622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 % GetLogName() returns the current log name.
626 % The format of the GetLogName method is:
628 % const char *GetLogName(void)
631 MagickExport const char *GetLogName(void)
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 + I s L o g C a c h e I n s t a n t i a t e d %
645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
647 % IsLogCacheInstantiated() determines if the log list is instantiated. If
648 % not, it instantiates the list and returns it.
650 % The format of the IsLogInstantiated method is:
652 % MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
654 % A description of each parameter follows.
656 % o exception: return any errors or warnings in this structure.
659 static MagickBooleanType IsLogCacheInstantiated(ExceptionInfo *exception)
661 if (log_cache == (LinkedListInfo *) NULL)
663 if (log_semaphore == (SemaphoreInfo *) NULL)
664 ActivateSemaphoreInfo(&log_semaphore);
665 LockSemaphoreInfo(log_semaphore);
666 if (log_cache == (LinkedListInfo *) NULL)
667 log_cache=AcquireLogCache(LogFilename,exception);
668 UnlockSemaphoreInfo(log_semaphore);
670 return(log_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 % I s E v e n t L o g g i n g %
682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
687 % The format of the IsEventLogging method is:
689 % MagickBooleanType IsEventLogging(void)
692 MagickExport MagickBooleanType IsEventLogging(void)
700 if ((log_cache == (LinkedListInfo *) NULL) ||
701 (IsLinkedListEmpty(log_cache) != MagickFalse))
703 exception=AcquireExceptionInfo();
704 log_info=GetLogInfo("*",exception);
705 exception=DestroyExceptionInfo(exception);
706 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % L i s t L o g I n f o %
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 % ListLogInfo() lists the log info to a file.
721 % The format of the ListLogInfo method is:
723 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
725 % A description of each parameter follows.
727 % o file: An pointer to a FILE.
729 % o exception: return any errors or warnings in this structure.
732 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
734 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
751 if (file == (const FILE *) NULL)
753 log_info=GetLogInfoList("*",&number_aliases,exception);
754 if (log_info == (const LogInfo **) NULL)
757 path=(const char *) NULL;
758 for (i=0; i < (ssize_t) number_aliases; i++)
760 if (log_info[i]->stealth != MagickFalse)
762 if ((path == (const char *) NULL) ||
763 (LocaleCompare(path,log_info[i]->path) != 0))
768 if (log_info[i]->path != (char *) NULL)
769 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
771 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
776 if (LogHandlers[j].name == (const char *) NULL)
780 if ((log_info[i]->handler_mask & mask) != 0)
782 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
783 length+=strlen(LogHandlers[j].name);
786 for (j=(ssize_t) length; j <= 12; j++)
787 (void) FormatLocaleFile(file," ");
788 (void) FormatLocaleFile(file," Generations Limit Format\n");
789 (void) FormatLocaleFile(file,"-----------------------------------------"
790 "--------------------------------------\n");
792 path=log_info[i]->path;
793 if (log_info[i]->filename != (char *) NULL)
795 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
796 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
797 (void) FormatLocaleFile(file," ");
799 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
800 (void) FormatLocaleFile(file,"%8g ",(double) log_info[i]->limit);
801 if (log_info[i]->format != (char *) NULL)
802 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
803 (void) FormatLocaleFile(file,"\n");
806 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
811 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 + L o g C o m p o n e n t G e n e s i s %
819 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821 % LogComponentGenesis() instantiates the log component.
823 % The format of the LogComponentGenesis method is:
825 % MagickBooleanType LogComponentGenesis(void)
828 MagickPrivate MagickBooleanType LogComponentGenesis(void)
833 if (log_semaphore == (SemaphoreInfo *) NULL)
834 log_semaphore=AcquireSemaphoreInfo();
835 exception=AcquireExceptionInfo();
836 (void) GetLogInfo("*",exception);
837 exception=DestroyExceptionInfo(exception);
838 event_semaphore=AcquireSemaphoreInfo();
843 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
847 + L o g C o m p o n e n t T e r m i n u s %
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
853 % LogComponentTerminus() destroys the logging component.
855 % The format of the LogComponentTerminus method is:
857 % LogComponentTerminus(void)
861 static void *DestroyLogElement(void *log_info)
866 p=(LogInfo *) log_info;
867 if (p->file != (FILE *) NULL)
869 (void) FormatLocaleFile(p->file,"</log>\n");
870 (void) fclose(p->file);
871 p->file=(FILE *) NULL;
873 if (p->format != (char *) NULL)
874 p->format=DestroyString(p->format);
875 if (p->path != (char *) NULL)
876 p->path=DestroyString(p->path);
877 if (p->filename != (char *) NULL)
878 p->filename=DestroyString(p->filename);
879 p=(LogInfo *) RelinquishMagickMemory(p);
880 return((void *) NULL);
883 MagickPrivate void LogComponentTerminus(void)
885 if (event_semaphore == (SemaphoreInfo *) NULL)
886 ActivateSemaphoreInfo(&event_semaphore);
887 LockSemaphoreInfo(event_semaphore);
888 UnlockSemaphoreInfo(event_semaphore);
889 RelinquishSemaphoreInfo(&event_semaphore);
890 if (log_semaphore == (SemaphoreInfo *) NULL)
891 ActivateSemaphoreInfo(&log_semaphore);
892 LockSemaphoreInfo(log_semaphore);
893 if (log_cache != (LinkedListInfo *) NULL)
894 log_cache=DestroyLinkedList(log_cache,DestroyLogElement);
895 UnlockSemaphoreInfo(log_semaphore);
896 RelinquishSemaphoreInfo(&log_semaphore);
900 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 % L o g M a g i c k E v e n t %
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 % LogMagickEvent() logs an event as determined by the log configuration file.
911 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
913 % The format of the LogMagickEvent method is:
915 % MagickBooleanType LogMagickEvent(const LogEventType type,
916 % const char *module,const char *function,const size_t line,
917 % const char *format,...)
919 % A description of each parameter follows:
921 % o type: the event type.
923 % o filename: the source module filename.
925 % o function: the function name.
927 % o line: the line number of the source module.
929 % o format: the output format.
932 static char *TranslateEvent(const char *module,const char *function,
933 const size_t line,const char *domain,const char *event)
960 exception=AcquireExceptionInfo();
961 log_info=(LogInfo *) GetLogInfo("*",exception);
962 exception=DestroyExceptionInfo(exception);
963 seconds=time((time_t *) NULL);
964 elapsed_time=GetElapsedTime(&log_info->timer);
965 user_time=GetUserTime(&log_info->timer);
966 text=AcquireString(event);
967 if (log_info->format == (char *) NULL)
969 extent=strlen(event)+MagickPathExtent;
970 if (LocaleCompare(log_info->format,"xml") == 0)
973 timestamp[MagickPathExtent];
976 Translate event in "XML" format.
978 (void) FormatMagickTime(seconds,extent,timestamp);
979 (void) FormatLocaleString(text,extent,
981 " <timestamp>%s</timestamp>\n"
982 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
983 " <user-time>%0.3f</user-time>\n"
984 " <process-id>%.20g</process-id>\n"
985 " <thread-id>%.20g</thread-id>\n"
986 " <module>%s</module>\n"
987 " <function>%s</function>\n"
988 " <line>%.20g</line>\n"
989 " <domain>%s</domain>\n"
990 " <event>%s</event>\n"
991 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
992 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
993 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
994 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
995 (double) line,domain,event);
999 Translate event in "human readable" format.
1002 for (p=log_info->format; *p != '\0'; p++)
1005 if ((size_t) (q-text+MagickPathExtent) >= extent)
1007 extent+=MagickPathExtent;
1008 text=(char *) ResizeQuantumMemory(text,extent+MagickPathExtent,
1010 if (text == (char *) NULL)
1011 return((char *) NULL);
1012 q=text+strlen(text);
1015 The format of the log is defined by embedding special format characters:
1034 if ((*p == '\\') && (*(p+1) == 'r'))
1040 if ((*p == '\\') && (*(p+1) == 'n'))
1056 q+=CopyMagickString(q,GetClientName(),extent);
1061 q+=CopyMagickString(q,domain,extent);
1066 q+=CopyMagickString(q,event,extent);
1071 q+=CopyMagickString(q,function,extent);
1076 if (log_info->generations == 0)
1078 (void) CopyMagickString(q,"0",extent);
1082 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1083 log_info->generations));
1088 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
1096 for (p=module+strlen(module)-1; p > module; p--)
1097 if (*p == *DirectorySeparator)
1102 q+=CopyMagickString(q,p,extent);
1107 q+=CopyMagickString(q,GetLogName(),extent);
1112 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1117 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
1118 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
1119 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
1124 q+=FormatMagickTime(seconds,extent,q);
1129 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
1134 q+=CopyMagickString(q,MagickLibVersionText,extent);
1154 static char *TranslateFilename(const LogInfo *log_info)
1169 Translate event in "human readable" format.
1171 assert(log_info != (LogInfo *) NULL);
1172 assert(log_info->filename != (char *) NULL);
1173 filename=AcquireString((char *) NULL);
1174 extent=MagickPathExtent;
1176 for (p=log_info->filename; *p != '\0'; p++)
1179 if ((size_t) (q-filename+MagickPathExtent) >= extent)
1181 extent+=MagickPathExtent;
1182 filename=(char *) ResizeQuantumMemory(filename,extent+MagickPathExtent,
1184 if (filename == (char *) NULL)
1185 return((char *) NULL);
1186 q=filename+strlen(filename);
1189 The format of the filename is defined by embedding special format
1208 q+=CopyMagickString(q,GetClientName(),extent);
1213 if (log_info->generations == 0)
1215 (void) CopyMagickString(q,"0",extent);
1219 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
1220 log_info->generations));
1225 q+=CopyMagickString(q,GetLogName(),extent);
1230 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
1235 q+=CopyMagickString(q,MagickLibVersionText,extent);
1255 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1256 const char *function,const size_t line,const char *format,va_list operands)
1259 event[MagickPathExtent],
1274 if (IsEventLogging() == MagickFalse)
1275 return(MagickFalse);
1276 exception=AcquireExceptionInfo();
1277 log_info=(LogInfo *) GetLogInfo("*",exception);
1278 exception=DestroyExceptionInfo(exception);
1279 if (event_semaphore == (SemaphoreInfo *) NULL)
1280 ActivateSemaphoreInfo(&event_semaphore);
1281 LockSemaphoreInfo(event_semaphore);
1282 if ((log_info->event_mask & type) == 0)
1284 UnlockSemaphoreInfo(event_semaphore);
1287 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
1288 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1289 n=vsnprintf(event,MagickPathExtent,format,operands);
1291 n=vsprintf(event,format,operands);
1294 event[MagickPathExtent-1]='\0';
1295 text=TranslateEvent(module,function,line,domain,event);
1296 if (text == (char *) NULL)
1298 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1299 UnlockSemaphoreInfo(event_semaphore);
1300 return(MagickFalse);
1302 if ((log_info->handler_mask & ConsoleHandler) != 0)
1304 (void) FormatLocaleFile(stderr,"%s\n",text);
1305 (void) fflush(stderr);
1307 if ((log_info->handler_mask & DebugHandler) != 0)
1309 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1310 OutputDebugString(text);
1311 OutputDebugString("\n");
1314 if ((log_info->handler_mask & EventHandler) != 0)
1316 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1317 (void) NTReportEvent(text,MagickFalse);
1320 if ((log_info->handler_mask & FileHandler) != 0)
1325 file_info.st_size=0;
1326 if (log_info->file != (FILE *) NULL)
1327 (void) fstat(fileno(log_info->file),&file_info);
1328 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
1330 (void) FormatLocaleFile(log_info->file,"</log>\n");
1331 (void) fclose(log_info->file);
1332 log_info->file=(FILE *) NULL;
1334 if (log_info->file == (FILE *) NULL)
1339 filename=TranslateFilename(log_info);
1340 if (filename == (char *) NULL)
1342 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1343 UnlockSemaphoreInfo(event_semaphore);
1344 return(MagickFalse);
1346 log_info->append=IsPathAccessible(filename);
1347 log_info->file=fopen_utf8(filename,"ab");
1348 filename=(char *) RelinquishMagickMemory(filename);
1349 if (log_info->file == (FILE *) NULL)
1351 UnlockSemaphoreInfo(event_semaphore);
1352 return(MagickFalse);
1354 log_info->generation++;
1355 if (log_info->append == MagickFalse)
1356 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1357 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1358 (void) FormatLocaleFile(log_info->file,"<log>\n");
1360 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
1361 (void) fflush(log_info->file);
1363 if ((log_info->handler_mask & MethodHandler) != 0)
1365 if (log_info->method != (MagickLogMethod) NULL)
1366 log_info->method(type,text);
1368 if ((log_info->handler_mask & StdoutHandler) != 0)
1370 (void) FormatLocaleFile(stdout,"%s\n",text);
1371 (void) fflush(stdout);
1373 if ((log_info->handler_mask & StderrHandler) != 0)
1375 (void) FormatLocaleFile(stderr,"%s\n",text);
1376 (void) fflush(stderr);
1378 text=(char *) RelinquishMagickMemory(text);
1379 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1380 UnlockSemaphoreInfo(event_semaphore);
1384 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1385 const char *function,const size_t line,const char *format,...)
1393 va_start(operands,format);
1394 status=LogMagickEventList(type,module,function,line,format,operands);
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 + L o a d L o g C a c h e %
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % LoadLogCache() loads the log configurations which provides a
1411 % mapping between log attributes and log name.
1413 % The format of the LoadLogCache method is:
1415 % MagickBooleanType LoadLogCache(LinkedListInfo *cache,const char *xml,
1416 % const char *filename,const size_t depth,ExceptionInfo *exception)
1418 % A description of each parameter follows:
1420 % o xml: The log list in XML format.
1422 % o filename: The log list filename.
1424 % o depth: depth of <include /> statements.
1426 % o exception: return any errors or warnings in this structure.
1429 static MagickBooleanType LoadLogCache(LinkedListInfo *cache,const char *xml,
1430 const char *filename,const size_t depth,ExceptionInfo *exception)
1433 keyword[MagickPathExtent],
1440 *log_info = (LogInfo *) NULL;
1449 Load the log map file.
1451 if (xml == (const char *) NULL)
1452 return(MagickFalse);
1454 token=AcquireString(xml);
1455 extent=strlen(token)+MagickPathExtent;
1456 for (q=(const char *) xml; *q != '\0'; )
1461 GetNextToken(q,&q,extent,token);
1464 (void) CopyMagickString(keyword,token,MagickPathExtent);
1465 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1470 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1471 GetNextToken(q,&q,extent,token);
1474 if (LocaleNCompare(keyword,"<!--",4) == 0)
1479 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1480 GetNextToken(q,&q,extent,token);
1483 if (LocaleCompare(keyword,"<include") == 0)
1488 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1490 (void) CopyMagickString(keyword,token,MagickPathExtent);
1491 GetNextToken(q,&q,extent,token);
1494 GetNextToken(q,&q,extent,token);
1495 if (LocaleCompare(keyword,"file") == 0)
1498 (void) ThrowMagickException(exception,GetMagickModule(),
1499 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1503 path[MagickPathExtent],
1506 GetPathComponent(filename,HeadPath,path);
1508 (void) ConcatenateMagickString(path,DirectorySeparator,
1510 if (*token == *DirectorySeparator)
1511 (void) CopyMagickString(path,token,MagickPathExtent);
1513 (void) ConcatenateMagickString(path,token,MagickPathExtent);
1514 file_xml=FileToXML(path,~0UL);
1515 if (file_xml != (char *) NULL)
1517 status&=LoadLogCache(cache,file_xml,path,depth+1,
1519 file_xml=DestroyString(file_xml);
1526 if (LocaleCompare(keyword,"<logmap>") == 0)
1529 Allocate memory for the log list.
1531 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1532 if (log_info == (LogInfo *) NULL)
1533 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1534 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1535 log_info->path=ConstantString(filename);
1536 GetTimerInfo((TimerInfo *) &log_info->timer);
1537 log_info->signature=MagickCoreSignature;
1540 if (log_info == (LogInfo *) NULL)
1542 if (LocaleCompare(keyword,"</logmap>") == 0)
1544 status=AppendValueToLinkedList(cache,log_info);
1545 if (status == MagickFalse)
1546 (void) ThrowMagickException(exception,GetMagickModule(),
1547 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1548 log_info=(LogInfo *) NULL;
1551 GetNextToken(q,(const char **) NULL,extent,token);
1554 GetNextToken(q,&q,extent,token);
1555 GetNextToken(q,&q,extent,token);
1561 if (LocaleCompare((char *) keyword,"events") == 0)
1563 log_info->event_mask=(LogEventType) (log_info->event_mask |
1564 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
1572 if (LocaleCompare((char *) keyword,"filename") == 0)
1574 if (log_info->filename != (char *) NULL)
1575 log_info->filename=(char *)
1576 RelinquishMagickMemory(log_info->filename);
1577 log_info->filename=ConstantString(token);
1580 if (LocaleCompare((char *) keyword,"format") == 0)
1582 if (log_info->format != (char *) NULL)
1583 log_info->format=(char *)
1584 RelinquishMagickMemory(log_info->format);
1585 log_info->format=ConstantString(token);
1593 if (LocaleCompare((char *) keyword,"generations") == 0)
1595 if (LocaleCompare(token,"unlimited") == 0)
1597 log_info->generations=(~0UL);
1600 log_info->generations=StringToUnsignedLong(token);
1608 if (LocaleCompare((char *) keyword,"limit") == 0)
1610 if (LocaleCompare(token,"unlimited") == 0)
1612 log_info->limit=(~0UL);
1615 log_info->limit=StringToUnsignedLong(token);
1623 if (LocaleCompare((char *) keyword,"output") == 0)
1625 log_info->handler_mask=(LogHandlerType)
1626 (log_info->handler_mask | ParseLogHandlers(token));
1635 token=DestroyString(token);
1636 if (cache == (LinkedListInfo *) NULL)
1637 return(MagickFalse);
1638 return(status != 0 ? MagickTrue : MagickFalse);
1642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1646 + P a r s e L o g H a n d l e r s %
1650 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1652 % ParseLogHandlers() parses a string defining which handlers takes a log
1653 % message and exports them.
1655 % The format of the ParseLogHandlers method is:
1657 % LogHandlerType ParseLogHandlers(const char *handlers)
1659 % A description of each parameter follows:
1661 % o handlers: one or more handlers separated by commas.
1664 static LogHandlerType ParseLogHandlers(const char *handlers)
1678 handler_mask=NoHandler;
1679 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1681 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1684 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1686 length=strlen(LogHandlers[i].name);
1687 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1689 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1693 if (LogHandlers[i].name == (char *) NULL)
1694 return(UndefinedHandler);
1696 return(handler_mask);
1700 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1704 % S e t L o g E v e n t M a s k %
1708 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710 % SetLogEventMask() accepts a list that determines which events to log. All
1711 % other events are ignored. By default, no debug is enabled. This method
1712 % returns the previous log event mask.
1714 % The format of the SetLogEventMask method is:
1716 % LogEventType SetLogEventMask(const char *events)
1718 % A description of each parameter follows:
1720 % o events: log these events.
1723 MagickExport LogEventType SetLogEventMask(const char *events)
1734 exception=AcquireExceptionInfo();
1735 log_info=(LogInfo *) GetLogInfo("*",exception);
1736 exception=DestroyExceptionInfo(exception);
1737 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
1738 LockSemaphoreInfo(log_semaphore);
1739 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1740 log_info->event_mask=(LogEventType) option;
1742 log_info->event_mask=UndefinedEvents;
1743 UnlockSemaphoreInfo(log_semaphore);
1744 return(log_info->event_mask);
1748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1752 % S e t L o g F o r m a t %
1756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1758 % SetLogFormat() sets the format for the "human readable" log record.
1760 % The format of the LogMagickFormat method is:
1762 % SetLogFormat(const char *format)
1764 % A description of each parameter follows:
1766 % o format: the log record format.
1769 MagickExport void SetLogFormat(const char *format)
1777 exception=AcquireExceptionInfo();
1778 log_info=(LogInfo *) GetLogInfo("*",exception);
1779 exception=DestroyExceptionInfo(exception);
1780 LockSemaphoreInfo(log_semaphore);
1781 if (log_info->format != (char *) NULL)
1782 log_info->format=DestroyString(log_info->format);
1783 log_info->format=ConstantString(format);
1784 UnlockSemaphoreInfo(log_semaphore);
1788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1792 % S e t L o g M e t h o d %
1796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1798 % SetLogMethod() sets the method that will be called when an event is logged.
1800 % The format of the SetLogMethod method is:
1802 % void SetLogMethod(MagickLogMethod method)
1804 % A description of each parameter follows:
1806 % o method: pointer to a method that will be called when LogMagickEvent is
1810 MagickExport void SetLogMethod(MagickLogMethod method)
1818 exception=AcquireExceptionInfo();
1819 log_info=(LogInfo *) GetLogInfo("*",exception);
1820 exception=DestroyExceptionInfo(exception);
1821 LockSemaphoreInfo(log_semaphore);
1822 log_info=(LogInfo *) GetValueFromLinkedList(log_cache,0);
1823 log_info->handler_mask=(LogHandlerType) (log_info->handler_mask |
1825 log_info->method=method;
1826 UnlockSemaphoreInfo(log_semaphore);
1830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1834 % S e t L o g N a m e %
1838 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1840 % SetLogName() sets the log name and returns it.
1842 % The format of the SetLogName method is:
1844 % const char *SetLogName(const char *name)
1846 % A description of each parameter follows:
1848 % o log_name: SetLogName() returns the current client name.
1850 % o name: Specifies the new client name.
1853 MagickExport const char *SetLogName(const char *name)
1855 if ((name != (char *) NULL) && (*name != '\0'))
1856 (void) CopyMagickString(log_name,name,MagickPathExtent);