Commit 9c93b03ca6efdd3c51d937bd219d082ee0e27ce4

Authored by Jürgen Knödlseder
1 parent 9842940d

Add optional unit parameter to GApplication::log_value() methods (#4202)

ChangeLog
1   -2023-01-13
  1 +2023-01-19
2 2  
3 3 * Version 2.1.0 released
4 4 ========================
5 5  
  6 + Add optional unit parameter to GApplication::log_value() methods (#4202)
6 7 Fix segfault on saving empty GModelSpectralTable (#4198)
7 8 Implement response vector cache storage in FITS file (#4159)
8 9 Add GCOMObservation::npred() method
... ...
1 1 New Features and Important Changes in GammaLib 2.1.0
2 2  
3   -13 January 2023
  3 +19 January 2023
4 4  
5 5  
6 6 1. Introduction
... ... @@ -40,7 +40,7 @@ The following methods have been renamed:
40 40 - none
41 41  
42 42 The arguments for the following methods have been changed:
43   -- none
  43 +- GApplication::log_value() methods now have optional unit parameters
44 44  
45 45 The return value of the following methods has been changed:
46 46 - none
... ... @@ -58,7 +58,9 @@ None
58 58  
59 59 5. Application module
60 60 ---------------------
61   -None
  61 +Add optional "unit" parameter was added to the GApplication::log_value()
  62 +methods, and if this parameter is not a zero length string a unit will
  63 +be appended to the output (#4202).
62 64  
63 65  
64 66 6. Base module
... ...
README.md
1 1 GammaLib information
2 2 ====================
3   -* Version: 2.1.0.dev (13 January 2023)
  3 +* Version: 2.1.0.dev (19 January 2023)
4 4  
5 5 [![Build Status](https://cta-jenkins.irap.omp.eu/buildStatus/icon?job=gammalib-integrate-os)](https://cta-jenkins.irap.omp.eu/job/gammalib-integrate-os/)
6 6  
... ...
doc/source/admin/release_history/2.1.rst
... ... @@ -23,6 +23,8 @@ Bug fixes
23 23 Improvements
24 24 ------------
25 25  
  26 +* [`4202 <https://cta-redmine.irap.omp.eu/issues/4202>`_] -
  27 + Add optional unit parameter to ``GApplication::log_value()`` methods
26 28 * [`4159 <https://cta-redmine.irap.omp.eu/issues/4159>`_] -
27 29 Implement response vector cache storage in FITS file
28 30 * Add ``GCOMObservation::npred()`` method
... ...
include/GApplication.hpp
1 1 /***************************************************************************
2 2 * GApplication.hpp - GammaLib application base class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2022 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2023 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -103,13 +103,16 @@ public:
103 103 const bool& linefeed = true);
104 104 void log_value(const GChatter& chatter,
105 105 const std::string& name,
106   - const std::string& value);
  106 + const std::string& value,
  107 + const std::string& unit = "");
107 108 void log_value(const GChatter& chatter,
108 109 const std::string& name,
109   - const int& value);
  110 + const int& value,
  111 + const std::string& unit = "");
110 112 void log_value(const GChatter& chatter,
111 113 const std::string& name,
112   - const double& value);
  114 + const double& value,
  115 + const std::string& unit = "");
113 116 void log_header1(const GChatter& chatter,
114 117 const std::string& header);
115 118 void log_header2(const GChatter& chatter,
... ...
pyext/GApplication.i
1 1 /***************************************************************************
2 2 * GApplication.i - GammaLib application base class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2022 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2023 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -133,8 +133,11 @@ public:
133 133 ***************************************************************************/
134 134 %pythoncode %{
135 135 # Log the value of a parameter
136   -def _log_value(self, chatter, name, value):
137   - string = gammalib.parformat(str(name))+str(value)
  136 +def _log_value(self, chatter, name, value, *unit):
  137 + if len(unit) > 0:
  138 + string = gammalib.parformat(str(name))+str(value)+' '+str(unit[0])
  139 + else:
  140 + string = gammalib.parformat(str(name))+str(value)
138 141 self._log_string(chatter, string)
139 142 GApplication._log_value = _log_value
140 143 %}
... ...
src/app/GApplication.cpp
1 1 /***************************************************************************
2 2 * GApplication.cpp - GammaLib application base class *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2022 by Juergen Knoedlseder *
  4 + * copyright (C) 2010-2023 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -885,13 +885,15 @@ void GApplication::log_string(const GChatter&amp; chatter,
885 885 * @param[in] chatter Minimum required chattiness
886 886 * @param[in] name Parameter name string
887 887 * @param[in] value Value string
  888 + * @param[in] unit Value unit string
888 889 *
889 890 * Writes a parameter value into the log file if chattiness is at least
890 891 * @p chatter.
891 892 ***************************************************************************/
892 893 void GApplication::log_value(const GChatter& chatter,
893 894 const std::string& name,
894   - const std::string& value)
  895 + const std::string& value,
  896 + const std::string& unit)
895 897 {
896 898 // Get chattiness of application
897 899 GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
... ... @@ -900,7 +902,11 @@ void GApplication::log_value(const GChatter&amp; chatter,
900 902 // required chattiness
901 903 if (chattiness >= chatter) {
902 904 log << gammalib::parformat(name);
903   - log << value << std::endl;
  905 + log << value;
  906 + if (!unit.empty()) {
  907 + log << " " << unit;
  908 + }
  909 + log << std::endl;
904 910 }
905 911  
906 912 // Return
... ... @@ -914,13 +920,15 @@ void GApplication::log_value(const GChatter&amp; chatter,
914 920 * @param[in] chatter Minimum required chattiness
915 921 * @param[in] name Parameter name string
916 922 * @param[in] value Integer value
  923 + * @param[in] unit Value unit string
917 924 *
918 925 * Writes a parameter value into the log file if chattiness is at least
919 926 * @p chatter.
920 927 ***************************************************************************/
921 928 void GApplication::log_value(const GChatter& chatter,
922 929 const std::string& name,
923   - const int& value)
  930 + const int& value,
  931 + const std::string& unit)
924 932 {
925 933 // Get chattiness of application
926 934 GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
... ... @@ -929,7 +937,11 @@ void GApplication::log_value(const GChatter&amp; chatter,
929 937 // required chattiness
930 938 if (chattiness >= chatter) {
931 939 log << gammalib::parformat(name);
932   - log << value << std::endl;
  940 + log << value;
  941 + if (!unit.empty()) {
  942 + log << " " << unit;
  943 + }
  944 + log << std::endl;
933 945 }
934 946  
935 947 // Return
... ... @@ -943,13 +955,15 @@ void GApplication::log_value(const GChatter&amp; chatter,
943 955 * @param[in] chatter Minimum required chattiness
944 956 * @param[in] name Parameter name string
945 957 * @param[in] value Floating point value
  958 + * @param[in] unit Value unit string
946 959 *
947 960 * Writes a parameter value into the log file if chattiness is at least
948 961 * @p chatter.
949 962 ***************************************************************************/
950 963 void GApplication::log_value(const GChatter& chatter,
951 964 const std::string& name,
952   - const double& value)
  965 + const double& value,
  966 + const std::string& unit)
953 967 {
954 968 // Get chattiness of application
955 969 GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
... ... @@ -958,7 +972,11 @@ void GApplication::log_value(const GChatter&amp; chatter,
958 972 // required chattiness
959 973 if (chattiness >= chatter) {
960 974 log << gammalib::parformat(name);
961   - log << value << std::endl;
  975 + log << value;
  976 + if (!unit.empty()) {
  977 + log << " " << unit;
  978 + }
  979 + log << std::endl;
962 980 }
963 981  
964 982 // Return
... ...
test/test_GApplication.cpp
1 1 /***************************************************************************
2 2 * test_GApplication.cpp - test GApplication classes *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2012-2022 by Juergen Knoedlseder *
  4 + * copyright (C) 2012-2023 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -538,6 +538,9 @@ void TestGApplication::test_GApplication(void)
538 538 app1.log_value(NORMAL, "String parameter", "3.14");
539 539 app1.log_value(NORMAL, "Floating parameter", 3.14);
540 540 app1.log_value(NORMAL, "Integer parameter", 3);
  541 + app1.log_value(NORMAL, "String parameter", "3.14", "pi");
  542 + app1.log_value(NORMAL, "Floating parameter", 3.14, "keV");
  543 + app1.log_value(NORMAL, "Integer parameter", 3, "units");
541 544 app1.log_value(VERBOSE, "Verbose string parameter", "3.14!!");
542 545 app1.log_value(VERBOSE, "Verbose floating parameter", 99.9);
543 546 app1.log_value(VERBOSE, "Verbose integer parameter", -1);
... ... @@ -598,77 +601,88 @@ void TestGApplication::test_GApplication(void)
598 601 test_value(line, " Integer parameter .........: 3\n",
599 602 "Check log file line 9");
600 603  
  604 + // Test logging of user parameter with units
  605 + fgets(line, 100, fp);
  606 + test_value(line, " String parameter ..........: 3.14 pi\n",
  607 + "Check log file line 10");
  608 + fgets(line, 100, fp);
  609 + test_value(line, " Floating parameter ........: 3.14 keV\n",
  610 + "Check log file line 11");
  611 + fgets(line, 100, fp);
  612 + test_value(line, " Integer parameter .........: 3 units\n",
  613 + "Check log file line 12");
  614 +
601 615 // Test logging of header 1
602 616 fgets(line, 100, fp);
603   - test_value(line, "\n", "Check log file line 10");
  617 + test_value(line, "\n", "Check log file line 13");
604 618 fgets(line, 100, fp);
605   - test_value(line, "+==========+\n", "Check log file line 11");
  619 + test_value(line, "+==========+\n", "Check log file line 14");
606 620 fgets(line, 100, fp);
607   - test_value(line, "| Header 1 |\n", "Check log file line 12");
  621 + test_value(line, "| Header 1 |\n", "Check log file line 15");
608 622 fgets(line, 100, fp);
609   - test_value(line, "+==========+\n", "Check log file line 13");
  623 + test_value(line, "+==========+\n", "Check log file line 16");
610 624  
611 625 // Test logging of header 2
612 626 fgets(line, 100, fp);
613   - test_value(line, "+----------+\n", "Check log file line 14");
  627 + test_value(line, "+----------+\n", "Check log file line 17");
614 628 fgets(line, 100, fp);
615   - test_value(line, "| Header 2 |\n", "Check log file line 15");
  629 + test_value(line, "| Header 2 |\n", "Check log file line 18");
616 630 fgets(line, 100, fp);
617   - test_value(line, "+----------+\n", "Check log file line 16");
  631 + test_value(line, "+----------+\n", "Check log file line 19");
618 632  
619 633 // Test logging of header 3
620 634 fgets(line, 100, fp);
621   - test_value(line, "=== Header 3 ===\n", "Check log file line 17");
  635 + test_value(line, "=== Header 3 ===\n", "Check log file line 20");
622 636  
623 637 // Test logging of parameters
624 638 fgets(line, 100, fp);
625   - test_value(line, "+============+\n", "Check log file line 18");
  639 + test_value(line, "+============+\n", "Check log file line 21");
626 640 fgets(line, 100, fp);
627   - test_value(line, "| Parameters |\n", "Check log file line 19");
  641 + test_value(line, "| Parameters |\n", "Check log file line 22");
628 642 fgets(line, 100, fp);
629   - test_value(line, "+============+\n", "Check log file line 20");
  643 + test_value(line, "+============+\n", "Check log file line 23");
630 644 fgets(line, 100, fp);
631 645 test_value(line, " real ......................: 83.63\n",
632   - "Check log file line 21");
  646 + "Check log file line 24");
633 647 fgets(line, 100, fp);
634 648 test_value(line, " integer ...................: 1\n",
635   - "Check log file line 22");
  649 + "Check log file line 25");
636 650 fgets(line, 100, fp);
637 651 test_value(line, " string ....................: CEL\n",
638   - "Check log file line 23");
  652 + "Check log file line 26");
639 653 fgets(line, 100, fp);
640 654 test_value(line, " filename ..................: file.fits\n",
641   - "Check log file line 24");
  655 + "Check log file line 27");
642 656 fgets(line, 100, fp);
643 657 test_value(line, " time ......................: 2005-10-08T14:30:25\n",
644   - "Check log file line 25");
  658 + "Check log file line 28");
645 659 fgets(line, 100, fp);
646 660 test_value(line, " chatter ...................: 2\n",
647   - "Check log file line 26");
  661 + "Check log file line 29");
648 662 fgets(line, 100, fp);
649 663 test_value(line, " clobber ...................: yes\n",
650   - "Check log file line 27");
  664 + "Check log file line 30");
651 665 fgets(line, 100, fp);
652 666 test_value(line, " debug .....................: no\n",
653   - "Check log file line 28");
  667 + "Check log file line 31");
654 668 fgets(line, 100, fp);
655 669 test_value(line, " mode ......................: ql\n",
656   - "Check log file line 29");
  670 + "Check log file line 32");
657 671 fgets(line, 100, fp);
658 672 test_value(line, " logfile ...................: test_application.log\n",
659   - "Check log file line 30");
  673 + "Check log file line 33");
660 674 fgets(line, 32, fp);
661 675 test_value(line, "Application \"test_GApplication\"",
662   - "Check log file line 31");
  676 + "Check log file line 34");
663 677  
664 678 // Empty remaining characters (not checked since they are machine
665 679 // dependent)
666 680 fgets(line, 200, fp);
667 681 fgets(line, 100, fp);
668   - test_value(line, "\n", "Check log file line 32");
  682 + test_value(line, "\n", "Check log file line 35");
669 683 fgets(line, 32, fp);
670 684 test_value(line, "Application \"test_GApplication\"",
671   - "Check log file line 33");
  685 + "Check log file line 36");
672 686  
673 687 // Close log file
674 688 fclose(fp);
... ...
test/test_GApplication.py
1 1 # ==========================================================================
2 2 # This module performs unit tests for the GammaLib application module
3 3 #
4   -# Copyright (C) 2012-2022 Juergen Knoedlseder
  4 +# Copyright (C) 2012-2023 Juergen Knoedlseder
5 5 #
6 6 # This program is free software: you can redistribute it and/or modify
7 7 # it under the terms of the GNU General Public License as published by
... ... @@ -350,6 +350,30 @@ class Test(gammalib.GPythonTestSuite):
350 350 self.test_assert(app['real'].is_notanumber(), 'NAN real parameter')
351 351 self.test_assert(app['integer'].is_valid(), 'NAN integer parameter')
352 352  
  353 + # Check value logging
  354 + app.logFileOpen()
  355 + app._log_value(gammalib.NORMAL, 'String parameter', '3.14');
  356 + app._log_value(gammalib.NORMAL, 'Floating parameter', 3.14);
  357 + app._log_value(gammalib.NORMAL, 'Integer parameter', 3);
  358 + app._log_value(gammalib.NORMAL, 'String parameter', '3.14', 'pi');
  359 + app._log_value(gammalib.NORMAL, 'Floating parameter', 3.14, 'keV');
  360 + app._log_value(gammalib.NORMAL, 'Integer parameter', 3, 'units')
  361 + app.logFileClose()
  362 + fp = open('test_application.log', 'r')
  363 + line = fp.readlines()
  364 + self.test_value(len(line), 13, 'Check number of lines in log file')
  365 + self.test_value(line[0], '********************************************************************************\n','Line 1')
  366 + self.test_value(line[1], '* test_GApplication *\n','Line 2')
  367 + self.test_value(line[2], '* ---------------------------------------------------------------------------- *\n','Line 3')
  368 + self.test_value(line[4], '********************************************************************************\n','Line 5')
  369 + self.test_value(line[5], ' String parameter ..........: 3.14\n','Line 6')
  370 + self.test_value(line[6], ' Floating parameter ........: 3.14\n','Line 7')
  371 + self.test_value(line[7], ' Integer parameter .........: 3\n','Line 8')
  372 + self.test_value(line[8], ' String parameter ..........: 3.14 pi\n','Line 9')
  373 + self.test_value(line[9], ' Floating parameter ........: 3.14 keV\n','Line 10')
  374 + self.test_value(line[10], ' Integer parameter .........: 3 units\n','Line 11')
  375 + fp.close()
  376 +
353 377 # Return
354 378 return
355 379  
... ...