Changeset 4972
- Timestamp:
- Sep 7, 2005, 2:27:50 PM (21 years ago)
- Location:
- trunk/psLib/src/sys
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psTrace.c
r4951 r4972 9 9 * @author GLG, MHPCC 10 10 * 11 * @version $Revision: 1.5 7$ $Name: not supported by cvs2svn $12 * @date $Date: 2005-09-0 7 00:15:48$11 * @version $Revision: 1.58 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2005-09-08 00:27:50 $ 13 13 * 14 14 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 55 55 static p_psComponent* cRoot = NULL; // The root of the trace component 56 56 static FILE *traceFP = NULL; // File destination for messages. 57 static psBool traceTime = false; // Flag to include time info 58 static psBool traceHost = false; // Flag to include host info 59 static psBool traceLevel = false; // Flag to include level info 60 static psBool traceName = false; // Flag to include name info 61 static psBool traceMsg = true; // Flag to include message info 57 62 //static int traceFD = 0; 58 63 … … 476 481 } 477 482 483 void psTraceV(const char *comp, 484 int level, 485 const char *format, 486 va_list ap) 487 { 488 char *fmt = NULL; 489 // va_list ap; 490 static psS32 first = 1; // Flag for calling gethostname() 491 static char hostname[256 + 1]; 492 char clevel = 0; // letter-name for level 493 psS32 i = 0; 494 char head[256 + 2]; // the added two are for the ending | and \0 495 char *head_ptr = head; // where we've got to in head 496 psS32 maxLength = 256; 497 time_t clock = time(NULL); // The current time. 498 struct tm *utc = gmtime(&clock); // The current gm time. 499 500 if (traceFP == NULL) { 501 traceFP = stdout; 502 } 503 504 if (NULL == comp) { 505 psError(PS_ERR_BAD_PARAMETER_NULL, true, 506 PS_ERRORTEXT_psTrace_NULL_TRACETREE, 507 __func__); 508 return; 509 } 510 511 if (first) { 512 first = 0; 513 gethostname(hostname, 256); 514 } 515 switch (level) { 516 case PS_LOG_ABORT: 517 clevel = 'A'; 518 break; 519 520 case PS_LOG_ERROR: 521 clevel = 'E'; 522 break; 523 524 case PS_LOG_WARN: 525 clevel = 'W'; 526 break; 527 528 case PS_LOG_INFO: 529 clevel = 'I'; 530 break; 531 532 case 4: 533 case 5: 534 case 6: 535 case 7: 536 case 8: 537 case 9: 538 clevel = level + '0'; 539 break; 540 541 default: 542 psTrace("utils.logMsg", 2, "Invalid logMsg level: %d (%s)\n", level, format); 543 level = (level < 0) ? 0 : 9; 544 clevel = level + '0'; 545 break; 546 } 547 548 // Only display this message if it's trace level is less than the level 549 // of it's associatedcomponent. 550 if (level <= psTraceGetLevel(comp)) { 551 // va_start(ap, format); 552 553 // Create the various log fields... 554 if (traceTime) { 555 maxLength -= snprintf(head_ptr, maxLength, "%4d:%02d:%02d %02d:%02d:%02dZ", 556 utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday, 557 utc->tm_hour, utc->tm_min, utc->tm_sec) - 1; 558 head_ptr += strlen(head_ptr); 559 } 560 // Hostname should be 20 characters. 561 if (traceHost) { 562 if (head_ptr > head) { 563 *head_ptr++ = '|'; 564 } 565 maxLength -= snprintf(head_ptr, maxLength, "%-20s", hostname); 566 head_ptr += strlen(head_ptr); 567 } 568 if (traceLevel) { 569 if (head_ptr > head) { 570 *head_ptr++ = '|'; 571 } 572 maxLength -= snprintf(head_ptr, maxLength, "%c", clevel); 573 head_ptr += strlen(head_ptr); 574 } 575 if (traceName) { 576 if (head_ptr > head) { 577 *head_ptr++ = '|'; 578 } 579 maxLength -= snprintf(head_ptr, maxLength, "%s", comp); 580 581 head_ptr += strlen(head_ptr); 582 } 583 584 if (head_ptr > head) { 585 *head_ptr++ = '\n'; 586 } else if (!traceMsg) { // no output desired 587 return; 588 } 589 *head_ptr = '\0'; 590 591 592 593 fputs(head, traceFP); 594 if (traceMsg) { 595 // The following functions get the variable list of parameters with 596 // which this function was called, and print them to the standard 597 // output. 598 fmt = va_arg(ap, char *); 599 600 // We indent each message one space for each level of the message. 601 for (i = 0; i < level; i++) { 602 // fprintf(traceFP, " "); 603 fprintf(traceFP, "%s", format); 604 } 605 vfprintf(traceFP, fmt, ap); 606 // vfprintf(traceFP, format, ap); 607 // va_end(ap); 608 /* char msg[1024]; 609 char* msgPtr; 610 vsnprintf(msg,1024, format, ap); // create message 611 612 // detect multiple lines in message and indent each line by 4 spaces. 613 char* line = strtok_r(msg,"\n",&msgPtr); 614 while (line != NULL) { 615 fprintf(logDest," %s\n",line); 616 line = strtok_r(NULL,"\n",&msgPtr); 617 } 618 */ 619 } else { 620 fputc('\n', traceFP); 621 } 622 623 624 } 625 626 } 627 478 628 /***************************************************************************** 479 629 p_psTrace(): we display the trace message to standard output if the trace … … 495 645 ...) // arguments 496 646 { 497 char *fmt = NULL;498 647 va_list ap; 499 psS32 i = 0; 500 501 if (traceFP == NULL) { 502 traceFP = stdout; 503 } 504 505 if (NULL == comp) { 506 psError(PS_ERR_BAD_PARAMETER_NULL, true, 507 PS_ERRORTEXT_psTrace_NULL_TRACETREE, 508 __func__); 509 return; 510 } 511 // Only display this message if it's trace level is less than the level 512 // of it's associatedcomponent. 513 if (level <= psTraceGetLevel(comp)) { 514 va_start(ap, format); 515 516 // The following functions get the variable list of parameters with 517 // which this function was called, and print them to the standard 518 // output. 519 fmt = va_arg(ap, char *); 520 521 // We indent each message one space for each level of the message. 522 for (i = 0; i < level; i++) { 523 // fprintf(traceFP, " "); 524 fprintf(traceFP, "%s", format); 525 } 526 vfprintf(traceFP, fmt, ap); 527 // vfprintf(traceFP, format, ap); 528 va_end(ap); 529 } 648 va_start(ap, format); 649 psTraceV(comp, level, format, ap); 650 va_end(ap); 530 651 fflush(traceFP); 531 652 } … … 576 697 } 577 698 699 /***************************************************************************** 700 psTraceSetFormat(): Set the format of psTrace output. More precisely, 701 provide a string consisting of the letters {H (host), L (level), M 702 (message), N (name), T (time)}. The default is "HLMNT". This string 703 determines whether or not they associated type of information will be 704 included in message traces. It does not determine the order in which that 705 information will appear (that order is fixed). 706 707 Input: 708 fmt: a string specifying the format. 709 Return: 710 NULL. 711 *****************************************************************************/ 712 bool psTraceSetFormat(const char *format) 713 { 714 // assume none. 715 traceHost = false; 716 traceLevel = false; 717 traceMsg = false; 718 traceName = false; 719 traceTime = false; 720 721 // if fmt is NULL, no logging is desired. 722 if (format == NULL) { 723 return false; 724 } 725 726 // XXX: What is the purpose of this conditional. 727 if (strlen(format) == 0) { 728 format = "THLNM"; 729 } 730 // Step through each character in the format string. For each letter 731 // in that string, set the appropriate logging. 732 733 for (const char *ptr = format; *ptr != '\0'; ptr++) { 734 switch (*ptr) { 735 case 'H': 736 case 'h': 737 traceHost = true; 738 break; 739 case 'L': 740 case 'l': 741 traceLevel = true; 742 break; 743 case 'M': 744 case 'm': 745 traceMsg = true; 746 break; 747 case 'N': 748 case 'n': 749 traceName = true; 750 break; 751 case 'T': 752 case 't': 753 traceTime = true; 754 break; 755 default: 756 psError(PS_ERR_BAD_PARAMETER_VALUE, true, 757 PS_ERRORTEXT_psLogMsg_UNKNOWN_KEY, *ptr); 758 return false; 759 } 760 } 761 762 // XXX: If one must at least log error messages, why don't we set logMsg = true here? 763 if (!traceMsg) { 764 psTrace("utils.traceMsg", 1, "You must at least trace error messages (You chose \"%s\")", format); 765 766 } 767 return true; 768 } 769 770 771 578 772 #endif // #ifndef PS_NO_TRACE -
trunk/psLib/src/sys/psTrace.h
r4951 r4972 9 9 * @author GLG, MHPCC 10 10 * 11 * @version $Revision: 1.3 7$ $Name: not supported by cvs2svn $12 * @date $Date: 2005-09-0 7 00:15:48$11 * @version $Revision: 1.38 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2005-09-08 00:27:50 $ 13 13 * 14 14 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 59 59 p_psComponent; 60 60 61 #ifdef DOXYGEN 62 /** This procedure sets the trace format for future trace messages. The argument 63 * must be a character string consistsing of the letters H (host), L 64 * (level), M (message), N (name), and T (time). The default is "THLNM". 65 * Deleting a letter from the string will cause the associated information 66 * to not be logged. This procedure does not alter the order in which 67 * the messages are displayed. 68 * 69 * @return bool: True if successful, otherwise false. 70 */ 71 bool psTraceSetFormat( 72 const char *format ///< Specifies the system trace format 73 ); 74 61 75 /** Sends a trace message. */ 62 #ifdef DOXYGEN63 76 void psTrace( 64 77 const char *facil, ///< facilty of interest … … 79 92 #ifndef SWIG 80 93 #define psTrace(facil, level, ...) p_psTrace(facil, level, __VA_ARGS__) 94 /** Sends a trace message. */ 95 void psTraceV( 96 const char *facil, ///< facilty of interest 97 int level, ///< desired trace level 98 const char *format, ///< printf-style format command 99 va_list ap ///< varargs argument list 100 ); 101 81 102 #endif /* SWIG */ 82 103
Note:
See TracChangeset
for help on using the changeset viewer.
