2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % MagickCore Log Events %
20 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/client.h"
45 #include "magick/configure.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/hashmap.h"
49 #include "magick/log.h"
50 #include "magick/memory_.h"
51 #include "magick/option.h"
52 #include "magick/semaphore.h"
53 #include "magick/timer.h"
54 #include "magick/string_.h"
55 #include "magick/string-private.h"
56 #include "magick/token.h"
57 #include "magick/thread_.h"
58 #include "magick/thread-private.h"
59 #include "magick/utility.h"
60 #include "magick/version.h"
61 #include "magick/xml-tree.h"
66 #define LogFilename "log.xml"
73 UndefinedHandler = 0x0000,
75 ConsoleHandler = 0x0001,
76 StdoutHandler = 0x0002,
77 StderrHandler = 0x0004,
79 DebugHandler = 0x0010,
83 typedef struct _EventInfo
92 typedef struct _HandlerInfo
137 typedef struct _LogMapInfo
153 static const HandlerInfo
156 { "console", ConsoleHandler },
157 { "debug", DebugHandler },
158 { "event", EventHandler },
159 { "file", FileHandler },
160 { "none", NoHandler },
161 { "stderr", StderrHandler },
162 { "stdout", StdoutHandler },
163 { (char *) NULL, UndefinedHandler }
166 static const LogMapInfo
169 { NoEvents, ConsoleHandler, "Magick-%d.log",
170 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\n %e" }
174 log_name[MaxTextExtent] = "Magick";
176 static LinkedListInfo
177 *log_list = (LinkedListInfo *) NULL;
180 *log_semaphore = (SemaphoreInfo *) NULL;
182 static volatile MagickBooleanType
183 instantiate_log = MagickFalse;
186 Forward declarations.
188 static LogHandlerType
189 ParseLogHandlers(const char *);
192 *GetLogInfo(const char *,ExceptionInfo *);
194 static MagickBooleanType
195 InitializeLogList(ExceptionInfo *),
196 LoadLogLists(const char *,ExceptionInfo *);
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203 % C l o s e M a g i c k L o g %
207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % CloseMagickLog() closes the Magick log.
211 % The format of the CloseMagickLog method is:
213 % CloseMagickLog(void)
216 MagickExport void CloseMagickLog(void)
224 if (IsEventLogging() == MagickFalse)
226 exception=AcquireExceptionInfo();
227 log_info=GetLogInfo("*",exception);
228 exception=DestroyExceptionInfo(exception);
229 LockSemaphoreInfo(log_semaphore);
230 if (log_info->file != (FILE *) NULL)
232 if (log_info->append == MagickFalse)
233 (void) fprintf(log_info->file,"</log>\n");
234 (void) fclose(log_info->file);
235 log_info->file=(FILE *) NULL;
237 UnlockSemaphoreInfo(log_semaphore);
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 + G e t L o g I n f o %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % GetLogInfo() searches the log list for the specified name and if found
252 % returns attributes for that log.
254 % The format of the GetLogInfo method is:
256 % LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
258 % A description of each parameter follows:
260 % o name: the log name.
262 % o exception: return any errors or warnings in this structure.
265 static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
270 assert(exception != (ExceptionInfo *) NULL);
271 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
272 if (InitializeLogList(exception) == MagickFalse)
273 return((LogInfo *) NULL);
274 if ((log_list == (LinkedListInfo *) NULL) ||
275 (IsLinkedListEmpty(log_list) != MagickFalse))
276 return((LogInfo *) NULL);
277 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
278 return((LogInfo *) GetValueFromLinkedList(log_list,0));
282 LockSemaphoreInfo(log_semaphore);
283 ResetLinkedListIterator(log_list);
284 p=(LogInfo *) GetNextValueInLinkedList(log_list);
285 while (p != (LogInfo *) NULL)
287 if (LocaleCompare(name,p->name) == 0)
289 p=(LogInfo *) GetNextValueInLinkedList(log_list);
291 if (p != (LogInfo *) NULL)
292 (void) InsertValueInLinkedList(log_list,0,
293 RemoveElementByValueFromLinkedList(log_list,p));
294 UnlockSemaphoreInfo(log_semaphore);
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303 % G e t L o g I n f o L i s t %
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % GetLogInfoList() returns any logs that match the specified pattern.
311 % The format of the GetLogInfoList function is:
313 % const LogInfo **GetLogInfoList(const char *pattern,
314 % unsigned long *number_preferences,ExceptionInfo *exception)
316 % A description of each parameter follows:
318 % o pattern: Specifies a pointer to a text string containing a pattern.
320 % o number_preferences: This integer returns the number of logs in the list.
322 % o exception: return any errors or warnings in this structure.
325 #if defined(__cplusplus) || defined(c_plusplus)
329 static int LogInfoCompare(const void *x,const void *y)
335 p=(const LogInfo **) x,
336 q=(const LogInfo **) y;
337 if (LocaleCompare((*p)->path,(*q)->path) == 0)
338 return(LocaleCompare((*p)->name,(*q)->name));
339 return(LocaleCompare((*p)->path,(*q)->path));
342 #if defined(__cplusplus) || defined(c_plusplus)
346 MagickExport const LogInfo **GetLogInfoList(const char *pattern,
347 unsigned long *number_preferences,ExceptionInfo *exception)
352 register const LogInfo
361 assert(pattern != (char *) NULL);
362 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
363 assert(number_preferences != (unsigned long *) NULL);
364 *number_preferences=0;
365 p=GetLogInfo("*",exception);
366 if (p == (const LogInfo *) NULL)
367 return((const LogInfo **) NULL);
368 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
369 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
370 if (preferences == (const LogInfo **) NULL)
371 return((const LogInfo **) NULL);
375 LockSemaphoreInfo(log_semaphore);
376 ResetLinkedListIterator(log_list);
377 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
378 for (i=0; p != (const LogInfo *) NULL; )
380 if ((p->stealth == MagickFalse) &&
381 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
383 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
385 UnlockSemaphoreInfo(log_semaphore);
386 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
387 preferences[i]=(LogInfo *) NULL;
388 *number_preferences=(unsigned long) i;
393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 % G e t L o g L i s t %
401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 % GetLogList() returns any logs that match the specified pattern.
405 % The format of the GetLogList function is:
407 % char **GetLogList(const char *pattern,unsigned long *number_preferences,
408 % ExceptionInfo *exception)
410 % A description of each parameter follows:
412 % o pattern: Specifies a pointer to a text string containing a pattern.
414 % o number_preferences: This integer returns the number of logs in the list.
416 % o exception: return any errors or warnings in this structure.
420 #if defined(__cplusplus) || defined(c_plusplus)
424 static int LogCompare(const void *x,const void *y)
432 return(LocaleCompare(*p,*q));
435 #if defined(__cplusplus) || defined(c_plusplus)
439 MagickExport char **GetLogList(const char *pattern,
440 unsigned long *number_preferences,ExceptionInfo *exception)
445 register const LogInfo
454 assert(pattern != (char *) NULL);
455 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
456 assert(number_preferences != (unsigned long *) NULL);
457 *number_preferences=0;
458 p=GetLogInfo("*",exception);
459 if (p == (const LogInfo *) NULL)
460 return((char **) NULL);
461 preferences=(char **) AcquireQuantumMemory((size_t)
462 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
463 if (preferences == (char **) NULL)
464 return((char **) NULL);
468 LockSemaphoreInfo(log_semaphore);
469 ResetLinkedListIterator(log_list);
470 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
471 for (i=0; p != (const LogInfo *) NULL; )
473 if ((p->stealth == MagickFalse) &&
474 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
475 preferences[i++]=ConstantString(p->name);
476 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
478 UnlockSemaphoreInfo(log_semaphore);
479 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
480 preferences[i]=(char *) NULL;
481 *number_preferences=(unsigned long) i;
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % G e t L o g N a m e %
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 % GetLogName() returns the current log name.
498 % The format of the GetLogName method is:
500 % const char *GetLogName(void)
503 MagickExport const char *GetLogName(void)
509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513 + I n i t i a l i z e L o g L i s t %
517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519 % InitializeLogList() initialize the log list.
521 % The format of the InitializeLogList method is:
523 % MagickBooleanType InitializeLogList(ExceptionInfo *exception)
525 % A description of each parameter follows.
527 % o exception: return any errors or warnings in this structure.
530 static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
532 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
534 if (log_semaphore == (SemaphoreInfo *) NULL)
535 AcquireSemaphoreInfo(&log_semaphore);
536 LockSemaphoreInfo(log_semaphore);
537 if ((log_list == (LinkedListInfo *) NULL) &&
538 (instantiate_log == MagickFalse))
540 (void) LoadLogLists(LogFilename,exception);
541 instantiate_log=MagickTrue;
543 UnlockSemaphoreInfo(log_semaphore);
545 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % I s E v e n t L o g g i n g %
557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
562 % The format of the IsEventLogging method is:
564 % MagickBooleanType IsEventLogging(void)
567 MagickExport MagickBooleanType IsEventLogging(void)
575 if ((log_list == (LinkedListInfo *) NULL) ||
576 (IsLinkedListEmpty(log_list) != MagickFalse))
578 exception=AcquireExceptionInfo();
579 log_info=GetLogInfo("*",exception);
580 exception=DestroyExceptionInfo(exception);
581 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588 % L i s t L o g I n f o %
592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
594 % ListLogInfo() lists the log info to a file.
596 % The format of the ListLogInfo method is:
598 % MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
600 % A description of each parameter follows.
602 % o file: An pointer to a FILE.
604 % o exception: return any errors or warnings in this structure.
607 MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
609 #define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
612 limit[MaxTextExtent];
629 if (file == (const FILE *) NULL)
631 log_info=GetLogInfoList("*",&number_aliases,exception);
632 if (log_info == (const LogInfo **) NULL)
635 path=(const char *) NULL;
636 for (i=0; i < (long) number_aliases; i++)
638 if (log_info[i]->stealth != MagickFalse)
640 if ((path == (const char *) NULL) ||
641 (LocaleCompare(path,log_info[i]->path) != 0))
643 if (log_info[i]->path != (char *) NULL)
644 (void) fprintf(file,"\nPath: %s\n\n",log_info[i]->path);
645 (void) fprintf(file,"Filename Generations Limit Format\n");
646 (void) fprintf(file,"-------------------------------------------------"
647 "------------------------------\n");
649 path=log_info[i]->path;
650 if (log_info[i]->filename != (char *) NULL)
652 (void) fprintf(file,"%s",log_info[i]->filename);
653 for (j=(long) strlen(log_info[i]->filename); j <= 16; j++)
654 (void) fprintf(file," ");
656 (void) fprintf(file,"%9lu ",log_info[i]->generations);
657 (void) FormatMagickSize(MegabytesToBytes(log_info[i]->limit),MagickFalse,
659 (void) fprintf(file,"%8sB ",limit);
660 if (log_info[i]->format != (char *) NULL)
661 (void) fprintf(file,"%s",log_info[i]->format);
662 (void) fprintf(file,"\n");
665 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
674 + L o g C o m p o n e n t G e n e s i s %
678 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % LogComponentGenesis() instantiates the log component.
682 % The format of the LogComponentGenesis method is:
684 % MagickBooleanType LogComponentGenesis(void)
687 MagickExport MagickBooleanType LogComponentGenesis(void)
689 AcquireSemaphoreInfo(&log_semaphore);
694 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 + L o g C o m p o n e n t T e r m i n u s %
702 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
704 % LogComponentTerminus() destroys the logging component.
706 % The format of the LogComponentTerminus method is:
708 % LogComponentTerminus(void)
712 static void *DestroyLogElement(void *log_info)
717 p=(LogInfo *) log_info;
718 if (p->file != (FILE *) NULL)
720 if (p->append == MagickFalse)
721 (void) fprintf(p->file,"</log>\n");
722 (void) fclose(p->file);
723 p->file=(FILE *) NULL;
725 if (p->exempt == MagickFalse)
727 if (p->format != (char *) NULL)
728 p->format=DestroyString(p->format);
729 if (p->path != (char *) NULL)
730 p->path=DestroyString(p->path);
731 if (p->filename != (char *) NULL)
732 p->filename=DestroyString(p->filename);
734 p=(LogInfo *) RelinquishMagickMemory(p);
735 return((void *) NULL);
738 MagickExport void LogComponentTerminus(void)
740 if (log_semaphore == (SemaphoreInfo *) NULL)
741 AcquireSemaphoreInfo(&log_semaphore);
742 LockSemaphoreInfo(log_semaphore);
743 if (log_list != (LinkedListInfo *) NULL)
744 log_list=DestroyLinkedList(log_list,DestroyLogElement);
745 instantiate_log=MagickFalse;
746 UnlockSemaphoreInfo(log_semaphore);
747 DestroySemaphoreInfo(&log_semaphore);
751 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
755 % L o g M a g i c k E v e n t %
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
761 % LogMagickEvent() logs an event as determined by the log configuration file.
762 % If an error occurs, MagickFalse is returned otherwise MagickTrue.
764 % The format of the LogMagickEvent method is:
766 % MagickBooleanType LogMagickEvent(const LogEventType type,
767 % const char *module,const char *function,const unsigned long line,
768 % const char *format,...)
770 % A description of each parameter follows:
772 % o type: the event type.
774 % o filename: the source module filename.
776 % o function: the function name.
778 % o line: the line number of the source module.
780 % o format: the output format.
783 static char *TranslateEvent(const LogEventType magick_unused(type),
784 const char *module,const char *function,const unsigned long line,
785 const char *domain,const char *event)
812 exception=AcquireExceptionInfo();
813 log_info=(LogInfo *) GetLogInfo("*",exception);
814 exception=DestroyExceptionInfo(exception);
815 seconds=time((time_t *) NULL);
816 elapsed_time=GetElapsedTime(&log_info->timer);
817 user_time=GetUserTime(&log_info->timer);
818 text=AcquireString(event);
819 if (log_info->format == (char *) NULL)
821 extent=strlen(event)+MaxTextExtent;
822 if (LocaleCompare(log_info->format,"xml") == 0)
825 timestamp[MaxTextExtent];
828 Translate event in "XML" format.
830 (void) FormatMagickTime(seconds,extent,timestamp);
831 (void) FormatMagickString(text,extent,
833 " <timestamp>%s</timestamp>\n"
834 " <elapsed-time>%ld:%02ld.%03ld</elapsed-time>\n"
835 " <user-time>%0.3f</user-time>\n"
836 " <process-id>%ld</process-id>\n"
837 " <thread-id>%lu</thread-id>\n"
838 " <module>%s</module>\n"
839 " <function>%s</function>\n"
840 " <line>%lu</line>\n"
841 " <domain>%s</domain>\n"
842 " <event>%s</event>\n"
843 "</entry>",timestamp,(long) (elapsed_time/60.0),(long)
844 floor(fmod(elapsed_time,60.0)),(long) (1000.0*(elapsed_time-
845 floor(elapsed_time))+0.5),user_time,(long) getpid(),
846 GetMagickThreadSignature(),module,function,line,domain,event);
850 Translate event in "human readable" format.
853 for (p=log_info->format; *p != '\0'; p++)
856 if ((size_t) (q-text+MaxTextExtent) >= extent)
858 extent+=MaxTextExtent;
859 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
861 if (text == (char *) NULL)
862 return((char *) NULL);
866 The format of the log is defined by embedding special format characters:
885 if ((*p == '\\') && (*(p+1) == 'r'))
891 if ((*p == '\\') && (*(p+1) == 'n'))
907 q+=CopyMagickString(q,GetClientName(),extent);
912 q+=CopyMagickString(q,domain,extent);
917 q+=CopyMagickString(q,event,extent);
922 q+=CopyMagickString(q,function,extent);
927 if (log_info->generations == 0)
929 (void) CopyMagickString(q,"0",extent);
933 q+=FormatMagickString(q,extent,"%lu",log_info->generation %
934 log_info->generations);
939 q+=FormatMagickString(q,extent,"%lu",line);
947 for (p=module+strlen(module)-1; p > module; p--)
948 if (*p == *DirectorySeparator)
953 q+=CopyMagickString(q,p,extent);
958 q+=CopyMagickString(q,GetLogName(),extent);
963 q+=FormatMagickString(q,extent,"%ld",(long) getpid());
968 q+=FormatMagickString(q,extent,"%ld:%02ld.%03ld",(long)
969 (elapsed_time/60.0),(long) floor(fmod(elapsed_time,60.0)),
970 (long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
975 q+=FormatMagickTime(seconds,extent,q);
980 q+=FormatMagickString(q,extent,"%0.3fu",user_time);
985 q+=CopyMagickString(q,MagickLibVersionText,extent);
1005 static char *TranslateFilename(const LogInfo *log_info)
1020 Translate event in "human readable" format.
1022 assert(log_info != (LogInfo *) NULL);
1023 assert(log_info->filename != (char *) NULL);
1024 filename=AcquireString((char *) NULL);
1025 extent=MaxTextExtent;
1027 for (p=log_info->filename; *p != '\0'; p++)
1030 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1032 extent+=MaxTextExtent;
1033 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1035 if (filename == (char *) NULL)
1036 return((char *) NULL);
1037 q=filename+strlen(filename);
1040 The format of the filename is defined by embedding special format
1059 q+=CopyMagickString(q,GetClientName(),extent);
1064 if (log_info->generations == 0)
1066 (void) CopyMagickString(q,"0",extent);
1070 q+=FormatMagickString(q,extent,"%lu",log_info->generation %
1071 log_info->generations);
1076 q+=CopyMagickString(q,GetLogName(),extent);
1081 q+=FormatMagickString(q,extent,"%ld",(long) getpid());
1086 q+=CopyMagickString(q,MagickLibVersionText,extent);
1106 MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1107 const char *function,const unsigned long line,const char *format,
1111 event[MaxTextExtent],
1126 if (IsEventLogging() == MagickFalse)
1127 return(MagickFalse);
1128 exception=AcquireExceptionInfo();
1129 log_info=(LogInfo *) GetLogInfo("*",exception);
1130 exception=DestroyExceptionInfo(exception);
1131 LockSemaphoreInfo(log_semaphore);
1132 if ((log_info->event_mask & type) == 0)
1134 UnlockSemaphoreInfo(log_semaphore);
1137 domain=MagickOptionToMnemonic(MagickLogEventOptions,type);
1138 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
1139 n=vsnprintf(event,MaxTextExtent,format,operands);
1141 n=vsprintf(event,format,operands);
1144 event[MaxTextExtent-1]='\0';
1145 text=TranslateEvent(type,module,function,line,domain,event);
1146 if (text == (char *) NULL)
1148 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1149 UnlockSemaphoreInfo(log_semaphore);
1150 return(MagickFalse);
1152 if ((log_info->handler_mask & ConsoleHandler) != 0)
1154 (void) fprintf(stderr,"%s\n",text);
1155 (void) fflush(stderr);
1157 if ((log_info->handler_mask & DebugHandler) != 0)
1159 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1160 OutputDebugString(text);
1163 if ((log_info->handler_mask & EventHandler) != 0)
1165 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1166 (void) NTReportEvent(text,MagickFalse);
1169 if ((log_info->handler_mask & FileHandler) != 0)
1174 file_info.st_size=0;
1175 if (log_info->file != (FILE *) NULL)
1176 (void) fstat(fileno(log_info->file),&file_info);
1177 if (file_info.st_size > (off_t) (1024*1024*log_info->limit))
1179 (void) fprintf(log_info->file,"</log>\n");
1180 (void) fclose(log_info->file);
1181 log_info->file=(FILE *) NULL;
1183 if (log_info->file == (FILE *) NULL)
1188 filename=TranslateFilename(log_info);
1189 if (filename == (char *) NULL)
1191 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1192 UnlockSemaphoreInfo(log_semaphore);
1193 return(MagickFalse);
1195 log_info->append=IsPathAccessible(filename);
1196 log_info->file=OpenMagickStream(filename,"ab");
1197 filename=(char *) RelinquishMagickMemory(filename);
1198 if (log_info->file == (FILE *) NULL)
1200 UnlockSemaphoreInfo(log_semaphore);
1201 return(MagickFalse);
1203 log_info->generation++;
1204 if (log_info->append == MagickFalse)
1206 (void) fprintf(log_info->file,"<?xml version=\"1.0\" "
1207 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1208 (void) fprintf(log_info->file,"<log>\n");
1211 (void) fprintf(log_info->file,"%s\n",text);
1212 (void) fflush(log_info->file);
1214 if ((log_info->handler_mask & StdoutHandler) != 0)
1216 (void) fprintf(stdout,"%s\n",text);
1217 (void) fflush(stdout);
1219 if ((log_info->handler_mask & StderrHandler) != 0)
1221 (void) fprintf(stderr,"%s\n",text);
1222 (void) fflush(stderr);
1224 text=(char *) RelinquishMagickMemory(text);
1225 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1226 UnlockSemaphoreInfo(log_semaphore);
1230 MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1231 const char *function,const unsigned long line,const char *format,...)
1239 va_start(operands,format);
1240 status=LogMagickEventList(type,module,function,line,format,operands);
1246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1250 + L o a d L o g L i s t %
1254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 % LoadLogList() loads the log configuration file which provides a
1257 % mapping between log attributes and log name.
1259 % The format of the LoadLogList method is:
1261 % MagickBooleanType LoadLogList(const char *xml,const char *filename,
1262 % const unsigned long depth,ExceptionInfo *exception)
1264 % A description of each parameter follows:
1266 % o xml: The log list in XML format.
1268 % o filename: The log list filename.
1270 % o depth: depth of <include /> statements.
1272 % o exception: return any errors or warnings in this structure.
1275 static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1276 const unsigned long depth,ExceptionInfo *exception)
1279 keyword[MaxTextExtent],
1286 *log_info = (LogInfo *) NULL;
1292 Load the log map file.
1294 if (xml == (const char *) NULL)
1295 return(MagickFalse);
1296 if (log_list == (LinkedListInfo *) NULL)
1298 log_list=NewLinkedList(0);
1299 if (log_list == (LinkedListInfo *) NULL)
1301 ThrowFileException(exception,ResourceLimitError,
1302 "MemoryAllocationFailed",filename);
1303 return(MagickFalse);
1307 token=AcquireString((const char *) xml);
1308 for (q=(const char *) xml; *q != '\0'; )
1313 GetMagickToken(q,&q,token);
1316 (void) CopyMagickString(keyword,token,MaxTextExtent);
1317 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1322 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1323 GetMagickToken(q,&q,token);
1326 if (LocaleNCompare(keyword,"<!--",4) == 0)
1331 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1332 GetMagickToken(q,&q,token);
1335 if (LocaleCompare(keyword,"<include") == 0)
1340 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1342 (void) CopyMagickString(keyword,token,MaxTextExtent);
1343 GetMagickToken(q,&q,token);
1346 GetMagickToken(q,&q,token);
1347 if (LocaleCompare(keyword,"file") == 0)
1350 (void) ThrowMagickException(exception,GetMagickModule(),
1351 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1355 path[MaxTextExtent],
1358 GetPathComponent(filename,HeadPath,path);
1360 (void) ConcatenateMagickString(path,DirectorySeparator,
1362 if (*token == *DirectorySeparator)
1363 (void) CopyMagickString(path,token,MaxTextExtent);
1365 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1366 xml=FileToString(path,~0,exception);
1367 if (xml != (char *) NULL)
1369 status|=LoadLogList(xml,path,depth+1,exception);
1370 xml=DestroyString(xml);
1377 if (LocaleCompare(keyword,"<logmap>") == 0)
1380 Allocate memory for the log list.
1382 log_info=(LogInfo *) AcquireAlignedMemory(1,sizeof(*log_info));
1383 if (log_info == (LogInfo *) NULL)
1384 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1385 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1386 log_info->path=ConstantString(filename);
1387 GetTimerInfo((TimerInfo *) &log_info->timer);
1388 log_info->exempt=MagickFalse;
1389 log_info->signature=MagickSignature;
1392 if (log_info == (LogInfo *) NULL)
1394 if (LocaleCompare(keyword,"</logmap>") == 0)
1396 status=AppendValueToLinkedList(log_list,log_info);
1397 if (status == MagickFalse)
1398 (void) ThrowMagickException(exception,GetMagickModule(),
1399 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1400 log_info=(LogInfo *) NULL;
1402 GetMagickToken(q,(const char **) NULL,token);
1405 GetMagickToken(q,&q,token);
1406 GetMagickToken(q,&q,token);
1412 if (LocaleCompare((char *) keyword,"events") == 0)
1414 log_info->event_mask=(LogEventType) (log_info->event_mask |
1415 ParseMagickOption(MagickLogEventOptions,MagickTrue,token));
1423 if (LocaleCompare((char *) keyword,"filename") == 0)
1425 if (log_info->filename != (char *) NULL)
1426 log_info->filename=(char *)
1427 RelinquishMagickMemory(log_info->filename);
1428 log_info->filename=ConstantString(token);
1431 if (LocaleCompare((char *) keyword,"format") == 0)
1433 if (log_info->format != (char *) NULL)
1434 log_info->format=(char *)
1435 RelinquishMagickMemory(log_info->format);
1436 log_info->format=ConstantString(token);
1444 if (LocaleCompare((char *) keyword,"generations") == 0)
1446 if (LocaleCompare(token,"unlimited") == 0)
1448 log_info->generations=(~0UL);
1451 log_info->generations=StringToUnsignedLong(token);
1459 if (LocaleCompare((char *) keyword,"limit") == 0)
1461 if (LocaleCompare(token,"unlimited") == 0)
1463 log_info->limit=(~0UL);
1466 log_info->limit=StringToUnsignedLong(token);
1474 if (LocaleCompare((char *) keyword,"output") == 0)
1476 log_info->handler_mask=(LogHandlerType)
1477 (log_info->handler_mask | ParseLogHandlers(token));
1486 token=DestroyString(token);
1487 if (log_list == (LinkedListInfo *) NULL)
1488 return(MagickFalse);
1489 return(status != 0 ? MagickTrue : MagickFalse);
1493 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1497 % L o a d L o g L i s t s %
1501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1503 % LoadLogLists() loads one or more log configuration file which provides a
1504 % mapping between log attributes and log name.
1506 % The format of the LoadLogLists method is:
1508 % MagickBooleanType LoadLogLists(const char *filename,
1509 % ExceptionInfo *exception)
1511 % A description of each parameter follows:
1513 % o filename: the log configuration filename.
1515 % o exception: return any errors or warnings in this structure.
1518 static MagickBooleanType LoadLogLists(const char *filename,
1519 ExceptionInfo *exception)
1534 Load built-in log map.
1537 if (log_list == (LinkedListInfo *) NULL)
1539 log_list=NewLinkedList(0);
1540 if (log_list == (LinkedListInfo *) NULL)
1542 ThrowFileException(exception,ResourceLimitError,
1543 "MemoryAllocationFailed",filename);
1544 return(MagickFalse);
1547 for (i=0; i < (long) (sizeof(LogMap)/sizeof(*LogMap)); i++)
1552 register const LogMapInfo
1556 log_info=(LogInfo *) AcquireAlignedMemory(1,sizeof(*log_info));
1557 if (log_info == (LogInfo *) NULL)
1559 (void) ThrowMagickException(exception,GetMagickModule(),
1560 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1563 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1564 log_info->path=(char *) "[built-in]";
1565 GetTimerInfo((TimerInfo *) &log_info->timer);
1566 log_info->event_mask=p->event_mask;
1567 log_info->handler_mask=p->handler_mask;
1568 log_info->filename=ConstantString(p->filename);
1569 log_info->format=ConstantString(p->format);
1570 log_info->exempt=MagickTrue;
1571 log_info->signature=MagickSignature;
1572 status=AppendValueToLinkedList(log_list,log_info);
1573 if (status == MagickFalse)
1574 (void) ThrowMagickException(exception,GetMagickModule(),
1575 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
1578 Load external log map.
1580 options=GetConfigureOptions(filename,exception);
1581 option=(const StringInfo *) GetNextValueInLinkedList(options);
1582 while (option != (const StringInfo *) NULL)
1584 status|=LoadLogList((const char *) GetStringInfoDatum(option),
1585 GetStringInfoPath(option),0,exception);
1586 option=(const StringInfo *) GetNextValueInLinkedList(options);
1588 options=DestroyConfigureOptions(options);
1589 return(status != 0 ? MagickTrue : MagickFalse);
1593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1597 + P a r s e L o g H a n d l e r s %
1601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1603 % ParseLogHandlers() parses a string defining which handlers takes a log
1604 % message and exports them.
1606 % The format of the ParseLogHandlers method is:
1608 % LogHandlerType ParseLogHandlers(const char *handlers)
1610 % A description of each parameter follows:
1612 % o handlers: one or more handlers separated by commas.
1615 static LogHandlerType ParseLogHandlers(const char *handlers)
1629 handler_mask=NoHandler;
1630 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1632 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1635 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1637 length=strlen(LogHandlers[i].name);
1638 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1640 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1644 if (LogHandlers[i].name == (char *) NULL)
1645 return(UndefinedHandler);
1647 return(handler_mask);
1651 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1655 % S e t L o g E v e n t M a s k %
1659 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1661 % SetLogEventMask() accepts a list that determines which events to log. All
1662 % other events are ignored. By default, no debug is enabled. This method
1663 % returns the previous log event mask.
1665 % The format of the SetLogEventMask method is:
1667 % LogEventType SetLogEventMask(const char *events)
1669 % A description of each parameter follows:
1671 % o events: log these events.
1674 MagickExport LogEventType SetLogEventMask(const char *events)
1685 exception=AcquireExceptionInfo();
1686 log_info=(LogInfo *) GetLogInfo("*",exception);
1687 exception=DestroyExceptionInfo(exception);
1688 option=ParseMagickOption(MagickLogEventOptions,MagickTrue,events);
1689 LockSemaphoreInfo(log_semaphore);
1690 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1691 log_info->event_mask=(LogEventType) option;
1693 log_info->event_mask=UndefinedEvents;
1694 UnlockSemaphoreInfo(log_semaphore);
1695 return(log_info->event_mask);
1699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1703 % S e t L o g F o r m a t %
1707 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1709 % SetLogFormat() sets the format for the "human readable" log record.
1711 % The format of the LogMagickFormat method is:
1713 % SetLogFormat(const char *format)
1715 % A description of each parameter follows:
1717 % o format: the log record format.
1720 MagickExport void SetLogFormat(const char *format)
1728 exception=AcquireExceptionInfo();
1729 log_info=(LogInfo *) GetLogInfo("*",exception);
1730 exception=DestroyExceptionInfo(exception);
1731 LockSemaphoreInfo(log_semaphore);
1732 if (log_info->format != (char *) NULL)
1733 log_info->format=DestroyString(log_info->format);
1734 log_info->format=ConstantString(format);
1735 UnlockSemaphoreInfo(log_semaphore);
1739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1743 % S e t L o g N a m e %
1747 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1749 % SetLogName() sets the log name and returns it.
1751 % The format of the SetLogName method is:
1753 % const char *SetLogName(const char *name)
1755 % A description of each parameter follows:
1757 % o log_name: SetLogName() returns the current client name.
1759 % o name: Specifies the new client name.
1762 MagickExport const char *SetLogName(const char *name)
1764 if ((name != (char *) NULL) && (*name != '\0'))
1765 (void) CopyMagickString(log_name,name,MaxTextExtent);