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