Commit 2ee39e3bc71311e0867595969c35bb8f9127c4b3

Authored by Jürgen Knödlseder
1 parent 3f1b7ddb

Add GLog::is_open() method and flush logfile from kernel buffer (#1837).

ChangeLog
1 -2016-08-04 1 +2016-08-05
2 2
3 * Version 1.2.0 released 3 * Version 1.2.0 released
4 ========================= 4 =========================
5 5
  6 + Add GLog::is_open() method (#1837)
6 Fix invalid pointer problem in GLog copies (#1823) 7 Fix invalid pointer problem in GLog copies (#1823)
7 Fix GSkyMap::publish() bug for empty sky maps (#1835) 8 Fix GSkyMap::publish() bug for empty sky maps (#1835)
8 Fix loss of application name and version (#1835) 9 Fix loss of application name and version (#1835)
1 New Features and Important Changes in GammaLib 1.2.0 1 New Features and Important Changes in GammaLib 1.2.0
2 2
3 -4 August 2016 3 +5 August 2016
4 4
5 5
6 1. Introduction 6 1. Introduction
@@ -27,6 +27,7 @@ The following methods have been added: @@ -27,6 +27,7 @@ The following methods have been added:
27 - GApplication::log_header1() 27 - GApplication::log_header1()
28 - GApplication::log_header2() 28 - GApplication::log_header2()
29 - GApplication::log_header3() 29 - GApplication::log_header3()
  30 +- GLog::is_open()
30 31
31 The following methods have been removed: 32 The following methods have been removed:
32 - ... 33 - ...
@@ -69,6 +70,9 @@ pointer in the second instance became invalid. This has been fixed by @@ -69,6 +70,9 @@ pointer in the second instance became invalid. This has been fixed by
69 duplicating the file descriptor and opening the log file in the copied 70 duplicating the file descriptor and opening the log file in the copied
70 instance using the duplicated descriptor. This solves the problems (#1823). 71 instance using the duplicated descriptor. This solves the problems (#1823).
71 72
  73 +A GLog::is_open() method has been added to assess whether a log file is
  74 +actually opened (#1837).
  75 +
72 76
73 5. Base module 77 5. Base module
74 -------------- 78 --------------
README.md
1 GammaLib information 1 GammaLib information
2 ==================== 2 ====================
3 -* Version: 1.2.0.dev1 (4 August 2016) 3 +* Version: 1.2.0.dev1 (5 August 2016)
4 * Author: Juergen Knoedlseder (jurgen.knodlseder@irap.omp.eu) 4 * Author: Juergen Knoedlseder (jurgen.knodlseder@irap.omp.eu)
5 5
6 [![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 [![Build Status](https://cta-jenkins.irap.omp.eu/buildStatus/icon?job=gammalib-integrate-os)](https://cta-jenkins.irap.omp.eu/job/gammalib-integrate-os/)
include/GLog.hpp
@@ -86,6 +86,7 @@ public: @@ -86,6 +86,7 @@ public:
86 void clear(void); 86 void clear(void);
87 int size(void) const; 87 int size(void) const;
88 std::string classname(void) const; 88 std::string classname(void) const;
  89 + bool is_open(void) const;
89 void open(const GFilename& filename, const bool& clobber = false); 90 void open(const GFilename& filename, const bool& clobber = false);
90 void close(void); 91 void close(void);
91 void flush(const bool& force = false); 92 void flush(const bool& force = false);
@@ -147,6 +148,18 @@ std::string GLog::classname(void) const @@ -147,6 +148,18 @@ std::string GLog::classname(void) const
147 148
148 149
149 /***********************************************************************//** 150 /***********************************************************************//**
  151 + * @brief Signal that log file is open
  152 + *
  153 + * @return True if log file is open, false otherwise.
  154 + ***************************************************************************/
  155 +inline
  156 +bool GLog::is_open(void) const
  157 +{
  158 + return (m_file != NULL);
  159 +}
  160 +
  161 +
  162 +/***********************************************************************//**
150 * @brief Write string as centred header into logger 163 * @brief Write string as centred header into logger
151 * 164 *
152 * @param[in] arg Header string. 165 * @param[in] arg Header string.
pyext/GLog.i
@@ -47,6 +47,7 @@ public: @@ -47,6 +47,7 @@ public:
47 void clear(void); 47 void clear(void);
48 int size(void) const; 48 int size(void) const;
49 std::string classname(void) const; 49 std::string classname(void) const;
  50 + bool is_open(void) const;
50 void open(const GFilename& filename, const bool& clobber = false); 51 void open(const GFilename& filename, const bool& clobber = false);
51 void close(void); 52 void close(void);
52 void flush(const bool& force = false); 53 void flush(const bool& force = false);
src/app/GApplication.cpp
@@ -332,7 +332,10 @@ void GApplication::logFileOpen(const bool& clobber) @@ -332,7 +332,10 @@ void GApplication::logFileOpen(const bool& clobber)
332 // Initialise the application logger 332 // Initialise the application logger
333 log.open(log_filename(), clobber); 333 log.open(log_filename(), clobber);
334 334
335 - } 335 + // Write header into log file
  336 + log_header();
  337 +
  338 + } // endif: file name was not empty
336 339
337 // Set logger chattiness 340 // Set logger chattiness
338 set_log_chatter(); 341 set_log_chatter();
@@ -349,11 +352,16 @@ void GApplication::logFileOpen(const bool& clobber) @@ -349,11 +352,16 @@ void GApplication::logFileOpen(const bool& clobber)
349 ***************************************************************************/ 352 ***************************************************************************/
350 void GApplication::logFileClose(void) 353 void GApplication::logFileClose(void)
351 { 354 {
352 - // Put trailer in log file  
353 - log_trailer(); 355 + // Continue only if log file is open
  356 + if (log.is_open()) {
  357 +
  358 + // Write trailer into log file
  359 + log_trailer();
354 360
355 - // Close log file  
356 - log.close(); 361 + // Close log file
  362 + log.close();
  363 +
  364 + } // endif: log file was open
357 365
358 // Return 366 // Return
359 return; 367 return;
@@ -872,11 +880,8 @@ void GApplication::free_members(void) @@ -872,11 +880,8 @@ void GApplication::free_members(void)
872 // Save application parameters 880 // Save application parameters
873 m_pars.save(par_filename()); 881 m_pars.save(par_filename());
874 882
875 - // Put trailer in log file  
876 - log_trailer();  
877 -  
878 // Close log file 883 // Close log file
879 - log.close(); 884 + logFileClose();
880 885
881 // Return 886 // Return
882 return; 887 return;
src/app/GLog.cpp
@@ -554,9 +554,18 @@ void GLog::init_members(void) @@ -554,9 +554,18 @@ void GLog::init_members(void)
554 * @brief Copy class members 554 * @brief Copy class members
555 * 555 *
556 * @param[in] log Logger. 556 * @param[in] log Logger.
  557 + *
  558 + * Copy all class members from @p log to the current logger instance. This
  559 + * method will flush the buffer of @p log before copying.
557 ***************************************************************************/ 560 ***************************************************************************/
558 void GLog::copy_members(const GLog& log) 561 void GLog::copy_members(const GLog& log)
559 { 562 {
  563 + // Force flushing the buffer before doing the copy. This assures that the
  564 + // log file is left in a clean state, and in particular, that logging
  565 + // will be done in order. We have to circument const correctness for
  566 + // doing this.
  567 + const_cast<GLog*>(&log)->flush(true);
  568 +
560 // Copy attributes 569 // Copy attributes
561 m_max_length = log.m_max_length; 570 m_max_length = log.m_max_length;
562 m_indent = log.m_indent; 571 m_indent = log.m_indent;
@@ -644,6 +653,9 @@ void GLog::flush(const bool&amp; force) @@ -644,6 +653,9 @@ void GLog::flush(const bool&amp; force)
644 653
645 } // endwhile: flush until empty 654 } // endwhile: flush until empty
646 655
  656 + // And now flush the kernel buffer
  657 + std::fflush(m_file);
  658 +
647 } // endif: flush was required 659 } // endif: flush was required
648 660
649 // Return 661 // Return
test/test_GApplication.cpp
@@ -381,7 +381,6 @@ void TestGApplication::test_GApplication(void) @@ -381,7 +381,6 @@ void TestGApplication::test_GApplication(void)
381 "Check application log file name"); 381 "Check application log file name");
382 382
383 // Write something into the logfile and close it 383 // Write something into the logfile and close it
384 - app1.log_header();  
385 app1.log_string(SILENT, "Silent", false); 384 app1.log_string(SILENT, "Silent", false);
386 app1.log_string(TERSE, "Terse", false); 385 app1.log_string(TERSE, "Terse", false);
387 app1.log_string(NORMAL, "Normal", false); 386 app1.log_string(NORMAL, "Normal", false);