Commit e48b71956da3e163fa4a52d6b6ce12e7e475d456
1 parent
cfd19b6e
Add log_value() methods for integer and floating point (#1828)
Showing
5 changed files
with
106 additions
and
24 deletions
NEWS
| ... | ... | @@ -54,7 +54,9 @@ The GApplication::log_parameters() method now takes a mandatory GChatter |
| 54 | 54 | argument to enable displaying of parameters only above a given chattiness |
| 55 | 55 | level. This avoids a log-level check in the client code. For the same |
| 56 | 56 | purpose, the GApplication methods log_string(), log_value(), log_header1(), |
| 57 | -log_header2(), and log_header3() have been introduced (#1828). | |
| 57 | +log_header2(), and log_header3() have been introduced. There are three | |
| 58 | +variants of log_value() for strings, integers and double precision values | |
| 59 | +(#1828). | |
| 58 | 60 | |
| 59 | 61 | |
| 60 | 62 | 5. Base module | ... | ... |
include/GApplication.hpp
| ... | ... | @@ -88,6 +88,12 @@ public: |
| 88 | 88 | void log_value(const GChatter& chatter, |
| 89 | 89 | const std::string& name, |
| 90 | 90 | const std::string& value); |
| 91 | + void log_value(const GChatter& chatter, | |
| 92 | + const std::string& name, | |
| 93 | + const int& value); | |
| 94 | + void log_value(const GChatter& chatter, | |
| 95 | + const std::string& name, | |
| 96 | + const double& value); | |
| 91 | 97 | void log_header1(const GChatter& chatter, |
| 92 | 98 | const std::string& header); |
| 93 | 99 | void log_header2(const GChatter& chatter, | ... | ... |
pyext/GApplication.i
| ... | ... | @@ -110,6 +110,12 @@ public: |
| 110 | 110 | void log_value(const GChatter& chatter, |
| 111 | 111 | const std::string& name, |
| 112 | 112 | const std::string& value); |
| 113 | + void log_value(const GChatter& chatter, | |
| 114 | + const std::string& name, | |
| 115 | + const int& value); | |
| 116 | + void log_value(const GChatter& chatter, | |
| 117 | + const std::string& name, | |
| 118 | + const double& value); | |
| 113 | 119 | void log_header1(const GChatter& chatter, |
| 114 | 120 | const std::string& header); |
| 115 | 121 | void log_header2(const GChatter& chatter, | ... | ... |
src/app/GApplication.cpp
| ... | ... | @@ -552,6 +552,64 @@ void GApplication::log_value(const GChatter& chatter, |
| 552 | 552 | |
| 553 | 553 | |
| 554 | 554 | /***********************************************************************//** |
| 555 | + * @brief Write parameter value in log file | |
| 556 | + * | |
| 557 | + * @param[in] chatter Minimum required chattiness | |
| 558 | + * @param[in] name Parameter name string | |
| 559 | + * @param[in] value Integer value | |
| 560 | + * | |
| 561 | + * Writes a parameter value into the log file if chattiness is at least | |
| 562 | + * @p chatter. | |
| 563 | + ***************************************************************************/ | |
| 564 | +void GApplication::log_value(const GChatter& chatter, | |
| 565 | + const std::string& name, | |
| 566 | + const int& value) | |
| 567 | +{ | |
| 568 | + // Get chattiness of application | |
| 569 | + GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer()); | |
| 570 | + | |
| 571 | + // Only write message if chattiness is at least equal to the minimum | |
| 572 | + // required chattiness | |
| 573 | + if (chattiness >= chatter) { | |
| 574 | + log << gammalib::parformat(name); | |
| 575 | + log << value << std::endl; | |
| 576 | + } | |
| 577 | + | |
| 578 | + // Return | |
| 579 | + return; | |
| 580 | +} | |
| 581 | + | |
| 582 | + | |
| 583 | +/***********************************************************************//** | |
| 584 | + * @brief Write parameter value in log file | |
| 585 | + * | |
| 586 | + * @param[in] chatter Minimum required chattiness | |
| 587 | + * @param[in] name Parameter name string | |
| 588 | + * @param[in] value Floating point value | |
| 589 | + * | |
| 590 | + * Writes a parameter value into the log file if chattiness is at least | |
| 591 | + * @p chatter. | |
| 592 | + ***************************************************************************/ | |
| 593 | +void GApplication::log_value(const GChatter& chatter, | |
| 594 | + const std::string& name, | |
| 595 | + const double& value) | |
| 596 | +{ | |
| 597 | + // Get chattiness of application | |
| 598 | + GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer()); | |
| 599 | + | |
| 600 | + // Only write message if chattiness is at least equal to the minimum | |
| 601 | + // required chattiness | |
| 602 | + if (chattiness >= chatter) { | |
| 603 | + log << gammalib::parformat(name); | |
| 604 | + log << value << std::endl; | |
| 605 | + } | |
| 606 | + | |
| 607 | + // Return | |
| 608 | + return; | |
| 609 | +} | |
| 610 | + | |
| 611 | + | |
| 612 | +/***********************************************************************//** | |
| 555 | 613 | * @brief Write header 1 in log file |
| 556 | 614 | * |
| 557 | 615 | * @param[in] chatter Minimum required chattiness | ... | ... |
test/test_GApplication.cpp
| ... | ... | @@ -388,8 +388,12 @@ void TestGApplication::test_GApplication(void) |
| 388 | 388 | app1.log_string(EXPLICIT, "Explicit"); |
| 389 | 389 | app1.log_string(VERBOSE, "Verbose"); |
| 390 | 390 | app1.log_string(TERSE, "\n"); |
| 391 | - app1.log_value(NORMAL, "User parameter", "3.14"); | |
| 392 | - app1.log_value(VERBOSE, "Verbose parameter", "3.14!!"); | |
| 391 | + app1.log_value(NORMAL, "String parameter", "3.14"); | |
| 392 | + app1.log_value(NORMAL, "Floating parameter", 3.14); | |
| 393 | + app1.log_value(NORMAL, "Integer parameter", 3); | |
| 394 | + app1.log_value(VERBOSE, "Verbose string parameter", "3.14!!"); | |
| 395 | + app1.log_value(VERBOSE, "Verbose floating parameter", 99.9); | |
| 396 | + app1.log_value(VERBOSE, "Verbose integer parameter", -1); | |
| 393 | 397 | app1.log_header1(NORMAL, "Header 1"); |
| 394 | 398 | app1.log_header2(NORMAL, "Header 2"); |
| 395 | 399 | app1.log_header3(NORMAL, "Header 3"); |
| ... | ... | @@ -437,66 +441,72 @@ void TestGApplication::test_GApplication(void) |
| 437 | 441 | |
| 438 | 442 | // Test logging of user parameter |
| 439 | 443 | fgets(line, 100, fp); |
| 440 | - test_value(line, " User parameter ............: 3.14\n", | |
| 444 | + test_value(line, " String parameter ..........: 3.14\n", | |
| 441 | 445 | "Check log file line 7"); |
| 446 | + fgets(line, 100, fp); | |
| 447 | + test_value(line, " Floating parameter ........: 3.14\n", | |
| 448 | + "Check log file line 8"); | |
| 449 | + fgets(line, 100, fp); | |
| 450 | + test_value(line, " Integer parameter .........: 3\n", | |
| 451 | + "Check log file line 9"); | |
| 442 | 452 | |
| 443 | 453 | // Test logging of header 1 |
| 444 | 454 | fgets(line, 100, fp); |
| 445 | - test_value(line, "\n", "Check log file line 8"); | |
| 455 | + test_value(line, "\n", "Check log file line 10"); | |
| 446 | 456 | fgets(line, 100, fp); |
| 447 | - test_value(line, "+==========+\n", "Check log file line 9"); | |
| 457 | + test_value(line, "+==========+\n", "Check log file line 11"); | |
| 448 | 458 | fgets(line, 100, fp); |
| 449 | - test_value(line, "| Header 1 |\n", "Check log file line 10"); | |
| 459 | + test_value(line, "| Header 1 |\n", "Check log file line 12"); | |
| 450 | 460 | fgets(line, 100, fp); |
| 451 | - test_value(line, "+==========+\n", "Check log file line 11"); | |
| 461 | + test_value(line, "+==========+\n", "Check log file line 13"); | |
| 452 | 462 | |
| 453 | 463 | // Test logging of header 2 |
| 454 | 464 | fgets(line, 100, fp); |
| 455 | - test_value(line, "\n", "Check log file line 12"); | |
| 465 | + test_value(line, "\n", "Check log file line 14"); | |
| 456 | 466 | fgets(line, 100, fp); |
| 457 | - test_value(line, "+----------+\n", "Check log file line 13"); | |
| 467 | + test_value(line, "+----------+\n", "Check log file line 15"); | |
| 458 | 468 | fgets(line, 100, fp); |
| 459 | - test_value(line, "| Header 2 |\n", "Check log file line 14"); | |
| 469 | + test_value(line, "| Header 2 |\n", "Check log file line 16"); | |
| 460 | 470 | fgets(line, 100, fp); |
| 461 | - test_value(line, "+----------+\n", "Check log file line 15"); | |
| 471 | + test_value(line, "+----------+\n", "Check log file line 17"); | |
| 462 | 472 | |
| 463 | 473 | // Test logging of header 3 |
| 464 | 474 | fgets(line, 100, fp); |
| 465 | - test_value(line, "\n", "Check log file line 16"); | |
| 475 | + test_value(line, "\n", "Check log file line 18"); | |
| 466 | 476 | fgets(line, 100, fp); |
| 467 | - test_value(line, "=== Header 3 ===\n", "Check log file line 17"); | |
| 477 | + test_value(line, "=== Header 3 ===\n", "Check log file line 19"); | |
| 468 | 478 | |
| 469 | 479 | // Test logging of parameters |
| 470 | 480 | fgets(line, 100, fp); |
| 471 | - test_value(line, "+============+\n", "Check log file line 18"); | |
| 481 | + test_value(line, "+============+\n", "Check log file line 20"); | |
| 472 | 482 | fgets(line, 100, fp); |
| 473 | - test_value(line, "| Parameters |\n", "Check log file line 19"); | |
| 483 | + test_value(line, "| Parameters |\n", "Check log file line 21"); | |
| 474 | 484 | fgets(line, 100, fp); |
| 475 | - test_value(line, "+============+\n", "Check log file line 20"); | |
| 485 | + test_value(line, "+============+\n", "Check log file line 22"); | |
| 476 | 486 | fgets(line, 100, fp); |
| 477 | 487 | test_value(line, " chatter ...................: 2\n", |
| 478 | - "Check log file line 21"); | |
| 488 | + "Check log file line 23"); | |
| 479 | 489 | fgets(line, 100, fp); |
| 480 | 490 | test_value(line, " clobber ...................: yes\n", |
| 481 | - "Check log file line 22"); | |
| 491 | + "Check log file line 24"); | |
| 482 | 492 | fgets(line, 100, fp); |
| 483 | 493 | test_value(line, " debug .....................: no\n", |
| 484 | - "Check log file line 23"); | |
| 494 | + "Check log file line 25"); | |
| 485 | 495 | fgets(line, 100, fp); |
| 486 | 496 | test_value(line, " mode ......................: ql\n", |
| 487 | - "Check log file line 24"); | |
| 497 | + "Check log file line 26"); | |
| 488 | 498 | fgets(line, 100, fp); |
| 489 | 499 | test_value(line, " logfile ...................: test_logfile.log\n", |
| 490 | - "Check log file line 25"); | |
| 500 | + "Check log file line 27"); | |
| 491 | 501 | fgets(line, 32, fp); |
| 492 | 502 | test_value(line, "Application \"test_GApplication\"", |
| 493 | - "Check log file line 26"); | |
| 503 | + "Check log file line 28"); | |
| 494 | 504 | // Empty remaining characters (not checked since they are machine |
| 495 | 505 | // dependent) |
| 496 | 506 | fgets(line, 200, fp); |
| 497 | 507 | fgets(line, 32, fp); |
| 498 | 508 | test_value(line, "Application \"test_GApplication\"", |
| 499 | - "Check log file line 27"); | |
| 509 | + "Check log file line 29"); | |
| 500 | 510 | |
| 501 | 511 | // Close log file |
| 502 | 512 | fclose(fp); | ... | ... |