GammaLib  2.1.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-2022 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 prefix(void) const;
124  void append(const std::string& string);
125 
126  // Protected data members
127  int m_max_length; //!< Maximum buffer length
128  int m_indent; //!< Indentation of text
129  long int m_written_size; //!< Number of char's written though this logger
130  bool m_stdout; //!< Dump in standard output
131  bool m_stderr; //!< Dump in standard error
132  bool m_use_date; //!< Dump date in prefix
133  bool m_linestart; //!< Signals that buffer is at line start
134  FILE* m_file; //!< Log file pointer
135  GFilename m_filename; //!< Log file name
136  std::string m_name; //!< Name for prefix
137  std::string m_buffer; //!< Output string buffer
138  GChatter m_chatter; //!< Chattiness for print() method
139 };
140 
141 
142 /***********************************************************************//**
143  * @brief Return class name
144  *
145  * @return String containing the class name ("GLog").
146  ***************************************************************************/
147 inline
148 std::string GLog::classname(void) const
149 {
150  return ("GLog");
151 }
152 
153 
154 /***********************************************************************//**
155  * @brief Return number of characters written through this logger
156  *
157  * @return Number of characters written.
158  ***************************************************************************/
159 inline
160 long int GLog::written_size(void) const
161 {
162  return (m_written_size);
163 }
164 
165 
166 /***********************************************************************//**
167  * @brief Signal that log file is open
168  *
169  * @return True if log file is open, false otherwise.
170  ***************************************************************************/
171 inline
172 bool GLog::is_open(void) const
173 {
174  return (m_file != NULL);
175 }
176 
177 
178 /***********************************************************************//**
179  * @brief Signal if log file is empty
180  *
181  * @return True if log file is empty, false otherwise.
182  ***************************************************************************/
183 inline
184 bool GLog::is_empty(void) const
185 {
186  return (size() == 0);
187 }
188 
189 
190 /***********************************************************************//**
191  * @brief Write string as centred header into logger
192  *
193  * @param[in] arg Header string.
194  *
195  * Writes a string as 80 character wide centred header.
196  ***************************************************************************/
197 inline
198 void GLog::header0(const std::string& arg)
199 {
200  header(arg, 0);
201  return;
202 }
203 
204 
205 /***********************************************************************//**
206  * @brief Write string as header framed by '=' characters into logger
207  *
208  * @param[in] arg Header string.
209  *
210  * Writes a string as header framed by '=' characters into logger.
211  ***************************************************************************/
212 inline
213 void GLog::header1(const std::string& arg)
214 {
215  header(arg, 1);
216  return;
217 }
218 
219 
220 /***********************************************************************//**
221  * @brief Write string as header framed by '-' characters into logger
222  *
223  * @param[in] arg Header string.
224  *
225  * Writes a string as header framed by '-' characters into logger.
226  ***************************************************************************/
227 inline
228 void GLog::header2(const std::string& arg)
229 {
230  header(arg, 2);
231  return;
232 }
233 
234 
235 /***********************************************************************//**
236  * @brief Write string as header enclosed by '===' into logger
237  *
238  * @param[in] arg Header string.
239  *
240  * Writes a string as header enclosed by '===' into logger.
241  ***************************************************************************/
242 inline
243 void GLog::header3(const std::string& arg)
244 {
245  header(arg, 3);
246  return;
247 }
248 
249 
250 /***********************************************************************//**
251  * @brief Set date flag that controls date prefixing
252  *
253  * @param[in] flag Enable/disable date flag (true/false).
254  *
255  * The date flag specifies whether each new line will be prepended by the
256  * actual date when the writing of the line was started.
257  ***************************************************************************/
258 inline
259 void GLog::date(const bool& flag)
260 {
261  m_use_date = flag;
262  return;
263 }
264 
265 
266 /***********************************************************************//**
267  * @brief Set standard output stream (cout) flag
268  *
269  * @param[in] flag Enable/disable logging (true/false).
270  *
271  * The cout flag specifies whether the logging should go in addition to the
272  * log file also into the standard output stream.
273  ***************************************************************************/
274 inline
275 void GLog::cout(const bool& flag)
276 {
277  m_stdout = flag;
278  return;
279 }
280 
281 
282 /***********************************************************************//**
283  * @brief Enables/disables logging into standard error stream
284  *
285  * @param[in] flag Enable/disable logging (true/false).
286  *
287  * The cerr flag specifies whether the logging should go in addition to the
288  * log file also into the standard error stream.
289  ***************************************************************************/
290 inline
291 void GLog::cerr(const bool& flag)
292 {
293  m_stderr = flag;
294  return;
295 }
296 
297 
298 /***********************************************************************//**
299  * @brief Set name to put into prefix
300  *
301  * @param[in] name Name to put into prefix.
302  *
303  * Sets the @p name that will be prefixed to each new line. If the date flag
304  * is true, the @p name will be inserted after the date at the beginning of
305  * each line.
306  *
307  * Specifying an empty string will suppress the insertion of the name into
308  * the logger.
309  ***************************************************************************/
310 inline
311 void GLog::name(const std::string& name)
312 {
313  m_name = name;
314  return;
315 }
316 
317 
318 /***********************************************************************//**
319  * @brief Set the buffer size
320  *
321  * @param[in] size Buffer size.
322  *
323  * Set the number of characters allowed in the internal buffer before the
324  * buffer is flushed into the specified streams or file.
325  ***************************************************************************/
326 inline
327 void GLog::buffer_size(const int& size)
328 {
329  m_max_length = size;
330  return;
331 }
332 
333 
334 /***********************************************************************//**
335  * @brief Set indentation
336  *
337  * @param[in] indent Number of indentation characters.
338  *
339  * Sets the number of whitespace characters that are added after the
340  * prefix and before the log message content for each line.
341  *
342  * Indentation allows for easy formatting of text in the log file.
343  ***************************************************************************/
344 inline
345 void GLog::indent(const int& indent)
346 {
347  m_indent = indent;
348  return;
349 }
350 
351 
352 /***********************************************************************//**
353  * @brief Set chattiness
354  *
355  * @param[in] chatter Chattiness.
356  *
357  * Sets the chattiness that is applied to print() methods that are piped into
358  * the logger. The following chattiness values are available:
359  * - SILENT: no logging will be performed
360  * - TERSE: minimum logging
361  * - NORMAL: normal level of logging
362  * - EXPLICIT: detailed information will be logged
363  * - VERBOSE: maximum information will be logged
364  ***************************************************************************/
365 inline
366 void GLog::chatter(const GChatter& chatter)
367 {
368  m_chatter = chatter;
369  return;
370 }
371 
372 
373 /***********************************************************************//**
374  * @brief Return date flag
375  *
376  * @return True if date flag is set.
377  *
378  * The date flag specifies whether each new line will be prepended by the
379  * actual date when the writing of the line was started.
380  ***************************************************************************/
381 inline
382 const bool& GLog::date(void) const
383 {
384  return m_use_date;
385 }
386 
387 
388 /***********************************************************************//**
389  * @brief Return standard output stream (cout) flag
390  *
391  * @return True if cout flag is set.
392  *
393  * The cout flag specifies whether the logging should go in addition to the
394  * log file also into the standard output stream.
395  ***************************************************************************/
396 inline
397 const bool& GLog::cout(void) const
398 {
399  return m_stdout;
400 }
401 
402 
403 /***********************************************************************//**
404  * @brief Return standard error stream (cerr) flag
405  *
406  * @return True if cerr flag is set.
407  *
408  * The cerr flag specifies whether the logging should go in addition to the
409  * log file also into the standard error stream.
410  ***************************************************************************/
411 inline
412 const bool& GLog::cerr(void) const
413 {
414  return m_stderr;
415 }
416 
417 
418 /***********************************************************************//**
419  * @brief Return prefix name
420  *
421  * @return Name to put into prefix.
422  *
423  * Returns the name that will be prefixed to each new line. If the name is
424  * empty, nothing will be prefixed.
425  ***************************************************************************/
426 inline
427 const std::string& GLog::name(void) const
428 {
429  return m_name;
430 }
431 
432 
433 /***********************************************************************//**
434  * @brief Return the buffer size
435  *
436  * @return Buffer size.
437  *
438  * Returns the number of characters that are allowed in the internal buffer
439  * before the buffer is flushed into the specified streams or file.
440  ***************************************************************************/
441 inline
442 const int& GLog::buffer_size(void) const
443 {
444  return m_max_length;
445 }
446 
447 
448 /***********************************************************************//**
449  * @brief Return indentation
450  *
451  * @return Number of indentation characters.
452  *
453  * Returns the number of whitespace characters that are added after the
454  * prefix and before the log message content for each line.
455  ***************************************************************************/
456 inline
457 const int& GLog::indent(void) const
458 {
459  return m_indent;
460 }
461 
462 
463 /***********************************************************************//**
464  * @brief Return chattiness
465  *
466  * @return Chattiness.
467  *
468  * Returns the chattiness that is applied to print() methods that are piped
469  * into the logger. The following chattiness values are available:
470  * - SILENT: no logging will be performed
471  * - TERSE: minimum logging
472  * - NORMAL: normal level of logging
473  * - EXPLICIT: detailed information will be logged
474  * - VERBOSE: maximum information will be logged
475  ***************************************************************************/
476 inline
477 const GChatter& GLog::chatter(void) const
478 {
479  return m_chatter;
480 }
481 
482 
483 /***********************************************************************//**
484  * @brief Return log filename
485  *
486  * @return Log filename.
487  *
488  * Returns the filename of the log file.
489  ***************************************************************************/
490 inline
491 const GFilename& GLog::filename(void) const
492 {
493  return m_filename;
494 }
495 
496 
497 /***********************************************************************//**
498  * @brief Return logger buffer
499  *
500  * @return Logger buffer.
501  *
502  * Returns the buffer of the logger as string.
503  ***************************************************************************/
504 inline
505 const std::string& GLog::buffer(void) const
506 {
507  return m_buffer;
508 }
509 
510 #endif /* GLOG_HPP */
const bool & date(void) const
Return date flag.
Definition: GLog.hpp:382
const GFilename & filename(void) const
Return log filename.
Definition: GLog.hpp:491
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:213
const GChatter & chatter(void) const
Return chattiness.
Definition: GLog.hpp:477
void header2(const std::string &arg)
Write string as header framed by &#39;-&#39; characters into logger.
Definition: GLog.hpp:228
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:172
void free_members(void)
Delete class members.
Definition: GLog.cpp:640
int m_indent
Indentation of text.
Definition: GLog.hpp:128
long int m_written_size
Number of char&#39;s written though this logger.
Definition: GLog.hpp:129
void header3(const std::string &arg)
Write string as header enclosed by &#39;===&#39; into logger.
Definition: GLog.hpp:243
const bool & cout(void) const
Return standard output stream (cout) flag.
Definition: GLog.hpp:397
int m_max_length
Maximum buffer length.
Definition: GLog.hpp:127
const std::string & name(void) const
Return prefix name.
Definition: GLog.hpp:427
const int & buffer_size(void) const
Return the buffer size.
Definition: GLog.hpp:442
Information logger interface definition.
Definition: GLog.hpp:62
std::string m_buffer
Output string buffer.
Definition: GLog.hpp:137
bool is_empty(void) const
Signal if log file is empty.
Definition: GLog.hpp:184
const bool & cerr(void) const
Return standard error stream (cerr) flag.
Definition: GLog.hpp:412
std::string classname(void) const
Return class name.
Definition: GLog.hpp:148
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
GVector log(const GVector &vector)
Computes natural logarithm of vector elements.
Definition: GVector.cpp:1274
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:808
bool m_linestart
Signals that buffer is at line start.
Definition: GLog.hpp:133
const int & indent(void) const
Return indentation.
Definition: GLog.hpp:457
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:136
FILE * m_file
Log file pointer.
Definition: GLog.hpp:134
Definition of GammaLib typemaps.
const std::string & buffer(void) const
Return logger buffer.
Definition: GLog.hpp:505
bool m_use_date
Dump date in prefix.
Definition: GLog.hpp:132
long int written_size(void) const
Return number of characters written through this logger.
Definition: GLog.hpp:160
GChatter m_chatter
Chattiness for print() method.
Definition: GLog.hpp:138
bool m_stderr
Dump in standard error.
Definition: GLog.hpp:131
std::string prefix(void) const
Return prefix.
Definition: GLog.cpp:772
bool m_stdout
Dump in standard output.
Definition: GLog.hpp:130
GFilename m_filename
Log file name.
Definition: GLog.hpp:135
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:198
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:489