GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GLog.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GLog.hpp - Information logger *
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 GLog.hpp
23  * @brief Information logger class definition
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GLOG_HPP
28 #define GLOG_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include <iostream>
33 #include "GFilename.hpp"
34 #include "GTypemaps.hpp"
35 
36 /* __ Forward declarations _______________________________________________ */
37 
38 
39 /***********************************************************************//**
40  * @class GLog
41  *
42  * @brief Information logger interface definition.
43  *
44  * This class implements an interface for logging of text messages to the
45  * standard output and/or error streams and into an ASCII log file. It
46  * provides a C++ style interface (allowing to stream messages using code
47  * such as
48  *
49  * log << "This is a message";
50  *
51  * and a C style interface that allows making use of C style text formating
52  * using code such as
53  *
54  * log("These are %d messages with %d items.", n, m);
55  *
56  * The logger interface implements an internal character buffer that is
57  * flushed once a maximum size parameter is exceeded. The buffer limit can
58  * be modified using the buffer_size() method; by default it is set to 8192
59  * characters. The logger allows prepending of the current date and the task
60  * name.
61  ***************************************************************************/
62 class GLog {
63 
64 public:
65  // Constructors and destructors
66  GLog(void);
67  GLog(const GFilename& filename, const bool& clobber = false);
68  GLog(const GLog& log);
69  virtual ~GLog(void);
70 
71  // Operators
72  GLog& operator=(const GLog& log);
73  void operator()(const char *msgFormat, ...);
74  GLog& operator<<(GLog& log);
75  GLog& operator<<(const std::string& str);
76  GLog& operator<<(const char* str);
77  GLog& operator<<(const char& value);
78  GLog& operator<<(const unsigned char& value);
79  GLog& operator<<(const bool& value);
80  GLog& operator<<(const int& value);
81  GLog& operator<<(const unsigned int& value);
82  GLog& operator<<(const double& value);
83  GLog& operator<<(std::ostream& (*fn)(std::ostream&));
84 
85  // Methods
86  void clear(void);
87  long int size(void) const;
88  long int written_size(void) const;
89  std::string classname(void) const;
90  bool is_open(void) const;
91  bool is_empty(void) const;
92  void open(const GFilename& filename,
93  const bool& clobber = false);
94  void close(void);
95  void flush(const bool& force = false);
96  void date(const bool& flag);
97  void cout(const bool& flag);
98  void cerr(const bool& flag);
99  void name(const std::string& name);
100  void buffer_size(const int& size);
101  void indent(const int& indent);
102  void chatter(const GChatter& chatter);
103  void header0(const std::string& arg);
104  void header1(const std::string& arg);
105  void header2(const std::string& arg);
106  void header3(const std::string& arg);
107  const bool& date(void) const;
108  const bool& cout(void) const;
109  const bool& cerr(void) const;
110  const std::string& name(void) const;
111  const int& buffer_size(void) const;
112  const int& indent(void) const;
113  const GChatter& chatter(void) const;
114  const GFilename& filename(void) const;
115  const std::string& buffer(void) const;
116 
117 protected:
118  // Protected methods
119  void init_members(void);
120  void copy_members(const GLog& log);
121  void free_members(void);
122  void header(const std::string& arg, int level);
123  std::string strdate(void) const;
124  std::string prefix(void) const;
125  void append(const std::string& string);
126 
127  // Protected data members
128  int m_max_length; //!< Maximum buffer length
129  int m_indent; //!< Indentation of text
130  long int m_written_size; //!< Number of char's written though this logger
131  bool m_stdout; //!< Dump in standard output
132  bool m_stderr; //!< Dump in standard error
133  bool m_use_date; //!< Dump date in prefix
134  bool m_linestart; //!< Signals that buffer is at line start
135  FILE* m_file; //!< Log file pointer
136  GFilename m_filename; //!< Log file name
137  std::string m_name; //!< Name for prefix
138  std::string m_buffer; //!< Output string buffer
139  GChatter m_chatter; //!< Chattiness for print() method
140 };
141 
142 
143 /***********************************************************************//**
144  * @brief Return class name
145  *
146  * @return String containing the class name ("GLog").
147  ***************************************************************************/
148 inline
149 std::string GLog::classname(void) const
150 {
151  return ("GLog");
152 }
153 
154 
155 /***********************************************************************//**
156  * @brief Return number of characters written through this logger
157  *
158  * @return Number of characters written.
159  ***************************************************************************/
160 inline
161 long int GLog::written_size(void) const
162 {
163  return (m_written_size);
164 }
165 
166 
167 /***********************************************************************//**
168  * @brief Signal that log file is open
169  *
170  * @return True if log file is open, false otherwise.
171  ***************************************************************************/
172 inline
173 bool GLog::is_open(void) const
174 {
175  return (m_file != NULL);
176 }
177 
178 
179 /***********************************************************************//**
180  * @brief Signal if log file is empty
181  *
182  * @return True if log file is empty, false otherwise.
183  ***************************************************************************/
184 inline
185 bool GLog::is_empty(void) const
186 {
187  return (size() == 0);
188 }
189 
190 
191 /***********************************************************************//**
192  * @brief Write string as centred header into logger
193  *
194  * @param[in] arg Header string.
195  *
196  * Writes a string as 80 character wide centred header.
197  ***************************************************************************/
198 inline
199 void GLog::header0(const std::string& arg)
200 {
201  header(arg, 0);
202  return;
203 }
204 
205 
206 /***********************************************************************//**
207  * @brief Write string as header framed by '=' characters into logger
208  *
209  * @param[in] arg Header string.
210  *
211  * Writes a string as header framed by '=' characters into logger.
212  ***************************************************************************/
213 inline
214 void GLog::header1(const std::string& arg)
215 {
216  header(arg, 1);
217  return;
218 }
219 
220 
221 /***********************************************************************//**
222  * @brief Write string as header framed by '-' characters into logger
223  *
224  * @param[in] arg Header string.
225  *
226  * Writes a string as header framed by '-' characters into logger.
227  ***************************************************************************/
228 inline
229 void GLog::header2(const std::string& arg)
230 {
231  header(arg, 2);
232  return;
233 }
234 
235 
236 /***********************************************************************//**
237  * @brief Write string as header enclosed by '===' into logger
238  *
239  * @param[in] arg Header string.
240  *
241  * Writes a string as header enclosed by '===' into logger.
242  ***************************************************************************/
243 inline
244 void GLog::header3(const std::string& arg)
245 {
246  header(arg, 3);
247  return;
248 }
249 
250 
251 /***********************************************************************//**
252  * @brief Set date flag that controls date prefixing
253  *
254  * @param[in] flag Enable/disable date flag (true/false).
255  *
256  * The date flag specifies whether each new line will be prepended by the
257  * actual date when the writing of the line was started.
258  ***************************************************************************/
259 inline
260 void GLog::date(const bool& flag)
261 {
262  m_use_date = flag;
263  return;
264 }
265 
266 
267 /***********************************************************************//**
268  * @brief Set standard output stream (cout) flag
269  *
270  * @param[in] flag Enable/disable logging (true/false).
271  *
272  * The cout flag specifies whether the logging should go in addition to the
273  * log file also into the standard output stream.
274  ***************************************************************************/
275 inline
276 void GLog::cout(const bool& flag)
277 {
278  m_stdout = flag;
279  return;
280 }
281 
282 
283 /***********************************************************************//**
284  * @brief Enables/disables logging into standard error stream
285  *
286  * @param[in] flag Enable/disable logging (true/false).
287  *
288  * The cerr flag specifies whether the logging should go in addition to the
289  * log file also into the standard error stream.
290  ***************************************************************************/
291 inline
292 void GLog::cerr(const bool& flag)
293 {
294  m_stderr = flag;
295  return;
296 }
297 
298 
299 /***********************************************************************//**
300  * @brief Set name to put into prefix
301  *
302  * @param[in] name Name to put into prefix.
303  *
304  * Sets the @p name that will be prefixed to each new line. If the date flag
305  * is true, the @p name will be inserted after the date at the beginning of
306  * each line.
307  *
308  * Specifying an empty string will suppress the insertion of the name into
309  * the logger.
310  ***************************************************************************/
311 inline
312 void GLog::name(const std::string& name)
313 {
314  m_name = name;
315  return;
316 }
317 
318 
319 /***********************************************************************//**
320  * @brief Set the buffer size
321  *
322  * @param[in] size Buffer size.
323  *
324  * Set the number of characters allowed in the internal buffer before the
325  * buffer is flushed into the specified streams or file.
326  ***************************************************************************/
327 inline
328 void GLog::buffer_size(const int& size)
329 {
330  m_max_length = size;
331  return;
332 }
333 
334 
335 /***********************************************************************//**
336  * @brief Set indentation
337  *
338  * @param[in] indent Number of indentation characters.
339  *
340  * Sets the number of whitespace characters that are added after the
341  * prefix and before the log message content for each line.
342  *
343  * Indentation allows for easy formatting of text in the log file.
344  ***************************************************************************/
345 inline
346 void GLog::indent(const int& indent)
347 {
348  m_indent = indent;
349  return;
350 }
351 
352 
353 /***********************************************************************//**
354  * @brief Set chattiness
355  *
356  * @param[in] chatter Chattiness.
357  *
358  * Sets the chattiness that is applied to print() methods that are piped into
359  * the logger. The following chattiness values are available:
360  * - SILENT: no logging will be performed
361  * - TERSE: minimum logging
362  * - NORMAL: normal level of logging
363  * - EXPLICIT: detailed information will be logged
364  * - VERBOSE: maximum information will be logged
365  ***************************************************************************/
366 inline
367 void GLog::chatter(const GChatter& chatter)
368 {
369  m_chatter = chatter;
370  return;
371 }
372 
373 
374 /***********************************************************************//**
375  * @brief Return date flag
376  *
377  * @return True if date flag is set.
378  *
379  * The date flag specifies whether each new line will be prepended by the
380  * actual date when the writing of the line was started.
381  ***************************************************************************/
382 inline
383 const bool& GLog::date(void) const
384 {
385  return m_use_date;
386 }
387 
388 
389 /***********************************************************************//**
390  * @brief Return standard output stream (cout) flag
391  *
392  * @return True if cout flag is set.
393  *
394  * The cout flag specifies whether the logging should go in addition to the
395  * log file also into the standard output stream.
396  ***************************************************************************/
397 inline
398 const bool& GLog::cout(void) const
399 {
400  return m_stdout;
401 }
402 
403 
404 /***********************************************************************//**
405  * @brief Return standard error stream (cerr) flag
406  *
407  * @return True if cerr flag is set.
408  *
409  * The cerr flag specifies whether the logging should go in addition to the
410  * log file also into the standard error stream.
411  ***************************************************************************/
412 inline
413 const bool& GLog::cerr(void) const
414 {
415  return m_stderr;
416 }
417 
418 
419 /***********************************************************************//**
420  * @brief Return prefix name
421  *
422  * @return Name to put into prefix.
423  *
424  * Returns the name that will be prefixed to each new line. If the name is
425  * empty, nothing will be prefixed.
426  ***************************************************************************/
427 inline
428 const std::string& GLog::name(void) const
429 {
430  return m_name;
431 }
432 
433 
434 /***********************************************************************//**
435  * @brief Return the buffer size
436  *
437  * @return Buffer size.
438  *
439  * Returns the number of characters that are allowed in the internal buffer
440  * before the buffer is flushed into the specified streams or file.
441  ***************************************************************************/
442 inline
443 const int& GLog::buffer_size(void) const
444 {
445  return m_max_length;
446 }
447 
448 
449 /***********************************************************************//**
450  * @brief Return indentation
451  *
452  * @return Number of indentation characters.
453  *
454  * Returns the number of whitespace characters that are added after the
455  * prefix and before the log message content for each line.
456  ***************************************************************************/
457 inline
458 const int& GLog::indent(void) const
459 {
460  return m_indent;
461 }
462 
463 
464 /***********************************************************************//**
465  * @brief Return chattiness
466  *
467  * @return Chattiness.
468  *
469  * Returns the chattiness that is applied to print() methods that are piped
470  * into the logger. The following chattiness values are available:
471  * - SILENT: no logging will be performed
472  * - TERSE: minimum logging
473  * - NORMAL: normal level of logging
474  * - EXPLICIT: detailed information will be logged
475  * - VERBOSE: maximum information will be logged
476  ***************************************************************************/
477 inline
478 const GChatter& GLog::chatter(void) const
479 {
480  return m_chatter;
481 }
482 
483 
484 /***********************************************************************//**
485  * @brief Return log filename
486  *
487  * @return Log filename.
488  *
489  * Returns the filename of the log file.
490  ***************************************************************************/
491 inline
492 const GFilename& GLog::filename(void) const
493 {
494  return m_filename;
495 }
496 
497 
498 /***********************************************************************//**
499  * @brief Return logger buffer
500  *
501  * @return Logger buffer.
502  *
503  * Returns the buffer of the logger as string.
504  ***************************************************************************/
505 inline
506 const std::string& GLog::buffer(void) const
507 {
508  return m_buffer;
509 }
510 
511 #endif /* GLOG_HPP */
const bool & date(void) const
Return date flag.
Definition: GLog.hpp:383
const GFilename & filename(void) const
Return log filename.
Definition: GLog.hpp:492
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
const GChatter & chatter(void) const
Return chattiness.
Definition: GLog.hpp:478
void header2(const std::string &arg)
Write string as header framed by &#39;-&#39; characters into logger.
Definition: GLog.hpp:229
GLog & operator=(const GLog &log)
Assignment operator.
Definition: GLog.cpp:139
bool is_open(void) const
Signal that log file is open.
Definition: GLog.hpp:173
void free_members(void)
Delete class members.
Definition: GLog.cpp:640
int m_indent
Indentation of text.
Definition: GLog.hpp:129
long int m_written_size
Number of char&#39;s written though this logger.
Definition: GLog.hpp:130
void header3(const std::string &arg)
Write string as header enclosed by &#39;===&#39; into logger.
Definition: GLog.hpp:244
const bool & cout(void) const
Return standard output stream (cout) flag.
Definition: GLog.hpp:398
int m_max_length
Maximum buffer length.
Definition: GLog.hpp:128
const std::string & name(void) const
Return prefix name.
Definition: GLog.hpp:428
const int & buffer_size(void) const
Return the buffer size.
Definition: GLog.hpp:443
Information logger interface definition.
Definition: GLog.hpp:62
std::string m_buffer
Output string buffer.
Definition: GLog.hpp:138
bool is_empty(void) const
Signal if log file is empty.
Definition: GLog.hpp:185
const bool & cerr(void) const
Return standard error stream (cerr) flag.
Definition: GLog.hpp:413
std::string classname(void) const
Return class name.
Definition: GLog.hpp:149
Filename class.
Definition: GFilename.hpp:62
void header(const std::string &arg, int level)
Write string as header into logger.
Definition: GLog.cpp:725
void copy_members(const GLog &log)
Copy class members.
Definition: GLog.cpp:600
std::string strdate(void) const
Return current date.
Definition: GLog.cpp:772
GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition: GVector.cpp:1184
void close(void)
Close log file.
Definition: GLog.cpp:538
void flush(const bool &force=false)
Flush string buffer into log file.
Definition: GLog.cpp:665
long int size(void) const
Return the number of characters in the log file.
Definition: GLog.cpp:474
GChatter
Definition: GTypemaps.hpp:33
void operator()(const char *msgFormat,...)
Message logging operator.
Definition: GLog.cpp:175
void append(const std::string &string)
Append string to the buffer.
Definition: GLog.cpp:845
bool m_linestart
Signals that buffer is at line start.
Definition: GLog.hpp:134
const int & indent(void) const
Return indentation.
Definition: GLog.hpp:458
virtual ~GLog(void)
Destructor.
Definition: GLog.cpp:118
GLog & operator<<(GLog &log)
Insert logger into logger.
Definition: GLog.cpp:204
void init_members(void)
Initialise class members.
Definition: GLog.cpp:569
std::string m_name
Name for prefix.
Definition: GLog.hpp:137
FILE * m_file
Log file pointer.
Definition: GLog.hpp:135
Definition of GammaLib typemaps.
const std::string & buffer(void) const
Return logger buffer.
Definition: GLog.hpp:506
bool m_use_date
Dump date in prefix.
Definition: GLog.hpp:133
long int written_size(void) const
Return number of characters written through this logger.
Definition: GLog.hpp:161
GChatter m_chatter
Chattiness for print() method.
Definition: GLog.hpp:139
bool m_stderr
Dump in standard error.
Definition: GLog.hpp:132
std::string prefix(void) const
Return prefix.
Definition: GLog.cpp:809
bool m_stdout
Dump in standard output.
Definition: GLog.hpp:131
GFilename m_filename
Log file name.
Definition: GLog.hpp:136
Filename class interface definition.
GLog(void)
Void constructor.
Definition: GLog.cpp:66
void header0(const std::string &arg)
Write string as centred header into logger.
Definition: GLog.hpp:199
void clear(void)
Clear object.
Definition: GLog.cpp:456
std::string str(const unsigned short int &value)
Convert unsigned short integer value into string.
Definition: GTools.cpp:413