E-MailRelay
glog.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file glog.h
19 ///
20 
21 #ifndef G_LOG_H
22 #define G_LOG_H
23 
24 #include "gdef.h"
25 #include <sstream>
26 #include <string>
27 
28 namespace G
29 {
30  class Log ;
31 }
32 
33 /// \class G::Log
34 /// A class for doing iostream-based logging. The G_LOG/G_DEBUG/G_WARNING/G_ERROR
35 /// macros are provided as a convenient way of using this interface.
36 ///
37 /// Usage:
38 /// \code
39 /// G::Log(G::Log::s_LogSummary,__FILE__,__LINE__) << a << b ;
40 /// \endcode
41 /// or
42 /// \code
43 /// G_LOG( a << b ) ;
44 /// \endcode
45 ///
46 /// \see G::LogOutput
47 ///
48 class G::Log
49 {
50 public:
51  enum Severity { s_LogVerbose , s_LogSummary , s_Debug , s_Warning , s_Error , s_Assertion } ;
52 
53  class Line /// A class for adding line number information to the Log output.
54  { public: const char * m_file ; int m_line ; Line( const char *file , int line ) : m_file(file) , m_line(line) {} } ;
55 
56  Log( Severity , const char * file , int line ) ;
57  ///< Constructor.
58 
59  ~Log() ;
60  ///< Destructor. Writes the accumulated string to the log output.
61 
62  std::ostream & operator<<( const char * s ) ;
63  ///< Streams 's' and then returns a stream for streaming more stuff into.
64 
65  std::ostream & operator<<( const std::string & s ) ;
66  ///< Streams 's' and then returns a stream for streaming more stuff into.
67 
68  static bool at( Severity ) ;
69  ///< Returns true if G::LogOutput::output() would log at the given level.
70  ///< This can be used as an optimisation to short-ciruit the stream-out
71  ///< expression evaluation.
72 
73  static bool at( Severity , const char * group ) ;
74  ///< An overload that adds a logging group name to the test.
75 
76 private:
77  void flush() ;
78  bool active() ;
79 
80 private:
81  friend class G::Log::Line ;
82  Severity m_severity ;
83  std::ostringstream m_ss ;
84  const char * m_file ;
85  int m_line ;
86 } ;
87 
88 /// The debug macro is for debugging during development. The log macro
89 /// is used for progress logging, typically in long-lived server processes.
90 /// The warning and error macros are used for error warning/error messages.
91 /// In programs where logging can be disabled completely (see LogOutput)
92 /// then warning/error messages should also get raised by some another
93 /// independent means.
94 ///
95 #define G_LOG_IMP( expr , severity ) do { if(G::Log::at(severity)) G::Log(severity,__FILE__,__LINE__) << expr ; } while(0)
96 #define G_LOG_IMP_IF( cond , expr , severity ) do { if(G::Log::at(severity)&&(cond)) G::Log(severity,__FILE__,__LINE__) << expr ; } while(0)
97 #define G_LOG_IMP_GROUP( grp , expr , severity ) do { if(G::Log::at(severity,grp)) G::Log(severity,__FILE__,__LINE__) << expr ; } while(0)
98 #define G_LOG_IMP_ONCE( expr , severity ) do { static bool done__ = false ; if(!done__) G::Log(severity,__FILE__,__LINE__) << expr ; done__ = true ; } while(0)
99 
100 #if defined(G_WITH_DEBUG) || ( defined(_DEBUG) && ! defined(G_NO_DEBUG) )
101 #define G_DEBUG( expr ) G_LOG_IMP( expr , G::Log::s_Debug )
102 #define G_DEBUG_IF( cond , expr ) G_LOG_IMP_IF( cond , expr , G::Log::s_Debug )
103 #define G_DEBUG_GROUP( group , expr ) G_LOG_IMP_GROUP( group , expr , G::Log::s_Debug )
104 #define G_DEBUG_ONCE( expr ) G_LOG_IMP_ONCE( expr , G::Log::s_Debug )
105 #else
106 #define G_DEBUG( expr )
107 #define G_DEBUG_IF( cond , expr )
108 #define G_DEBUG_GROUP( group , expr )
109 #define G_DEBUG_ONCE( group , expr )
110 #endif
111 
112 #if ! defined(G_NO_LOG)
113 #define G_LOG( expr ) G_LOG_IMP( expr , G::Log::s_LogVerbose )
114 #define G_LOG_IF( cond , expr ) G_LOG_IMP_IF( cond , expr , G::Log::s_LogVerbose )
115 #define G_LOG_GROUP( grp , expr ) G_LOG_IMP_GROUP( grp , expr , G::Log::s_LogVerbose )
116 #define G_LOG_ONCE( expr ) G_LOG_IMP_ONCE( cond , expr , G::Log::s_LogVerbose )
117 #else
118 #define G_LOG( expr )
119 #define G_LOG_IF( cond , expr )
120 #define G_LOG_GROUP( grp , expr )
121 #define G_LOG_ONCE( expr )
122 #endif
123 
124 #if ! defined(G_NO_LOG_S)
125 #define G_LOG_S( expr ) G_LOG_IMP( expr , G::Log::s_LogSummary )
126 #else
127 #define G_LOG_S( expr )
128 #endif
129 
130 #if ! defined(G_NO_WARNING)
131 #define G_WARNING( expr ) G_LOG_IMP( expr , G::Log::s_Warning )
132 #define G_WARNING_ONCE( expr ) G_LOG_IMP_ONCE( expr , G::Log::s_Warning )
133 #else
134 #define G_WARNING( expr )
135 #define G_WARNING_ONCE( expr )
136 #endif
137 
138 #if ! defined(G_NO_ERROR)
139 #define G_ERROR( expr ) G_LOG_IMP( expr , G::Log::s_Error )
140 #else
141 #define G_ERROR( expr )
142 #endif
143 
144 #endif
A class for adding line number information to the Log output.
Definition: glog.h:53
static bool at(Severity)
Returns true if G::LogOutput::output() would log at the given level.
Definition: glog.cpp:43
Log(Severity, const char *file, int line)
Constructor.
Definition: glog.cpp:25
Low-level classes.
Definition: garg.h:29
A class for doing iostream-based logging.
Definition: glog.h:48
std::ostream & operator<<(const char *s)
Streams 's' and then returns a stream for streaming more stuff into.
Definition: glog.cpp:79
~Log()
Destructor. Writes the accumulated string to the log output.
Definition: glog.cpp:32