blob: 07a7c9f6ba5ab232dd76f75c86cd2323569ce175 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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 {
159 { "console", ConsoleHandler },
160 { "debug", DebugHandler },
161 { "event", EventHandler },
162 { "file", FileHandler },
163 { "none", NoHandler },
164 { "stderr", StderrHandler },
165 { "stdout", StdoutHandler },
166 { (char *) NULL, UndefinedHandler }
167 };
168
cristy85340292009-10-21 19:32:19 +0000169static const LogMapInfo
170 LogMap[] =
171 {
172 { NoEvents, ConsoleHandler, "Magick-%d.log",
173 "%t %r %u %v %d %c[%p]: %m/%f/%l/%d\n %e" }
174 };
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 {
235 if (log_info->append == MagickFalse)
cristyb51dff52011-05-19 16:55:47 +0000236 (void) FormatLocaleFile(log_info->file,"</log>\n");
cristy3ed852e2009-09-05 21:47:34 +0000237 (void) fclose(log_info->file);
238 log_info->file=(FILE *) NULL;
239 }
cristyf84a1932010-01-03 18:00:18 +0000240 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000241}
242
243/*
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245% %
246% %
247% %
cristy3ed852e2009-09-05 21:47:34 +0000248+ G e t L o g I n f o %
249% %
250% %
251% %
252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253%
254% GetLogInfo() searches the log list for the specified name and if found
255% returns attributes for that log.
256%
257% The format of the GetLogInfo method is:
258%
259% LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
260%
261% A description of each parameter follows:
262%
263% o name: the log name.
264%
265% o exception: return any errors or warnings in this structure.
266%
267*/
268static LogInfo *GetLogInfo(const char *name,ExceptionInfo *exception)
269{
270 register LogInfo
271 *p;
272
273 assert(exception != (ExceptionInfo *) NULL);
274 if ((log_list == (LinkedListInfo *) NULL) || (instantiate_log == MagickFalse))
275 if (InitializeLogList(exception) == MagickFalse)
276 return((LogInfo *) NULL);
277 if ((log_list == (LinkedListInfo *) NULL) ||
278 (IsLinkedListEmpty(log_list) != MagickFalse))
279 return((LogInfo *) NULL);
280 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
281 return((LogInfo *) GetValueFromLinkedList(log_list,0));
282 /*
283 Search for log tag.
284 */
cristyf84a1932010-01-03 18:00:18 +0000285 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000286 ResetLinkedListIterator(log_list);
287 p=(LogInfo *) GetNextValueInLinkedList(log_list);
288 while (p != (LogInfo *) NULL)
289 {
290 if (LocaleCompare(name,p->name) == 0)
291 break;
292 p=(LogInfo *) GetNextValueInLinkedList(log_list);
293 }
294 if (p != (LogInfo *) NULL)
295 (void) InsertValueInLinkedList(log_list,0,
296 RemoveElementByValueFromLinkedList(log_list,p));
cristyf84a1932010-01-03 18:00:18 +0000297 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000298 return(p);
299}
300
301/*
302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303% %
304% %
305% %
306% G e t L o g I n f o L i s t %
307% %
308% %
309% %
310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311%
312% GetLogInfoList() returns any logs that match the specified pattern.
313%
314% The format of the GetLogInfoList function is:
315%
316% const LogInfo **GetLogInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000317% size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000318%
319% A description of each parameter follows:
320%
321% o pattern: Specifies a pointer to a text string containing a pattern.
322%
323% o number_preferences: This integer returns the number of logs in the list.
324%
325% o exception: return any errors or warnings in this structure.
326%
327*/
328#if defined(__cplusplus) || defined(c_plusplus)
329extern "C" {
330#endif
331
332static int LogInfoCompare(const void *x,const void *y)
333{
334 const LogInfo
335 **p,
336 **q;
337
338 p=(const LogInfo **) x,
339 q=(const LogInfo **) y;
340 if (LocaleCompare((*p)->path,(*q)->path) == 0)
341 return(LocaleCompare((*p)->name,(*q)->name));
342 return(LocaleCompare((*p)->path,(*q)->path));
343}
344
345#if defined(__cplusplus) || defined(c_plusplus)
346}
347#endif
348
349MagickExport const LogInfo **GetLogInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000350 size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000351{
352 const LogInfo
353 **preferences;
354
355 register const LogInfo
356 *p;
357
cristybb503372010-05-27 20:51:26 +0000358 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000359 i;
360
361 /*
362 Allocate log list.
363 */
364 assert(pattern != (char *) NULL);
365 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000366 assert(number_preferences != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000367 *number_preferences=0;
368 p=GetLogInfo("*",exception);
369 if (p == (const LogInfo *) NULL)
370 return((const LogInfo **) NULL);
371 preferences=(const LogInfo **) AcquireQuantumMemory((size_t)
372 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
373 if (preferences == (const LogInfo **) NULL)
374 return((const LogInfo **) NULL);
375 /*
376 Generate log list.
377 */
cristyf84a1932010-01-03 18:00:18 +0000378 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000379 ResetLinkedListIterator(log_list);
380 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
381 for (i=0; p != (const LogInfo *) NULL; )
382 {
383 if ((p->stealth == MagickFalse) &&
384 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
385 preferences[i++]=p;
386 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
387 }
cristyf84a1932010-01-03 18:00:18 +0000388 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000389 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogInfoCompare);
390 preferences[i]=(LogInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000391 *number_preferences=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000392 return(preferences);
393}
394
395/*
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397% %
398% %
399% %
400% G e t L o g L i s t %
401% %
402% %
403% %
404%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405%
406% GetLogList() returns any logs that match the specified pattern.
407%
408% The format of the GetLogList function is:
409%
cristybb503372010-05-27 20:51:26 +0000410% char **GetLogList(const char *pattern,size_t *number_preferences,
cristy3ed852e2009-09-05 21:47:34 +0000411% ExceptionInfo *exception)
412%
413% A description of each parameter follows:
414%
415% o pattern: Specifies a pointer to a text string containing a pattern.
416%
417% o number_preferences: This integer returns the number of logs in the list.
418%
419% o exception: return any errors or warnings in this structure.
420%
421*/
422
423#if defined(__cplusplus) || defined(c_plusplus)
424extern "C" {
425#endif
426
427static int LogCompare(const void *x,const void *y)
428{
429 register const char
430 **p,
431 **q;
432
433 p=(const char **) x;
434 q=(const char **) y;
435 return(LocaleCompare(*p,*q));
436}
437
438#if defined(__cplusplus) || defined(c_plusplus)
439}
440#endif
441
442MagickExport char **GetLogList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000443 size_t *number_preferences,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000444{
445 char
446 **preferences;
447
448 register const LogInfo
449 *p;
450
cristybb503372010-05-27 20:51:26 +0000451 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000452 i;
453
454 /*
455 Allocate log list.
456 */
457 assert(pattern != (char *) NULL);
458 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000459 assert(number_preferences != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000460 *number_preferences=0;
461 p=GetLogInfo("*",exception);
462 if (p == (const LogInfo *) NULL)
463 return((char **) NULL);
464 preferences=(char **) AcquireQuantumMemory((size_t)
465 GetNumberOfElementsInLinkedList(log_list)+1UL,sizeof(*preferences));
466 if (preferences == (char **) NULL)
467 return((char **) NULL);
468 /*
469 Generate log list.
470 */
cristyf84a1932010-01-03 18:00:18 +0000471 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000472 ResetLinkedListIterator(log_list);
473 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
474 for (i=0; p != (const LogInfo *) NULL; )
475 {
476 if ((p->stealth == MagickFalse) &&
477 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
478 preferences[i++]=ConstantString(p->name);
479 p=(const LogInfo *) GetNextValueInLinkedList(log_list);
480 }
cristyf84a1932010-01-03 18:00:18 +0000481 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000482 qsort((void *) preferences,(size_t) i,sizeof(*preferences),LogCompare);
483 preferences[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000484 *number_preferences=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000485 return(preferences);
486}
487
488/*
489%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490% %
491% %
492% %
493% G e t L o g N a m e %
494% %
495% %
496% %
497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498%
499% GetLogName() returns the current log name.
500%
501% The format of the GetLogName method is:
502%
503% const char *GetLogName(void)
504%
505*/
506MagickExport const char *GetLogName(void)
507{
508 return(log_name);
509}
510
511/*
512%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513% %
514% %
515% %
516+ I n i t i a l i z e L o g L i s t %
517% %
518% %
519% %
520%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
521%
522% InitializeLogList() initialize the log list.
523%
524% The format of the InitializeLogList method is:
525%
526% MagickBooleanType InitializeLogList(ExceptionInfo *exception)
527%
528% A description of each parameter follows.
529%
530% o exception: return any errors or warnings in this structure.
531%
532*/
533static MagickBooleanType InitializeLogList(ExceptionInfo *exception)
534{
535 if ((log_list == (LinkedListInfo *) NULL) && (instantiate_log == MagickFalse))
536 {
cristy4e1dff62009-10-25 20:36:03 +0000537 if (log_semaphore == (SemaphoreInfo *) NULL)
538 AcquireSemaphoreInfo(&log_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000539 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000540 if ((log_list == (LinkedListInfo *) NULL) &&
541 (instantiate_log == MagickFalse))
542 {
543 (void) LoadLogLists(LogFilename,exception);
544 instantiate_log=MagickTrue;
545 }
cristyf84a1932010-01-03 18:00:18 +0000546 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000547 }
548 return(log_list != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
549}
550
551/*
552%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553% %
554% %
555% %
556% I s E v e n t L o g g i n g %
557% %
558% %
559% %
560%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
561%
562% IsEventLogging() returns MagickTrue if debug of events is enabled otherwise
563% MagickFalse.
564%
565% The format of the IsEventLogging method is:
566%
567% MagickBooleanType IsEventLogging(void)
568%
569*/
570MagickExport MagickBooleanType IsEventLogging(void)
571{
572 const LogInfo
573 *log_info;
574
575 ExceptionInfo
576 *exception;
577
578 if ((log_list == (LinkedListInfo *) NULL) ||
579 (IsLinkedListEmpty(log_list) != MagickFalse))
580 return(MagickFalse);
581 exception=AcquireExceptionInfo();
582 log_info=GetLogInfo("*",exception);
583 exception=DestroyExceptionInfo(exception);
584 return(log_info->event_mask != NoEvents ? MagickTrue : MagickFalse);
585}
586/*
587%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588% %
589% %
590% %
591% L i s t L o g I n f o %
592% %
593% %
594% %
595%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596%
597% ListLogInfo() lists the log info to a file.
598%
599% The format of the ListLogInfo method is:
600%
601% MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
602%
603% A description of each parameter follows.
604%
605% o file: An pointer to a FILE.
606%
607% o exception: return any errors or warnings in this structure.
608%
609*/
610MagickExport MagickBooleanType ListLogInfo(FILE *file,ExceptionInfo *exception)
611{
612#define MegabytesToBytes(value) ((MagickSizeType) (value)*1024*1024)
613
614 char
615 limit[MaxTextExtent];
616
617 const char
618 *path;
619
620 const LogInfo
621 **log_info;
622
cristybb503372010-05-27 20:51:26 +0000623 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000624 i;
625
cristybb503372010-05-27 20:51:26 +0000626 size_t
cristy3ed852e2009-09-05 21:47:34 +0000627 number_aliases;
628
cristy9d314ff2011-03-09 01:30:28 +0000629 ssize_t
630 j;
631
cristy3ed852e2009-09-05 21:47:34 +0000632 if (file == (const FILE *) NULL)
633 file=stdout;
634 log_info=GetLogInfoList("*",&number_aliases,exception);
635 if (log_info == (const LogInfo **) NULL)
636 return(MagickFalse);
637 j=0;
638 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000639 for (i=0; i < (ssize_t) number_aliases; i++)
cristy3ed852e2009-09-05 21:47:34 +0000640 {
641 if (log_info[i]->stealth != MagickFalse)
642 continue;
643 if ((path == (const char *) NULL) ||
644 (LocaleCompare(path,log_info[i]->path) != 0))
645 {
646 if (log_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000647 (void) FormatLocaleFile(file,"\nPath: %s\n\n",log_info[i]->path);
cristy1e604812011-05-19 18:07:50 +0000648 (void) FormatLocaleFile(file,
649 "Filename Generations Limit Format\n");
650 (void) FormatLocaleFile(file,
651 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000652 "------------------------------\n");
653 }
654 path=log_info[i]->path;
655 if (log_info[i]->filename != (char *) NULL)
656 {
cristyb51dff52011-05-19 16:55:47 +0000657 (void) FormatLocaleFile(file,"%s",log_info[i]->filename);
cristybb503372010-05-27 20:51:26 +0000658 for (j=(ssize_t) strlen(log_info[i]->filename); j <= 16; j++)
cristyb51dff52011-05-19 16:55:47 +0000659 (void) FormatLocaleFile(file," ");
cristy3ed852e2009-09-05 21:47:34 +0000660 }
cristyb51dff52011-05-19 16:55:47 +0000661 (void) FormatLocaleFile(file,"%9g ",(double) log_info[i]->generations);
cristyb9080c92009-12-01 20:13:26 +0000662 (void) FormatMagickSize(MegabytesToBytes(log_info[i]->limit),MagickFalse,
663 limit);
cristyb51dff52011-05-19 16:55:47 +0000664 (void) FormatLocaleFile(file,"%8sB ",limit);
cristy3ed852e2009-09-05 21:47:34 +0000665 if (log_info[i]->format != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000666 (void) FormatLocaleFile(file,"%s",log_info[i]->format);
667 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000668 }
669 (void) fflush(file);
670 log_info=(const LogInfo **) RelinquishMagickMemory((void *) log_info);
671 return(MagickTrue);
672}
673
674/*
675%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676% %
677% %
678% %
cristyf34a1452009-10-24 22:29:27 +0000679+ L o g C o m p o n e n t G e n e s i s %
680% %
681% %
682% %
683%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684%
685% LogComponentGenesis() instantiates the log component.
686%
687% The format of the LogComponentGenesis method is:
688%
689% MagickBooleanType LogComponentGenesis(void)
690%
691*/
cristy5ff4eaf2011-09-03 01:38:02 +0000692MagickPrivate MagickBooleanType LogComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +0000693{
cristy165b6092009-10-26 13:52:10 +0000694 AcquireSemaphoreInfo(&log_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000695 return(MagickTrue);
696}
697
698/*
699%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
700% %
701% %
702% %
703+ L o g C o m p o n e n t T e r m i n u s %
704% %
705% %
706% %
707%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
708%
709% LogComponentTerminus() destroys the logging component.
710%
711% The format of the LogComponentTerminus method is:
712%
713% LogComponentTerminus(void)
714%
715*/
716
717static void *DestroyLogElement(void *log_info)
718{
719 register LogInfo
720 *p;
721
722 p=(LogInfo *) log_info;
723 if (p->file != (FILE *) NULL)
724 {
725 if (p->append == MagickFalse)
cristyb51dff52011-05-19 16:55:47 +0000726 (void) FormatLocaleFile(p->file,"</log>\n");
cristyf34a1452009-10-24 22:29:27 +0000727 (void) fclose(p->file);
728 p->file=(FILE *) NULL;
729 }
730 if (p->exempt == MagickFalse)
731 {
732 if (p->format != (char *) NULL)
733 p->format=DestroyString(p->format);
734 if (p->path != (char *) NULL)
735 p->path=DestroyString(p->path);
736 if (p->filename != (char *) NULL)
737 p->filename=DestroyString(p->filename);
738 }
739 p=(LogInfo *) RelinquishMagickMemory(p);
740 return((void *) NULL);
741}
742
cristy5ff4eaf2011-09-03 01:38:02 +0000743MagickPrivate void LogComponentTerminus(void)
cristyf34a1452009-10-24 22:29:27 +0000744{
cristy18b17442009-10-25 18:36:48 +0000745 if (log_semaphore == (SemaphoreInfo *) NULL)
746 AcquireSemaphoreInfo(&log_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000747 LockSemaphoreInfo(log_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000748 if (log_list != (LinkedListInfo *) NULL)
749 log_list=DestroyLinkedList(log_list,DestroyLogElement);
750 instantiate_log=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +0000751 UnlockSemaphoreInfo(log_semaphore);
cristyf34a1452009-10-24 22:29:27 +0000752 DestroySemaphoreInfo(&log_semaphore);
753}
754
755/*
756%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757% %
758% %
759% %
cristy3ed852e2009-09-05 21:47:34 +0000760% L o g M a g i c k E v e n t %
761% %
762% %
763% %
764%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765%
766% LogMagickEvent() logs an event as determined by the log configuration file.
767% If an error occurs, MagickFalse is returned otherwise MagickTrue.
768%
769% The format of the LogMagickEvent method is:
770%
771% MagickBooleanType LogMagickEvent(const LogEventType type,
cristybb503372010-05-27 20:51:26 +0000772% const char *module,const char *function,const size_t line,
cristy3ed852e2009-09-05 21:47:34 +0000773% const char *format,...)
774%
775% A description of each parameter follows:
776%
777% o type: the event type.
778%
779% o filename: the source module filename.
780%
781% o function: the function name.
782%
783% o line: the line number of the source module.
784%
785% o format: the output format.
786%
787*/
788static char *TranslateEvent(const LogEventType magick_unused(type),
cristybb503372010-05-27 20:51:26 +0000789 const char *module,const char *function,const size_t line,
cristy3ed852e2009-09-05 21:47:34 +0000790 const char *domain,const char *event)
791{
792 char
793 *text;
794
795 double
796 elapsed_time,
797 user_time;
798
799 ExceptionInfo
800 *exception;
801
802 LogInfo
803 *log_info;
804
805 register char
806 *q;
807
808 register const char
809 *p;
810
811 size_t
812 extent;
813
814 time_t
815 seconds;
816
817 exception=AcquireExceptionInfo();
818 log_info=(LogInfo *) GetLogInfo("*",exception);
819 exception=DestroyExceptionInfo(exception);
820 seconds=time((time_t *) NULL);
821 elapsed_time=GetElapsedTime(&log_info->timer);
822 user_time=GetUserTime(&log_info->timer);
823 text=AcquireString(event);
824 if (log_info->format == (char *) NULL)
825 return(text);
826 extent=strlen(event)+MaxTextExtent;
827 if (LocaleCompare(log_info->format,"xml") == 0)
828 {
829 char
830 timestamp[MaxTextExtent];
831
832 /*
833 Translate event in "XML" format.
834 */
835 (void) FormatMagickTime(seconds,extent,timestamp);
cristyb51dff52011-05-19 16:55:47 +0000836 (void) FormatLocaleString(text,extent,
cristy3ed852e2009-09-05 21:47:34 +0000837 "<entry>\n"
838 " <timestamp>%s</timestamp>\n"
cristy91f905a2010-06-03 01:01:32 +0000839 " <elapsed-time>%lu:%02lu.%03lu</elapsed-time>\n"
cristy3ed852e2009-09-05 21:47:34 +0000840 " <user-time>%0.3f</user-time>\n"
cristye8c25f92010-06-03 00:53:06 +0000841 " <process-id>%.20g</process-id>\n"
842 " <thread-id>%.20g</thread-id>\n"
cristy3ed852e2009-09-05 21:47:34 +0000843 " <module>%s</module>\n"
844 " <function>%s</function>\n"
cristye8c25f92010-06-03 00:53:06 +0000845 " <line>%.20g</line>\n"
cristy3ed852e2009-09-05 21:47:34 +0000846 " <domain>%s</domain>\n"
847 " <event>%s</event>\n"
cristy91f905a2010-06-03 01:01:32 +0000848 "</entry>",timestamp,(unsigned long) (elapsed_time/60.0),
849 (unsigned long) floor(fmod(elapsed_time,60.0)),(unsigned long)
850 (1000.0*(elapsed_time-floor(elapsed_time))+0.5),user_time,
851 (double) getpid(),(double) GetMagickThreadSignature(),module,function,
852 (double) line,domain,event);
cristy3ed852e2009-09-05 21:47:34 +0000853 return(text);
854 }
855 /*
856 Translate event in "human readable" format.
857 */
858 q=text;
859 for (p=log_info->format; *p != '\0'; p++)
860 {
861 *q='\0';
862 if ((size_t) (q-text+MaxTextExtent) >= extent)
863 {
864 extent+=MaxTextExtent;
865 text=(char *) ResizeQuantumMemory(text,extent+MaxTextExtent,
866 sizeof(*text));
867 if (text == (char *) NULL)
868 return((char *) NULL);
869 q=text+strlen(text);
870 }
871 /*
872 The format of the log is defined by embedding special format characters:
873
874 %c client name
875 %d domain
876 %e event
877 %f function
878 %g generation
879 %l line
880 %m module
881 %n log name
882 %p process id
883 %r real CPU time
884 %t wall clock time
885 %u user CPU time
886 %v version
887 %% percent sign
888 \n newline
889 \r carriage return
890 */
891 if ((*p == '\\') && (*(p+1) == 'r'))
892 {
893 *q++='\r';
894 p++;
895 continue;
896 }
897 if ((*p == '\\') && (*(p+1) == 'n'))
898 {
899 *q++='\n';
900 p++;
901 continue;
902 }
903 if (*p != '%')
904 {
905 *q++=(*p);
906 continue;
907 }
908 p++;
909 switch (*p)
910 {
911 case 'c':
912 {
913 q+=CopyMagickString(q,GetClientName(),extent);
914 break;
915 }
916 case 'd':
917 {
918 q+=CopyMagickString(q,domain,extent);
919 break;
920 }
921 case 'e':
922 {
923 q+=CopyMagickString(q,event,extent);
924 break;
925 }
926 case 'f':
927 {
928 q+=CopyMagickString(q,function,extent);
929 break;
930 }
931 case 'g':
932 {
933 if (log_info->generations == 0)
934 {
935 (void) CopyMagickString(q,"0",extent);
936 q++;
937 break;
938 }
cristyb51dff52011-05-19 16:55:47 +0000939 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
cristye8c25f92010-06-03 00:53:06 +0000940 log_info->generations));
cristy3ed852e2009-09-05 21:47:34 +0000941 break;
942 }
943 case 'l':
944 {
cristyb51dff52011-05-19 16:55:47 +0000945 q+=FormatLocaleString(q,extent,"%.20g",(double) line);
cristy3ed852e2009-09-05 21:47:34 +0000946 break;
947 }
948 case 'm':
949 {
950 register const char
951 *p;
952
953 for (p=module+strlen(module)-1; p > module; p--)
954 if (*p == *DirectorySeparator)
955 {
956 p++;
957 break;
958 }
959 q+=CopyMagickString(q,p,extent);
960 break;
961 }
962 case 'n':
963 {
964 q+=CopyMagickString(q,GetLogName(),extent);
965 break;
966 }
967 case 'p':
968 {
cristyb51dff52011-05-19 16:55:47 +0000969 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
cristy3ed852e2009-09-05 21:47:34 +0000970 break;
971 }
972 case 'r':
973 {
cristyb51dff52011-05-19 16:55:47 +0000974 q+=FormatLocaleString(q,extent,"%lu:%02lu.%03lu",(unsigned long)
cristy91f905a2010-06-03 01:01:32 +0000975 (elapsed_time/60.0),(unsigned long) floor(fmod(elapsed_time,60.0)),
976 (unsigned long) (1000.0*(elapsed_time-floor(elapsed_time))+0.5));
cristy3ed852e2009-09-05 21:47:34 +0000977 break;
978 }
979 case 't':
980 {
981 q+=FormatMagickTime(seconds,extent,q);
982 break;
983 }
984 case 'u':
985 {
cristyb51dff52011-05-19 16:55:47 +0000986 q+=FormatLocaleString(q,extent,"%0.3fu",user_time);
cristy3ed852e2009-09-05 21:47:34 +0000987 break;
988 }
989 case 'v':
990 {
991 q+=CopyMagickString(q,MagickLibVersionText,extent);
992 break;
993 }
994 case '%':
995 {
996 *q++=(*p);
997 break;
998 }
999 default:
1000 {
1001 *q++='%';
1002 *q++=(*p);
1003 break;
1004 }
1005 }
1006 }
1007 *q='\0';
1008 return(text);
1009}
1010
1011static char *TranslateFilename(const LogInfo *log_info)
1012{
1013 char
1014 *filename;
1015
1016 register char
1017 *q;
1018
1019 register const char
1020 *p;
1021
1022 size_t
1023 extent;
1024
1025 /*
1026 Translate event in "human readable" format.
1027 */
1028 assert(log_info != (LogInfo *) NULL);
1029 assert(log_info->filename != (char *) NULL);
1030 filename=AcquireString((char *) NULL);
1031 extent=MaxTextExtent;
1032 q=filename;
1033 for (p=log_info->filename; *p != '\0'; p++)
1034 {
1035 *q='\0';
1036 if ((size_t) (q-filename+MaxTextExtent) >= extent)
1037 {
1038 extent+=MaxTextExtent;
1039 filename=(char *) ResizeQuantumMemory(filename,extent+MaxTextExtent,
1040 sizeof(*filename));
1041 if (filename == (char *) NULL)
1042 return((char *) NULL);
1043 q=filename+strlen(filename);
1044 }
1045 /*
1046 The format of the filename is defined by embedding special format
1047 characters:
1048
1049 %c client name
1050 %n log name
1051 %p process id
1052 %v version
1053 %% percent sign
1054 */
1055 if (*p != '%')
1056 {
1057 *q++=(*p);
1058 continue;
1059 }
1060 p++;
1061 switch (*p)
1062 {
1063 case 'c':
1064 {
1065 q+=CopyMagickString(q,GetClientName(),extent);
1066 break;
1067 }
1068 case 'g':
1069 {
1070 if (log_info->generations == 0)
1071 {
1072 (void) CopyMagickString(q,"0",extent);
1073 q++;
1074 break;
1075 }
cristyb51dff52011-05-19 16:55:47 +00001076 q+=FormatLocaleString(q,extent,"%.20g",(double) (log_info->generation %
cristye8c25f92010-06-03 00:53:06 +00001077 log_info->generations));
cristy3ed852e2009-09-05 21:47:34 +00001078 break;
1079 }
1080 case 'n':
1081 {
1082 q+=CopyMagickString(q,GetLogName(),extent);
1083 break;
1084 }
1085 case 'p':
1086 {
cristyb51dff52011-05-19 16:55:47 +00001087 q+=FormatLocaleString(q,extent,"%.20g",(double) getpid());
cristy3ed852e2009-09-05 21:47:34 +00001088 break;
1089 }
1090 case 'v':
1091 {
1092 q+=CopyMagickString(q,MagickLibVersionText,extent);
1093 break;
1094 }
1095 case '%':
1096 {
1097 *q++=(*p);
1098 break;
1099 }
1100 default:
1101 {
1102 *q++='%';
1103 *q++=(*p);
1104 break;
1105 }
1106 }
1107 }
1108 *q='\0';
1109 return(filename);
1110}
1111
1112MagickBooleanType LogMagickEventList(const LogEventType type,const char *module,
cristybb503372010-05-27 20:51:26 +00001113 const char *function,const size_t line,const char *format,
cristy3ed852e2009-09-05 21:47:34 +00001114 va_list operands)
1115{
1116 char
1117 event[MaxTextExtent],
1118 *text;
1119
1120 const char
1121 *domain;
1122
1123 ExceptionInfo
1124 *exception;
1125
1126 int
1127 n;
1128
1129 LogInfo
1130 *log_info;
1131
1132 if (IsEventLogging() == MagickFalse)
1133 return(MagickFalse);
1134 exception=AcquireExceptionInfo();
1135 log_info=(LogInfo *) GetLogInfo("*",exception);
1136 exception=DestroyExceptionInfo(exception);
cristyf84a1932010-01-03 18:00:18 +00001137 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001138 if ((log_info->event_mask & type) == 0)
1139 {
cristyf84a1932010-01-03 18:00:18 +00001140 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001141 return(MagickTrue);
1142 }
cristy042ee782011-04-22 18:48:30 +00001143 domain=CommandOptionToMnemonic(MagickLogEventOptions,type);
cristy3ed852e2009-09-05 21:47:34 +00001144#if defined(MAGICKCORE_HAVE_VSNPRINTF)
1145 n=vsnprintf(event,MaxTextExtent,format,operands);
1146#else
1147 n=vsprintf(event,format,operands);
1148#endif
1149 if (n < 0)
1150 event[MaxTextExtent-1]='\0';
1151 text=TranslateEvent(type,module,function,line,domain,event);
1152 if (text == (char *) NULL)
1153 {
1154 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001155 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001156 return(MagickFalse);
1157 }
1158 if ((log_info->handler_mask & ConsoleHandler) != 0)
1159 {
cristyb51dff52011-05-19 16:55:47 +00001160 (void) FormatLocaleFile(stderr,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001161 (void) fflush(stderr);
1162 }
1163 if ((log_info->handler_mask & DebugHandler) != 0)
1164 {
cristy0157aea2010-04-24 21:12:18 +00001165#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001166 OutputDebugString(text);
1167#endif
1168 }
1169 if ((log_info->handler_mask & EventHandler) != 0)
1170 {
cristy0157aea2010-04-24 21:12:18 +00001171#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001172 (void) NTReportEvent(text,MagickFalse);
1173#endif
1174 }
1175 if ((log_info->handler_mask & FileHandler) != 0)
1176 {
1177 struct stat
1178 file_info;
1179
1180 file_info.st_size=0;
1181 if (log_info->file != (FILE *) NULL)
1182 (void) fstat(fileno(log_info->file),&file_info);
cristy94b11832011-09-08 19:46:03 +00001183 if (file_info.st_size > (ssize_t) (1024*1024*log_info->limit))
cristy3ed852e2009-09-05 21:47:34 +00001184 {
cristyb51dff52011-05-19 16:55:47 +00001185 (void) FormatLocaleFile(log_info->file,"</log>\n");
cristy3ed852e2009-09-05 21:47:34 +00001186 (void) fclose(log_info->file);
1187 log_info->file=(FILE *) NULL;
1188 }
1189 if (log_info->file == (FILE *) NULL)
1190 {
1191 char
1192 *filename;
1193
1194 filename=TranslateFilename(log_info);
1195 if (filename == (char *) NULL)
1196 {
1197 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001198 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001199 return(MagickFalse);
1200 }
1201 log_info->append=IsPathAccessible(filename);
cristy18c6c272011-09-23 14:40:37 +00001202 log_info->file=fopen_utf8(filename,"ab");
cristy3ed852e2009-09-05 21:47:34 +00001203 filename=(char *) RelinquishMagickMemory(filename);
1204 if (log_info->file == (FILE *) NULL)
1205 {
cristyf84a1932010-01-03 18:00:18 +00001206 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001207 return(MagickFalse);
1208 }
1209 log_info->generation++;
1210 if (log_info->append == MagickFalse)
1211 {
cristyb51dff52011-05-19 16:55:47 +00001212 (void) FormatLocaleFile(log_info->file,"<?xml version=\"1.0\" "
cristy3ed852e2009-09-05 21:47:34 +00001213 "encoding=\"UTF-8\" standalone=\"yes\"?>\n");
cristyb51dff52011-05-19 16:55:47 +00001214 (void) FormatLocaleFile(log_info->file,"<log>\n");
cristy3ed852e2009-09-05 21:47:34 +00001215 }
1216 }
cristyb51dff52011-05-19 16:55:47 +00001217 (void) FormatLocaleFile(log_info->file,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001218 (void) fflush(log_info->file);
1219 }
1220 if ((log_info->handler_mask & StdoutHandler) != 0)
1221 {
cristyb51dff52011-05-19 16:55:47 +00001222 (void) FormatLocaleFile(stdout,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001223 (void) fflush(stdout);
1224 }
1225 if ((log_info->handler_mask & StderrHandler) != 0)
1226 {
cristyb51dff52011-05-19 16:55:47 +00001227 (void) FormatLocaleFile(stderr,"%s\n",text);
cristy3ed852e2009-09-05 21:47:34 +00001228 (void) fflush(stderr);
1229 }
1230 text=(char *) RelinquishMagickMemory(text);
1231 (void) ContinueTimer((TimerInfo *) &log_info->timer);
cristyf84a1932010-01-03 18:00:18 +00001232 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001233 return(MagickTrue);
1234}
1235
1236MagickBooleanType LogMagickEvent(const LogEventType type,const char *module,
cristybb503372010-05-27 20:51:26 +00001237 const char *function,const size_t line,const char *format,...)
cristy3ed852e2009-09-05 21:47:34 +00001238{
1239 va_list
1240 operands;
1241
1242 MagickBooleanType
1243 status;
1244
1245 va_start(operands,format);
1246 status=LogMagickEventList(type,module,function,line,format,operands);
1247 va_end(operands);
1248 return(status);
1249}
1250
1251/*
1252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1253% %
1254% %
1255% %
1256+ L o a d L o g L i s t %
1257% %
1258% %
1259% %
1260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1261%
1262% LoadLogList() loads the log configuration file which provides a
1263% mapping between log attributes and log name.
1264%
1265% The format of the LoadLogList method is:
1266%
1267% MagickBooleanType LoadLogList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001268% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001269%
1270% A description of each parameter follows:
1271%
1272% o xml: The log list in XML format.
1273%
1274% o filename: The log list filename.
1275%
1276% o depth: depth of <include /> statements.
1277%
1278% o exception: return any errors or warnings in this structure.
1279%
1280*/
1281static MagickBooleanType LoadLogList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001282 const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001283{
1284 char
1285 keyword[MaxTextExtent],
1286 *token;
1287
1288 const char
1289 *q;
1290
1291 LogInfo
1292 *log_info = (LogInfo *) NULL;
1293
1294 MagickStatusType
1295 status;
1296
1297 /*
1298 Load the log map file.
1299 */
1300 if (xml == (const char *) NULL)
1301 return(MagickFalse);
1302 if (log_list == (LinkedListInfo *) NULL)
1303 {
1304 log_list=NewLinkedList(0);
1305 if (log_list == (LinkedListInfo *) NULL)
1306 {
1307 ThrowFileException(exception,ResourceLimitError,
1308 "MemoryAllocationFailed",filename);
1309 return(MagickFalse);
1310 }
1311 }
1312 status=MagickTrue;
1313 token=AcquireString((const char *) xml);
1314 for (q=(const char *) xml; *q != '\0'; )
1315 {
1316 /*
1317 Interpret XML.
1318 */
1319 GetMagickToken(q,&q,token);
1320 if (*token == '\0')
1321 break;
1322 (void) CopyMagickString(keyword,token,MaxTextExtent);
1323 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1324 {
1325 /*
1326 Doctype element.
1327 */
1328 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1329 GetMagickToken(q,&q,token);
1330 continue;
1331 }
1332 if (LocaleNCompare(keyword,"<!--",4) == 0)
1333 {
1334 /*
1335 Comment element.
1336 */
1337 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1338 GetMagickToken(q,&q,token);
1339 continue;
1340 }
1341 if (LocaleCompare(keyword,"<include") == 0)
1342 {
1343 /*
1344 Include element.
1345 */
1346 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1347 {
1348 (void) CopyMagickString(keyword,token,MaxTextExtent);
1349 GetMagickToken(q,&q,token);
1350 if (*token != '=')
1351 continue;
1352 GetMagickToken(q,&q,token);
1353 if (LocaleCompare(keyword,"file") == 0)
1354 {
1355 if (depth > 200)
1356 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001357 ConfigureError,"IncludeElementNestedTooDeeply","'%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00001358 else
1359 {
1360 char
1361 path[MaxTextExtent],
1362 *xml;
1363
1364 GetPathComponent(filename,HeadPath,path);
1365 if (*path != '\0')
1366 (void) ConcatenateMagickString(path,DirectorySeparator,
1367 MaxTextExtent);
1368 if (*token == *DirectorySeparator)
1369 (void) CopyMagickString(path,token,MaxTextExtent);
1370 else
1371 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1372 xml=FileToString(path,~0,exception);
1373 if (xml != (char *) NULL)
1374 {
1375 status|=LoadLogList(xml,path,depth+1,exception);
1376 xml=DestroyString(xml);
1377 }
1378 }
1379 }
1380 }
1381 continue;
1382 }
1383 if (LocaleCompare(keyword,"<logmap>") == 0)
1384 {
1385 /*
1386 Allocate memory for the log list.
1387 */
cristy73bd4a52010-10-05 11:24:23 +00001388 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
cristy3ed852e2009-09-05 21:47:34 +00001389 if (log_info == (LogInfo *) NULL)
1390 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1391 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1392 log_info->path=ConstantString(filename);
1393 GetTimerInfo((TimerInfo *) &log_info->timer);
cristy85340292009-10-21 19:32:19 +00001394 log_info->exempt=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001395 log_info->signature=MagickSignature;
1396 continue;
1397 }
1398 if (log_info == (LogInfo *) NULL)
1399 continue;
1400 if (LocaleCompare(keyword,"</logmap>") == 0)
1401 {
1402 status=AppendValueToLinkedList(log_list,log_info);
1403 if (status == MagickFalse)
1404 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001405 ResourceLimitError,"MemoryAllocationFailed","'%s'",filename);
cristy3ed852e2009-09-05 21:47:34 +00001406 log_info=(LogInfo *) NULL;
1407 }
1408 GetMagickToken(q,(const char **) NULL,token);
1409 if (*token != '=')
1410 continue;
1411 GetMagickToken(q,&q,token);
1412 GetMagickToken(q,&q,token);
1413 switch (*keyword)
1414 {
1415 case 'E':
1416 case 'e':
1417 {
1418 if (LocaleCompare((char *) keyword,"events") == 0)
1419 {
1420 log_info->event_mask=(LogEventType) (log_info->event_mask |
cristy042ee782011-04-22 18:48:30 +00001421 ParseCommandOption(MagickLogEventOptions,MagickTrue,token));
cristy3ed852e2009-09-05 21:47:34 +00001422 break;
1423 }
1424 break;
1425 }
1426 case 'F':
1427 case 'f':
1428 {
1429 if (LocaleCompare((char *) keyword,"filename") == 0)
1430 {
1431 if (log_info->filename != (char *) NULL)
1432 log_info->filename=(char *)
1433 RelinquishMagickMemory(log_info->filename);
1434 log_info->filename=ConstantString(token);
1435 break;
1436 }
1437 if (LocaleCompare((char *) keyword,"format") == 0)
1438 {
1439 if (log_info->format != (char *) NULL)
1440 log_info->format=(char *)
1441 RelinquishMagickMemory(log_info->format);
1442 log_info->format=ConstantString(token);
1443 break;
1444 }
1445 break;
1446 }
1447 case 'G':
1448 case 'g':
1449 {
1450 if (LocaleCompare((char *) keyword,"generations") == 0)
1451 {
1452 if (LocaleCompare(token,"unlimited") == 0)
1453 {
1454 log_info->generations=(~0UL);
1455 break;
1456 }
cristye27293e2009-12-18 02:53:20 +00001457 log_info->generations=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00001458 break;
1459 }
1460 break;
1461 }
1462 case 'L':
1463 case 'l':
1464 {
1465 if (LocaleCompare((char *) keyword,"limit") == 0)
1466 {
1467 if (LocaleCompare(token,"unlimited") == 0)
1468 {
1469 log_info->limit=(~0UL);
1470 break;
1471 }
cristye27293e2009-12-18 02:53:20 +00001472 log_info->limit=StringToUnsignedLong(token);
cristy3ed852e2009-09-05 21:47:34 +00001473 break;
1474 }
1475 break;
1476 }
1477 case 'O':
1478 case 'o':
1479 {
1480 if (LocaleCompare((char *) keyword,"output") == 0)
1481 {
1482 log_info->handler_mask=(LogHandlerType)
1483 (log_info->handler_mask | ParseLogHandlers(token));
1484 break;
1485 }
1486 break;
1487 }
1488 default:
1489 break;
1490 }
1491 }
1492 token=DestroyString(token);
1493 if (log_list == (LinkedListInfo *) NULL)
1494 return(MagickFalse);
1495 return(status != 0 ? MagickTrue : MagickFalse);
1496}
1497
1498/*
1499%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1500% %
1501% %
1502% %
1503% L o a d L o g L i s t s %
1504% %
1505% %
1506% %
1507%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1508%
1509% LoadLogLists() loads one or more log configuration file which provides a
1510% mapping between log attributes and log name.
1511%
1512% The format of the LoadLogLists method is:
1513%
1514% MagickBooleanType LoadLogLists(const char *filename,
1515% ExceptionInfo *exception)
1516%
1517% A description of each parameter follows:
1518%
1519% o filename: the log configuration filename.
1520%
1521% o exception: return any errors or warnings in this structure.
1522%
1523*/
1524static MagickBooleanType LoadLogLists(const char *filename,
1525 ExceptionInfo *exception)
1526{
cristy3ed852e2009-09-05 21:47:34 +00001527 const StringInfo
1528 *option;
1529
1530 LinkedListInfo
1531 *options;
1532
1533 MagickStatusType
1534 status;
1535
cristybb503372010-05-27 20:51:26 +00001536 register ssize_t
cristy85340292009-10-21 19:32:19 +00001537 i;
1538
1539 /*
1540 Load built-in log map.
1541 */
cristy3ed852e2009-09-05 21:47:34 +00001542 status=MagickFalse;
cristy85340292009-10-21 19:32:19 +00001543 if (log_list == (LinkedListInfo *) NULL)
1544 {
1545 log_list=NewLinkedList(0);
1546 if (log_list == (LinkedListInfo *) NULL)
1547 {
1548 ThrowFileException(exception,ResourceLimitError,
1549 "MemoryAllocationFailed",filename);
1550 return(MagickFalse);
1551 }
1552 }
cristybb503372010-05-27 20:51:26 +00001553 for (i=0; i < (ssize_t) (sizeof(LogMap)/sizeof(*LogMap)); i++)
cristy85340292009-10-21 19:32:19 +00001554 {
1555 LogInfo
1556 *log_info;
1557
1558 register const LogMapInfo
1559 *p;
1560
1561 p=LogMap+i;
cristy73bd4a52010-10-05 11:24:23 +00001562 log_info=(LogInfo *) AcquireMagickMemory(sizeof(*log_info));
cristy85340292009-10-21 19:32:19 +00001563 if (log_info == (LogInfo *) NULL)
1564 {
1565 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001566 ResourceLimitError,"MemoryAllocationFailed","'%s'",log_info->name);
cristy85340292009-10-21 19:32:19 +00001567 continue;
1568 }
1569 (void) ResetMagickMemory(log_info,0,sizeof(*log_info));
1570 log_info->path=(char *) "[built-in]";
1571 GetTimerInfo((TimerInfo *) &log_info->timer);
1572 log_info->event_mask=p->event_mask;
1573 log_info->handler_mask=p->handler_mask;
cristy5aaf5742009-11-22 00:56:44 +00001574 log_info->filename=ConstantString(p->filename);
1575 log_info->format=ConstantString(p->format);
cristy85340292009-10-21 19:32:19 +00001576 log_info->exempt=MagickTrue;
1577 log_info->signature=MagickSignature;
1578 status=AppendValueToLinkedList(log_list,log_info);
1579 if (status == MagickFalse)
1580 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00001581 ResourceLimitError,"MemoryAllocationFailed","'%s'",log_info->name);
cristy85340292009-10-21 19:32:19 +00001582 }
1583 /*
1584 Load external log map.
1585 */
cristy3ed852e2009-09-05 21:47:34 +00001586 options=GetConfigureOptions(filename,exception);
1587 option=(const StringInfo *) GetNextValueInLinkedList(options);
1588 while (option != (const StringInfo *) NULL)
1589 {
1590 status|=LoadLogList((const char *) GetStringInfoDatum(option),
1591 GetStringInfoPath(option),0,exception);
1592 option=(const StringInfo *) GetNextValueInLinkedList(options);
1593 }
1594 options=DestroyConfigureOptions(options);
cristy3ed852e2009-09-05 21:47:34 +00001595 return(status != 0 ? MagickTrue : MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001596}
1597
1598/*
1599%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600% %
1601% %
1602% %
1603+ P a r s e L o g H a n d l e r s %
1604% %
1605% %
1606% %
1607%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1608%
1609% ParseLogHandlers() parses a string defining which handlers takes a log
1610% message and exports them.
1611%
1612% The format of the ParseLogHandlers method is:
1613%
1614% LogHandlerType ParseLogHandlers(const char *handlers)
1615%
1616% A description of each parameter follows:
1617%
1618% o handlers: one or more handlers separated by commas.
1619%
1620*/
1621static LogHandlerType ParseLogHandlers(const char *handlers)
1622{
1623 LogHandlerType
1624 handler_mask;
1625
1626 register const char
1627 *p;
1628
cristybb503372010-05-27 20:51:26 +00001629 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001630 i;
1631
1632 size_t
1633 length;
1634
1635 handler_mask=NoHandler;
1636 for (p=handlers; p != (char *) NULL; p=strchr(p,','))
1637 {
1638 while ((*p != '\0') && ((isspace((int) ((unsigned char) *p)) != 0) ||
1639 (*p == ',')))
1640 p++;
1641 for (i=0; LogHandlers[i].name != (char *) NULL; i++)
1642 {
1643 length=strlen(LogHandlers[i].name);
1644 if (LocaleNCompare(p,LogHandlers[i].name,length) == 0)
1645 {
1646 handler_mask=(LogHandlerType) (handler_mask | LogHandlers[i].handler);
1647 break;
1648 }
1649 }
1650 if (LogHandlers[i].name == (char *) NULL)
1651 return(UndefinedHandler);
1652 }
1653 return(handler_mask);
1654}
1655
1656/*
1657%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1658% %
1659% %
1660% %
1661% S e t L o g E v e n t M a s k %
1662% %
1663% %
1664% %
1665%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1666%
1667% SetLogEventMask() accepts a list that determines which events to log. All
1668% other events are ignored. By default, no debug is enabled. This method
1669% returns the previous log event mask.
1670%
1671% The format of the SetLogEventMask method is:
1672%
1673% LogEventType SetLogEventMask(const char *events)
1674%
1675% A description of each parameter follows:
1676%
1677% o events: log these events.
1678%
1679*/
1680MagickExport LogEventType SetLogEventMask(const char *events)
1681{
1682 ExceptionInfo
1683 *exception;
1684
1685 LogInfo
1686 *log_info;
1687
cristybb503372010-05-27 20:51:26 +00001688 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001689 option;
1690
1691 exception=AcquireExceptionInfo();
1692 log_info=(LogInfo *) GetLogInfo("*",exception);
1693 exception=DestroyExceptionInfo(exception);
cristy042ee782011-04-22 18:48:30 +00001694 option=ParseCommandOption(MagickLogEventOptions,MagickTrue,events);
cristyf84a1932010-01-03 18:00:18 +00001695 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001696 log_info=(LogInfo *) GetValueFromLinkedList(log_list,0);
1697 log_info->event_mask=(LogEventType) option;
1698 if (option == -1)
1699 log_info->event_mask=UndefinedEvents;
cristyf84a1932010-01-03 18:00:18 +00001700 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001701 return(log_info->event_mask);
1702}
1703
1704/*
1705%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1706% %
1707% %
1708% %
1709% S e t L o g F o r m a t %
1710% %
1711% %
1712% %
1713%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1714%
1715% SetLogFormat() sets the format for the "human readable" log record.
1716%
1717% The format of the LogMagickFormat method is:
1718%
1719% SetLogFormat(const char *format)
1720%
1721% A description of each parameter follows:
1722%
1723% o format: the log record format.
1724%
1725*/
1726MagickExport void SetLogFormat(const char *format)
1727{
1728 LogInfo
1729 *log_info;
1730
1731 ExceptionInfo
1732 *exception;
1733
1734 exception=AcquireExceptionInfo();
1735 log_info=(LogInfo *) GetLogInfo("*",exception);
1736 exception=DestroyExceptionInfo(exception);
cristyf84a1932010-01-03 18:00:18 +00001737 LockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001738 if (log_info->format != (char *) NULL)
1739 log_info->format=DestroyString(log_info->format);
1740 log_info->format=ConstantString(format);
cristyf84a1932010-01-03 18:00:18 +00001741 UnlockSemaphoreInfo(log_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001742}
1743
1744/*
1745%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1746% %
1747% %
1748% %
1749% S e t L o g N a m e %
1750% %
1751% %
1752% %
1753%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1754%
1755% SetLogName() sets the log name and returns it.
1756%
1757% The format of the SetLogName method is:
1758%
1759% const char *SetLogName(const char *name)
1760%
1761% A description of each parameter follows:
1762%
1763% o log_name: SetLogName() returns the current client name.
1764%
1765% o name: Specifies the new client name.
1766%
1767*/
1768MagickExport const char *SetLogName(const char *name)
1769{
1770 if ((name != (char *) NULL) && (*name != '\0'))
1771 (void) CopyMagickString(log_name,name,MaxTextExtent);
1772 return(log_name);
1773}