Commit 6151517c0ca90e83f060a85d3ed14469a2309c42

Authored by Jürgen Knödlseder
1 parent e78b598b

Make compliant to coding standards and do minor fixes.

The code was overseen to comply to the GammaLib coding standards.

The m_newline member has been removed as this member is not used any longer.

The m_filename member is now initialized correctly in init_members() and copied in copy_members().

A new method named prefix() has been added that provides the prefix. This reduces code duplication in the append() method. Furthermore, a possible indent was put back that was removed during code revision.
include/GLog.hpp
1 1 /***************************************************************************
2 2 * GLog.hpp - Information logger *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010 by Jurgen Knodlseder *
  4 + * copyright (C) 2010-2012 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -21,7 +21,7 @@
21 21 /**
22 22 * @file GLog.hpp
23 23 * @brief Information logger class definition
24   - * @author Jurgen Knodlseder
  24 + * @author Juergen Knoedlseder
25 25 */
26 26  
27 27 #ifndef GLOG_HPP
... ... @@ -50,13 +50,8 @@
50 50 * The logger interface implements an internal character buffer that is
51 51 * flushed once a maximum size parameter is exceeded. The buffer limit can
52 52 * be modified using the max_size() method; by default it is set to 8192
53   - * characters. Note that the buffer is flushed each time a carriage return
54   - * occurs, irrespectively of the buffer size.
55   - * The logger allows prepending of the current date and the task name. Note
56   - * that the current date is the one that applies when the buffer is actually
57   - * flushed, hence when a good precision in the date is required the maximum
58   - * buffer length should be set to 0 (i.e. the buffer is flushed after each
59   - * logging).
  53 + * characters. The logger allows prepending of the current date and the task
  54 + * name.
60 55 ***************************************************************************/
61 56 class GLog {
62 57  
... ... @@ -111,8 +106,9 @@ protected:
111 106 void free_members(void);
112 107 void flush(bool force = false);
113 108 void header(const std::string& arg, int level);
114   - std::string strdate(void);
115   - void append(const std::string& arg);
  109 + std::string strdate(void) const;
  110 + std::string prefix(void) const;
  111 + void append(std::string arg);
116 112  
117 113 // Protected data members
118 114 int m_max_length; //!< Maximum buffer length
... ... @@ -120,9 +116,8 @@ protected:
120 116 bool m_stdout; //!< Dump in standard output
121 117 bool m_stderr; //!< Dump in standard error
122 118 bool m_use_date; //!< Dump date in prefix
123   - bool m_newline; //!< Signal beginning of new line
124 119 FILE* m_file; //!< Log file pointer
125   - std::string m_filename; //!< Log file name
  120 + std::string m_filename; //!< Log file name
126 121 std::string m_name; //!< Name for prefix
127 122 std::string m_buffer; //!< Output string buffer
128 123 };
... ...
src/app/GLog.cpp
1 1 /***************************************************************************
2 2 * GLog.cpp - Information logger *
3 3 * ----------------------------------------------------------------------- *
4   - * copyright (C) 2010-2011 by Jurgen Knodlseder *
  4 + * copyright (C) 2010-2012 by Juergen Knoedlseder *
5 5 * ----------------------------------------------------------------------- *
6 6 * *
7 7 * This program is free software: you can redistribute it and/or modify *
... ... @@ -21,7 +21,7 @@
21 21 /**
22 22 * @file GLog.cpp
23 23 * @brief Information logger class implementation
24   - * @author Jurgen Knodlseder
  24 + * @author Juergen Knoedlseder
25 25 */
26 26  
27 27 /* __ Includes ___________________________________________________________ */
... ... @@ -38,6 +38,7 @@
38 38 #include <fstream>
39 39 #include "GLog.hpp"
40 40 #include "GTools.hpp"
  41 +
