blob: 3b92cd7a677a840f24b6b37af2e3950d15f442eb [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% September 2002 %
18% %
19% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/client.h"
45#include "MagickCore/configure.h"
cristy9639ba32011-09-05 15:09:42 +000046#include "MagickCore/configure-private.h"
cristy4c08aed2011-07-01 19:47:50 +000047#include "MagickCore/exception.h"
48#include "MagickCore/exception-private.h"
49#include "MagickCore/hashmap.h"
50#include "MagickCore/log.h"
cristy5ff4eaf2011-09-03 01:38:02 +000051#include "MagickCore/log-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/memory_.h"
53#include "MagickCore/option.h"
54#include "MagickCore/semaphore.h"
55#include "MagickCore/timer.h"
56#include "MagickCore/string_.h"
57#include "MagickCore/string-private.h"
58#include "MagickCore/token.h"
59#include "MagickCore/thread_.h"
60#include "MagickCore/thread-private.h"
61#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000062#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000063#include "MagickCore/version.h"
64#include "MagickCore/xml-tree.h"
cristy3ed852e2009-09-05 21:47:34 +000065
66/*
67 Define declarations.
68*/
69#define LogFilename "log.xml"
70
71/*
72 Typedef declarations.
73*/
74typedef enum
75{
76 UndefinedHandler = 0x0000,
77 NoHandler = 0x0000,
78 ConsoleHandler = 0x0001,
79 StdoutHandler = 0x0002,
80 StderrHandler = 0x0004,
81 FileHandler = 0x0008,
82 DebugHandler = 0x0010,
83 EventHandler = 0x0020
84} LogHandlerType;
85
86typedef struct _EventInfo
87{
88 char
89 *name;
90
91 LogEventType
92 event;
93} EventInfo;
94
95typedef struct _HandlerInfo
96{
97 const char
98 *name;
99
100 LogHandlerType
101 handler;
102} HandlerInfo;
103
104struct _LogInfo
105{
106 LogEventType
107 event_mask;
108
109 LogHandlerType
110 handler_mask;
111
112 char
113 *path,
114 *name,
115 *filename,
116 *format;
117
cristybb503372010-05-27 20:51:26 +0000118 size_t
cristy3ed852e2009-09-05 21:47:34 +0000119 generations,
120 limit;
121
122 FILE
123 *file;
124
cristybb503372010-05-27 20:51:26 +0000125 size_t
cristy3ed852e2009-09-05 21:47:34 +0000126 generation;
127
128 MagickBooleanType
129 append,
130 stealth;
131
132 TimerInfo
133 timer;
134
cristybb503372010-05-27 20:51:26 +0000135 size_t
cristy3ed852e2009-09-05 21:47:34 +0000136 signature;
137};
cristy85340292009-10-21 19:32:19 +0000138
139typedef struct _LogMapInfo
140{
141 const LogEventType
142 event_mask;
143
144 const LogHandlerType
145 handler_mask;
146
147 const char
148 *filename,
149 *format;
150} LogMapInfo;
cristy3ed852e2009-09-05 21:47:34 +0000151
152/*
cristy85340292009-10-21 19:32:19 +0000153 Static declarations.
cristy3ed852e2009-09-05 21:47:34 +0000154*/
155static const HandlerInfo
156 LogHandlers[] =
157 {
cristy398fd022013-06-26 23:08:33 +0000158 { "Console", ConsoleHandler },
159 { "Debug", DebugHandler },
160 { "Event", EventHandler },
161 { "File", FileHandler },
162 { "None", NoHandler },
163 { "Stderr", StderrHandler },
164 { "Stdout", StdoutHandler },
cristy3ed852e2009-09-05 21:47:34 +0000165 { (char *) NULL, UndefinedHandler }
166 };
167
cristy85340292009-10-21 19:32:19 +0000168static const LogMapInfo
169 LogMap[] =
170 {
cristy398fd022013-06-26 23:08:33 +0000171 { NoEvents, ConsoleHandler, "Magick-%g.log",
172 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n %e" }
cristy85340292009-10-21 19:32:19 +0000173 };
174
cristy3ed852e2009-09-05 21:47:34 +0000175static char
176 log_name[MaxTextExtent] = "Magick";
177
178static LinkedListInfo
179 *log_list = (LinkedListInfo *) NULL;
180
181static SemaphoreInfo
182 *log_semaphore = (SemaphoreInfo *) NULL;
183
184static volatile MagickBooleanType
185 instantiate_log = MagickFalse;
186
187/*
188 Forward declarations.
189*/
190static LogHandlerType
191 ParseLogHandlers(const char *);
192
193static LogInfo
194 *GetLogInfo(const char *,ExceptionInfo *);
195
196static MagickBooleanType
197 InitializeLogList(ExceptionInfo *),
198 LoadLogLists(const char *,ExceptionInfo *);
199
200/*
201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202% %
203% %
204% %
205% C l o s e M a g i c k L o g %
206% %
207% %
208% %
209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210%
211% CloseMagickLog() closes the Magick log.
212%
213% The format of the CloseMagickLog method is:
214%
215% CloseMagickLog(void)
216%
217*/
218MagickExport void CloseMagickLog(void)
219{
220 ExceptionInfo
221 *exception;
222
223 LogInfo
224 *log_info;
225
226 if (IsEventLogging() == MagickFalse)
227 return;
228 exception=AcquireExceptionInfo();
229 log_info=GetLogInfo("*",exception);
230 exception=DestroyExceptionInfo(exception);
cristyf84a1932010-01-03 18:00:18 +0000231 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000232 if (log_info->file != (FILE *) NULL)
233 {
cristy398fd022013-06-26 23:08:33 +0000234 (void) FormatLocaleFile(log_info->file,"</log>\n");
cristy3ed852e2009-09-05 21:47:34 +0000235 (void) fclose(log_info->file);
236 log_info->file=(FILE *) NULL;
237 }
cristyf84a1932010-01-03 18:00:18 +0000238 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000239}
240
241/*
242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243% %
244% %
245% %
cristy3ed852e2009-09-05 21:47:34 +0000246+ G e t L o g I n f o %
247% %
248% %
249% %
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251%
252% GetLogInfo() searches the log list for the specified name and if found
253% returns attributes for that log.
254%
255% The format of the GetLogInfo method is:
256%
257% LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
258%
259% A description of each parameter follows:
260%
261% o name: the log name.
262%
263% o exception: return any errors or warnings in this structure.
264%
265*/
266static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
267{
268 register LogInfo
269 *p;
270
271 assert(exception != (ExceptionInfo *) NULL);
272 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
273 if (InitializeLogList(exception) == MagickFalse)
274 return((LogInfo *) NULL);
275 if ((log_list == (LinkedListInfo *) NULL) ||
276 (IsLinkedListEmpty(log_list) != MagickFalse))
277 return((LogInfo *) NULL);
278 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
279 return((LogInfo *) GetValueFromLinkedList(log_list,0));
280 /*
281 Search for log tag.
282 */
cristyf84a1932010-01-03 18:00:18 +0000283 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000284 ResetLinkedListIterator(log_list);
285 p=(LogInfo *) GetNextValueInLinkedList(log_list);
286 while (p != (LogInfo *) NULL)
287 {
288 if (LocaleCompare(name,p->name) == 0)
289 break;
290 p=(LogInfo *) GetNextValueInLinkedList(log_list);
291 }
292 if (p != (LogInfo *) NULL)
293 (void) InsertValueInLinkedList(log_list,0,
294 RemoveElementByValueFromLinkedList(log_list,p));
cristyf84a1932010-01-03 18:00:18 +0000295 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000296 return(p);
297}
298
299/*
300%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301% %
302% %
303% %
304% G e t L o g I n f o L i s t %
305% %
306% %
307% %
308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309%
310% GetLogInfoList() returns any logs that match the specified pattern.
311%
312% The format of the GetLogInfoList function is:
313%
314% const LogInfo **GetLogInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000315% size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000316%
317% A description of each parameter follows:
318%
319% o pattern: Specifies a pointer to a text string containing a pattern.
320%
321% o number_preferences: This integer returns the number of logs in the list.
322%
323% o exception: return any errors or warnings in this structure.
324%
325*/
326#if defined(__cplusplus) || defined(c_plusplus)
327extern "C" {
328#endif
329
330static int LogInfoCompare(const void *x,const void *y)
331{
332 const LogInfo
333 **p,
334 **q;
335
336 p=(const LogInfo **) x,
337 q=(const LogInfo **) y;
338 if (LocaleCompare((*p)->path,(*q)->path) == 0)
339 return(LocaleCompare((*p)->name,(*q)->name));
340 return(LocaleCompare((*p)->path,(*q)->path));
341}
342
343#if defined(__cplusplus) || defined(c_plusplus)
344}
345#endif
346
347MagickExport const LogInfo **GetLogInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000348 size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000349{
350 const LogInfo
351 **preferences;
352
353 register const LogInfo
354 *p;
355
cristybb503372010-05-27 20:51:26 +0000356 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000357 i;
358
359 /*
360 Allocate log list.
361 */
362 assert(pattern != (char *) NULL);
363 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000364 assert(number_preferences != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000365 *number_preferences=0;
366 p=GetLogInfo("*",exception);
367 if (p == (const LogInfo *) NULL)
368 return((const LogInfo **) NULL);
369 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
370 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
371 if (preferences == (const LogInfo **) NULL)
372 return((const LogInfo **) NULL);
373 /*
374 Generate log list.
375 */
cristyf84a1932010-01-03 18:00:18 +0000376 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000377 ResetLinkedListIterator(log_list);
378 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
379 for (i=0; p != (const LogInfo *) NULL; )
380 {
381 if ((p->stealth == MagickFalse) &&
382 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
383 preferences[i++]=p;
384 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
385 }
cristyf84a1932010-01-03 18:00:18 +0000386 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000387 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
388 preferences[i]=(LogInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000389 *number_preferences=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000390 return(preferences);
391}
392
393/*
394%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395% %
396% %
397% %
398% G e t L o g L i s t %
399% %
400% %
401% %
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403%
404% GetLogList() returns any logs that match the specified pattern.
405%
406% The format of the GetLogList function is:
407%
cristybb503372010-05-27 20:51:26 +0000408% char **GetLogList(const char *pattern,size_t *number_preferences,
cristy3ed852e2009-09-05 21:47:34 +0000409% ExceptionInfo *exception)
410%
411% A description of each parameter follows:
412%
413% o pattern: Specifies a pointer to a text string containing a pattern.
414%
415% o number_preferences: This integer returns the number of logs in the list.
416%
417% o exception: return any errors or warnings in this structure.
418%
419*/
420
421#if defined(__cplusplus) || defined(c_plusplus)
422extern "C" {
423#endif
424
425static int LogCompare(const void *x,const void *y)
426{
427 register const char
428 **p,
429 **q;
430
431 p=(const char **) x;
432 q=(const char **) y;
433 return(LocaleCompare(*p,*q));
434}
435
436#if defined(__cplusplus) || defined(c_plusplus)
437}
438#endif
439
440MagickExport char **GetLogList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000441 size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000442{
443 char
444 **preferences;
445
446 register const LogInfo
447 *p;
448
cristybb503372010-05-27 20:51:26 +0000449 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000450 i;
451
452 /*
453 Allocate log list.
454 */
455 assert(pattern != (char *) NULL);
456 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000457 assert(number_preferences != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000458 *number_preferences=0;
459 p=GetLogInfo("*",exception);
460 if (p == (const LogInfo *) NULL)
461 return((char **) NULL);
462 preferences=(char **) AcquireQuantumMemory((size_t)
463 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
464 if (preferences == (char **) NULL)
465 return((char **) NULL);
466 /*
467 Generate log list.
468 */
cristyf84a1932010-01-03 18:00:18 +0000469 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000470 ResetLinkedListIterator(log_list);
471 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
472 for (i=0; p != (const LogInfo *) NULL; )
473 {
474 if ((p->stealth == MagickFalse) &&
475 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
476 preferences[i++]=ConstantString(p->name);
477 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
478 }
cristyf84a1932010-01-03 18:00:18 +0000479 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000480 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
481 preferences[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000482 *number_preferences=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000483 return(preferences);
484}
485
486/*
487%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
488% %
489% %
490% %
491% G e t L o g N a m e %
492% %
493% %
494% %
495%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496%
497% GetLogName() returns the current log name.
498%
499% The format of the GetLogName method is:
500%
501% const char *GetLogName(void)
502%
503*/
504MagickExport const char *GetLogName(void)
505{
506 return(log_name);
507}
508
509/*
510%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
511% %
512% %
513% %
514+ I n i t i a l i z e L o g L i s t %
515% %
516% %
517% %
518%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
519%
520% InitializeLogList() initialize the log list.
521%
522% The format of the InitializeLogList method is:
523%
524% MagickBooleanType InitializeLogList(ExceptionInfo *exception)
525%
526% A description of each parameter follows.
527%
528% o exception: return any errors or warnings in this structure.
529%
530*/
531static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
532{
533 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
534 {
cristy4e1dff62009-10-25 20:36:03 +0000535 if (log_semaphore == (SemaphoreInfo *) NULL)
536 AcquireSemaphoreInfo(&log_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000537 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000538 if ((log_list == (LinkedListInfo *) NULL) &&
539 (instantiate_log == MagickFalse))
540 {
541 (void) LoadLogLists(LogFilename,exception);
542 instantiate_log=MagickTrue;
543 }
cristyf84a1932010-01-03 18:00:18 +0000544 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000545 }
546 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
547}
548
549/*
550%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551% %
552% %
553% %
554% I s E v e n t L o g g i n g %
555% %
556% %
557% %
558%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559%
560% IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
561% MagickFalse.
562%
563% The format of the IsEventLogging method is:
564%
565% MagickBooleanType IsEventLogging(void)
566%
567*/
568MagickExport MagickBooleanType IsEventLogging(void)
569{
570 const LogInfo
571 *log_info;
572
573 ExceptionInfo
574 *exception;
575
576 if ((log_list == (LinkedListInfo *) NULL) ||
577 (IsLinkedListEmpty(log_list) != MagickFalse))
578 return(MagickFalse);
579 exception=AcquireExceptionInfo();
580 log_info=GetLogInfo("*",exception);
581 exception=DestroyExceptionInfo(exception);
582 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
583}
584/*
585%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
586% %
587% %
588% %
589% L i s t L o g I n f o %
590% %
591% %
592% %
593%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
594%
595% ListLogInfo() lists the log info to a file.
596%
597% The format of the ListLogInfo method is:
598%
599% MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
600%
601% A description of each parameter follows.
602%
603% o file: An pointer to a FILE.
604%
605% o exception: return any errors or warnings in this structure.
606%
607*/
608MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
609{
610#define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
611
cristy3ed852e2009-09-05 21:47:34 +0000612 const char
613 *path;
614
615 const LogInfo
616 **log_info;
617
cristybb503372010-05-27 20:51:26 +0000618 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000619 i;
620
cristybb503372010-05-27 20:51:26 +0000621 size_t
cristy3ed852e2009-09-05 21:47:34 +0000622 number_aliases;
623
cristy9d314ff2011-03-09 01:30:28 +0000624 ssize_t
625 j;
626
cristy3ed852e2009-09-05 21:47:34 +0000627 if (file == (const FILE *) NULL)
628 file=stdout;
629 log_info=GetLogInfoList("*",&number_aliases,exception);
630 if (log_info == (const LogInfo **) NULL)
631 return(MagickFalse);
632 j=0;
633 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000634 for (i=0; i < (ssize_t) number_aliases; i++)
cristy3ed852e2009-09-05 21:47:34 +0000635 {
636 if (log_info[i]->stealth != MagickFalse)
637 continue;
638 if ((path == (const char *) NULL) ||
639 (LocaleCompare(path,log_info[i]->path) != 0))
640 {
cristy398fd022013-06-26 23:08:33 +0000641 size_t
642 length;
643
cristy3ed852e2009-09-05 21:47:34 +0000644 if (log_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000645 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
cristy398fd022013-06-26 23:08:33 +0000646 length=0;
647 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
648 {
649 size_t
650 mask;
651
cristyfb8f6e12013-07-10 12:35:27 +0000652 mask=1;
653 mask<<=j;
cristy398fd022013-06-26 23:08:33 +0000654 if ((log_info[i]->handler_mask & mask) != 0)
655 {
656 (void) FormatLocaleFile(file,"%s ",LogHandlers[j].name);
657 length+=strlen(LogHandlers[j].name);
658 }
659 }
660 for (j=(ssize_t) length; j <= 12; j++)
661 (void) FormatLocaleFile(file," ");
662 (void) FormatLocaleFile(file," Generations Limit Format\n");
663 (void) FormatLocaleFile(file,"-----------------------------------------"
664 "--------------------------------------\n");
cristy3ed852e2009-09-05 21:47:34 +0000665 }
666 path=log_info[i]->path;
667 if (log_info[i]->filename != (char *) NULL)
668 {
cristyb51dff52011-05-19 16:55:47 +0000669 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
cristybb503372010-05-27 20:51:26 +0000670 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
cristyb51dff52011-05-19 16:55:47 +0000671 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000672 }
cristyb51dff52011-05-19 16:55:47 +0000673 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
cristy398fd022013-06-26 23:08:33 +0000674 (void) FormatLocaleFile(file,"%8g ",(double) log_info[i]->limit);
cristy3ed852e2009-09-05 21:47:34 +0000675 if (log_info[i]->format != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000676 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
677 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000678 }
679 (void) fflush(file);
680 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
681 return(MagickTrue);
682}
683
684/*
685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686% %
687% %
688% %
cristyf34a1452009-10-24 22:29:27 +0000689+ L o g C o m p o n e n t G e n e s i s %
690% %
691% %
692% %
693%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
694%
695% LogComponentGenesis() instantiates the log component.
696%
697% The format of the LogComponentGenesis method is:
698%
699% MagickBooleanType LogComponentGenesis(void)
700%
701*/
cristy5ff4eaf2011-09-03 01:38:02 +0000702MagickPrivate MagickBooleanType LogComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000703{
cristy85dfbbb2013-08-02 16:02:35 +0000704 ExceptionInfo
705 *exception;
706
cristy165b6092009-10-26 13:52:10 +0000707 AcquireSemaphoreInfo(&log_semaphore);
cristy85dfbbb2013-08-02 16:02:35 +0000708 exception=AcquireExceptionInfo();
709 (void) GetLogInfo("*",exception);
710 exception=DestroyExceptionInfo(exception);
cristyf34a1452009-10-24 22:29:27 +0000711 return(MagickTrue);
712}
713
714/*
715%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
716% %
717% %
718% %
719+ L o g C o m p o n e n t T e r m i n u s %
720% %
721% %
722% %
723%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
724%
725% LogComponentTerminus() destroys the logging component.
726%
727% The format of the LogComponentTerminus method is:
728%
729% LogComponentTerminus(void)
730%
731*/
732
733static void *DestroyLogElement(void *log_info)
734{
735 register LogInfo
736 *p;
737
738 p=(LogInfo *) log_info;
739 if (p->file != (FILE *) NULL)
740 {
cristy398fd022013-06-26 23:08:33 +0000741 (void) FormatLocaleFile(p->file,"</log>\n");
cristyf34a1452009-10-24 22:29:27 +0000742 (void) fclose(p->file);
743 p->file=(FILE *) NULL;
744 }
cristy5965b492013-08-17 14:04:22 +0000745 if (p->format != (char *) NULL)
746 p->format=DestroyString(p->format);
747 if (p->path != (char *) NULL)
748 p->path=DestroyString(p->path);
749 if (p->filename != (char *) NULL)
750 p->filename=DestroyString(p->filename);
cristyf34a1452009-10-24 22:29:27 +0000751 p=(LogInfo *) RelinquishMagickMemory(p);
752 return((void *) NULL);
753}
754
cristy5ff4eaf2011-09-03 01:38:02 +0000755MagickPrivate void LogComponentTerminus(void)
cristyf34a1452009-10-24 22:29:27 +0000756{
cristy18b17442009-10-25 18:36:48 +0000757 if (log_semaphore == (SemaphoreInfo *) NULL)
758 AcquireSemaphoreInfo(&log_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000759 LockSemaphoreInfo(log_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000760 if (log_list != (LinkedListInfo *) NULL)
761 log_list=DestroyLinkedList(log_list,DestroyLogElement);
762 instantiate_log=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000763 UnlockSemaphoreInfo(log_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000764 DestroySemaphoreInfo(&log_semaphore);
765}
766
767/*
768%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769% %
770% %
771% %
cristy3ed852e2009-09-05 21:47:34 +0000772% L o g M a g i c k E v e n t %
773% %
774% %
775% %
776%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777%
778% LogMagickEvent() logs an event as determined by the log configuration file.
779% If an error occurs, MagickFalse is returned otherwise MagickTrue.
780%
781% The format of the LogMagickEvent method is:
782%
783% MagickBooleanType LogMagickEvent(const LogEventType type,
cristybb503372010-05-27 20:51:26 +0000784% const char *module,const char *function,const size_t line,
cristy3ed852e2009-09-05 21:47:34 +0000785% const char *format,...)
786%
787% A description of each parameter follows:
788%
789% o type: the event type.
790%
791% o filename: the source module filename.
792%
793% o function: the function name.
794%
795% o line: the line number of the source module.
796%
797% o format: the output format.
798%
799*/
800static char *TranslateEvent(const LogEventType magick_unused(type),
cristybb503372010-05-27 20:51:26 +0000801 const char *module,const char *function,const size_t line,
cristy3ed852e2009-09-05 21:47:34 +0000802 const char *domain,const char *event)
803{
804 char
805 *text;
806
807 double
808 elapsed_time,
809 user_time;
810
811 ExceptionInfo
812 *exception;
813
814 LogInfo
815 *log_info;
816
817 register char
818 *q;
819
820 register const char
821 *p;
822
823 size_t
824 extent;
825
826 time_t
827 seconds;
828
829 exception=AcquireExceptionInfo();
830 log_info=(LogInfo *) GetLogInfo("*",exception);
831 exception=DestroyExceptionInfo(exception);
832 seconds=time((time_t *) NULL);
833 elapsed_time=GetElapsedTime(&log_info->timer);
834 user_time=GetUserTime(&log_info->timer);
835 text=AcquireString(event);
836 if (log_info->format == (char *) NULL)
837 return(text);
838 extent=strlen(event)+MaxTextExtent;
839 if (LocaleCompare(log_info->format,"xml") == 0)
840 {
841 char
842 timestamp[MaxTextExtent];
843
844 /*
845 Translate event in "XML" format.
846 */
847 (void) FormatMagickTime(seconds,extent,timestamp);
cristyb51dff52011-05-19 16:55:47 +0000848 (void) FormatLocaleString(text,extent,
cristy3ed852e2009-09-05 21:47:34 +0000849 "<entry>\n"
850 " <timestamp>%s</timestamp>\n"
cristy91f905a2010-06-03 01:01:32 +0000851 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
cristy3ed852e2009-09-05 21:47:34 +0000852 " <user-time>%0.3f</user-time>\n"
cristye8c25f92010-06-03 00:53:06 +0000853 " <process-id>%.20g</process-id>\n"
854 " <thread-id>%.20g</thread-id>\n"
cristy3ed852e2009-09-05 21:47:34 +0000855 " <module>%s</module>\n"
856 " <function>%s</function>\n"
cristye8c25f92010-06-03 00:53:06 +0000857 " <line>%.20g</line>\n"
cristy3ed852e2009-09-05 21:47:34 +0000858 " <domain>%s</domain>\n"
859 " <event>%s</event>\n"
cristy91f905a2010-06-03 01:01:32 +0000860 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
861 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
862 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
863 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
864 (double) line,domain,event);
cristy3ed852e2009-09-05 21:47:34 +0000865 return(text);
866 }
867 /*
868 Translate event in "human readable" format.
869 */
870 q=text;
871 for (p=log_info->format; *p != '\0'; p++)
872 {
873 *q='\0';
874 if ((size_t) (q-text+MaxTextExtent) >= extent)
875 {
876 extent+=MaxTextExtent;
877 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
878 sizeof(*text));
879 if (text == (char *) NULL)
880 return((char *) NULL);
881 q=text+strlen(text);
882 }
883 /*
884 The format of the log is defined by embedding special format characters:
885
886 %c client name
887 %d domain
888 %e event
889 %f function
890 %g generation
891 %l line
892 %m module
893 %n log name
894 %p process id
895 %r real CPU time
896 %t wall clock time
897 %u user CPU time
898 %v version
899 %% percent sign
900 \n newline
901 \r carriage return
902 */
903 if ((*p == '\\') && (*(p+1) == 'r'))
904 {
905 *q++='\r';
906 p++;
907 continue;
908 }
909 if ((*p == '\\') && (*(p+1) == 'n'))
910 {
911 *q++='\n';
912 p++;
913 continue;
914 }
915 if (*p != '%')
916 {
917 *q++=(*p);
918 continue;
919 }
920 p++;
921 switch (*p)
922 {
923 case 'c':
924 {
925 q+=CopyMagickString(q,GetClientName(),extent);
926 break;
927 }
928 case 'd':
929 {
930 q+=CopyMagickString(q,domain,extent);
931 break;
932 }
933 case 'e':
934 {
935 q+=CopyMagickString(q,event,extent);
936 break;
937 }
938 case 'f':
939 {
940 q+=CopyMagickString(q,function,extent);
941 break;
942 }
943 case 'g':
944 {
945 if (log_info->generations == 0)
946 {
947 (void) CopyMagickString(q,"0",extent);
948 q++;
949 break;
950 }
cristyb51dff52011-05-19 16:55:47 +0000951 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
cristye8c25f92010-06-03 00:53:06 +0000952 log_info->generations));
cristy3ed852e2009-09-05 21:47:34 +0000953 break;
954 }
955 case 'l':
956 {
cristyb51dff52011-05-19 16:55:47 +0000957 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
cristy3ed852e2009-09-05 21:47:34 +0000958 break;
959 }
960 case 'm':
961 {
962 register const char
963 *p;
964
965 for (p=module+strlen(module)-1; p > module; p--)
966 if (*p == *DirectorySeparator)
967 {
968 p++;
969 break;
970 }
971 q+=CopyMagickString(q,p,extent);
972 break;
973 }
974 case 'n':
975 {
976 q+=CopyMagickString(q,GetLogName(),extent);
977 break;
978 }
979 case 'p':
980 {
cristyb51dff52011-05-19 16:55:47 +0000981 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
cristy3ed852e2009-09-05 21:47:34 +0000982 break;
983 }
984 case 'r':
985 {
cristyb51dff52011-05-19 16:55:47 +0000986 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
cristy91f905a2010-06-03 01:01:32 +0000987 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
988 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
cristy3ed852e2009-09-05 21:47:34 +0000989 break;
990 }
991 case 't':
992 {
993 q+=FormatMagickTime(seconds,extent,q);
994 break;
995 }
996 case 'u':
997 {
cristyb51dff52011-05-19 16:55:47 +0000998 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
cristy3ed852e2009-09-05 21:47:34 +0000999 break;
1000 }
1001 case 'v':
1002 {
1003 q+=CopyMagickString(q,MagickLibVersionText,extent);
1004 break;
1005 }
1006 case '%':
1007 {
1008 *q++=(*p);
1009 break;
1010 }
1011 default:
1012 {
1013 *q++='%';
1014 *q++=(*p);
1015 break;
1016 }
1017 }
1018 }
1019 *q='\0';
1020 return(text);
1021}
1022
1023static char *TranslateFilename(const LogInfo *log_info)
1024{
1025 char
1026 *filename;
1027
1028 register char
1029 *q;
1030
1031 register const char
1032 *p;
1033
1034 size_t
1035 extent;
1036
1037 /*
1038 Translate event in "human readable" format.
1039 */
1040 assert(log_info != (LogInfo *) NULL);
1041 assert(log_info->filename != (char *) NULL);
1042 filename=AcquireString((char *) NULL);
1043 extent=MaxTextExtent;
1044 q=filename;
1045 for (p=log_info->filename; *p != '\0'; p++)
1046 {
1047 *q='\0';
1048 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1049 {
1050 extent+=MaxTextExtent;
1051 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1052 sizeof(*filename));
1053 if (filename == (char *) NULL)
1054 return((char *) NULL);
1055 q=filename+strlen(filename);
1056 }
1057 /*
1058 The format of the filename is defined by embedding special format
1059 characters:
1060
1061 %c client name
1062 %n log name
1063 %p process id
1064 %v version
1065 %% percent sign
1066 */
1067 if (*p != '%')
1068 {
1069 *q++=(*p);
1070 continue;
1071 }
1072 p++;
1073 switch (*p)
1074 {
1075 case 'c':
1076 {
1077 q+=CopyMagickString(q,GetClientName(),extent);
1078 break;
1079 }
1080 case 'g':
1081 {
1082 if (log_info->generations == 0)
1083 {
1084 (void) CopyMagickString(q,"0",extent);
1085 q++;
1086 break;
1087 }
cristyb51dff52011-05-19 16:55:47 +00001088 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
cristye8c25f92010-06-03 00:53:06 +00001089 log_info->generations));
cristy3ed852e2009-09-05 21:47:34 +00001090 break;
1091 }
1092 case 'n':
1093 {
1094 q+=CopyMagickString(q,GetLogName(),extent);
1095 break;
1096 }
1097 case 'p':
1098 {
cristyb51dff52011-05-19 16:55:47 +00001099 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
cristy3ed852e2009-09-05 21:47:34 +00001100 break;
1101 }
1102 case 'v':
1103 {
1104 q+=CopyMagickString(q,MagickLibVersionText,extent);
1105 break;
1106 }
1107 case '%':
1108 {
1109 *q++=(*p);
1110 break;
1111 }
1112 default:
1113 {
1114 *q++='%';
1115 *q++=(*p);
1116 break;
1117 }
1118 }
1119 }
1120 *q='\0';
1121 return(filename);
1122}
1123
1124MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
cristybb503372010-05-27 20:51:26 +00001125 const char *function,const size_t line,const char *format,
cristy3ed852e2009-09-05 21:47:34 +00001126 va_list operands)
1127{
1128 char
1129 event[MaxTextExtent],
1130 *text;
1131
1132 const char
1133 *domain;
1134
1135 ExceptionInfo
1136 *exception;
1137
1138 int
1139 n;
1140
1141 LogInfo
1142 *log_info;
1143
1144 if (IsEventLogging() == MagickFalse)
1145 return(MagickFalse);
1146 exception=AcquireExceptionInfo();
1147 log_info=(LogInfo *) GetLogInfo("*",exception);
1148 exception=DestroyExceptionInfo(exception);
cristyf84a1932010-01-03 18:00:18 +00001149 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001150 if ((log_info->event_mask & type) == 0)
1151 {
cristyf84a1932010-01-03 18:00:18 +00001152 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001153 return(MagickTrue);
1154 }
cristy042ee782011-04-22 18:48:30 +00001155 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
cristy3ed852e2009-09-05 21:47:34 +00001156#if defined(MAGICKCORE_HAVE_VSNPRINTF)
1157 n=vsnprintf(event,MaxTextExtent,format,operands);
1158#else
1159 n=vsprintf(event,format,operands);
1160#endif
1161 if (n < 0)
1162 event[MaxTextExtent-1]='\0';
1163 text=TranslateEvent(type,module,function,line,domain,event);
1164 if (text == (char *) NULL)
1165 {
1166 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001167 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001168 return(MagickFalse);
1169 }
1170 if ((log_info->handler_mask & ConsoleHandler) != 0)
1171 {
cristyb51dff52011-05-19 16:55:47 +00001172 (void) FormatLocaleFile(stderr,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001173 (void) fflush(stderr);
1174 }
1175 if ((log_info->handler_mask & DebugHandler) != 0)
1176 {
cristy0157aea2010-04-24 21:12:18 +00001177#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001178 OutputDebugString(text);
cristy690a0eb2013-07-24 16:43:22 +00001179 OutputDebugString("\n");
cristy3ed852e2009-09-05 21:47:34 +00001180#endif
1181 }
1182 if ((log_info->handler_mask & EventHandler) != 0)
1183 {
cristy0157aea2010-04-24 21:12:18 +00001184#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001185 (void) NTReportEvent(text,MagickFalse);
1186#endif
1187 }
1188 if ((log_info->handler_mask & FileHandler) != 0)
1189 {
1190 struct stat
1191 file_info;
1192
1193 file_info.st_size=0;
1194 if (log_info->file != (FILE *) NULL)
1195 (void) fstat(fileno(log_info->file),&file_info);
cristy94b11832011-09-08 19:46:03 +00001196 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
cristy3ed852e2009-09-05 21:47:34 +00001197 {
cristyb51dff52011-05-19 16:55:47 +00001198 (void) FormatLocaleFile(log_info->file,"</log>\n");
cristy3ed852e2009-09-05 21:47:34 +00001199 (void) fclose(log_info->file);
1200 log_info->file=(FILE *) NULL;
1201 }
1202 if (log_info->file == (FILE *) NULL)
1203 {
1204 char
1205 *filename;
1206
1207 filename=TranslateFilename(log_info);
1208 if (filename == (char *) NULL)
1209 {
1210 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001211 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001212 return(MagickFalse);
1213 }
1214 log_info->append=IsPathAccessible(filename);
cristy18c6c272011-09-23 14:40:37 +00001215 log_info->file=fopen_utf8(filename,"ab");
cristy3ed852e2009-09-05 21:47:34 +00001216 filename=(char *) RelinquishMagickMemory(filename);
1217 if (log_info->file == (FILE *) NULL)
1218 {
cristyf84a1932010-01-03 18:00:18 +00001219 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001220 return(MagickFalse);
1221 }
1222 log_info->generation++;
1223 if (log_info->append == MagickFalse)
cristy398fd022013-06-26 23:08:33 +00001224 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1225 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1226 (void) FormatLocaleFile(log_info->file,"<log>\n");
cristy3ed852e2009-09-05 21:47:34 +00001227 }
cristy398fd022013-06-26 23:08:33 +00001228 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001229 (void) fflush(log_info->file);
1230 }
1231 if ((log_info->handler_mask & StdoutHandler) != 0)
1232 {
cristyb51dff52011-05-19 16:55:47 +00001233 (void) FormatLocaleFile(stdout,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001234 (void) fflush(stdout);
1235 }
1236 if ((log_info->handler_mask & StderrHandler) != 0)
1237 {
cristyb51dff52011-05-19 16:55:47 +00001238 (void) FormatLocaleFile(stderr,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001239 (void) fflush(stderr);
1240 }
1241 text=(char *) RelinquishMagickMemory(text);
1242 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001243 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001244 return(MagickTrue);
1245}
1246
1247MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
cristybb503372010-05-27 20:51:26 +00001248 const char *function,const size_t line,const char *format,...)
cristy3ed852e2009-09-05 21:47:34 +00001249{
1250 va_list
1251 operands;
1252
1253 MagickBooleanType
1254 status;
1255
1256 va_start(operands,format);
1257 status=LogMagickEventList(type,module,function,line,format,operands);
1258 va_end(operands);
1259 return(status);
1260}
1261
1262/*
1263%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1264% %
1265% %
1266% %
1267+ L o a d L o g L i s t %
1268% %
1269% %
1270% %
1271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1272%
1273% LoadLogList() loads the log configuration file which provides a
1274% mapping between log attributes and log name.
1275%
1276% The format of the LoadLogList method is:
1277%
1278% MagickBooleanType LoadLogList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001279% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001280%
1281% A description of each parameter follows:
1282%
1283% o xml: The log list in XML format.
1284%
1285% o filename: The log list filename.
1286%
1287% o depth: depth of <include /> statements.
1288%
1289% o exception: return any errors or warnings in this structure.
1290%
1291*/
1292static MagickBooleanType LoadLogList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001293 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001294{
1295 char
1296 keyword[MaxTextExtent],
1297 *token;
1298
1299 const char
1300 *q;
1301
1302 LogInfo
1303 *log_info = (LogInfo *) NULL;
1304
1305 MagickStatusType
1306 status;
1307
1308 /*
1309 Load the log map file.
1310 */
1311 if (xml == (const char *) NULL)
1312 return(MagickFalse);
1313 if (log_list == (LinkedListInfo *) NULL)
1314 {
1315 log_list=NewLinkedList(0);
1316 if (log_list == (LinkedListInfo *) NULL)
1317 {
1318 ThrowFileException(exception,ResourceLimitError,
1319 "MemoryAllocationFailed",filename);
1320 return(MagickFalse);
1321 }
1322 }
1323 status=MagickTrue;
1324 token=AcquireString((const char *) xml);
1325 for (q=(const char *) xml; *q != '\0'; )
1326 {
1327 /*
1328 Interpret XML.
1329 */
1330 GetMagickToken(q,&q,token);
1331 if (*token == '\0')
1332 break;
1333 (void) CopyMagickString(keyword,token,MaxTextExtent);
1334 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1335 {
1336 /*
1337 Doctype element.
1338 */
1339 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1340 GetMagickToken(q,&q,token);
1341 continue;
1342 }
1343 if (LocaleNCompare(keyword,"<!--",4) == 0)
1344 {
1345 /*
1346 Comment element.
1347 */
1348 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1349 GetMagickToken(q,&q,token);
1350 continue;
1351 }
1352 if (LocaleCompare(keyword,"<include") == 0)
1353 {
1354 /*
1355 Include element.
1356 */
1357 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1358 {
1359 (void) CopyMagickString(keyword,token,MaxTextExtent);
1360 GetMagickToken(q,&q,token);
1361 if (*token != '=')
1362 continue;
1363 GetMagickToken(q,&q,token);
1364 if (LocaleCompare(keyword,"file") == 0)
1365 {
1366 if (depth > 200)
1367 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001368 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00001369 else
1370 {
1371 char
1372 path[MaxTextExtent],
1373 *xml;
1374
1375 GetPathComponent(filename,HeadPath,path);
1376 if (*path != '\0')
1377 (void) ConcatenateMagickString(path,DirectorySeparator,
1378 MaxTextExtent);
1379 if (*token == *DirectorySeparator)
1380 (void) CopyMagickString(path,token,MaxTextExtent);
1381 else
1382 (void) ConcatenateMagickString(path,token,MaxTextExtent);
cristy3a5987c2013-11-07 14:18:46 +00001383 xml=FileToString(path,~0UL,exception);
cristy3ed852e2009-09-05 21:47:34 +00001384 if (xml != (char *) NULL)
1385 {
cristyaae13f62013-08-15 14:41:32 +00001386 status&=LoadLogList(xml,path,depth+1,exception);
cristy3ed852e2009-09-05 21:47:34 +00001387 xml=DestroyString(xml);
1388 }
1389 }
1390 }
1391 }
1392 continue;
1393 }
1394 if (LocaleCompare(keyword,"<logmap>") == 0)
1395 {
1396 /*
1397 Allocate memory for the log list.
1398 */
cristy73bd4a52010-10-05 11:24:23 +00001399 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
cristy3ed852e2009-09-05 21:47:34 +00001400 if (log_info == (LogInfo *) NULL)
1401 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1402 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1403 log_info->path=ConstantString(filename);
1404 GetTimerInfo((TimerInfo *) &log_info->timer);
1405 log_info->signature=MagickSignature;
1406 continue;
1407 }
1408 if (log_info == (LogInfo *) NULL)
1409 continue;
1410 if (LocaleCompare(keyword,"</logmap>") == 0)
1411 {
1412 status=AppendValueToLinkedList(log_list,log_info);
1413 if (status == MagickFalse)
1414 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001415 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
cristy3ed852e2009-09-05 21:47:34 +00001416 log_info=(LogInfo *) NULL;
1417 }
1418 GetMagickToken(q,(const char **) NULL,token);
1419 if (*token != '=')
1420 continue;
1421 GetMagickToken(q,&q,token);
1422 GetMagickToken(q,&q,token);
1423 switch (*keyword)
1424 {
1425 case 'E':
1426 case 'e':
1427 {
1428 if (LocaleCompare((char *) keyword,"events") == 0)
1429 {
1430 log_info->event_mask=(LogEventType) (log_info->event_mask |
cristy042ee782011-04-22 18:48:30 +00001431 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
cristy3ed852e2009-09-05 21:47:34 +00001432 break;
1433 }
1434 break;
1435 }
1436 case 'F':
1437 case 'f':
1438 {
1439 if (LocaleCompare((char *) keyword,"filename") == 0)
1440 {
1441 if (log_info->filename != (char *) NULL)
1442 log_info->filename=(char *)
1443 RelinquishMagickMemory(log_info->filename);
1444 log_info->filename=ConstantString(token);
1445 break;
1446 }
1447 if (LocaleCompare((char *) keyword,"format") == 0)
1448 {
1449 if (log_info->format != (char *) NULL)
1450 log_info->format=(char *)
1451 RelinquishMagickMemory(log_info->format);
1452 log_info->format=ConstantString(token);
1453 break;
1454 }
1455 break;
1456 }
1457 case 'G':
1458 case 'g':
1459 {
1460 if (LocaleCompare((char *) keyword,"generations") == 0)
1461 {
1462 if (LocaleCompare(token,"unlimited") == 0)
1463 {
1464 log_info->generations=(~0UL);
1465 break;
1466 }
cristye27293e2009-12-18 02:53:20 +00001467 log_info->generations=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00001468 break;
1469 }
1470 break;
1471 }
1472 case 'L':
1473 case 'l':
1474 {
1475 if (LocaleCompare((char *) keyword,"limit") == 0)
1476 {
1477 if (LocaleCompare(token,"unlimited") == 0)
1478 {
1479 log_info->limit=(~0UL);
1480 break;
1481 }
cristye27293e2009-12-18 02:53:20 +00001482 log_info->limit=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00001483 break;
1484 }
1485 break;
1486 }
1487 case 'O':
1488 case 'o':
1489 {
1490 if (LocaleCompare((char *) keyword,"output") == 0)
1491 {
1492 log_info->handler_mask=(LogHandlerType)
1493 (log_info->handler_mask | ParseLogHandlers(token));
1494 break;
1495 }
1496 break;
1497 }
1498 default:
1499 break;
1500 }
1501 }
1502 token=DestroyString(token);
1503 if (log_list == (LinkedListInfo *) NULL)
1504 return(MagickFalse);
1505 return(status != 0 ? MagickTrue : MagickFalse);
1506}
1507
1508/*
1509%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1510% %
1511% %
1512% %
1513% L o a d L o g L i s t s %
1514% %
1515% %
1516% %
1517%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1518%
1519% LoadLogLists() loads one or more log configuration file which provides a
1520% mapping between log attributes and log name.
1521%
1522% The format of the LoadLogLists method is:
1523%
1524% MagickBooleanType LoadLogLists(const char *filename,
1525% ExceptionInfo *exception)
1526%
1527% A description of each parameter follows:
1528%
1529% o filename: the log configuration filename.
1530%
1531% o exception: return any errors or warnings in this structure.
1532%
1533*/
1534static MagickBooleanType LoadLogLists(const char *filename,
1535 ExceptionInfo *exception)
1536{
cristy3ed852e2009-09-05 21:47:34 +00001537 const StringInfo
1538 *option;
1539
1540 LinkedListInfo
1541 *options;
1542
1543 MagickStatusType
1544 status;
1545
cristybb503372010-05-27 20:51:26 +00001546 register ssize_t
cristy85340292009-10-21 19:32:19 +00001547 i;
1548
1549 /*
cristy398fd022013-06-26 23:08:33 +00001550 Load external log map.
cristy85340292009-10-21 19:32:19 +00001551 */
cristy85340292009-10-21 19:32:19 +00001552 if (log_list == (LinkedListInfo *) NULL)
1553 {
1554 log_list=NewLinkedList(0);
1555 if (log_list == (LinkedListInfo *) NULL)
1556 {
1557 ThrowFileException(exception,ResourceLimitError,
1558 "MemoryAllocationFailed",filename);
1559 return(MagickFalse);
1560 }
1561 }
cristy398fd022013-06-26 23:08:33 +00001562 status=MagickTrue;
1563 options=GetConfigureOptions(filename,exception);
1564 option=(const StringInfo *) GetNextValueInLinkedList(options);
1565 while (option != (const StringInfo *) NULL)
1566 {
cristyaae13f62013-08-15 14:41:32 +00001567 status&=LoadLogList((const char *) GetStringInfoDatum(option),
cristy398fd022013-06-26 23:08:33 +00001568 GetStringInfoPath(option),0,exception);
1569 option=(const StringInfo *) GetNextValueInLinkedList(options);
1570 }
1571 options=DestroyConfigureOptions(options);
1572 /*
1573 Load built-in log map.
1574 */
cristybb503372010-05-27 20:51:26 +00001575 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
cristy85340292009-10-21 19:32:19 +00001576 {
1577 LogInfo
1578 *log_info;
1579
1580 register const LogMapInfo
1581 *p;
1582
1583 p=LogMap+i;
cristy73bd4a52010-10-05 11:24:23 +00001584 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
cristy85340292009-10-21 19:32:19 +00001585 if (log_info == (LogInfo *) NULL)
1586 {
1587 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001588 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
cristy85340292009-10-21 19:32:19 +00001589 continue;
1590 }
1591 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
cristy5965b492013-08-17 14:04:22 +00001592 log_info->path=ConstantString("[built-in]");
cristy85340292009-10-21 19:32:19 +00001593 GetTimerInfo((TimerInfo *) &log_info->timer);
1594 log_info->event_mask=p->event_mask;
1595 log_info->handler_mask=p->handler_mask;
cristy5aaf5742009-11-22 00:56:44 +00001596 log_info->filename=ConstantString(p->filename);
1597 log_info->format=ConstantString(p->format);
cristy85340292009-10-21 19:32:19 +00001598 log_info->signature=MagickSignature;
cristyaae13f62013-08-15 14:41:32 +00001599 status&=AppendValueToLinkedList(log_list,log_info);
cristy85340292009-10-21 19:32:19 +00001600 if (status == MagickFalse)
1601 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001602 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
cristy85340292009-10-21 19:32:19 +00001603 }
cristy3ed852e2009-09-05 21:47:34 +00001604 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001605}
1606
1607/*
1608%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1609% %
1610% %
1611% %
1612+ P a r s e L o g H a n d l e r s %
1613% %
1614% %
1615% %
1616%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1617%
1618% ParseLogHandlers() parses a string defining which handlers takes a log
1619% message and exports them.
1620%
1621% The format of the ParseLogHandlers method is:
1622%
1623% LogHandlerType ParseLogHandlers(const char *handlers)
1624%
1625% A description of each parameter follows:
1626%
1627% o handlers: one or more handlers separated by commas.
1628%
1629*/
1630static LogHandlerType ParseLogHandlers(const char *handlers)
1631{
1632 LogHandlerType
1633 handler_mask;
1634
1635 register const char
1636 *p;
1637
cristybb503372010-05-27 20:51:26 +00001638 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001639 i;
1640
1641 size_t
1642 length;
1643
1644 handler_mask=NoHandler;
1645 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1646 {
1647 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1648 (*p == ',')))
1649 p++;
1650 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1651 {
1652 length=strlen(LogHandlers[i].name);
1653 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1654 {
1655 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1656 break;
1657 }
1658 }
1659 if (LogHandlers[i].name == (char *) NULL)
1660 return(UndefinedHandler);
1661 }
1662 return(handler_mask);
1663}
1664
1665/*
1666%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1667% %
1668% %
1669% %
1670% S e t L o g E v e n t M a s k %
1671% %
1672% %
1673% %
1674%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1675%
1676% SetLogEventMask() accepts a list that determines which events to log. All
1677% other events are ignored. By default, no debug is enabled. This method
1678% returns the previous log event mask.
1679%
1680% The format of the SetLogEventMask method is:
1681%
1682% LogEventType SetLogEventMask(const char *events)
1683%
1684% A description of each parameter follows:
1685%
1686% o events: log these events.
1687%
1688*/
1689MagickExport LogEventType SetLogEventMask(const char *events)
1690{
1691 ExceptionInfo
1692 *exception;
1693
1694 LogInfo
1695 *log_info;
1696
cristybb503372010-05-27 20:51:26 +00001697 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001698 option;
1699
1700 exception=AcquireExceptionInfo();
1701 log_info=(LogInfo *) GetLogInfo("*",exception);
1702 exception=DestroyExceptionInfo(exception);
cristy042ee782011-04-22 18:48:30 +00001703 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
cristyf84a1932010-01-03 18:00:18 +00001704 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001705 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1706 log_info->event_mask=(LogEventType) option;
1707 if (option == -1)
1708 log_info->event_mask=UndefinedEvents;
cristyf84a1932010-01-03 18:00:18 +00001709 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001710 return(log_info->event_mask);
1711}
1712
1713/*
1714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715% %
1716% %
1717% %
1718% S e t L o g F o r m a t %
1719% %
1720% %
1721% %
1722%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1723%
1724% SetLogFormat() sets the format for the "human readable" log record.
1725%
1726% The format of the LogMagickFormat method is:
1727%
1728% SetLogFormat(const char *format)
1729%
1730% A description of each parameter follows:
1731%
1732% o format: the log record format.
1733%
1734*/
1735MagickExport void SetLogFormat(const char *format)
1736{
1737 LogInfo
1738 *log_info;
1739
1740 ExceptionInfo
1741 *exception;
1742
1743 exception=AcquireExceptionInfo();
1744 log_info=(LogInfo *) GetLogInfo("*",exception);
1745 exception=DestroyExceptionInfo(exception);
cristyf84a1932010-01-03 18:00:18 +00001746 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001747 if (log_info->format != (char *) NULL)
1748 log_info->format=DestroyString(log_info->format);
1749 log_info->format=ConstantString(format);
cristyf84a1932010-01-03 18:00:18 +00001750 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001751}
1752
1753/*
1754%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1755% %
1756% %
1757% %
1758% S e t L o g N a m e %
1759% %
1760% %
1761% %
1762%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1763%
1764% SetLogName() sets the log name and returns it.
1765%
1766% The format of the SetLogName method is:
1767%
1768% const char *SetLogName(const char *name)
1769%
1770% A description of each parameter follows:
1771%
1772% o log_name: SetLogName() returns the current client name.
1773%
1774% o name: Specifies the new client name.
1775%
1776*/
1777MagickExport const char *SetLogName(const char *name)
1778{
1779 if ((name != (char *) NULL) && (*name != '\0'))
1780 (void) CopyMagickString(log_name,name,MaxTextExtent);
1781 return(log_name);
1782}