blob: 31dc0f794ec8d76964b87c9d9be7b4699691f7e7 [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% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 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,
cristy85340292009-10-21 19:32:19 +0000130 exempt,
cristy3ed852e2009-09-05 21:47:34 +0000131 stealth;
132
133 TimerInfo
134 timer;
135
cristybb503372010-05-27 20:51:26 +0000136 size_t
cristy3ed852e2009-09-05 21:47:34 +0000137 signature;
138};
cristy85340292009-10-21 19:32:19 +0000139
140typedef struct _LogMapInfo
141{
142 const LogEventType
143 event_mask;
144
145 const LogHandlerType
146 handler_mask;
147
148 const char
149 *filename,
150 *format;
151} LogMapInfo;
cristy3ed852e2009-09-05 21:47:34 +0000152
153/*
cristy85340292009-10-21 19:32:19 +0000154 Static declarations.
cristy3ed852e2009-09-05 21:47:34 +0000155*/
156static const HandlerInfo
157 LogHandlers[] =
158 {
cristy398fd022013-06-26 23:08:33 +0000159 { "Console", ConsoleHandler },
160 { "Debug", DebugHandler },
161 { "Event", EventHandler },
162 { "File", FileHandler },
163 { "None", NoHandler },
164 { "Stderr", StderrHandler },
165 { "Stdout", StdoutHandler },
cristy3ed852e2009-09-05 21:47:34 +0000166 { (char *) NULL, UndefinedHandler }
167 };
168
cristy85340292009-10-21 19:32:19 +0000169static const LogMapInfo
170 LogMap[] =
171 {
cristy398fd022013-06-26 23:08:33 +0000172 { NoEvents, ConsoleHandler, "Magick-%g.log",
173 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\\n %e" }
cristy85340292009-10-21 19:32:19 +0000174 };
175
cristy3ed852e2009-09-05 21:47:34 +0000176static char
177 log_name[MaxTextExtent] = "Magick";
178
179static LinkedListInfo
180 *log_list = (LinkedListInfo *) NULL;
181
182static SemaphoreInfo
183 *log_semaphore = (SemaphoreInfo *) NULL;
184
185static volatile MagickBooleanType
186 instantiate_log = MagickFalse;
187
188/*
189 Forward declarations.
190*/
191static LogHandlerType
192 ParseLogHandlers(const char *);
193
194static LogInfo
195 *GetLogInfo(const char *,ExceptionInfo *);
196
197static MagickBooleanType
198 InitializeLogList(ExceptionInfo *),
199 LoadLogLists(const char *,ExceptionInfo *);
200
201/*
202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203% %
204% %
205% %
206% C l o s e M a g i c k L o g %
207% %
208% %
209% %
210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211%
212% CloseMagickLog() closes the Magick log.
213%
214% The format of the CloseMagickLog method is:
215%
216% CloseMagickLog(void)
217%
218*/
219MagickExport void CloseMagickLog(void)
220{
221 ExceptionInfo
222 *exception;
223
224 LogInfo
225 *log_info;
226
227 if (IsEventLogging() == MagickFalse)
228 return;
229 exception=AcquireExceptionInfo();
230 log_info=GetLogInfo("*",exception);
231 exception=DestroyExceptionInfo(exception);
cristyf84a1932010-01-03 18:00:18 +0000232 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000233 if (log_info->file != (FILE *) NULL)
234 {
cristy398fd022013-06-26 23:08:33 +0000235 (void) FormatLocaleFile(log_info->file,"</log>\n");
cristy3ed852e2009-09-05 21:47:34 +0000236 (void) fclose(log_info->file);
237 log_info->file=(FILE *) NULL;
238 }
cristyf84a1932010-01-03 18:00:18 +0000239 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000240}
241
242/*
243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244% %
245% %
246% %
cristy3ed852e2009-09-05 21:47:34 +0000247+ G e t L o g I n f o %
248% %
249% %
250% %
251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252%
253% GetLogInfo() searches the log list for the specified name and if found
254% returns attributes for that log.
255%
256% The format of the GetLogInfo method is:
257%
258% LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
259%
260% A description of each parameter follows:
261%
262% o name: the log name.
263%
264% o exception: return any errors or warnings in this structure.
265%
266*/
267static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
268{
269 register LogInfo
270 *p;
271
272 assert(exception != (ExceptionInfo *) NULL);
273 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
274 if (InitializeLogList(exception) == MagickFalse)
275 return((LogInfo *) NULL);
276 if ((log_list == (LinkedListInfo *) NULL) ||
277 (IsLinkedListEmpty(log_list) != MagickFalse))
278 return((LogInfo *) NULL);
279 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
280 return((LogInfo *) GetValueFromLinkedList(log_list,0));
281 /*
282 Search for log tag.
283 */
cristyf84a1932010-01-03 18:00:18 +0000284 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000285 ResetLinkedListIterator(log_list);
286 p=(LogInfo *) GetNextValueInLinkedList(log_list);
287 while (p != (LogInfo *) NULL)
288 {
289 if (LocaleCompare(name,p->name) == 0)
290 break;
291 p=(LogInfo *) GetNextValueInLinkedList(log_list);
292 }
293 if (p != (LogInfo *) NULL)
294 (void) InsertValueInLinkedList(log_list,0,
295 RemoveElementByValueFromLinkedList(log_list,p));
cristyf84a1932010-01-03 18:00:18 +0000296 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000297 return(p);
298}
299
300/*
301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302% %
303% %
304% %
305% G e t L o g I n f o L i s t %
306% %
307% %
308% %
309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310%
311% GetLogInfoList() returns any logs that match the specified pattern.
312%
313% The format of the GetLogInfoList function is:
314%
315% const LogInfo **GetLogInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000316% size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000317%
318% A description of each parameter follows:
319%
320% o pattern: Specifies a pointer to a text string containing a pattern.
321%
322% o number_preferences: This integer returns the number of logs in the list.
323%
324% o exception: return any errors or warnings in this structure.
325%
326*/
327#if defined(__cplusplus) || defined(c_plusplus)
328extern "C" {
329#endif
330
331static int LogInfoCompare(const void *x,const void *y)
332{
333 const LogInfo
334 **p,
335 **q;
336
337 p=(const LogInfo **) x,
338 q=(const LogInfo **) y;
339 if (LocaleCompare((*p)->path,(*q)->path) == 0)
340 return(LocaleCompare((*p)->name,(*q)->name));
341 return(LocaleCompare((*p)->path,(*q)->path));
342}
343
344#if defined(__cplusplus) || defined(c_plusplus)
345}
346#endif
347
348MagickExport const LogInfo **GetLogInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000349 size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000350{
351 const LogInfo
352 **preferences;
353
354 register const LogInfo
355 *p;
356
cristybb503372010-05-27 20:51:26 +0000357 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000358 i;
359
360 /*
361 Allocate log list.
362 */
363 assert(pattern != (char *) NULL);
364 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000365 assert(number_preferences != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000366 *number_preferences=0;
367 p=GetLogInfo("*",exception);
368 if (p == (const LogInfo *) NULL)
369 return((const LogInfo **) NULL);
370 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
371 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
372 if (preferences == (const LogInfo **) NULL)
373 return((const LogInfo **) NULL);
374 /*
375 Generate log list.
376 */
cristyf84a1932010-01-03 18:00:18 +0000377 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000378 ResetLinkedListIterator(log_list);
379 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
380 for (i=0; p != (const LogInfo *) NULL; )
381 {
382 if ((p->stealth == MagickFalse) &&
383 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
384 preferences[i++]=p;
385 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
386 }
cristyf84a1932010-01-03 18:00:18 +0000387 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000388 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
389 preferences[i]=(LogInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000390 *number_preferences=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000391 return(preferences);
392}
393
394/*
395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396% %
397% %
398% %
399% G e t L o g L i s t %
400% %
401% %
402% %
403%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404%
405% GetLogList() returns any logs that match the specified pattern.
406%
407% The format of the GetLogList function is:
408%
cristybb503372010-05-27 20:51:26 +0000409% char **GetLogList(const char *pattern,size_t *number_preferences,
cristy3ed852e2009-09-05 21:47:34 +0000410% ExceptionInfo *exception)
411%
412% A description of each parameter follows:
413%
414% o pattern: Specifies a pointer to a text string containing a pattern.
415%
416% o number_preferences: This integer returns the number of logs in the list.
417%
418% o exception: return any errors or warnings in this structure.
419%
420*/
421
422#if defined(__cplusplus) || defined(c_plusplus)
423extern "C" {
424#endif
425
426static int LogCompare(const void *x,const void *y)
427{
428 register const char
429 **p,
430 **q;
431
432 p=(const char **) x;
433 q=(const char **) y;
434 return(LocaleCompare(*p,*q));
435}
436
437#if defined(__cplusplus) || defined(c_plusplus)
438}
439#endif
440
441MagickExport char **GetLogList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000442 size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000443{
444 char
445 **preferences;
446
447 register const LogInfo
448 *p;
449
cristybb503372010-05-27 20:51:26 +0000450 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000451 i;
452
453 /*
454 Allocate log list.
455 */
456 assert(pattern != (char *) NULL);
457 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000458 assert(number_preferences != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000459 *number_preferences=0;
460 p=GetLogInfo("*",exception);
461 if (p == (const LogInfo *) NULL)
462 return((char **) NULL);
463 preferences=(char **) AcquireQuantumMemory((size_t)
464 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
465 if (preferences == (char **) NULL)
466 return((char **) NULL);
467 /*
468 Generate log list.
469 */
cristyf84a1932010-01-03 18:00:18 +0000470 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000471 ResetLinkedListIterator(log_list);
472 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
473 for (i=0; p != (const LogInfo *) NULL; )
474 {
475 if ((p->stealth == MagickFalse) &&
476 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
477 preferences[i++]=ConstantString(p->name);
478 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
479 }
cristyf84a1932010-01-03 18:00:18 +0000480 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000481 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
482 preferences[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000483 *number_preferences=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000484 return(preferences);
485}
486
487/*
488%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
489% %
490% %
491% %
492% G e t L o g N a m e %
493% %
494% %
495% %
496%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497%
498% GetLogName() returns the current log name.
499%
500% The format of the GetLogName method is:
501%
502% const char *GetLogName(void)
503%
504*/
505MagickExport const char *GetLogName(void)
506{
507 return(log_name);
508}
509
510/*
511%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
512% %
513% %
514% %
515+ I n i t i a l i z e L o g L i s t %
516% %
517% %
518% %
519%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520%
521% InitializeLogList() initialize the log list.
522%
523% The format of the InitializeLogList method is:
524%
525% MagickBooleanType InitializeLogList(ExceptionInfo *exception)
526%
527% A description of each parameter follows.
528%
529% o exception: return any errors or warnings in this structure.
530%
531*/
532static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
533{
534 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
535 {
cristy4e1dff62009-10-25 20:36:03 +0000536 if (log_semaphore == (SemaphoreInfo *) NULL)
537 AcquireSemaphoreInfo(&log_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000538 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000539 if ((log_list == (LinkedListInfo *) NULL) &&
540 (instantiate_log == MagickFalse))
541 {
542 (void) LoadLogLists(LogFilename,exception);
543 instantiate_log=MagickTrue;
544 }
cristyf84a1932010-01-03 18:00:18 +0000545 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000546 }
547 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
548}
549
550/*
551%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552% %
553% %
554% %
555% I s E v e n t L o g g i n g %
556% %
557% %
558% %
559%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560%
561% IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
562% MagickFalse.
563%
564% The format of the IsEventLogging method is:
565%
566% MagickBooleanType IsEventLogging(void)
567%
568*/
569MagickExport MagickBooleanType IsEventLogging(void)
570{
571 const LogInfo
572 *log_info;
573
574 ExceptionInfo
575 *exception;
576
577 if ((log_list == (LinkedListInfo *) NULL) ||
578 (IsLinkedListEmpty(log_list) != MagickFalse))
579 return(MagickFalse);
580 exception=AcquireExceptionInfo();
581 log_info=GetLogInfo("*",exception);
582 exception=DestroyExceptionInfo(exception);
583 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
584}
585/*
586%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587% %
588% %
589% %
590% L i s t L o g I n f o %
591% %
592% %
593% %
594%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
595%
596% ListLogInfo() lists the log info to a file.
597%
598% The format of the ListLogInfo method is:
599%
600% MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
601%
602% A description of each parameter follows.
603%
604% o file: An pointer to a FILE.
605%
606% o exception: return any errors or warnings in this structure.
607%
608*/
609MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
610{
611#define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
612
cristy3ed852e2009-09-05 21:47:34 +0000613 const char
614 *path;
615
616 const LogInfo
617 **log_info;
618
cristybb503372010-05-27 20:51:26 +0000619 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000620 i;
621
cristybb503372010-05-27 20:51:26 +0000622 size_t
cristy3ed852e2009-09-05 21:47:34 +0000623 number_aliases;
624
cristy9d314ff2011-03-09 01:30:28 +0000625 ssize_t
626 j;
627
cristy3ed852e2009-09-05 21:47:34 +0000628 if (file == (const FILE *) NULL)
629 file=stdout;
630 log_info=GetLogInfoList("*",&number_aliases,exception);
631 if (log_info == (const LogInfo **) NULL)
632 return(MagickFalse);
633 j=0;
634 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000635 for (i=0; i < (ssize_t) number_aliases; i++)
cristy3ed852e2009-09-05 21:47:34 +0000636 {
637 if (log_info[i]->stealth != MagickFalse)
638 continue;
639 if ((path == (const char *) NULL) ||
640 (LocaleCompare(path,log_info[i]->path) != 0))
641 {
cristy398fd022013-06-26 23:08:33 +0000642 size_t
643 length;
644
cristy3ed852e2009-09-05 21:47:34 +0000645 if (log_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000646 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
cristy398fd022013-06-26 23:08:33 +0000647 length=0;
648 for (j=0; j < (ssize_t) (8*sizeof(LogHandlerType)); j++)
649 {
650 size_t
651 mask;
652
653 mask=1U << j;
654 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{
cristy165b6092009-10-26 13:52:10 +0000704 AcquireSemaphoreInfo(&log_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000705 return(MagickTrue);
706}
707
708/*
709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
710% %
711% %
712% %
713+ L o g C o m p o n e n t T e r m i n u s %
714% %
715% %
716% %
717%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
718%
719% LogComponentTerminus() destroys the logging component.
720%
721% The format of the LogComponentTerminus method is:
722%
723% LogComponentTerminus(void)
724%
725*/
726
727static void *DestroyLogElement(void *log_info)
728{
729 register LogInfo
730 *p;
731
732 p=(LogInfo *) log_info;
733 if (p->file != (FILE *) NULL)
734 {
cristy398fd022013-06-26 23:08:33 +0000735 (void) FormatLocaleFile(p->file,"</log>\n");
cristyf34a1452009-10-24 22:29:27 +0000736 (void) fclose(p->file);
737 p->file=(FILE *) NULL;
738 }
739 if (p->exempt == MagickFalse)
740 {
741 if (p->format != (char *) NULL)
742 p->format=DestroyString(p->format);
743 if (p->path != (char *) NULL)
744 p->path=DestroyString(p->path);
745 if (p->filename != (char *) NULL)
746 p->filename=DestroyString(p->filename);
747 }
748 p=(LogInfo *) RelinquishMagickMemory(p);
749 return((void *) NULL);
750}
751
cristy5ff4eaf2011-09-03 01:38:02 +0000752MagickPrivate void LogComponentTerminus(void)
cristyf34a1452009-10-24 22:29:27 +0000753{
cristy18b17442009-10-25 18:36:48 +0000754 if (log_semaphore == (SemaphoreInfo *) NULL)
755 AcquireSemaphoreInfo(&log_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000756 LockSemaphoreInfo(log_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000757 if (log_list != (LinkedListInfo *) NULL)
758 log_list=DestroyLinkedList(log_list,DestroyLogElement);
759 instantiate_log=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000760 UnlockSemaphoreInfo(log_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000761 DestroySemaphoreInfo(&log_semaphore);
762}
763
764/*
765%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766% %
767% %
768% %
cristy3ed852e2009-09-05 21:47:34 +0000769% L o g M a g i c k E v e n t %
770% %
771% %
772% %
773%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774%
775% LogMagickEvent() logs an event as determined by the log configuration file.
776% If an error occurs, MagickFalse is returned otherwise MagickTrue.
777%
778% The format of the LogMagickEvent method is:
779%
780% MagickBooleanType LogMagickEvent(const LogEventType type,
cristybb503372010-05-27 20:51:26 +0000781% const char *module,const char *function,const size_t line,
cristy3ed852e2009-09-05 21:47:34 +0000782% const char *format,...)
783%
784% A description of each parameter follows:
785%
786% o type: the event type.
787%
788% o filename: the source module filename.
789%
790% o function: the function name.
791%
792% o line: the line number of the source module.
793%
794% o format: the output format.
795%
796*/
797static char *TranslateEvent(const LogEventType magick_unused(type),
cristybb503372010-05-27 20:51:26 +0000798 const char *module,const char *function,const size_t line,
cristy3ed852e2009-09-05 21:47:34 +0000799 const char *domain,const char *event)
800{
801 char
802 *text;
803
804 double
805 elapsed_time,
806 user_time;
807
808 ExceptionInfo
809 *exception;
810
811 LogInfo
812 *log_info;
813
814 register char
815 *q;
816
817 register const char
818 *p;
819
820 size_t
821 extent;
822
823 time_t
824 seconds;
825
826 exception=AcquireExceptionInfo();
827 log_info=(LogInfo *) GetLogInfo("*",exception);
828 exception=DestroyExceptionInfo(exception);
829 seconds=time((time_t *) NULL);
830 elapsed_time=GetElapsedTime(&log_info->timer);
831 user_time=GetUserTime(&log_info->timer);
832 text=AcquireString(event);
833 if (log_info->format == (char *) NULL)
834 return(text);
835 extent=strlen(event)+MaxTextExtent;
836 if (LocaleCompare(log_info->format,"xml") == 0)
837 {
838 char
839 timestamp[MaxTextExtent];
840
841 /*
842 Translate event in "XML" format.
843 */
844 (void) FormatMagickTime(seconds,extent,timestamp);
cristyb51dff52011-05-19 16:55:47 +0000845 (void) FormatLocaleString(text,extent,
cristy3ed852e2009-09-05 21:47:34 +0000846 "<entry>\n"
847 " <timestamp>%s</timestamp>\n"
cristy91f905a2010-06-03 01:01:32 +0000848 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
cristy3ed852e2009-09-05 21:47:34 +0000849 " <user-time>%0.3f</user-time>\n"
cristye8c25f92010-06-03 00:53:06 +0000850 " <process-id>%.20g</process-id>\n"
851 " <thread-id>%.20g</thread-id>\n"
cristy3ed852e2009-09-05 21:47:34 +0000852 " <module>%s</module>\n"
853 " <function>%s</function>\n"
cristye8c25f92010-06-03 00:53:06 +0000854 " <line>%.20g</line>\n"
cristy3ed852e2009-09-05 21:47:34 +0000855 " <domain>%s</domain>\n"
856 " <event>%s</event>\n"
cristy91f905a2010-06-03 01:01:32 +0000857 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
858 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
859 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
860 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
861 (double) line,domain,event);
cristy3ed852e2009-09-05 21:47:34 +0000862 return(text);
863 }
864 /*
865 Translate event in "human readable" format.
866 */
867 q=text;
868 for (p=log_info->format; *p != '\0'; p++)
869 {
870 *q='\0';
871 if ((size_t) (q-text+MaxTextExtent) >= extent)
872 {
873 extent+=MaxTextExtent;
874 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
875 sizeof(*text));
876 if (text == (char *) NULL)
877 return((char *) NULL);
878 q=text+strlen(text);
879 }
880 /*
881 The format of the log is defined by embedding special format characters:
882
883 %c client name
884 %d domain
885 %e event
886 %f function
887 %g generation
888 %l line
889 %m module
890 %n log name
891 %p process id
892 %r real CPU time
893 %t wall clock time
894 %u user CPU time
895 %v version
896 %% percent sign
897 \n newline
898 \r carriage return
899 */
900 if ((*p == '\\') && (*(p+1) == 'r'))
901 {
902 *q++='\r';
903 p++;
904 continue;
905 }
906 if ((*p == '\\') && (*(p+1) == 'n'))
907 {
908 *q++='\n';
909 p++;
910 continue;
911 }
912 if (*p != '%')
913 {
914 *q++=(*p);
915 continue;
916 }
917 p++;
918 switch (*p)
919 {
920 case 'c':
921 {
922 q+=CopyMagickString(q,GetClientName(),extent);
923 break;
924 }
925 case 'd':
926 {
927 q+=CopyMagickString(q,domain,extent);
928 break;
929 }
930 case 'e':
931 {
932 q+=CopyMagickString(q,event,extent);
933 break;
934 }
935 case 'f':
936 {
937 q+=CopyMagickString(q,function,extent);
938 break;
939 }
940 case 'g':
941 {
942 if (log_info->generations == 0)
943 {
944 (void) CopyMagickString(q,"0",extent);
945 q++;
946 break;
947 }
cristyb51dff52011-05-19 16:55:47 +0000948 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
cristye8c25f92010-06-03 00:53:06 +0000949 log_info->generations));
cristy3ed852e2009-09-05 21:47:34 +0000950 break;
951 }
952 case 'l':
953 {
cristyb51dff52011-05-19 16:55:47 +0000954 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
cristy3ed852e2009-09-05 21:47:34 +0000955 break;
956 }
957 case 'm':
958 {
959 register const char
960 *p;
961
962 for (p=module+strlen(module)-1; p > module; p--)
963 if (*p == *DirectorySeparator)
964 {
965 p++;
966 break;
967 }
968 q+=CopyMagickString(q,p,extent);
969 break;
970 }
971 case 'n':
972 {
973 q+=CopyMagickString(q,GetLogName(),extent);
974 break;
975 }
976 case 'p':
977 {
cristyb51dff52011-05-19 16:55:47 +0000978 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
cristy3ed852e2009-09-05 21:47:34 +0000979 break;
980 }
981 case 'r':
982 {
cristyb51dff52011-05-19 16:55:47 +0000983 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
cristy91f905a2010-06-03 01:01:32 +0000984 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
985 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
cristy3ed852e2009-09-05 21:47:34 +0000986 break;
987 }
988 case 't':
989 {
990 q+=FormatMagickTime(seconds,extent,q);
991 break;
992 }
993 case 'u':
994 {
cristyb51dff52011-05-19 16:55:47 +0000995 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
cristy3ed852e2009-09-05 21:47:34 +0000996 break;
997 }
998 case 'v':
999 {
1000 q+=CopyMagickString(q,MagickLibVersionText,extent);
1001 break;
1002 }
1003 case '%':
1004 {
1005 *q++=(*p);
1006 break;
1007 }
1008 default:
1009 {
1010 *q++='%';
1011 *q++=(*p);
1012 break;
1013 }
1014 }
1015 }
1016 *q='\0';
1017 return(text);
1018}
1019
1020static char *TranslateFilename(const LogInfo *log_info)
1021{
1022 char
1023 *filename;
1024
1025 register char
1026 *q;
1027
1028 register const char
1029 *p;
1030
1031 size_t
1032 extent;
1033
1034 /*
1035 Translate event in "human readable" format.
1036 */
1037 assert(log_info != (LogInfo *) NULL);
1038 assert(log_info->filename != (char *) NULL);
1039 filename=AcquireString((char *) NULL);
1040 extent=MaxTextExtent;
1041 q=filename;
1042 for (p=log_info->filename; *p != '\0'; p++)
1043 {
1044 *q='\0';
1045 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1046 {
1047 extent+=MaxTextExtent;
1048 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1049 sizeof(*filename));
1050 if (filename == (char *) NULL)
1051 return((char *) NULL);
1052 q=filename+strlen(filename);
1053 }
1054 /*
1055 The format of the filename is defined by embedding special format
1056 characters:
1057
1058 %c client name
1059 %n log name
1060 %p process id
1061 %v version
1062 %% percent sign
1063 */
1064 if (*p != '%')
1065 {
1066 *q++=(*p);
1067 continue;
1068 }
1069 p++;
1070 switch (*p)
1071 {
1072 case 'c':
1073 {
1074 q+=CopyMagickString(q,GetClientName(),extent);
1075 break;
1076 }
1077 case 'g':
1078 {
1079 if (log_info->generations == 0)
1080 {
1081 (void) CopyMagickString(q,"0",extent);
1082 q++;
1083 break;
1084 }
cristyb51dff52011-05-19 16:55:47 +00001085 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
cristye8c25f92010-06-03 00:53:06 +00001086 log_info->generations));
cristy3ed852e2009-09-05 21:47:34 +00001087 break;
1088 }
1089 case 'n':
1090 {
1091 q+=CopyMagickString(q,GetLogName(),extent);
1092 break;
1093 }
1094 case 'p':
1095 {
cristyb51dff52011-05-19 16:55:47 +00001096 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
cristy3ed852e2009-09-05 21:47:34 +00001097 break;
1098 }
1099 case 'v':
1100 {
1101 q+=CopyMagickString(q,MagickLibVersionText,extent);
1102 break;
1103 }
1104 case '%':
1105 {
1106 *q++=(*p);
1107 break;
1108 }
1109 default:
1110 {
1111 *q++='%';
1112 *q++=(*p);
1113 break;
1114 }
1115 }
1116 }
1117 *q='\0';
1118 return(filename);
1119}
1120
1121MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
cristybb503372010-05-27 20:51:26 +00001122 const char *function,const size_t line,const char *format,
cristy3ed852e2009-09-05 21:47:34 +00001123 va_list operands)
1124{
1125 char
1126 event[MaxTextExtent],
1127 *text;
1128
1129 const char
1130 *domain;
1131
1132 ExceptionInfo
1133 *exception;
1134
1135 int
1136 n;
1137
1138 LogInfo
1139 *log_info;
1140
1141 if (IsEventLogging() == MagickFalse)
1142 return(MagickFalse);
1143 exception=AcquireExceptionInfo();
1144 log_info=(LogInfo *) GetLogInfo("*",exception);
1145 exception=DestroyExceptionInfo(exception);
cristyf84a1932010-01-03 18:00:18 +00001146 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001147 if ((log_info->event_mask & type) == 0)
1148 {
cristyf84a1932010-01-03 18:00:18 +00001149 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001150 return(MagickTrue);
1151 }
cristy042ee782011-04-22 18:48:30 +00001152 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
cristy3ed852e2009-09-05 21:47:34 +00001153#if defined(MAGICKCORE_HAVE_VSNPRINTF)
1154 n=vsnprintf(event,MaxTextExtent,format,operands);
1155#else
1156 n=vsprintf(event,format,operands);
1157#endif
1158 if (n < 0)
1159 event[MaxTextExtent-1]='\0';
1160 text=TranslateEvent(type,module,function,line,domain,event);
1161 if (text == (char *) NULL)
1162 {
1163 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001164 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001165 return(MagickFalse);
1166 }
1167 if ((log_info->handler_mask & ConsoleHandler) != 0)
1168 {
cristyb51dff52011-05-19 16:55:47 +00001169 (void) FormatLocaleFile(stderr,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001170 (void) fflush(stderr);
1171 }
1172 if ((log_info->handler_mask & DebugHandler) != 0)
1173 {
cristy0157aea2010-04-24 21:12:18 +00001174#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001175 OutputDebugString(text);
1176#endif
1177 }
1178 if ((log_info->handler_mask & EventHandler) != 0)
1179 {
cristy0157aea2010-04-24 21:12:18 +00001180#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001181 (void) NTReportEvent(text,MagickFalse);
1182#endif
1183 }
1184 if ((log_info->handler_mask & FileHandler) != 0)
1185 {
1186 struct stat
1187 file_info;
1188
1189 file_info.st_size=0;
1190 if (log_info->file != (FILE *) NULL)
1191 (void) fstat(fileno(log_info->file),&file_info);
cristy94b11832011-09-08 19:46:03 +00001192 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
cristy3ed852e2009-09-05 21:47:34 +00001193 {
cristyb51dff52011-05-19 16:55:47 +00001194 (void) FormatLocaleFile(log_info->file,"</log>\n");
cristy3ed852e2009-09-05 21:47:34 +00001195 (void) fclose(log_info->file);
1196 log_info->file=(FILE *) NULL;
1197 }
1198 if (log_info->file == (FILE *) NULL)
1199 {
1200 char
1201 *filename;
1202
1203 filename=TranslateFilename(log_info);
1204 if (filename == (char *) NULL)
1205 {
1206 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001207 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001208 return(MagickFalse);
1209 }
1210 log_info->append=IsPathAccessible(filename);
cristy18c6c272011-09-23 14:40:37 +00001211 log_info->file=fopen_utf8(filename,"ab");
cristy3ed852e2009-09-05 21:47:34 +00001212 filename=(char *) RelinquishMagickMemory(filename);
1213 if (log_info->file == (FILE *) NULL)
1214 {
cristyf84a1932010-01-03 18:00:18 +00001215 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001216 return(MagickFalse);
1217 }
1218 log_info->generation++;
1219 if (log_info->append == MagickFalse)
cristy398fd022013-06-26 23:08:33 +00001220 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
1221 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
1222 (void) FormatLocaleFile(log_info->file,"<log>\n");
cristy3ed852e2009-09-05 21:47:34 +00001223 }
cristy398fd022013-06-26 23:08:33 +00001224 (void) FormatLocaleFile(log_info->file," <event>%s</event>\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001225 (void) fflush(log_info->file);
1226 }
1227 if ((log_info->handler_mask & StdoutHandler) != 0)
1228 {
cristyb51dff52011-05-19 16:55:47 +00001229 (void) FormatLocaleFile(stdout,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001230 (void) fflush(stdout);
1231 }
1232 if ((log_info->handler_mask & StderrHandler) != 0)
1233 {
cristyb51dff52011-05-19 16:55:47 +00001234 (void) FormatLocaleFile(stderr,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001235 (void) fflush(stderr);
1236 }
1237 text=(char *) RelinquishMagickMemory(text);
1238 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001239 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001240 return(MagickTrue);
1241}
1242
1243MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
cristybb503372010-05-27 20:51:26 +00001244 const char *function,const size_t line,const char *format,...)
cristy3ed852e2009-09-05 21:47:34 +00001245{
1246 va_list
1247 operands;
1248
1249 MagickBooleanType
1250 status;
1251
1252 va_start(operands,format);
1253 status=LogMagickEventList(type,module,function,line,format,operands);
1254 va_end(operands);
1255 return(status);
1256}
1257
1258/*
1259%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1260% %
1261% %
1262% %
1263+ L o a d L o g L i s t %
1264% %
1265% %
1266% %
1267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268%
1269% LoadLogList() loads the log configuration file which provides a
1270% mapping between log attributes and log name.
1271%
1272% The format of the LoadLogList method is:
1273%
1274% MagickBooleanType LoadLogList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001275% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001276%
1277% A description of each parameter follows:
1278%
1279% o xml: The log list in XML format.
1280%
1281% o filename: The log list filename.
1282%
1283% o depth: depth of <include /> statements.
1284%
1285% o exception: return any errors or warnings in this structure.
1286%
1287*/
1288static MagickBooleanType LoadLogList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001289 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001290{
1291 char
1292 keyword[MaxTextExtent],
1293 *token;
1294
1295 const char
1296 *q;
1297
1298 LogInfo
1299 *log_info = (LogInfo *) NULL;
1300
1301 MagickStatusType
1302 status;
1303
1304 /*
1305 Load the log map file.
1306 */
1307 if (xml == (const char *) NULL)
1308 return(MagickFalse);
1309 if (log_list == (LinkedListInfo *) NULL)
1310 {
1311 log_list=NewLinkedList(0);
1312 if (log_list == (LinkedListInfo *) NULL)
1313 {
1314 ThrowFileException(exception,ResourceLimitError,
1315 "MemoryAllocationFailed",filename);
1316 return(MagickFalse);
1317 }
1318 }
1319 status=MagickTrue;
1320 token=AcquireString((const char *) xml);
1321 for (q=(const char *) xml; *q != '\0'; )
1322 {
1323 /*
1324 Interpret XML.
1325 */
1326 GetMagickToken(q,&q,token);
1327 if (*token == '\0')
1328 break;
1329 (void) CopyMagickString(keyword,token,MaxTextExtent);
1330 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1331 {
1332 /*
1333 Doctype element.
1334 */
1335 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1336 GetMagickToken(q,&q,token);
1337 continue;
1338 }
1339 if (LocaleNCompare(keyword,"<!--",4) == 0)
1340 {
1341 /*
1342 Comment element.
1343 */
1344 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1345 GetMagickToken(q,&q,token);
1346 continue;
1347 }
1348 if (LocaleCompare(keyword,"<include") == 0)
1349 {
1350 /*
1351 Include element.
1352 */
1353 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1354 {
1355 (void) CopyMagickString(keyword,token,MaxTextExtent);
1356 GetMagickToken(q,&q,token);
1357 if (*token != '=')
1358 continue;
1359 GetMagickToken(q,&q,token);
1360 if (LocaleCompare(keyword,"file") == 0)
1361 {
1362 if (depth > 200)
1363 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001364 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00001365 else
1366 {
1367 char
1368 path[MaxTextExtent],
1369 *xml;
1370
1371 GetPathComponent(filename,HeadPath,path);
1372 if (*path != '\0')
1373 (void) ConcatenateMagickString(path,DirectorySeparator,
1374 MaxTextExtent);
1375 if (*token == *DirectorySeparator)
1376 (void) CopyMagickString(path,token,MaxTextExtent);
1377 else
1378 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1379 xml=FileToString(path,~0,exception);
1380 if (xml != (char *) NULL)
1381 {
1382 status|=LoadLogList(xml,path,depth+1,exception);
1383 xml=DestroyString(xml);
1384 }
1385 }
1386 }
1387 }
1388 continue;
1389 }
1390 if (LocaleCompare(keyword,"<logmap>") == 0)
1391 {
1392 /*
1393 Allocate memory for the log list.
1394 */
cristy73bd4a52010-10-05 11:24:23 +00001395 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
cristy3ed852e2009-09-05 21:47:34 +00001396 if (log_info == (LogInfo *) NULL)
1397 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1398 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1399 log_info->path=ConstantString(filename);
1400 GetTimerInfo((TimerInfo *) &log_info->timer);
cristy85340292009-10-21 19:32:19 +00001401 log_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001402 log_info->signature=MagickSignature;
1403 continue;
1404 }
1405 if (log_info == (LogInfo *) NULL)
1406 continue;
1407 if (LocaleCompare(keyword,"</logmap>") == 0)
1408 {
1409 status=AppendValueToLinkedList(log_list,log_info);
1410 if (status == MagickFalse)
1411 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001412 ResourceLimitError,"MemoryAllocationFailed","`%s'",filename);
cristy3ed852e2009-09-05 21:47:34 +00001413 log_info=(LogInfo *) NULL;
1414 }
1415 GetMagickToken(q,(const char **) NULL,token);
1416 if (*token != '=')
1417 continue;
1418 GetMagickToken(q,&q,token);
1419 GetMagickToken(q,&q,token);
1420 switch (*keyword)
1421 {
1422 case 'E':
1423 case 'e':
1424 {
1425 if (LocaleCompare((char *) keyword,"events") == 0)
1426 {
1427 log_info->event_mask=(LogEventType) (log_info->event_mask |
cristy042ee782011-04-22 18:48:30 +00001428 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
cristy3ed852e2009-09-05 21:47:34 +00001429 break;
1430 }
1431 break;
1432 }
1433 case 'F':
1434 case 'f':
1435 {
1436 if (LocaleCompare((char *) keyword,"filename") == 0)
1437 {
1438 if (log_info->filename != (char *) NULL)
1439 log_info->filename=(char *)
1440 RelinquishMagickMemory(log_info->filename);
1441 log_info->filename=ConstantString(token);
1442 break;
1443 }
1444 if (LocaleCompare((char *) keyword,"format") == 0)
1445 {
1446 if (log_info->format != (char *) NULL)
1447 log_info->format=(char *)
1448 RelinquishMagickMemory(log_info->format);
1449 log_info->format=ConstantString(token);
1450 break;
1451 }
1452 break;
1453 }
1454 case 'G':
1455 case 'g':
1456 {
1457 if (LocaleCompare((char *) keyword,"generations") == 0)
1458 {
1459 if (LocaleCompare(token,"unlimited") == 0)
1460 {
1461 log_info->generations=(~0UL);
1462 break;
1463 }
cristye27293e2009-12-18 02:53:20 +00001464 log_info->generations=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00001465 break;
1466 }
1467 break;
1468 }
1469 case 'L':
1470 case 'l':
1471 {
1472 if (LocaleCompare((char *) keyword,"limit") == 0)
1473 {
1474 if (LocaleCompare(token,"unlimited") == 0)
1475 {
1476 log_info->limit=(~0UL);
1477 break;
1478 }
cristye27293e2009-12-18 02:53:20 +00001479 log_info->limit=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00001480 break;
1481 }
1482 break;
1483 }
1484 case 'O':
1485 case 'o':
1486 {
1487 if (LocaleCompare((char *) keyword,"output") == 0)
1488 {
1489 log_info->handler_mask=(LogHandlerType)
1490 (log_info->handler_mask | ParseLogHandlers(token));
1491 break;
1492 }
1493 break;
1494 }
1495 default:
1496 break;
1497 }
1498 }
1499 token=DestroyString(token);
1500 if (log_list == (LinkedListInfo *) NULL)
1501 return(MagickFalse);
1502 return(status != 0 ? MagickTrue : MagickFalse);
1503}
1504
1505/*
1506%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1507% %
1508% %
1509% %
1510% L o a d L o g L i s t s %
1511% %
1512% %
1513% %
1514%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1515%
1516% LoadLogLists() loads one or more log configuration file which provides a
1517% mapping between log attributes and log name.
1518%
1519% The format of the LoadLogLists method is:
1520%
1521% MagickBooleanType LoadLogLists(const char *filename,
1522% ExceptionInfo *exception)
1523%
1524% A description of each parameter follows:
1525%
1526% o filename: the log configuration filename.
1527%
1528% o exception: return any errors or warnings in this structure.
1529%
1530*/
1531static MagickBooleanType LoadLogLists(const char *filename,
1532 ExceptionInfo *exception)
1533{
cristy3ed852e2009-09-05 21:47:34 +00001534 const StringInfo
1535 *option;
1536
1537 LinkedListInfo
1538 *options;
1539
1540 MagickStatusType
1541 status;
1542
cristybb503372010-05-27 20:51:26 +00001543 register ssize_t
cristy85340292009-10-21 19:32:19 +00001544 i;
1545
1546 /*
cristy398fd022013-06-26 23:08:33 +00001547 Load external log map.
cristy85340292009-10-21 19:32:19 +00001548 */
cristy85340292009-10-21 19:32:19 +00001549 if (log_list == (LinkedListInfo *) NULL)
1550 {
1551 log_list=NewLinkedList(0);
1552 if (log_list == (LinkedListInfo *) NULL)
1553 {
1554 ThrowFileException(exception,ResourceLimitError,
1555 "MemoryAllocationFailed",filename);
1556 return(MagickFalse);
1557 }
1558 }
cristy398fd022013-06-26 23:08:33 +00001559 status=MagickTrue;
1560 options=GetConfigureOptions(filename,exception);
1561 option=(const StringInfo *) GetNextValueInLinkedList(options);
1562 while (option != (const StringInfo *) NULL)
1563 {
1564 status|=LoadLogList((const char *) GetStringInfoDatum(option),
1565 GetStringInfoPath(option),0,exception);
1566 option=(const StringInfo *) GetNextValueInLinkedList(options);
1567 }
1568 options=DestroyConfigureOptions(options);
1569 /*
1570 Load built-in log map.
1571 */
cristybb503372010-05-27 20:51:26 +00001572 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
cristy85340292009-10-21 19:32:19 +00001573 {
1574 LogInfo
1575 *log_info;
1576
1577 register const LogMapInfo
1578 *p;
1579
1580 p=LogMap+i;
cristy73bd4a52010-10-05 11:24:23 +00001581 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
cristy85340292009-10-21 19:32:19 +00001582 if (log_info == (LogInfo *) NULL)
1583 {
1584 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001585 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
cristy85340292009-10-21 19:32:19 +00001586 continue;
1587 }
1588 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1589 log_info->path=(char *) "[built-in]";
1590 GetTimerInfo((TimerInfo *) &log_info->timer);
1591 log_info->event_mask=p->event_mask;
1592 log_info->handler_mask=p->handler_mask;
cristy5aaf5742009-11-22 00:56:44 +00001593 log_info->filename=ConstantString(p->filename);
1594 log_info->format=ConstantString(p->format);
cristy85340292009-10-21 19:32:19 +00001595 log_info->exempt=MagickTrue;
1596 log_info->signature=MagickSignature;
cristy398fd022013-06-26 23:08:33 +00001597 status|=AppendValueToLinkedList(log_list,log_info);
cristy85340292009-10-21 19:32:19 +00001598 if (status == MagickFalse)
1599 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001600 ResourceLimitError,"MemoryAllocationFailed","`%s'",log_info->name);
cristy85340292009-10-21 19:32:19 +00001601 }
cristy3ed852e2009-09-05 21:47:34 +00001602 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001603}
1604
1605/*
1606%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1607% %
1608% %
1609% %
1610+ P a r s e L o g H a n d l e r s %
1611% %
1612% %
1613% %
1614%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1615%
1616% ParseLogHandlers() parses a string defining which handlers takes a log
1617% message and exports them.
1618%
1619% The format of the ParseLogHandlers method is:
1620%
1621% LogHandlerType ParseLogHandlers(const char *handlers)
1622%
1623% A description of each parameter follows:
1624%
1625% o handlers: one or more handlers separated by commas.
1626%
1627*/
1628static LogHandlerType ParseLogHandlers(const char *handlers)
1629{
1630 LogHandlerType
1631 handler_mask;
1632
1633 register const char
1634 *p;
1635
cristybb503372010-05-27 20:51:26 +00001636 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001637 i;
1638
1639 size_t
1640 length;
1641
1642 handler_mask=NoHandler;
1643 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1644 {
1645 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1646 (*p == ',')))
1647 p++;
1648 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1649 {
1650 length=strlen(LogHandlers[i].name);
1651 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1652 {
1653 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1654 break;
1655 }
1656 }
1657 if (LogHandlers[i].name == (char *) NULL)
1658 return(UndefinedHandler);
1659 }
1660 return(handler_mask);
1661}
1662
1663/*
1664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1665% %
1666% %
1667% %
1668% S e t L o g E v e n t M a s k %
1669% %
1670% %
1671% %
1672%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1673%
1674% SetLogEventMask() accepts a list that determines which events to log. All
1675% other events are ignored. By default, no debug is enabled. This method
1676% returns the previous log event mask.
1677%
1678% The format of the SetLogEventMask method is:
1679%
1680% LogEventType SetLogEventMask(const char *events)
1681%
1682% A description of each parameter follows:
1683%
1684% o events: log these events.
1685%
1686*/
1687MagickExport LogEventType SetLogEventMask(const char *events)
1688{
1689 ExceptionInfo
1690 *exception;
1691
1692 LogInfo
1693 *log_info;
1694
cristybb503372010-05-27 20:51:26 +00001695 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001696 option;
1697
1698 exception=AcquireExceptionInfo();
1699 log_info=(LogInfo *) GetLogInfo("*",exception);
1700 exception=DestroyExceptionInfo(exception);
cristy042ee782011-04-22 18:48:30 +00001701 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
cristyf84a1932010-01-03 18:00:18 +00001702 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001703 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1704 log_info->event_mask=(LogEventType) option;
1705 if (option == -1)
1706 log_info->event_mask=UndefinedEvents;
cristyf84a1932010-01-03 18:00:18 +00001707 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001708 return(log_info->event_mask);
1709}
1710
1711/*
1712%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1713% %
1714% %
1715% %
1716% S e t L o g F o r m a t %
1717% %
1718% %
1719% %
1720%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721%
1722% SetLogFormat() sets the format for the "human readable" log record.
1723%
1724% The format of the LogMagickFormat method is:
1725%
1726% SetLogFormat(const char *format)
1727%
1728% A description of each parameter follows:
1729%
1730% o format: the log record format.
1731%
1732*/
1733MagickExport void SetLogFormat(const char *format)
1734{
1735 LogInfo
1736 *log_info;
1737
1738 ExceptionInfo
1739 *exception;
1740
1741 exception=AcquireExceptionInfo();
1742 log_info=(LogInfo *) GetLogInfo("*",exception);
1743 exception=DestroyExceptionInfo(exception);
cristyf84a1932010-01-03 18:00:18 +00001744 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001745 if (log_info->format != (char *) NULL)
1746 log_info->format=DestroyString(log_info->format);
1747 log_info->format=ConstantString(format);
cristyf84a1932010-01-03 18:00:18 +00001748 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001749}
1750
1751/*
1752%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1753% %
1754% %
1755% %
1756% S e t L o g N a m e %
1757% %
1758% %
1759% %
1760%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1761%
1762% SetLogName() sets the log name and returns it.
1763%
1764% The format of the SetLogName method is:
1765%
1766% const char *SetLogName(const char *name)
1767%
1768% A description of each parameter follows:
1769%
1770% o log_name: SetLogName() returns the current client name.
1771%
1772% o name: Specifies the new client name.
1773%
1774*/
1775MagickExport const char *SetLogName(const char *name)
1776{
1777 if ((name != (char *) NULL) && (*name != '\0'))
1778 (void) CopyMagickString(log_name,name,MaxTextExtent);
1779 return(log_name);
1780}