GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GApplication.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GApplication.cpp - GammaLib application base class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2018 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GApplication.cpp
23  * @brief GammaLib application base class
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GApplication.hpp"
32 #include "GTools.hpp"
33 
34 /* __ Method name definitions ____________________________________________ */
35 
36 /* __ Macros _____________________________________________________________ */
37 
38 /* __ Coding definitions _________________________________________________ */
39 const int header_width = 80; //!< Header width
40 
41 /* __ Debug definitions __________________________________________________ */
42 
43 
44 /*==========================================================================
45  = =
46  = Constructors/destructors =
47  = =
48  ==========================================================================*/
49 
50 /***********************************************************************//**
51  * @brief Void constructor
52  *
53  * Constructs an empty application.
54  ***************************************************************************/
56 {
57  // Initialise members
58  init_members();
59 
60  // Return
61  return;
62 }
63 
64 
65 /***********************************************************************//**
66  * @brief Application constructor
67  *
68  * @param[in] name Application name.
69  * @param[in] version Application version.
70  *
71  * Constructs an application from an application @p name and @p version. The
72  * constructor will set the parameter filename to "<name>.par" and the log
73  * filename to "<name>".log. The parameters will be loaded from the parameter
74  * file.
75  *
76  * No log file will be opened. To open the log file an explicit call to the
77  * logFileOpen() method is required.
78  *
79  * This constructor should be used for Python scripts.
80  ***************************************************************************/
81 GApplication::GApplication(const std::string& name, const std::string& version)
82 {
83  // Initialise members
84  init_members();
85 
86  // Set application name and version
87  m_name = name;
89 
90  // Set default parfile and logfile name
91  m_parfile = name+".par";
92  m_logfile = name+".log";
93 
94  // Load application parameters
96 
97  // Signal that application parameters have been loaded
98  m_pars_loaded = true;
99 
100  // Set log filename and chattiness
102  set_log_chatter();
103 
104  // Return
105  return;
106 }
107 
108 
109 /***********************************************************************//**
110  * @brief Application constructor
111  *
112  * @param[in] name Application name.
113  * @param[in] version Application version.
114  * @param[in] pars Application parameters.
115  *
116  * Constructs an application from an application @p name and @p version. The
117  * constructor will set the parameter filename to "<name>.par" and the log
118  * filename to "<name>".log. The parameters will be loaded from the parameter
119  * file.
120  *
121  * No log file will be opened. To open the log file an explicit call to the
122  * logFileOpen() method is required.
123  *
124  * This constructor should be used for Python scripts.
125  ***************************************************************************/
126 GApplication::GApplication(const std::string& name,
127  const std::string& version,
128  const GApplicationPars& pars)
129 {
130  // Initialise members
131  init_members();
132 
133  // Set application name and version
134  m_name = name;
135  m_version = version;
136 
137  // Set default parfile and logfile name
138  m_parfile = name+".par";
139  m_logfile = name+".log";
140 
141  // Set application parameters
142  m_pars = pars;
143 
144  // Signal that application parameters have been loaded
145  m_pars_loaded = true;
146 
147  // Set log filename and chattiness
149  set_log_chatter();
150 
151  // Return
152  return;
153 }
154 
155 
156 /***********************************************************************//**
157  * @brief Application constructor
158  *
159  * @param[in] name Application name.
160  * @param[in] version Application version.
161  * @param[in] argc Number of command line arguments
162  * @param[in] argv Command line arguments
163  *
164  * Constructs an application from an application @p name, @p version and a
165  * number @p argc of command line arguments @p argv.
166  *
167  * This constructor should be used for C++ applications.
168  ***************************************************************************/
169 GApplication::GApplication(const std::string& name,
170  const std::string& version,
171  int argc,
172  char* argv[])
173 {
174  // Initialise members
175  init_members();
176 
177  // Set application name and version
178  m_name = name;
179  m_version = version;
180 
181  // Set default parfile and logfile name
182  m_parfile = name+".par";
183  m_logfile = name+".log";
184 
185  // Put arguments in vector string
186  m_args.clear();
187  for (int i = 0; i < argc; ++i) {
188  m_args.push_back(gammalib::strip_whitespace(argv[i]));
189  }
190 
191  // Catch --help option before doing anything else. The actual action
192  // needs to be done by the client, but we skip the loading of the
193  // parameter file and the opening of the logger if the help option
194  // was specified.
195  m_need_help = false;
196  if (m_args.size() >= 2) {
197  if (m_args[1] == "--help") {
198  m_need_help = true;
199  }
200  }
201 
202  // If no --help option has been specified then proceed with loading
203  // the parameter file and opening the logger
204  if (!m_need_help) {
205 
206  // Initialise application parameters
208 
209  // Signal that application parameters have been loaded
210  m_pars_loaded = true;
211 
212  // Set log filename and chattiness
214  set_log_chatter();
215 
216  } // endif: no --help option specified
217 
218  // Return
219  return;
220 }
221 
222 
223 /***********************************************************************//**
224  * @brief Copy constructor
225  *
226  * @param[in] app Application.
227  ***************************************************************************/
229 {
230  // Initialise members
231  init_members();
232 
233  // Copy members
234  copy_members(app);
235 
236  // Return
237  return;
238 }
239 
240 
241 /***********************************************************************//**
242  * @brief Destructor
243  ***************************************************************************/
245 {
246  // Free members
247  free_members();
248 
249  // Return
250  return;
251 }
252 
253 
254 /*==========================================================================
255  = =
256  = Operators =
257  = =
258  ==========================================================================*/
259 
260 /***********************************************************************//**
261  * @brief Assignment operator
262  *
263  * @param[in] app Application.
264  * @return Application.
265  ***************************************************************************/
267 {
268  // Execute only if object is not identical
269  if (this != &app) {
270 
271  // Free members
272  free_members();
273 
274  // Initialise private members for clean destruction
275  init_members();
276 
277  // Copy members
278  copy_members(app);
279 
280  } // endif: object was not identical
281 
282  // Return
283  return *this;
284 }
285 
286 
287 /*==========================================================================
288  = =
289  = Public methods =
290  = =
291  ==========================================================================*/
292 
293 /***********************************************************************//**
294  * @brief Clear application
295  ***************************************************************************/
297 {
298  // Save application name and version as we need them to reconstruct
299  // the application after freeing and initialising its members
300  std::string name = m_name;
301  std::string version = m_version;
302 
303  // Free members
304  free_members();
305 
306  // Initialise members
307  init_members();
308 
309  // Recover saved application name and version
310  m_name = name;
311  m_version = version;
312 
313  // Set default parfile and logfile name
314  m_parfile = name+".par";
315  m_logfile = name+".log";
316 
317  // Initialise application parameters
319 
320  // Signal that application parameters have been loaded
321  m_pars_loaded = true;
322 
323  // Set log filename and chattiness
325  set_log_chatter();
326 
327  // Return
328  return;
329 }
330 
331 
332 /***********************************************************************//**
333  * @brief Clone application
334  *
335  * @return Pointer to deep copy of application.
336  ***************************************************************************/
338 {
339  // Clone application
340  return new GApplication(*this);
341 }
342 
343 
344 /***********************************************************************//**
345  * @brief Return application elapsed time in calendar seconds
346  *
347  * @return Application elapsed time in calendar seconds.
348  ***************************************************************************/
349 double GApplication::telapse(void) const
350 {
351  // Get actual time
352  std::time_t acttime;
353  std::time(&acttime);
354 
355  // Compute elapsed time
356  double telapse = difftime(acttime, m_tstart);
357 
358  // Return elapsed time
359  return telapse;
360 }
361 
362 
363 /***********************************************************************//**
364  * @brief Return application elapsed time in CPU seconds
365  *
366  * @return Application elapsed time in CPU seconds.
367  ***************************************************************************/
368 double GApplication::celapse(void) const
369 {
370  // Compute elapsed CPU clock time
371  double celapse = ((double) (clock() - m_cstart)) / CLOCKS_PER_SEC;
372 
373  // Return elapsed time
374  return celapse;
375 }
376 
377 
378 /***********************************************************************//**
379  * @brief Open log file
380  *
381  * @param[in] clobber Overwrite (true) or append (false) to existing file
382  * (defaults to true)
383  *
384  * Opens application log file and sets the logger chattiness dependent on
385  * the chatter parameter of the application.
386  ***************************************************************************/
387 void GApplication::logFileOpen(const bool& clobber)
388 {
389  // Open logger only if filename is non-empty
390  if (!log_filename().empty()) {
391 
392  // Initialise the application logger
393  log.open(log_filename(), clobber);
394 
395  // Write header into log file
396  log_header();
397 
398  } // endif: file name was not empty
399 
400  // Set logger chattiness
401  set_log_chatter();
402 
403  // Return
404  return;
405 }
406 
407 
408 /***********************************************************************//**
409  * @brief Close log file
410  *
411  * Close the log file. In case that some characters have been written
412  * through the logger a trailer will be appended to the logger before
413  * closing the log file. The trailer informs about the computation time
414  * used by the application.
415  ***************************************************************************/
417 {
418  // Continue only if log file is open and if something has been written
419  // through this logger. This avoid writing trailers for logger than
420  // have not been used.
421  if (log.is_open() && (log.written_size() > 0)) {
422 
423  // Write line feed before trailer into logger
424  log << std::endl;
425 
426  // Write trailer into logger
427  log_trailer();
428 
429  // Close log file
430  log.close();
431 
432  } // endif: log file was open
433 
434  // Return
435  return;
436 }
437 
438 
439 /***********************************************************************//**
440  * @brief Signal terse logging
441  *
442  * @return True if at least terse logging is requested.
443  *
444  * The terse level is used for the most crucial application information that
445  * should be logged in all cases (chatter >= 1).
446  ***************************************************************************/
447 bool GApplication::logTerse(void) const
448 {
449  // Get chatter level (circumvent const correctness)
450  int chatter = const_cast<GApplicationPar*>(&m_pars["chatter"])->integer();
451 
452  // Return terse logging condition
453  return (chatter > 0);
454 }
455 
456 
457 /***********************************************************************//**
458  * @brief Signal normal logging
459  *
460  * @return True if at least normal logging is requested.
461  *
462  * The normal level is used for standard application information that should
463  * be logged in normal operations (chatter >= 2).
464  ***************************************************************************/
465 bool GApplication::logNormal(void) const
466 {
467  // Get chatter level (circumvent const correctness)
468  int chatter = const_cast<GApplicationPar*>(&m_pars["chatter"])->integer();
469 
470  // Return normal logging condition
471  return (chatter > 1);
472 }
473 
474 
475 /***********************************************************************//**
476  * @brief Signal explicit logging
477  *
478  * @return True if at least explicit logging is requested.
479  *
480  * The explicit level is used for detailed application information that
481  * should be logged in detailed studies (chatter >= 3).
482  ***************************************************************************/
484 {
485  // Get chatter level (circumvent const correctness)
486  int chatter = const_cast<GApplicationPar*>(&m_pars["chatter"])->integer();
487 
488  // Return explicit logging condition
489  return (chatter > 2);
490 }
491 
492 
493 /***********************************************************************//**
494  * @brief Signal verbose logging
495  *
496  * @return True if verbose logging is requested.
497  *
498  * The verbose level is used for full application information (chatter >= 4).
499  ***************************************************************************/
500 bool GApplication::logVerbose(void) const
501 {
502  // Get chatter level (circumvent const correctness)
503  int chatter = const_cast<GApplicationPar*>(&m_pars["chatter"])->integer();
504 
505  // Return verbose logging condition
506  return (chatter > 3);
507 }
508 
509 
510 /***********************************************************************//**
511  * @brief Signal debug logging
512  *
513  * @return True if the debugging mode has been selected.
514  *
515  * The debug level is used for application debugging.
516  ***************************************************************************/
517 bool GApplication::logDebug(void) const
518 {
519  // Get debug condition (circumvent const correctness)
520  bool debug = const_cast<GApplicationPar*>(&m_pars["debug"])->boolean();
521 
522  // Return debug condition
523  return (debug);
524 }
525 
526 
527 /***********************************************************************//**
528  * @brief Return clobber
529  *
530  * @return True if the clobber parameter is true.
531  *
532  * The clobber indicates if existing files should be overwritten.
533  ***************************************************************************/
534 bool GApplication::clobber(void) const
535 {
536  // Get clobber condition (circumvent const correctness)
537  bool clobber = const_cast<GApplicationPar*>(&m_pars["clobber"])->boolean();
538 
539  // Return clobber condition
540  return (clobber);
541 }
542 
543 
544 /***********************************************************************//**
545  * @brief Write application header in log file
546  *
547  * Dump the application header into the log file. The header is composed of
548  * a fixed width block delimined by "*" characters that contains information
549  * about the application name and version.
550  ***************************************************************************/
552 {
553  // Reset any indentation
554  log.indent(0);
555 
556  // Dump header
557  log << gammalib::fill("*", header_width) << std::endl;
558  log << "*" << gammalib::centre(m_name, header_width-2) << "*" << std::endl;
559  log << "* " << gammalib::fill("-", header_width-4) << " *" << std::endl;
560  log << "* Version: " << gammalib::left(m_version, header_width-12) << "*" << std::endl;
561  log << gammalib::fill("*", header_width) << std::endl;
562 
563  // Return
564  return;
565 }
566 
567 
568 /***********************************************************************//**
569  * @brief Write application trailer in log file
570  *
571  * The application trailer gives the total number of elapsed calendar and
572  * CPU seconds.
573  ***************************************************************************/
575 {
576  // Reset any indentation
577  log.indent(0);
578 
579  // Dump trailer
580  log << "Application \"" << m_name << "\" terminated after ";
581  log << telapse() << " wall clock seconds, consuming ";
582  log << celapse() << " seconds of CPU time.";
583  if (telapse() < 0.1) {
584  log << " This was rather quick!";
585  }
586  if (telapse() > 86400.0) {
587  log << " Hope it was worth waiting ...";
588  }
589  log << std::endl;
590 
591  // Return
592  return;
593 }
594 
595 
596 /***********************************************************************//**
597  * @brief Write string in log file
598  *
599  * @param[in] chatter Minimum required chattiness
600  * @param[in] string String
601  * @param[in] linefeed Terminate string with linefeed?
602  *
603  * Writes a string into the log file if chattiness is at least @p chatter.
604  * If @p linefeed is true the string is terminated with a linefeed.
605  ***************************************************************************/
606 void GApplication::log_string(const GChatter& chatter,
607  const std::string& string,
608  const bool& linefeed)
609 {
610  // Get chattiness of application
611  GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
612 
613  // Only write message if chattiness is at least equal to the minimum
614  // required chattiness
615  if (chattiness >= chatter) {
616  log << string;
617  if (linefeed) {
618  log << std::endl;
619  }
620  }
621 
622  // Return
623  return;
624 }
625 
626 
627 /***********************************************************************//**
628  * @brief Write parameter value in log file
629  *
630  * @param[in] chatter Minimum required chattiness
631  * @param[in] name Parameter name string
632  * @param[in] value Value string
633  *
634  * Writes a parameter value into the log file if chattiness is at least
635  * @p chatter.
636  ***************************************************************************/
637 void GApplication::log_value(const GChatter& chatter,
638  const std::string& name,
639  const std::string& value)
640 {
641  // Get chattiness of application
642  GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
643 
644  // Only write message if chattiness is at least equal to the minimum
645  // required chattiness
646  if (chattiness >= chatter) {
647  log << gammalib::parformat(name);
648  log << value << std::endl;
649  }
650 
651  // Return
652  return;
653 }
654 
655 
656 /***********************************************************************//**
657  * @brief Write parameter value in log file
658  *
659  * @param[in] chatter Minimum required chattiness
660  * @param[in] name Parameter name string
661  * @param[in] value Integer value
662  *
663  * Writes a parameter value into the log file if chattiness is at least
664  * @p chatter.
665  ***************************************************************************/
666 void GApplication::log_value(const GChatter& chatter,
667  const std::string& name,
668  const int& value)
669 {
670  // Get chattiness of application
671  GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
672 
673  // Only write message if chattiness is at least equal to the minimum
674  // required chattiness
675  if (chattiness >= chatter) {
676  log << gammalib::parformat(name);
677  log << value << std::endl;
678  }
679 
680  // Return
681  return;
682 }
683 
684 
685 /***********************************************************************//**
686  * @brief Write parameter value in log file
687  *
688  * @param[in] chatter Minimum required chattiness
689  * @param[in] name Parameter name string
690  * @param[in] value Floating point value
691  *
692  * Writes a parameter value into the log file if chattiness is at least
693  * @p chatter.
694  ***************************************************************************/
695 void GApplication::log_value(const GChatter& chatter,
696  const std::string& name,
697  const double& value)
698 {
699  // Get chattiness of application
700  GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
701 
702  // Only write message if chattiness is at least equal to the minimum
703  // required chattiness
704  if (chattiness >= chatter) {
705  log << gammalib::parformat(name);
706  log << value << std::endl;
707  }
708 
709  // Return
710  return;
711 }
712 
713 
714 /***********************************************************************//**
715  * @brief Write header 1 in log file
716  *
717  * @param[in] chatter Minimum required chattiness
718  * @param[in] header Header string
719  *
720  * Writes a header of level 1 into the log file if chattiness is at least
721  * @p chatter.
722  ***************************************************************************/
724  const std::string& header)
725 {
726  // Get chattiness of application
727  GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
728 
729  // Only write message if chattiness is at least equal to the minimum
730  // required chattiness
731  if (chattiness >= chatter) {
732  log << std::endl;
733  log.header1(header);
734  }
735 
736  // Return
737  return;
738 }
739 
740 
741 /***********************************************************************//**
742  * @brief Write header 2 in log file
743  *
744  * @param[in] chatter Minimum required chattiness
745  * @param[in] header Header string
746  *
747  * Writes a header of level 2 into the log file if chattiness is at least
748  * @p chatter.
749  ***************************************************************************/
751  const std::string& header)
752 {
753  // Get chattiness of application
754  GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
755 
756  // Only write message if chattiness is at least equal to the minimum
757  // required chattiness
758  if (chattiness >= chatter) {
759  log.header2(header);
760  }
761 
762  // Return
763  return;
764 }
765 
766 
767 /***********************************************************************//**
768  * @brief Write header 3 in log file
769  *
770  * @param[in] chatter Minimum required chattiness
771  * @param[in] header Header string
772  *
773  * Writes a header of level 3 into the log file if chattiness is at least
774  * @p chatter.
775  ***************************************************************************/
777  const std::string& header)
778 {
779  // Get chattiness of application
780  GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
781 
782  // Only write message if chattiness is at least equal to the minimum
783  // required chattiness
784  if (chattiness >= chatter) {
785  log.header3(header);
786  }
787 
788  // Return
789  return;
790 }
791 
792 
793 /***********************************************************************//**
794  * @brief Write application parameters in log file
795  *
796  * @param[in] chatter Minimum required chattiness
797  *
798  * Writes all application parameters in the log file. For parameters that
799  * have not yet been queried the method does not write the current value
800  * but signals [not queried].
801  ***************************************************************************/
803 {
804  // Get chattiness of application
805  GChatter chattiness = static_cast<GChatter>((&m_pars["chatter"])->integer());
806 
807  // Only write parameters if the chattiness is at least equal to the
808  // minimum required chattiness
809  if (chattiness >= chatter) {
810 
811  // Write header
812  log.header1("Parameters");
813 
814  // Write parameters in logger
815  for (int i = 0; i < m_pars.size(); ++i) {
816 
817  // Set parameter name
818  std::string name = " " + m_pars.m_pars[i].name() + " ";
819  name = name + gammalib::fill(".", 28-name.length()) + ": ";
820 
821  // Write parameter
822  if (m_pars.m_pars[i].is_query() &&
823  !m_pars.m_pars[i].was_queried()) {
824  log << name << "[not queried]" << std::endl;
825  }
826  else {
827  log << name << m_pars.m_pars[i].m_value << std::endl;
828  }
829 
830  } // endfor: looped over all parameters
831 
832  } // endif: chattiness satisfied minimum required level
833 
834  // Return
835  return;
836 }
837 
838 
839 /***********************************************************************//**
840  * @brief Print application
841  *
842  * @param[in] chatter Chattiness.
843  * @return String containing application information.
844  ***************************************************************************/
845 std::string GApplication::print(const GChatter& chatter) const
846 {
847  // Initialise result string
848  std::string result;
849 
850  // Continue only if chatter is not silent
851  if (chatter != SILENT) {
852 
853  // Append header
854  result.append("=== GApplication ===");
855 
856  // Append application name, version and arguments
857  result.append("\n"+gammalib::parformat("Name")+name());
858  result.append("\n"+gammalib::parformat("Version")+version());
859  for (int i = 0; i < m_args.size(); ++i) {
860  if (i == 0) {
861  result.append("\n"+gammalib::parformat("Command")+m_args[i]);
862  }
863  else if (i == 1) {
864  result.append("\n"+gammalib::parformat("Arguments")+m_args[i]);
865  }
866  else {
867  result.append("\n"+gammalib::parformat(" ")+m_args[i]);
868  }
869  }
870 
871  // Append parameters
872  for (int i = 0; i < m_pars.size(); ++i) {
873  result.append("\n"+gammalib::parformat(m_pars.m_pars[i].name()) +
874  m_pars.m_pars[i].m_value);
875  }
876 
877  } // endif: chatter was not silent
878 
879  // Return result
880  return result;
881 }
882 
883 
884 /*==========================================================================
885  = =
886  = Private methods =
887  = =
888  ==========================================================================*/
889 
890 /***********************************************************************//**
891  * @brief Initialise class members
892  ***************************************************************************/
894 {
895  // Initialise public members
896  log.clear();
897 
898  // Initialise protected members
899  m_name.clear();
900  m_version.clear();
901  m_parfile.clear();
902  m_logfile.clear();
903  m_args.clear();
904  m_pars.clear();
905  m_pars_loaded = false;
906  m_need_help = false;
907 
908  // Save the execution calendar start time
909  std::time(&m_tstart);
910 
911  // Save the execution start clock
912  m_cstart = std::clock();
913 
914  // Return
915  return;
916 }
917 
918 
919 /***********************************************************************//**
920  * @brief Copy class members
921  *
922  * @param[in] app Application.
923  *
924  * Copies all class members.
925  ***************************************************************************/
927 {
928  // Copy public attributes
929  log = app.log;
930 
931  // Copy protected attributes
932  m_name = app.m_name;
933  m_version = app.m_version;
934  m_parfile = app.m_parfile;
935  m_logfile = app.m_logfile;
936  m_args = app.m_args;
937  m_tstart = app.m_tstart;
938  m_cstart = app.m_cstart;
939  m_pars = app.m_pars;
941  m_need_help = app.m_need_help;
942 
943  // Return
944  return;
945 }
946 
947 
948 /***********************************************************************//**
949  * @brief Delete class members
950  ***************************************************************************/
952 {
953  // Save application parameters if they have been loaded
954  if (m_pars_loaded) {
956  }
957 
958  // Close log file
959  logFileClose();
960 
961  // Return
962  return;
963 }
964 
965 
966 /***********************************************************************//**
967  * @brief Set chattiness of logger
968  ***************************************************************************/
970 {
971  // Get chattiness from application parameter
972  int chatter = m_pars["chatter"].integer();
973 
974  // Make sure that chatter is within valid range
975  if (chatter < 0) {
976  chatter = 0;
977  }
978  else if (chatter > 4) {
979  chatter = 4;
980  }
981 
982  // Set logger chatter
983  log.chatter((GChatter)chatter);
984 
985  // Return
986  return;
987 }
988 
989 
990 /***********************************************************************//**
991  * @brief Set log filename from "logfile" parameter
992  *
993  * If the application parameters contain a "logfile" parameter then set the
994  * file name of the log file from this parameter.
995  *
996  * In case that the file name is not valid, the file name will be set to an
997  * empty string, which will prevent opening a log file.
998  ***************************************************************************/
1000 {
1001  // Continue only if logfile parameter exists
1002  if (m_pars.contains("logfile")) {
1003 
1004  // Get log filename from application parameter
1005  if (m_pars["logfile"].is_valid()) {
1006  m_logfile = m_pars["logfile"].filename();
1007  }
1008  else {
1009  m_logfile = "";
1010  }
1011 
1012  }
1013 
1014  // Return
1015  return;
1016 }
const int header_width
Header width.
GApplication(void)
Void constructor.
bool logTerse(void) const
Signal terse logging.
void set_log_chatter(void)
Set chattiness of logger.
Application parameter container class.
void open(const GFilename &filename, const bool &clobber=false)
Open log file.
Definition: GLog.cpp:505
void header1(const std::string &arg)
Write string as header framed by &#39;=&#39; characters into logger.
Definition: GLog.hpp:214
void log_header3(const GChatter &chatter, const std::string &header)
Write header 3 in log file.
void log_value(const GChatter &chatter, const std::string &name, const std::string &value)
Write parameter value in log file.
~GApplication(void)
Destructor.
const std::string & name(void) const
Return application name.
void header2(const std::string &arg)
Write string as header framed by &#39;-&#39; characters into logger.
Definition: GLog.hpp:229
void set_log_filename(void)
Set log filename from &quot;logfile&quot; parameter.
void copy_members(const GApplication &app)
Copy class members.
std::time_t m_tstart
Calendar start time of execution.
bool is_open(void) const
Signal that log file is open.
Definition: GLog.hpp:173
GApplication * clone(void) const
Clone application.
const std::string & log_filename(void) const
Returns log filename.
std::vector< GApplicationPar > m_pars
Parameters.
std::clock_t m_cstart
Clock start time of execution.
void logFileClose(void)
Close log file.
void chatter(const GChatter &chatter)
Set chattiness.
Definition: GLog.hpp:367
const std::string & par_filename(void) const
Returns parameter filename.
void log_header2(const GChatter &chatter, const std::string &header)
Write header 2 in log file.
std::string m_logfile
Log filename.
void header3(const std::string &arg)
Write string as header enclosed by &#39;===&#39; into logger.
Definition: GLog.hpp:244
void log_header(void)
Write application header in log file.
Gammalib tools definition.
void log_string(const GChatter &chatter, const std::string &string, const bool &linefeed=true)
Write string in log file.
std::string strip_whitespace(const std::string &arg)
Strip leading and trailing whitespace from string.
Definition: GTools.cpp:73
double celapse(void) const
Return application elapsed time in CPU seconds.
std::string centre(const std::string &s, const int &n, const char &c= ' ')
Centre string to achieve a length of n characters.
Definition: GTools.cpp:997
void log_trailer(void)
Write application trailer in log file.
GApplication & operator=(const GApplication &app)
Assignment operator.
bool logExplicit(void) const
Signal explicit logging.
void init_members(void)
Initialise class members.
void clear(void)
Clear application.
bool logNormal(void) const
Signal normal logging.
std::vector< std::string > m_args
Command line arguments.
int size(void) const
Return number of parameters in container.
void log_header1(const GChatter &chatter, const std::string &header)
Write header 1 in log file.
bool clobber(void) const
Return clobber.
std::string left(const std::string &s, const int &n, const char &c= ' ')
Left justify string to achieve a length of n characters.
Definition: GTools.cpp:949
GammaLib application base class.
double telapse(void) const
Return application elapsed time in calendar seconds.
void close(void)
Close log file.
Definition: GLog.cpp:538
GChatter
Definition: GTypemaps.hpp:33
void free_members(void)
Delete class members.
bool m_pars_loaded
Application parameters loaded.
void save(const GFilename &filename)
Save parameters.
std::string m_version
Application version.
std::string print(const GChatter &chatter=NORMAL) const
Print application.
bool m_need_help
–help specified
const std::string & version(void) const
Return application version.
void logFileOpen(const bool &clobber=true)
Open log file.
void log_parameters(const GChatter &chatter)
Write application parameters in log file.
const GApplicationPars & pars(void) const
Return application parameters.
GLog log
Application logger.
GApplicationPars m_pars
Application parameters.
bool logDebug(void) const
Signal debug logging.
long int written_size(void) const
Return number of characters written through this logger.
Definition: GLog.hpp:161
Application parameter class.
bool logVerbose(void) const
Signal verbose logging.
GammaLib application interface definition.
std::string parformat(const std::string &s, const int &indent=0)
Convert string in parameter format.
Definition: GTools.cpp:1022
std::string m_name
Application name.
std::string fill(const std::string &s, const int &n)
Fill string with n strings of same type.
Definition: GTools.cpp:923
void indent(const int &indent)
Set indentation.
Definition: GLog.hpp:346
std::string m_parfile
Parameter filename.
void load(const GFilename &filename)
Load parameters.
bool contains(const std::string &name) const
Check parameter exists.
void clear(void)
Clear object.
Definition: GLog.cpp:456
void clear(void)
Clear parameter container.