41 42 /* __ Method name definitions ____________________________________________ */
42 43  
43 44 /* __ Macros _____________________________________________________________ */
... ... @@ -176,10 +177,8 @@ void GLog::operator()(const char *msgFormat, ...)
176 177 std::vsprintf(buffer, msgFormat, vl);
177 178 va_end(vl);
178 179  
179   - std::string string = buffer;
180 180 // Append message to string buffer
181   - append(string);
182   - append("\n");
  181 + append(std::string(buffer)+"\n");
183 182  
184 183 // Return
185 184 return;
... ... @@ -191,44 +190,52 @@ void GLog::operator()(const char *msgFormat, ...)
191 190 * @brief Insert logger into logger
192 191 *
193 192 * @param[in] log Logger to be inserted.
  193 + *
  194 + * Insert a logger object into another logger object. This method handles
  195 + * information that has either been written into a file, or that is still
  196 + * present in the logger buffer.
194 197 ***************************************************************************/
195 198 GLog& GLog::operator<<(GLog& log)
196 199 {
197   - // If log does not write in a file
198   - if (log.m_file == NULL)
199   - {
200   - // Add log buffer to this buffer
  200 + // If log object has an open file then append the log buffer to this
  201 + // buffer
  202 + if (log.m_file == NULL) {
201 203 m_buffer.append(log.m_buffer);
202 204 }
203   - else
204   - {
205   - //Force flush
  205 +
  206 + // ... otherwise read all information from file and append it to the
  207 + // buffer
  208 + else {
  209 +
  210 + // Force flush so that the log object writes the entire buffer
  211 + // into the file
206 212 log.flush(true);
207 213  
208   - //Get the filename
  214 + // Get the filename
209 215 std::string filename = log.filename();
210 216  
211   - //Close file
  217 + // Close log's file
212 218 log.close();
213 219  
214   - // Open file in read mode
215   - std::ifstream file(log.filename().c_str(), std::ios::in );
216   - if (file)
217   - {
  220 + // Open log's file in read mode
  221 + std::ifstream file(filename.c_str(), std::ios::in);
  222 + if (file) {
  223 +
  224 + // Append all lines from file to this buffer
218 225 std::string line;
219 226 while(std::getline(file,line)){
220   - m_buffer.append(line);
221   - m_buffer.append("\n");
  227 + m_buffer.append(line+"\n");
222 228 }
223 229  
  230 + // Close log's file
224 231 file.close();
225 232 }
226 233  
227   - // Open the file
  234 + // Open log's file in append mode
228 235 log.open(filename,false);
229 236 }
230 237  
231   - //Flush
  238 + // Flush
232 239 flush();
233 240  
234 241 // Return logger
... ... @@ -240,6 +247,8 @@ GLog&amp; GLog::operator&lt;&lt;(GLog&amp; log)
240 247 * @brief Insert C++ string into logger
241 248 *
242 249 * @param[in] str C++ string to be inserted.
  250 + *
  251 + * This method insert a C++ string into the logger.
243 252 ***************************************************************************/
244 253 GLog& GLog::operator<<(const std::string& str)
245 254 {
... ... @@ -255,14 +264,13 @@ GLog&amp; GLog::operator&lt;&lt;(const std::string&amp; str)
255 264 * @brief Insert C string into logger
256 265 *
257 266 * @param[in] str C string to be inserted.
  267 + *
  268 + * This method insert a C string into the logger.
258 269 ***************************************************************************/
259 270 GLog& GLog::operator<<(const char* str)
260 271 {
261   - // Convert in string
262   - std::string string = str;
263   -
264 272 // Add string to buffer
265   - append(string);
  273 + append(std::string(str));
266 274  
267 275 // Return logger
268 276 return *this;
... ... @@ -273,6 +281,8 @@ GLog&amp; GLog::operator&lt;&lt;(const char* str)
273 281 * @brief Insert character value into logger
274 282 *
275 283 * @param[in] value Character value to be inserted.
  284 + *
  285 + * This method inserts a character value into the logger.
276 286 ***************************************************************************/
277 287 GLog& GLog::operator<<(const char& value)
278 288 {
... ... @@ -294,6 +304,8 @@ GLog&amp; GLog::operator&lt;&lt;(const char&amp; value)
294 304 * @brief Insert unsigned character value into logger
295 305 *
296 306 * @param[in] value Unsigned character value to be inserted.
  307 + *
  308 + * This method inserts an unsigned character value into the logger.
297 309 ***************************************************************************/
298 310 GLog& GLog::operator<<(const unsigned char& value)
299 311 {
... ... @@ -315,14 +327,19 @@ GLog&amp; GLog::operator&lt;&lt;(const unsigned char&amp; value)
315 327 * @brief Insert bool into logger
316 328 *
317 329 * @param[in] value Boolean to be inserted.
  330 + *
  331 + * Depending on the argument, this method inserts either the character string
  332 + * "true" or "false" into the logger.
318 333 ***************************************************************************/
319 334 GLog& GLog::operator<<(const bool& value)
320 335 {
321 336 // Add boolean to buffer
322   - if (value)
  337 + if (value) {
323 338 append("true");
324   - else
  339 + }
  340 + else {
325 341 append("false");
  342 + }
326 343  
327 344 // Return logger
328 345 return *this;
... ... @@ -333,6 +350,8 @@ GLog&amp; GLog::operator&lt;&lt;(const bool&amp; value)
333 350 * @brief Insert integer into logger
334 351 *
335 352 * @param[in] value Integer to be inserted.
  353 + *
  354 + * This method inserts an integer into the logger.
336 355 ***************************************************************************/
337 356 GLog& GLog::operator<<(const int& value)
338 357 {
... ... @@ -354,6 +373,8 @@ GLog&amp; GLog::operator&lt;&lt;(const int&amp; value)
354 373 * @brief Insert unsigned integer into logger
355 374 *
356 375 * @param[in] value Unsigned integer to be inserted.
  376 + *
  377 + * This method inserts an unsigned integer into the logger.
357 378 ***************************************************************************/
358 379 GLog& GLog::operator<<(const unsigned int& value)
359 380 {
... ... @@ -375,6 +396,8 @@ GLog&amp; GLog::operator&lt;&lt;(const unsigned int&amp; value)
375 396 * @brief Insert double precision value into logger
376 397 *
377 398 * @param[in] value Double precision value to be inserted.
  399 + *
  400 + * This method inserts a double precision value into the logger.
378 401 ***************************************************************************/
379 402 GLog& GLog::operator<<(const double& value)
380 403 {
... ... @@ -456,16 +479,19 @@ int GLog::size(void) const
456 479 ***************************************************************************/
457 480 void GLog::open(const std::string& filename, bool clobber)
458 481 {
  482 + // Store the filename
459 483 m_filename = filename;
460 484  
461 485 // Close any existing file
462 486 close();
463 487  
464 488 // Open file
465   - if (clobber)
  489 + if (clobber) {
466 490 m_file = std::fopen(filename.c_str(), "w");
467   - else
  491 + }
  492 + else {
468 493 m_file = std::fopen(filename.c_str(), "a");
  494 + }
469 495  
470 496 // Return
471 497 return;
... ... @@ -498,6 +524,9 @@ void GLog::close(void)
498 524 * @brief Set data flag that controls date prefixing
499 525 *
500 526 * @param[in] flag Enable/disable date flag (true/false).
  527 + *
  528 + * If the data flag is set to true, each new line will be prepended by the
  529 + * actual date when the writing of the line was started.
501 530 ***************************************************************************/
502 531 void GLog::date(bool flag)
503 532 {
... ... @@ -547,6 +576,13 @@ void GLog::cerr(bool flag)
547 576 * @brief Set name to put into prefix
548 577 *
549 578 * @param[in] name Name to put into prefix.
  579 + *
  580 + * If the name is set to a non-zero string, each new line will be prepended
  581 + * by the specified name. If also the date should be prepended, the name
  582 + * will be inserted after the date.
  583 + *
  584 + * Specifying an empty string will suppress the insertion of the name into
  585 + * the logger.
550 586 ***************************************************************************/
551 587 void GLog::name(const std::string& name)
552 588 {
... ... @@ -616,8 +652,8 @@ void GLog::init_members(void)
616 652 m_stdout = false;
617 653 m_stderr = false;
618 654 m_use_date = false;
619   - m_newline = true;
620 655 m_file = NULL;
  656 + m_filename.clear();
621 657 m_name.clear();
622 658 m_buffer.clear();
623 659  
... ... @@ -639,10 +675,10 @@ void GLog::copy_members(const GLog&amp; log)
639 675 m_stdout = log.m_stdout;
640 676 m_stderr = log.m_stderr;
641 677 m_use_date = log.m_use_date;
642   - m_newline = log.m_newline;
  678 + m_file = log.m_file;
  679 + m_filename = log.m_filename;
643 680 m_name = log.m_name;
644 681 m_buffer = log.m_buffer;
645   - m_file = log.m_file;
646 682  
647 683 // Return
648 684 return;
... ... @@ -674,10 +710,9 @@ void GLog::free_members(void)
674 710 *
675 711 * Flush string buffer if it is full or if flushing is enforced. This method
676 712 * writes the output string in the relevant streams. It decomposes the
677   - * buffer in lines that a separated by a '\n' character and preprends, if
678   - * requested, the current date and name. The following streams are currently
679   - * implemented (and will be filled in parallel): stdout, stderr, and an ASCII
680   - * file.
  713 + * buffer in lines that a separated by a '\n' character. The following
  714 + * streams are currently implemented (and will be filled in parallel):
  715 + * stdout, stderr, and an ASCII file.
681 716 ***************************************************************************/
682 717 void GLog::flush(bool force)
683 718 {
... ... @@ -690,33 +725,28 @@ void GLog::flush(bool force)
690 725 // Flush buffer until it is empty
691 726 while (m_buffer.size() > 0) {
692 727  
693   - // Initialise newline for next line
694   - bool newline = m_newline;
695   -
696 728 // Find next CR
697 729 std::string line;
698 730 std::size_t pos = m_buffer.find_first_of("\n", 0);
699 731 if (pos == std::string::npos) {
700 732 line = m_buffer;
701   - newline = false;
702 733 m_buffer.clear();
703 734 }
704 735 else {
705 736 line = m_buffer.substr(0, pos) + "\n";
706 737 m_buffer = m_buffer.substr(pos+1, m_buffer.size()-pos);
707   - newline = true;
708 738 }
709 739  
710 740 // Put line into requested streams
711   - if (m_stdout)
  741 + if (m_stdout) {
712 742 std::cout << line;
713   - if (m_stderr)
  743 + }
  744 + if (m_stderr) {
714 745 std::cerr << line;
715   - if (m_file != NULL)
  746 + }
  747 + if (m_file != NULL) {
716 748 std::fprintf(m_file, "%s", line.c_str());
717   -
718   - // Set newline flag for next line
719   - m_newline = newline;
  749 + }
720 750  
721 751 } // endwhile: flush until empty
722 752  
... ... @@ -786,7 +816,7 @@ void GLog::header(const std::string&amp; arg, int level)
786 816 *
787 817 * Returns the current date as string in the format yyyy-mm-ddThh:mm:ss.
788 818 ***************************************************************************/
789   -std::string GLog::strdate(void)
  819 +std::string GLog::strdate(void) const
790 820 {
791 821 // Allocate variables
792 822 struct std::tm timeStruct;
... ... @@ -817,60 +847,84 @@ std::string GLog::strdate(void)
817 847 return date;
818 848 }
819 849  
  850 +
820 851 /***********************************************************************//**
821   - * @brief Append a string in the buffer
822   - * @param[in] arg The string to append
  852 + * @brief Return prefix
  853 + *
  854 + * Returns the prefix for each line.
823 855 ***************************************************************************/
824   -void GLog::append(const std::string& arg)
  856 +std::string GLog::prefix(void) const
825 857 {
  858 + // Initialize prefix to empty string
  859 + std::string prefix = "";
826 860  
827   - std::string result(arg);
  861 + // Add date if requested
  862 + if (m_use_date){
  863 + prefix.append(strdate());
  864 + }
828 865  
829   - // If the buffer is empty or if the last charatere is a \n, start with the prefix
830   - if(m_buffer.size()==0 || m_buffer[m_buffer.size()-1]=='\n')
831   - {
832   - // Prepend prefix if we are at the beginning of a new line
833   - std::string prefix = "";
834   - if (m_use_date){
835   - prefix.append(strdate());
836   - }
837   - if (m_name.length() > 0){
838   - prefix.append(" "+m_name);
839   - }
840   - if (prefix.length() > 0){
841   - prefix.append(": ");
842   - result.insert(0,prefix);
843   - }
  866 + // Add name if requested
  867 + if (m_name.length() > 0){
  868 + prefix.append(" "+m_name);
844 869 }
845 870  
846   - //Search the first CR
847   - std::size_t pos= pos = result.find_first_of("\n",0);
  871 + // If there is a prefix then add separator
  872 + if (prefix.length() > 0){
  873 + prefix.append(": ");
  874 + }
848 875  
849   - // Search all \n characters. Ignore the last CR
850   - while(pos!=std::string::npos&&pos<result.size()-1)
851   - {
852   - // Prepend prefix if we are at the beginning of a new line
853   - std::string prefix = "";
854   - if (m_use_date){
855   - prefix.append(strdate());
856   - }
857   - if (m_name.length() > 0){
858   - prefix.append(" "+m_name);
859   - }
860   - if (prefix.length() > 0){
861   - prefix.append(": ");
862   - result.insert(pos+1,prefix);
863   - }
  876 + // Add any indent
  877 + prefix.append(fill(" ", m_indent));
  878 +
  879 + // Return prefix
  880 + return prefix;
  881 +}
864 882  
865   - pos = result.find_first_of("\n",pos+1+prefix.size());
  883 +
  884 +/***********************************************************************//**
  885 + * @brief Append string to the buffer
  886 + *
  887 + * @param[in] arg String to append
  888 + *
  889 + * This method appends a string to the buffer and prepends, if required, the
  890 + * current date and name at the beginning of each line.
  891 + ***************************************************************************/
  892 +void GLog::append(std::string arg)
  893 +{
  894 + // If the buffer is empty or if the last charater is a \n, prepend a
  895 + // prefix at the beginning of the string to be inserted.
  896 + if (m_buffer.size() == 0 || m_buffer[m_buffer.size()-1] == '\n') {
  897 +
  898 + // Prepend prefix
  899 + arg.insert(0, prefix());
866 900 }
867 901  
  902 + // Search the first CR (\n)
  903 + std::size_t pos = arg.find_first_of("\n",0);
  904 +
  905 + // Search all \n characters. Ignore the last CR.
  906 + while (pos != std::string::npos && pos < arg.size()-1) {
  907 +
  908 + // Prepend prefix
  909 + std::string pre = prefix();
  910 + arg.insert(pos+1, pre);
  911 +
  912 + // Search next CR
  913 + pos = arg.find_first_of("\n",pos+1+pre.size());
  914 +
  915 + } // endwhile
  916 +
868 917 // Add string to buffer
869   - m_buffer.append(result);
  918 + m_buffer.append(arg);
870 919  
871   - //Flush Buffer
  920 + // Flush Buffer
872 921 flush();
  922 +
  923 + // Return
  924 + return;
873 925 }
  926 +
  927 +
874 928 /*==========================================================================
875 929 = =
876 930 = Friends =
... ...