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