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,7 +54,9 @@ The GApplication::log_parameters() method now takes a mandatory GChatter | ||
| 54 | argument to enable displaying of parameters only above a given chattiness | 54 | argument to enable displaying of parameters only above a given chattiness |
| 55 | level. This avoids a log-level check in the client code. For the same | 55 | level. This avoids a log-level check in the client code. For the same |
| 56 | purpose, the GApplication methods log_string(), log_value(), log_header1(), | 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 | 5. Base module | 62 | 5. Base module |
include/GApplication.hpp
| @@ -88,6 +88,12 @@ public: | @@ -88,6 +88,12 @@ public: | ||
| 88 | void log_value(const GChatter& chatter, | 88 | void log_value(const GChatter& chatter, |
| 89 | const std::string& name, | 89 | const std::string& name, |
| 90 | const std::string& value); | 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 | void log_header1(const GChatter& chatter, | 97 | void log_header1(const GChatter& chatter, |
| 92 | const std::string& header); | 98 | const std::string& header); |
| 93 | void log_header2(const GChatter& chatter, | 99 | void log_header2(const GChatter& chatter, |
pyext/GApplication.i
| @@ -110,6 +110,12 @@ public: | @@ -110,6 +110,12 @@ public: | ||
| 110 | void log_value(const GChatter& chatter, | 110 | void log_value(const GChatter& chatter, |
| 111 | const std::string& name, | 111 | const std::string& name, |
| 112 | const std::string& value); | 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 | void log_header1(const GChatter& chatter, | 119 | void log_header1(const GChatter& chatter, |
| 114 | const std::string& header); | 120 | const std::string& header); |
| 115 | void log_header2(const GChatter& chatter, | 121 | void log_header2(const GChatter& chatter, |
src/app/GApplication.cpp
| @@ -552,6 +552,64 @@ void GApplication::log_value(const GChatter& chatter, | @@ -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 | * @brief Write header 1 in log file | 613 | * @brief Write header 1 in log file |
| 556 | * | 614 | * |
| 557 | * @param[in] chatter Minimum required chattiness | 615 | * @param[in] chatter Minimum required chattiness |
test/test_GApplication.cpp
| @@ -388,8 +388,12 @@ void TestGApplication::test_GApplication(void) | @@ -388,8 +388,12 @@ void TestGApplication::test_GApplication(void) | ||
| 388 | app1.log_string(EXPLICIT, "Explicit"); | 388 | app1.log_string(EXPLICIT, "Explicit"); |
| 389 | app1.log_string(VERBOSE, "Verbose"); | 389 | app1.log_string(VERBOSE, "Verbose"); |
| 390 | app1.log_string(TERSE, "\n"); | 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 | app1.log_header1(NORMAL, "Header 1"); | 397 | app1.log_header1(NORMAL, "Header 1"); |
| 394 | app1.log_header2(NORMAL, "Header 2"); | 398 | app1.log_header2(NORMAL, "Header 2"); |
| 395 | app1.log_header3(NORMAL, "Header 3"); | 399 | app1.log_header3(NORMAL, "Header 3"); |
| @@ -437,66 +441,72 @@ void TestGApplication::test_GApplication(void) | @@ -437,66 +441,72 @@ void TestGApplication::test_GApplication(void) | ||
| 437 | 441 | ||
| 438 | // Test logging of user parameter | 442 | // Test logging of user parameter |
| 439 | fgets(line, 100, fp); | 443 | fgets(line, 100, fp); |
| 440 | - test_value(line, " User parameter ............: 3.14\n", | 444 | + test_value(line, " String parameter ..........: 3.14\n", |
| 441 | "Check log file line 7"); | 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 | // Test logging of header 1 | 453 | // Test logging of header 1 |
| 444 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | // Test logging of header 2 | 463 | // Test logging of header 2 |
| 454 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | // Test logging of header 3 | 473 | // Test logging of header 3 |
| 464 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | // Test logging of parameters | 479 | // Test logging of parameters |
| 470 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 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 | fgets(line, 100, fp); | 486 | fgets(line, 100, fp); |
| 477 | test_value(line, " chatter ...................: 2\n", | 487 | test_value(line, " chatter ...................: 2\n", |
| 478 | - "Check log file line 21"); | 488 | + "Check log file line 23"); |
| 479 | fgets(line, 100, fp); | 489 | fgets(line, 100, fp); |
| 480 | test_value(line, " clobber ...................: yes\n", | 490 | test_value(line, " clobber ...................: yes\n", |
| 481 | - "Check log file line 22"); | 491 | + "Check log file line 24"); |
| 482 | fgets(line, 100, fp); | 492 | fgets(line, 100, fp); |
| 483 | test_value(line, " debug .....................: no\n", | 493 | test_value(line, " debug .....................: no\n", |
| 484 | - "Check log file line 23"); | 494 | + "Check log file line 25"); |
| 485 | fgets(line, 100, fp); | 495 | fgets(line, 100, fp); |
| 486 | test_value(line, " mode ......................: ql\n", | 496 | test_value(line, " mode ......................: ql\n", |
| 487 | - "Check log file line 24"); | 497 | + "Check log file line 26"); |
| 488 | fgets(line, 100, fp); | 498 | fgets(line, 100, fp); |
| 489 | test_value(line, " logfile ...................: test_logfile.log\n", | 499 | test_value(line, " logfile ...................: test_logfile.log\n", |
| 490 | - "Check log file line 25"); | 500 | + "Check log file line 27"); |
| 491 | fgets(line, 32, fp); | 501 | fgets(line, 32, fp); |
| 492 | test_value(line, "Application \"test_GApplication\"", | 502 | test_value(line, "Application \"test_GApplication\"", |
| 493 | - "Check log file line 26"); | 503 | + "Check log file line 28"); |
| 494 | // Empty remaining characters (not checked since they are machine | 504 | // Empty remaining characters (not checked since they are machine |
| 495 | // dependent) | 505 | // dependent) |
| 496 | fgets(line, 200, fp); | 506 | fgets(line, 200, fp); |
| 497 | fgets(line, 32, fp); | 507 | fgets(line, 32, fp); |
| 498 | test_value(line, "Application \"test_GApplication\"", | 508 | test_value(line, "Application \"test_GApplication\"", |
| 499 | - "Check log file line 27"); | 509 | + "Check log file line 29"); |
| 500 | 510 | ||
| 501 | // Close log file | 511 | // Close log file |
| 502 | fclose(fp); | 512 | fclose(fp); |