blob: 4c3a8fef2da664dbd724418f56f58553cabb9183 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% L OOO GGGG %
7% L O O G %
8% L O O G GG %
9% L O O G G %
10% LLLLL OOO GGG %
11% %
12% %
13% MagickCore Log Events %
14% %
15% Software Design %
16% John Cristy %
17% September 2002 %
18% %
19% %
20% Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
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. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/client.h"
45#include "magick/configure.h"
46#include "magick/exception.h"
47#include "magick/exception-private.h"
48#include "magick/hashmap.h"
49#include "magick/log.h"
50#include "magick/memory_.h"
51#include "magick/option.h"
52#include "magick/semaphore.h"
53#include "magick/timer.h"
54#include "magick/string_.h"
55#include "magick/token.h"
56#include "magick/thread_.h"
57#include "magick/thread-private.h"
58#include "magick/utility.h"
59#include "magick/version.h"
60#include "magick/xml-tree.h"
61
62/*
63 Define declarations.
64*/
65#define LogFilename "log.xml"
66
67/*
68 Typedef declarations.
69*/
70typedef enum
71{
72 UndefinedHandler = 0x0000,
73 NoHandler = 0x0000,
74 ConsoleHandler = 0x0001,
75 StdoutHandler = 0x0002,
76 StderrHandler = 0x0004,
77 FileHandler = 0x0008,
78 DebugHandler = 0x0010,
79 EventHandler = 0x0020
80} LogHandlerType;
81
82typedef struct _EventInfo
83{
84 char
85 *name;
86
87 LogEventType
88 event;
89} EventInfo;
90
91typedef struct _HandlerInfo
92{
93 const char
94 *name;
95
96 LogHandlerType
97 handler;
98} HandlerInfo;
99
100struct _LogInfo
101{
102 LogEventType
103 event_mask;
104
105 LogHandlerType
106 handler_mask;
107
108 char
109 *path,
110 *name,
111 *filename,
112 *format;
113
114 unsigned long
115 generations,
116 limit;
117
118 FILE
119 *file;
120
121 unsigned long
122 generation;
123
124 MagickBooleanType
125 append,
126 stealth;
127
128 TimerInfo
129 timer;
130
131 unsigned long
132 signature;
133};
134
135/*
136 Declare log map.
137*/
138static const HandlerInfo
139 LogHandlers[] =
140 {
141 { "console", ConsoleHandler },
142 { "debug", DebugHandler },
143 { "event", EventHandler },
144 { "file", FileHandler },
145 { "none", NoHandler },
146 { "stderr", StderrHandler },
147 { "stdout", StdoutHandler },
148 { (char *) NULL, UndefinedHandler }
149 };
150
151static const char
152 *LogMap = (const char *)
153 "<?xml version=\"1.0\"?>"
154 "<logmap>"
155 " <log events=\"None\" />"
156 " <log output=\"console\" />"
157 " <log filename=\"Magick-%d.log\" />"
158 " <log format=\"%t %r %u %v %d %c[%p]: %m/%f/%l/%d\n %e\" />"
159 "</logmap>";
160
161/*
162 Static declarations.
163*/
164static char
165 log_name[MaxTextExtent] = "Magick";
166
167static LinkedListInfo
168 *log_list = (LinkedListInfo *) NULL;
169
170static SemaphoreInfo
171 *log_semaphore = (SemaphoreInfo *) NULL;
172
173static volatile MagickBooleanType
174 instantiate_log = MagickFalse;
175
176/*
177 Forward declarations.
178*/
179static LogHandlerType
180 ParseLogHandlers(const char *);
181
182static LogInfo
183 *GetLogInfo(const char *,ExceptionInfo *);
184
185static MagickBooleanType
186 InitializeLogList(ExceptionInfo *),
187 LoadLogLists(const char *,ExceptionInfo *);
188
189/*
190%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191% %
192% %
193% %
194% C l o s e M a g i c k L o g %
195% %
196% %
197% %
198%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199%
200% CloseMagickLog() closes the Magick log.
201%
202% The format of the CloseMagickLog method is:
203%
204% CloseMagickLog(void)
205%
206*/
207MagickExport void CloseMagickLog(void)
208{
209 ExceptionInfo
210 *exception;
211
212 LogInfo
213 *log_info;
214
215 if (IsEventLogging() == MagickFalse)
216 return;
217 exception=AcquireExceptionInfo();
218 log_info=GetLogInfo("*",exception);
219 exception=DestroyExceptionInfo(exception);
220 AcquireSemaphoreInfo(&log_semaphore);
221 if (log_info->file != (FILE *) NULL)
222 {
223 if (log_info->append == MagickFalse)
224 (void) fprintf(log_info->file,"</log>\n");
225 (void) fclose(log_info->file);
226 log_info->file=(FILE *) NULL;
227 }
228 RelinquishSemaphoreInfo(log_semaphore);
229}
230
231/*
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233% %
234% %
235% %
cristy41c3c772009-10-19 02:17:37 +0000236+ D e s t r o y L o g F a c i l i t y %
cristy3ed852e2009-09-05 21:47:34 +0000237% %
238% %
239% %
240%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241%
cristy41c3c772009-10-19 02:17:37 +0000242% DestroyLogFacility() destroys the logging facility.
cristy3ed852e2009-09-05 21:47:34 +0000243%
cristy41c3c772009-10-19 02:17:37 +0000244% The format of the DestroyLogFacility method is:
cristy3ed852e2009-09-05 21:47:34 +0000245%
cristy41c3c772009-10-19 02:17:37 +0000246% DestroyLogFacility(void)
cristy3ed852e2009-09-05 21:47:34 +0000247%
248*/
249
250static void *DestroyLogElement(void *log_info)
251{
252 register LogInfo
253 *p;
254
255 p=(LogInfo *) log_info;
256 if (p->file != (FILE *) NULL)
257 {
258 if (p->append == MagickFalse)
259 (void) fprintf(p->file,"</log>\n");
260 (void) fclose(p->file);
261 p->file=(FILE *) NULL;
262 }
263 if (p->filename != (char *) NULL)
264 p->filename=DestroyString(p->filename);
265 if (p->path != (char *) NULL)
266 p->path=DestroyString(p->path);
267 if (p->format != (char *) NULL)
268 p->format=DestroyString(p->format);
269 p=(LogInfo *) RelinquishMagickMemory(p);
270 return((void *) NULL);
271}
272
cristy41c3c772009-10-19 02:17:37 +0000273MagickExport void DestroyLogFacility(void)
cristy3ed852e2009-09-05 21:47:34 +0000274{
275 AcquireSemaphoreInfo(&log_semaphore);
276 if (log_list != (LinkedListInfo *) NULL)
277 log_list=DestroyLinkedList(log_list,DestroyLogElement);
278 instantiate_log=MagickFalse;
279 RelinquishSemaphoreInfo(log_semaphore);
280 DestroySemaphoreInfo(&log_semaphore);
281}
282
283/*
284%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285% %
286% %
287% %
288+ G e t L o g I n f o %
289% %
290% %
291% %
292%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293%
294% GetLogInfo() searches the log list for the specified name and if found
295% returns attributes for that log.
296%
297% The format of the GetLogInfo method is:
298%
299% LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
300%
301% A description of each parameter follows:
302%
303% o name: the log name.
304%
305% o exception: return any errors or warnings in this structure.
306%
307*/
308static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
309{
310 register LogInfo
311 *p;
312
313 assert(exception != (ExceptionInfo *) NULL);
314 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
315 if (InitializeLogList(exception) == MagickFalse)
316 return((LogInfo *) NULL);
317 if ((log_list == (LinkedListInfo *) NULL) ||
318 (IsLinkedListEmpty(log_list) != MagickFalse))
319 return((LogInfo *) NULL);
320 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
321 return((LogInfo *) GetValueFromLinkedList(log_list,0));
322 /*
323 Search for log tag.
324 */
325 AcquireSemaphoreInfo(&log_semaphore);
326 ResetLinkedListIterator(log_list);
327 p=(LogInfo *) GetNextValueInLinkedList(log_list);
328 while (p != (LogInfo *) NULL)
329 {
330 if (LocaleCompare(name,p->name) == 0)
331 break;
332 p=(LogInfo *) GetNextValueInLinkedList(log_list);
333 }
334 if (p != (LogInfo *) NULL)
335 (void) InsertValueInLinkedList(log_list,0,
336 RemoveElementByValueFromLinkedList(log_list,p));
337 RelinquishSemaphoreInfo(log_semaphore);
338 return(p);
339}
340
341/*
342%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343% %
344% %
345% %
346% G e t L o g I n f o L i s t %
347% %
348% %
349% %
350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
351%
352% GetLogInfoList() returns any logs that match the specified pattern.
353%
354% The format of the GetLogInfoList function is:
355%
356% const LogInfo **GetLogInfoList(const char *pattern,
357% unsigned long *number_preferences,ExceptionInfo *exception)
358%
359% A description of each parameter follows:
360%
361% o pattern: Specifies a pointer to a text string containing a pattern.
362%
363% o number_preferences: This integer returns the number of logs in the list.
364%
365% o exception: return any errors or warnings in this structure.
366%
367*/
368#if defined(__cplusplus) || defined(c_plusplus)
369extern "C" {
370#endif
371
372static int LogInfoCompare(const void *x,const void *y)
373{
374 const LogInfo
375 **p,
376 **q;
377
378 p=(const LogInfo **) x,
379 q=(const LogInfo **) y;
380 if (LocaleCompare((*p)->path,(*q)->path) == 0)
381 return(LocaleCompare((*p)->name,(*q)->name));
382 return(LocaleCompare((*p)->path,(*q)->path));
383}
384
385#if defined(__cplusplus) || defined(c_plusplus)
386}
387#endif
388
389MagickExport const LogInfo **GetLogInfoList(const char *pattern,
390 unsigned long *number_preferences,ExceptionInfo *exception)
391{
392 const LogInfo
393 **preferences;
394
395 register const LogInfo
396 *p;
397
398 register long
399 i;
400
401 /*
402 Allocate log list.
403 */
404 assert(pattern != (char *) NULL);
405 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
406 assert(number_preferences != (unsigned long *) NULL);
407 *number_preferences=0;
408 p=GetLogInfo("*",exception);
409 if (p == (const LogInfo *) NULL)
410 return((const LogInfo **) NULL);
411 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
412 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
413 if (preferences == (const LogInfo **) NULL)
414 return((const LogInfo **) NULL);
415 /*
416 Generate log list.
417 */
418 AcquireSemaphoreInfo(&log_semaphore);
419 ResetLinkedListIterator(log_list);
420 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
421 for (i=0; p != (const LogInfo *) NULL; )
422 {
423 if ((p->stealth == MagickFalse) &&
424 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
425 preferences[i++]=p;
426 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
427 }
428 RelinquishSemaphoreInfo(log_semaphore);
429 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
430 preferences[i]=(LogInfo *) NULL;
431 *number_preferences=(unsigned long) i;
432 return(preferences);
433}
434
435/*
436%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437% %
438% %
439% %
440% G e t L o g L i s t %
441% %
442% %
443% %
444%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445%
446% GetLogList() returns any logs that match the specified pattern.
447%
448% The format of the GetLogList function is:
449%
450% char **GetLogList(const char *pattern,unsigned long *number_preferences,
451% ExceptionInfo *exception)
452%
453% A description of each parameter follows:
454%
455% o pattern: Specifies a pointer to a text string containing a pattern.
456%
457% o number_preferences: This integer returns the number of logs in the list.
458%
459% o exception: return any errors or warnings in this structure.
460%
461*/
462
463#if defined(__cplusplus) || defined(c_plusplus)
464extern "C" {
465#endif
466
467static int LogCompare(const void *x,const void *y)
468{
469 register const char
470 **p,
471 **q;
472
473 p=(const char **) x;
474 q=(const char **) y;
475 return(LocaleCompare(*p,*q));
476}
477
478#if defined(__cplusplus) || defined(c_plusplus)
479}
480#endif
481
482MagickExport char **GetLogList(const char *pattern,
483 unsigned long *number_preferences,ExceptionInfo *exception)
484{
485 char
486 **preferences;
487
488 register const LogInfo
489 *p;
490
491 register long
492 i;
493
494 /*
495 Allocate log list.
496 */
497 assert(pattern != (char *) NULL);
498 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
499 assert(number_preferences != (unsigned long *) NULL);
500 *number_preferences=0;
501 p=GetLogInfo("*",exception);
502 if (p == (const LogInfo *) NULL)
503 return((char **) NULL);
504 preferences=(char **) AcquireQuantumMemory((size_t)
505 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
506 if (preferences == (char **) NULL)
507 return((char **) NULL);
508 /*
509 Generate log list.
510 */
511 AcquireSemaphoreInfo(&log_semaphore);
512 ResetLinkedListIterator(log_list);
513 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
514 for (i=0; p != (const LogInfo *) NULL; )
515 {
516 if ((p->stealth == MagickFalse) &&
517 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
518 preferences[i++]=ConstantString(p->name);
519 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
520 }
521 RelinquishSemaphoreInfo(log_semaphore);
522 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
523 preferences[i]=(char *) NULL;
524 *number_preferences=(unsigned long) i;
525 return(preferences);
526}
527
528/*
529%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
530% %
531% %
532% %
533% G e t L o g N a m e %
534% %
535% %
536% %
537%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
538%
539% GetLogName() returns the current log name.
540%
541% The format of the GetLogName method is:
542%
543% const char *GetLogName(void)
544%
545*/
546MagickExport const char *GetLogName(void)
547{
548 return(log_name);
549}
550
551/*
552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553% %
554% %
555% %
556+ I n i t i a l i z e L o g L i s t %
557% %
558% %
559% %
560%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561%
562% InitializeLogList() initialize the log list.
563%
564% The format of the InitializeLogList method is:
565%
566% MagickBooleanType InitializeLogList(ExceptionInfo *exception)
567%
568% A description of each parameter follows.
569%
570% o exception: return any errors or warnings in this structure.
571%
572*/
573static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
574{
575 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
576 {
577 AcquireSemaphoreInfo(&log_semaphore);
578 if ((log_list == (LinkedListInfo *) NULL) &&
579 (instantiate_log == MagickFalse))
580 {
581 (void) LoadLogLists(LogFilename,exception);
582 instantiate_log=MagickTrue;
583 }
584 RelinquishSemaphoreInfo(log_semaphore);
585 }
586 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
587}
588
589/*
590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591% %
592% %
593% %
cristy41c3c772009-10-19 02:17:37 +0000594+ I n s t a n t i a t e L o g F a c i l i t y %
595% %
596% %
597% %
598%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599%
600% InstantiateLogFacility() instantiates the log facility.
601%
602% The format of the InstantiateLogFacility method is:
603%
604% MagickBooleanType InstantiateLogFacility(void)
605%
606*/
607MagickExport MagickBooleanType InstantiateLogFacility(void)
608{
609 AcquireSemaphoreInfo(&log_semaphore);
610 RelinquishSemaphoreInfo(log_semaphore);
611 return(MagickFalse);
612}
613
614/*
615%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
616% %
617% %
618% %
cristy3ed852e2009-09-05 21:47:34 +0000619% I s E v e n t L o g g i n g %
620% %
621% %
622% %
623%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624%
625% IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
626% MagickFalse.
627%
628% The format of the IsEventLogging method is:
629%
630% MagickBooleanType IsEventLogging(void)
631%
632*/
633MagickExport MagickBooleanType IsEventLogging(void)
634{
635 const LogInfo
636 *log_info;
637
638 ExceptionInfo
639 *exception;
640
641 if ((log_list == (LinkedListInfo *) NULL) ||
642 (IsLinkedListEmpty(log_list) != MagickFalse))
643 return(MagickFalse);
644 exception=AcquireExceptionInfo();
645 log_info=GetLogInfo("*",exception);
646 exception=DestroyExceptionInfo(exception);
647 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
648}
649/*
650%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651% %
652% %
653% %
654% L i s t L o g I n f o %
655% %
656% %
657% %
658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
659%
660% ListLogInfo() lists the log info to a file.
661%
662% The format of the ListLogInfo method is:
663%
664% MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
665%
666% A description of each parameter follows.
667%
668% o file: An pointer to a FILE.
669%
670% o exception: return any errors or warnings in this structure.
671%
672*/
673MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
674{
675#define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
676
677 char
678 limit[MaxTextExtent];
679
680 const char
681 *path;
682
683 const LogInfo
684 **log_info;
685
686 long
687 j;
688
689 register long
690 i;
691
692 unsigned long
693 number_aliases;
694
695 if (file == (const FILE *) NULL)
696 file=stdout;
697 log_info=GetLogInfoList("*",&number_aliases,exception);
698 if (log_info == (const LogInfo **) NULL)
699 return(MagickFalse);
700 j=0;
701 path=(const char *) NULL;
702 for (i=0; i < (long) number_aliases; i++)
703 {
704 if (log_info[i]->stealth != MagickFalse)
705 continue;
706 if ((path == (const char *) NULL) ||
707 (LocaleCompare(path,log_info[i]->path) != 0))
708 {
709 if (log_info[i]->path != (char *) NULL)
710 (void) fprintf(file,"\nPath: %s\n\n",log_info[i]->path);
711 (void) fprintf(file,"Filename Generations Limit Format\n");
712 (void) fprintf(file,"-------------------------------------------------"
713 "------------------------------\n");
714 }
715 path=log_info[i]->path;
716 if (log_info[i]->filename != (char *) NULL)
717 {
718 (void) fprintf(file,"%s",log_info[i]->filename);
719 for (j=(long) strlen(log_info[i]->filename); j <= 16; j++)
720 (void) fprintf(file," ");
721 }
722 (void) fprintf(file,"%9lu ",log_info[i]->generations);
723 (void) FormatMagickSize(MegabytesToBytes(log_info[i]->limit),limit);
724 (void) fprintf(file,"%8s ",limit);
725 if (log_info[i]->format != (char *) NULL)
726 (void) fprintf(file,"%s",log_info[i]->format);
727 (void) fprintf(file,"\n");
728 }
729 (void) fflush(file);
730 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
731 return(MagickTrue);
732}
733
734/*
735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736% %
737% %
738% %
739% L o g M a g i c k E v e n t %
740% %
741% %
742% %
743%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
744%
745% LogMagickEvent() logs an event as determined by the log configuration file.
746% If an error occurs, MagickFalse is returned otherwise MagickTrue.
747%
748% The format of the LogMagickEvent method is:
749%
750% MagickBooleanType LogMagickEvent(const LogEventType type,
751% const char *module,const char *function,const unsigned long line,
752% const char *format,...)
753%
754% A description of each parameter follows:
755%
756% o type: the event type.
757%
758% o filename: the source module filename.
759%
760% o function: the function name.
761%
762% o line: the line number of the source module.
763%
764% o format: the output format.
765%
766*/
767static char *TranslateEvent(const LogEventType magick_unused(type),
768 const char *module,const char *function,const unsigned long line,
769 const char *domain,const char *event)
770{
771 char
772 *text;
773
774 double
775 elapsed_time,
776 user_time;
777
778 ExceptionInfo
779 *exception;
780
781 LogInfo
782 *log_info;
783
784 register char
785 *q;
786
787 register const char
788 *p;
789
790 size_t
791 extent;
792
793 time_t
794 seconds;
795
796 exception=AcquireExceptionInfo();
797 log_info=(LogInfo *) GetLogInfo("*",exception);
798 exception=DestroyExceptionInfo(exception);
799 seconds=time((time_t *) NULL);
800 elapsed_time=GetElapsedTime(&log_info->timer);
801 user_time=GetUserTime(&log_info->timer);
802 text=AcquireString(event);
803 if (log_info->format == (char *) NULL)
804 return(text);
805 extent=strlen(event)+MaxTextExtent;
806 if (LocaleCompare(log_info->format,"xml") == 0)
807 {
808 char
809 timestamp[MaxTextExtent];
810
811 /*
812 Translate event in "XML" format.
813 */
814 (void) FormatMagickTime(seconds,extent,timestamp);
815 (void) FormatMagickString(text,extent,
816 "<entry>\n"
817 " <timestamp>%s</timestamp>\n"
cristy8b76e552009-09-28 18:20:20 +0000818 " <elapsed-time>%ld:%02ld.%03ld</elapsed-time>\n"
cristy3ed852e2009-09-05 21:47:34 +0000819 " <user-time>%0.3f</user-time>\n"
820 " <process-id>%ld</process-id>\n"
821 " <thread-id>%lu</thread-id>\n"
822 " <module>%s</module>\n"
823 " <function>%s</function>\n"
824 " <line>%lu</line>\n"
825 " <domain>%s</domain>\n"
826 " <event>%s</event>\n"
cristy8b76e552009-09-28 18:20:20 +0000827 "</entry>",timestamp,(long) (elapsed_time/60.0),(long)
828 floor(fmod(elapsed_time,60.0)),(long) (1000.0*(elapsed_time-
829 floor(elapsed_time))+0.5),user_time,(long) getpid(),
cristy3ed852e2009-09-05 21:47:34 +0000830 GetMagickThreadSignature(),module,function,line,domain,event);
831 return(text);
832 }
833 /*
834 Translate event in "human readable" format.
835 */
836 q=text;
837 for (p=log_info->format; *p != '\0'; p++)
838 {
839 *q='\0';
840 if ((size_t) (q-text+MaxTextExtent) >= extent)
841 {
842 extent+=MaxTextExtent;
843 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
844 sizeof(*text));
845 if (text == (char *) NULL)
846 return((char *) NULL);
847 q=text+strlen(text);
848 }
849 /*
850 The format of the log is defined by embedding special format characters:
851
852 %c client name
853 %d domain
854 %e event
855 %f function
856 %g generation
857 %l line
858 %m module
859 %n log name
860 %p process id
861 %r real CPU time
862 %t wall clock time
863 %u user CPU time
864 %v version
865 %% percent sign
866 \n newline
867 \r carriage return
868 */
869 if ((*p == '\\') && (*(p+1) == 'r'))
870 {
871 *q++='\r';
872 p++;
873 continue;
874 }
875 if ((*p == '\\') && (*(p+1) == 'n'))
876 {
877 *q++='\n';
878 p++;
879 continue;
880 }
881 if (*p != '%')
882 {
883 *q++=(*p);
884 continue;
885 }
886 p++;
887 switch (*p)
888 {
889 case 'c':
890 {
891 q+=CopyMagickString(q,GetClientName(),extent);
892 break;
893 }
894 case 'd':
895 {
896 q+=CopyMagickString(q,domain,extent);
897 break;
898 }
899 case 'e':
900 {
901 q+=CopyMagickString(q,event,extent);
902 break;
903 }
904 case 'f':
905 {
906 q+=CopyMagickString(q,function,extent);
907 break;
908 }
909 case 'g':
910 {
911 if (log_info->generations == 0)
912 {
913 (void) CopyMagickString(q,"0",extent);
914 q++;
915 break;
916 }
917 q+=FormatMagickString(q,extent,"%lu",log_info->generation %
918 log_info->generations);
919 break;
920 }
921 case 'l':
922 {
923 q+=FormatMagickString(q,extent,"%lu",line);
924 break;
925 }
926 case 'm':
927 {
928 register const char
929 *p;
930
931 for (p=module+strlen(module)-1; p > module; p--)
932 if (*p == *DirectorySeparator)
933 {
934 p++;
935 break;
936 }
937 q+=CopyMagickString(q,p,extent);
938 break;
939 }
940 case 'n':
941 {
942 q+=CopyMagickString(q,GetLogName(),extent);
943 break;
944 }
945 case 'p':
946 {
947 q+=FormatMagickString(q,extent,"%ld",(long) getpid());
948 break;
949 }
950 case 'r':
951 {
cristy8b76e552009-09-28 18:20:20 +0000952 q+=FormatMagickString(q,extent,"%ld:%02ld.%03ld",(long)
953 (elapsed_time/60.0),(long) floor(fmod(elapsed_time,60.0)),
954 (long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
cristy3ed852e2009-09-05 21:47:34 +0000955 break;
956 }
957 case 't':
958 {
959 q+=FormatMagickTime(seconds,extent,q);
960 break;
961 }
962 case 'u':
963 {
964 q+=FormatMagickString(q,extent,"%0.3fu",user_time);
965 break;
966 }
967 case 'v':
968 {
969 q+=CopyMagickString(q,MagickLibVersionText,extent);
970 break;
971 }
972 case '%':
973 {
974 *q++=(*p);
975 break;
976 }
977 default:
978 {
979 *q++='%';
980 *q++=(*p);
981 break;
982 }
983 }
984 }
985 *q='\0';
986 return(text);
987}
988
989static char *TranslateFilename(const LogInfo *log_info)
990{
991 char
992 *filename;
993
994 register char
995 *q;
996
997 register const char
998 *p;
999
1000 size_t
1001 extent;
1002
1003 /*
1004 Translate event in "human readable" format.
1005 */
1006 assert(log_info != (LogInfo *) NULL);
1007 assert(log_info->filename != (char *) NULL);
1008 filename=AcquireString((char *) NULL);
1009 extent=MaxTextExtent;
1010 q=filename;
1011 for (p=log_info->filename; *p != '\0'; p++)
1012 {
1013 *q='\0';
1014 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1015 {
1016 extent+=MaxTextExtent;
1017 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1018 sizeof(*filename));
1019 if (filename == (char *) NULL)
1020 return((char *) NULL);
1021 q=filename+strlen(filename);
1022 }
1023 /*
1024 The format of the filename is defined by embedding special format
1025 characters:
1026
1027 %c client name
1028 %n log name
1029 %p process id
1030 %v version
1031 %% percent sign
1032 */
1033 if (*p != '%')
1034 {
1035 *q++=(*p);
1036 continue;
1037 }
1038 p++;
1039 switch (*p)
1040 {
1041 case 'c':
1042 {
1043 q+=CopyMagickString(q,GetClientName(),extent);
1044 break;
1045 }
1046 case 'g':
1047 {
1048 if (log_info->generations == 0)
1049 {
1050 (void) CopyMagickString(q,"0",extent);
1051 q++;
1052 break;
1053 }
1054 q+=FormatMagickString(q,extent,"%lu",log_info->generation %
1055 log_info->generations);
1056 break;
1057 }
1058 case 'n':
1059 {
1060 q+=CopyMagickString(q,GetLogName(),extent);
1061 break;
1062 }
1063 case 'p':
1064 {
1065 q+=FormatMagickString(q,extent,"%ld",(long) getpid());
1066 break;
1067 }
1068 case 'v':
1069 {
1070 q+=CopyMagickString(q,MagickLibVersionText,extent);
1071 break;
1072 }
1073 case '%':
1074 {
1075 *q++=(*p);
1076 break;
1077 }
1078 default:
1079 {
1080 *q++='%';
1081 *q++=(*p);
1082 break;
1083 }
1084 }
1085 }
1086 *q='\0';
1087 return(filename);
1088}
1089
1090MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
1091 const char *function,const unsigned long line,const char *format,
1092 va_list operands)
1093{
1094 char
1095 event[MaxTextExtent],
1096 *text;
1097
1098 const char
1099 *domain;
1100
1101 ExceptionInfo
1102 *exception;
1103
1104 int
1105 n;
1106
1107 LogInfo
1108 *log_info;
1109
1110 if (IsEventLogging() == MagickFalse)
1111 return(MagickFalse);
1112 exception=AcquireExceptionInfo();
1113 log_info=(LogInfo *) GetLogInfo("*",exception);
1114 exception=DestroyExceptionInfo(exception);
1115 AcquireSemaphoreInfo(&log_semaphore);
1116 if ((log_info->event_mask & type) == 0)
1117 {
1118 RelinquishSemaphoreInfo(log_semaphore);
1119 return(MagickTrue);
1120 }
1121 domain=MagickOptionToMnemonic(MagickLogEventOptions,type);
1122#if defined(MAGICKCORE_HAVE_VSNPRINTF)
1123 n=vsnprintf(event,MaxTextExtent,format,operands);
1124#else
1125 n=vsprintf(event,format,operands);
1126#endif
1127 if (n < 0)
1128 event[MaxTextExtent-1]='\0';
1129 text=TranslateEvent(type,module,function,line,domain,event);
1130 if (text == (char *) NULL)
1131 {
1132 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1133 RelinquishSemaphoreInfo(log_semaphore);
1134 return(MagickFalse);
1135 }
1136 if ((log_info->handler_mask & ConsoleHandler) != 0)
1137 {
1138 (void) fprintf(stderr,"%s\n",text);
1139 (void) fflush(stderr);
1140 }
1141 if ((log_info->handler_mask & DebugHandler) != 0)
1142 {
1143#if defined(__WINDOWS__)
1144 OutputDebugString(text);
1145#endif
1146 }
1147 if ((log_info->handler_mask & EventHandler) != 0)
1148 {
1149#if defined(__WINDOWS__)
1150 (void) NTReportEvent(text,MagickFalse);
1151#endif
1152 }
1153 if ((log_info->handler_mask & FileHandler) != 0)
1154 {
1155 struct stat
1156 file_info;
1157
1158 file_info.st_size=0;
1159 if (log_info->file != (FILE *) NULL)
1160 (void) fstat(fileno(log_info->file),&file_info);
1161 if (file_info.st_size > (off_t) (1024*1024*log_info->limit))
1162 {
1163 (void) fprintf(log_info->file,"</log>\n");
1164 (void) fclose(log_info->file);
1165 log_info->file=(FILE *) NULL;
1166 }
1167 if (log_info->file == (FILE *) NULL)
1168 {
1169 char
1170 *filename;
1171
1172 filename=TranslateFilename(log_info);
1173 if (filename == (char *) NULL)
1174 {
1175 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1176 RelinquishSemaphoreInfo(log_semaphore);
1177 return(MagickFalse);
1178 }
1179 log_info->append=IsPathAccessible(filename);
1180 log_info->file=OpenMagickStream(filename,"ab");
1181 filename=(char *) RelinquishMagickMemory(filename);
1182 if (log_info->file == (FILE *) NULL)
1183 {
1184 RelinquishSemaphoreInfo(log_semaphore);
1185 return(MagickFalse);
1186 }
1187 log_info->generation++;
1188 if (log_info->append == MagickFalse)
1189 {
1190 (void) fprintf(log_info->file,"<?xml version=\"1.0\" "
1191 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1192 (void) fprintf(log_info->file,"<log>\n");
1193 }
1194 }
1195 (void) fprintf(log_info->file,"%s\n",text);
1196 (void) fflush(log_info->file);
1197 }
1198 if ((log_info->handler_mask & StdoutHandler) != 0)
1199 {
1200 (void) fprintf(stdout,"%s\n",text);
1201 (void) fflush(stdout);
1202 }
1203 if ((log_info->handler_mask & StderrHandler) != 0)
1204 {
1205 (void) fprintf(stderr,"%s\n",text);
1206 (void) fflush(stderr);
1207 }
1208 text=(char *) RelinquishMagickMemory(text);
1209 (void) ContinueTimer((TimerInfo *) &log_info->timer);
1210 RelinquishSemaphoreInfo(log_semaphore);
1211 return(MagickTrue);
1212}
1213
1214MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
1215 const char *function,const unsigned long line,const char *format,...)
1216{
1217 va_list
1218 operands;
1219
1220 MagickBooleanType
1221 status;
1222
1223 va_start(operands,format);
1224 status=LogMagickEventList(type,module,function,line,format,operands);
1225 va_end(operands);
1226 return(status);
1227}
1228
1229/*
1230%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1231% %
1232% %
1233% %
1234+ L o a d L o g L i s t %
1235% %
1236% %
1237% %
1238%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1239%
1240% LoadLogList() loads the log configuration file which provides a
1241% mapping between log attributes and log name.
1242%
1243% The format of the LoadLogList method is:
1244%
1245% MagickBooleanType LoadLogList(const char *xml,const char *filename,
1246% const unsigned long depth,ExceptionInfo *exception)
1247%
1248% A description of each parameter follows:
1249%
1250% o xml: The log list in XML format.
1251%
1252% o filename: The log list filename.
1253%
1254% o depth: depth of <include /> statements.
1255%
1256% o exception: return any errors or warnings in this structure.
1257%
1258*/
1259static MagickBooleanType LoadLogList(const char *xml,const char *filename,
1260 const unsigned long depth,ExceptionInfo *exception)
1261{
1262 char
1263 keyword[MaxTextExtent],
1264 *token;
1265
1266 const char
1267 *q;
1268
1269 LogInfo
1270 *log_info = (LogInfo *) NULL;
1271
1272 MagickStatusType
1273 status;
1274
1275 /*
1276 Load the log map file.
1277 */
1278 if (xml == (const char *) NULL)
1279 return(MagickFalse);
1280 if (log_list == (LinkedListInfo *) NULL)
1281 {
1282 log_list=NewLinkedList(0);
1283 if (log_list == (LinkedListInfo *) NULL)
1284 {
1285 ThrowFileException(exception,ResourceLimitError,
1286 "MemoryAllocationFailed",filename);
1287 return(MagickFalse);
1288 }
1289 }
1290 status=MagickTrue;
1291 token=AcquireString((const char *) xml);
1292 for (q=(const char *) xml; *q != '\0'; )
1293 {
1294 /*
1295 Interpret XML.
1296 */
1297 GetMagickToken(q,&q,token);
1298 if (*token == '\0')
1299 break;
1300 (void) CopyMagickString(keyword,token,MaxTextExtent);
1301 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1302 {
1303 /*
1304 Doctype element.
1305 */
1306 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1307 GetMagickToken(q,&q,token);
1308 continue;
1309 }
1310 if (LocaleNCompare(keyword,"<!--",4) == 0)
1311 {
1312 /*
1313 Comment element.
1314 */
1315 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1316 GetMagickToken(q,&q,token);
1317 continue;
1318 }
1319 if (LocaleCompare(keyword,"<include") == 0)
1320 {
1321 /*
1322 Include element.
1323 */
1324 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1325 {
1326 (void) CopyMagickString(keyword,token,MaxTextExtent);
1327 GetMagickToken(q,&q,token);
1328 if (*token != '=')
1329 continue;
1330 GetMagickToken(q,&q,token);
1331 if (LocaleCompare(keyword,"file") == 0)
1332 {
1333 if (depth > 200)
1334 (void) ThrowMagickException(exception,GetMagickModule(),
1335 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1336 else
1337 {
1338 char
1339 path[MaxTextExtent],
1340 *xml;
1341
1342 GetPathComponent(filename,HeadPath,path);
1343 if (*path != '\0')
1344 (void) ConcatenateMagickString(path,DirectorySeparator,
1345 MaxTextExtent);
1346 if (*token == *DirectorySeparator)
1347 (void) CopyMagickString(path,token,MaxTextExtent);
1348 else
1349 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1350 xml=FileToString(path,~0,exception);
1351 if (xml != (char *) NULL)
1352 {
1353 status|=LoadLogList(xml,path,depth+1,exception);
1354 xml=DestroyString(xml);
1355 }
1356 }
1357 }
1358 }
1359 continue;
1360 }
1361 if (LocaleCompare(keyword,"<logmap>") == 0)
1362 {
1363 /*
1364 Allocate memory for the log list.
1365 */
1366 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
1367 if (log_info == (LogInfo *) NULL)
1368 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1369 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1370 log_info->path=ConstantString(filename);
1371 GetTimerInfo((TimerInfo *) &log_info->timer);
1372 log_info->signature=MagickSignature;
1373 continue;
1374 }
1375 if (log_info == (LogInfo *) NULL)
1376 continue;
1377 if (LocaleCompare(keyword,"</logmap>") == 0)
1378 {
1379 status=AppendValueToLinkedList(log_list,log_info);
1380 if (status == MagickFalse)
1381 (void) ThrowMagickException(exception,GetMagickModule(),
1382 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
1383 log_info=(LogInfo *) NULL;
1384 }
1385 GetMagickToken(q,(const char **) NULL,token);
1386 if (*token != '=')
1387 continue;
1388 GetMagickToken(q,&q,token);
1389 GetMagickToken(q,&q,token);
1390 switch (*keyword)
1391 {
1392 case 'E':
1393 case 'e':
1394 {
1395 if (LocaleCompare((char *) keyword,"events") == 0)
1396 {
1397 log_info->event_mask=(LogEventType) (log_info->event_mask |
1398 ParseMagickOption(MagickLogEventOptions,MagickTrue,token));
1399 break;
1400 }
1401 break;
1402 }
1403 case 'F':
1404 case 'f':
1405 {
1406 if (LocaleCompare((char *) keyword,"filename") == 0)
1407 {
1408 if (log_info->filename != (char *) NULL)
1409 log_info->filename=(char *)
1410 RelinquishMagickMemory(log_info->filename);
1411 log_info->filename=ConstantString(token);
1412 break;
1413 }
1414 if (LocaleCompare((char *) keyword,"format") == 0)
1415 {
1416 if (log_info->format != (char *) NULL)
1417 log_info->format=(char *)
1418 RelinquishMagickMemory(log_info->format);
1419 log_info->format=ConstantString(token);
1420 break;
1421 }
1422 break;
1423 }
1424 case 'G':
1425 case 'g':
1426 {
1427 if (LocaleCompare((char *) keyword,"generations") == 0)
1428 {
1429 if (LocaleCompare(token,"unlimited") == 0)
1430 {
1431 log_info->generations=(~0UL);
1432 break;
1433 }
1434 log_info->generations=(unsigned long) atol(token);
1435 break;
1436 }
1437 break;
1438 }
1439 case 'L':
1440 case 'l':
1441 {
1442 if (LocaleCompare((char *) keyword,"limit") == 0)
1443 {
1444 if (LocaleCompare(token,"unlimited") == 0)
1445 {
1446 log_info->limit=(~0UL);
1447 break;
1448 }
1449 log_info->limit=(unsigned long) atol(token);
1450 break;
1451 }
1452 break;
1453 }
1454 case 'O':
1455 case 'o':
1456 {
1457 if (LocaleCompare((char *) keyword,"output") == 0)
1458 {
1459 log_info->handler_mask=(LogHandlerType)
1460 (log_info->handler_mask | ParseLogHandlers(token));
1461 break;
1462 }
1463 break;
1464 }
1465 default:
1466 break;
1467 }
1468 }
1469 token=DestroyString(token);
1470 if (log_list == (LinkedListInfo *) NULL)
1471 return(MagickFalse);
1472 return(status != 0 ? MagickTrue : MagickFalse);
1473}
1474
1475/*
1476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477% %
1478% %
1479% %
1480% L o a d L o g L i s t s %
1481% %
1482% %
1483% %
1484%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1485%
1486% LoadLogLists() loads one or more log configuration file which provides a
1487% mapping between log attributes and log name.
1488%
1489% The format of the LoadLogLists method is:
1490%
1491% MagickBooleanType LoadLogLists(const char *filename,
1492% ExceptionInfo *exception)
1493%
1494% A description of each parameter follows:
1495%
1496% o filename: the log configuration filename.
1497%
1498% o exception: return any errors or warnings in this structure.
1499%
1500*/
1501static MagickBooleanType LoadLogLists(const char *filename,
1502 ExceptionInfo *exception)
1503{
1504#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1505 return(LoadLogList(LogMap,"built-in",0,exception));
1506#else
1507 const StringInfo
1508 *option;
1509
1510 LinkedListInfo
1511 *options;
1512
1513 MagickStatusType
1514 status;
1515
1516 status=MagickFalse;
1517 options=GetConfigureOptions(filename,exception);
1518 option=(const StringInfo *) GetNextValueInLinkedList(options);
1519 while (option != (const StringInfo *) NULL)
1520 {
1521 status|=LoadLogList((const char *) GetStringInfoDatum(option),
1522 GetStringInfoPath(option),0,exception);
1523 option=(const StringInfo *) GetNextValueInLinkedList(options);
1524 }
1525 options=DestroyConfigureOptions(options);
1526 if ((log_list == (LinkedListInfo *) NULL) ||
1527 (IsLinkedListEmpty(log_list) != MagickFalse))
1528 status|=LoadLogList(LogMap,"built-in",0,exception);
1529 return(status != 0 ? MagickTrue : MagickFalse);
1530#endif
1531}
1532
1533/*
1534%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1535% %
1536% %
1537% %
1538+ P a r s e L o g H a n d l e r s %
1539% %
1540% %
1541% %
1542%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1543%
1544% ParseLogHandlers() parses a string defining which handlers takes a log
1545% message and exports them.
1546%
1547% The format of the ParseLogHandlers method is:
1548%
1549% LogHandlerType ParseLogHandlers(const char *handlers)
1550%
1551% A description of each parameter follows:
1552%
1553% o handlers: one or more handlers separated by commas.
1554%
1555*/
1556static LogHandlerType ParseLogHandlers(const char *handlers)
1557{
1558 LogHandlerType
1559 handler_mask;
1560
1561 register const char
1562 *p;
1563
1564 register long
1565 i;
1566
1567 size_t
1568 length;
1569
1570 handler_mask=NoHandler;
1571 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1572 {
1573 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1574 (*p == ',')))
1575 p++;
1576 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1577 {
1578 length=strlen(LogHandlers[i].name);
1579 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1580 {
1581 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1582 break;
1583 }
1584 }
1585 if (LogHandlers[i].name == (char *) NULL)
1586 return(UndefinedHandler);
1587 }
1588 return(handler_mask);
1589}
1590
1591/*
1592%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1593% %
1594% %
1595% %
1596% S e t L o g E v e n t M a s k %
1597% %
1598% %
1599% %
1600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1601%
1602% SetLogEventMask() accepts a list that determines which events to log. All
1603% other events are ignored. By default, no debug is enabled. This method
1604% returns the previous log event mask.
1605%
1606% The format of the SetLogEventMask method is:
1607%
1608% LogEventType SetLogEventMask(const char *events)
1609%
1610% A description of each parameter follows:
1611%
1612% o events: log these events.
1613%
1614*/
1615MagickExport LogEventType SetLogEventMask(const char *events)
1616{
1617 ExceptionInfo
1618 *exception;
1619
1620 LogInfo
1621 *log_info;
1622
1623 long
1624 option;
1625
1626 exception=AcquireExceptionInfo();
1627 log_info=(LogInfo *) GetLogInfo("*",exception);
1628 exception=DestroyExceptionInfo(exception);
1629 option=ParseMagickOption(MagickLogEventOptions,MagickTrue,events);
1630 AcquireSemaphoreInfo(&log_semaphore);
1631 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1632 log_info->event_mask=(LogEventType) option;
1633 if (option == -1)
1634 log_info->event_mask=UndefinedEvents;
1635 RelinquishSemaphoreInfo(log_semaphore);
1636 return(log_info->event_mask);
1637}
1638
1639/*
1640%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1641% %
1642% %
1643% %
1644% S e t L o g F o r m a t %
1645% %
1646% %
1647% %
1648%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1649%
1650% SetLogFormat() sets the format for the "human readable" log record.
1651%
1652% The format of the LogMagickFormat method is:
1653%
1654% SetLogFormat(const char *format)
1655%
1656% A description of each parameter follows:
1657%
1658% o format: the log record format.
1659%
1660*/
1661MagickExport void SetLogFormat(const char *format)
1662{
1663 LogInfo
1664 *log_info;
1665
1666 ExceptionInfo
1667 *exception;
1668
1669 exception=AcquireExceptionInfo();
1670 log_info=(LogInfo *) GetLogInfo("*",exception);
1671 exception=DestroyExceptionInfo(exception);
1672 AcquireSemaphoreInfo(&log_semaphore);
1673 if (log_info->format != (char *) NULL)
1674 log_info->format=DestroyString(log_info->format);
1675 log_info->format=ConstantString(format);
1676 RelinquishSemaphoreInfo(log_semaphore);
1677}
1678
1679/*
1680%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1681% %
1682% %
1683% %
1684% S e t L o g N a m e %
1685% %
1686% %
1687% %
1688%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1689%
1690% SetLogName() sets the log name and returns it.
1691%
1692% The format of the SetLogName method is:
1693%
1694% const char *SetLogName(const char *name)
1695%
1696% A description of each parameter follows:
1697%
1698% o log_name: SetLogName() returns the current client name.
1699%
1700% o name: Specifies the new client name.
1701%
1702*/
1703MagickExport const char *SetLogName(const char *name)
1704{
1705 if ((name != (char *) NULL) && (*name != '\0'))
1706 (void) CopyMagickString(log_name,name,MaxTextExtent);
1707 return(log_name);
1708}