VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
glog.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 private:
74  void flush() ;
75  bool active() ;
76 
77 private:
78  friend class G::Log::Line ;
79  Severity m_severity ;
80  std::ostringstream m_ss ;
81  const char * m_file ;
82  int m_line ;
83 } ;
84 
85 /// The debug macro is for debugging during development. The log macro
86 /// is used for progress logging, typically in long-lived server processes.
87 /// The warning and error macros are used for error warning/error messages.
88 /// In programs where logging can be disabled completely (see LogOutput)
89 /// then warning/error messages should also get raised by some another
90 /// independent means.
91 ///
92 #define G_LOG_OUTPUT( expr , severity ) do { if(G::Log::at(severity)) G::Log(severity,__FILE__,__LINE__) << expr ; } while(0)
93 #if defined(G_WITH_DEBUG) || ( defined(_DEBUG) && ! defined(G_NO_DEBUG) )
94 #define G_DEBUG( expr ) G_LOG_OUTPUT( expr , G::Log::s_Debug )
95 #else
96 #define G_DEBUG( expr )
97 #endif
98 #if ! defined(G_NO_LOG)
99 #define G_LOG( expr ) G_LOG_OUTPUT( expr , G::Log::s_LogVerbose )
100 #else
101 #define G_LOG( expr )
102 #endif
103 #if ! defined(G_NO_LOG_S)
104 #define G_LOG_S( expr ) G_LOG_OUTPUT( expr , G::Log::s_LogSummary )
105 #else
106 #define G_LOG_S( expr )
107 #endif
108 #if ! defined(G_NO_WARNING)
109 #define G_WARNING( expr ) G_LOG_OUTPUT( expr , G::Log::s_Warning )
110 #define G_WARNING_ONCE( expr ) do { static bool warned=false; if(!warned) G_LOG_OUTPUT(expr,G::Log::s_Warning); warned=true; } while(0)
111 #else
112 #define G_WARNING( expr )
113 #define G_WARNING_ONCE( expr )
114 #endif
115 #if ! defined(G_NO_ERROR)
116 #define G_ERROR( expr ) G_LOG_OUTPUT( expr , G::Log::s_Error )
117 #else
118 #define G_ERROR( expr )
119 #endif
120 
121 #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
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:73
~Log()
Destructor. Writes the accumulated string to the log output.
Definition: glog.cpp:32