E-MailRelay
glog.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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
28namespace 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::Severity::s_InfoSummary,__FILE__,__LINE__) << a << b ;
40/// \endcode
41/// or
42/// \code
43/// G_LOG( a << b ) ;
44/// \endcode
45///
46/// \see G::LogOutput
47///
48class G::Log
49{
50public:
51 enum class Severity { s_InfoVerbose , s_InfoSummary , s_Debug , s_Warning , s_Error , s_Assertion } ;
52
53 Log( Severity , const char * file , int line ) ;
54 ///< Constructor.
55
56 ~Log() ;
57 ///< Destructor. Writes the accumulated string to the log output.
58
59 std::ostream & operator<<( const char * s ) ;
60 ///< Streams 's' and then returns a stream for streaming more stuff into.
61
62 std::ostream & operator<<( const std::string & s ) ;
63 ///< Streams 's' and then returns a stream for streaming more stuff into.
64
65 static bool at( Severity ) ;
66 ///< Returns true if G::LogOutput::output() would log at the given level.
67 ///< This can be used as an optimisation to short-ciruit the stream-out
68 ///< expression evaluation.
69
70 static bool at( Severity , const char * group ) ;
71 ///< An overload that adds a logging group name to the test.
72
73public:
74 Log( const Log & ) = delete ;
75 Log( Log && ) = delete ;
76 void operator=( const Log & ) = delete ;
77 void operator=( Log && ) = delete ;
78
79private:
80 void flush() ;
81
82private:
83 Severity m_severity ;
84 const char * m_file ;
85 int m_line ;
86 std::ostream & m_ostream ;
87} ;
88
89/// The DEBUG macro is for debugging during development, the LOG macro
90/// generates informational logging in verbose mode only, the 'summary'
91/// LOG_S macro generates informational logging even when not verbose,
92/// and the WARNING and ERROR macros are used for error warning/error
93/// messages although in programs where logging can be disabled completely (see
94/// G::LogOutput) error conditions should be made visible by some other means
95/// (such as stderr).
96///
97#define G_LOG_IMP( expr , severity ) do { try { if(G::Log::at(severity)) G::Log((severity),__FILE__,__LINE__) << expr ; } catch(...) {} } while(0)
98#define G_LOG_IMP_IF( cond , expr , severity ) do { try { if(G::Log::at(severity)&&(cond)) G::Log((severity),__FILE__,__LINE__) << expr ; } catch(...) {} } while(0)
99#define G_LOG_IMP_ONCE( expr , severity ) do { static bool done__ = false ; try { if(!done__) G::Log((severity),__FILE__,__LINE__) << expr ; } catch(...) {} done__ = true ; } while(0)
100
101#if defined(G_WITH_DEBUG) || ( defined(_DEBUG) && ! defined(G_NO_DEBUG) )
102#define G_DEBUG( expr ) G_LOG_IMP( expr , G::Log::Severity::s_Debug )
103#define G_DEBUG_IF( cond , expr ) G_LOG_IMP_IF( cond , expr , G::Log::Severity::s_Debug )
104#define G_DEBUG_ONCE( expr ) G_LOG_IMP_ONCE( expr , G::Log::Severity::s_Debug )
105#else
106#define G_DEBUG( expr )
107#define G_DEBUG_IF( cond , expr )
108#define G_DEBUG_ONCE( group , expr )
109#endif
110
111#if ! defined(G_NO_LOG)
112#define G_LOG( expr ) G_LOG_IMP( expr , G::Log::Severity::s_InfoVerbose )
113#define G_LOG_IF( cond , expr ) G_LOG_IMP_IF( cond , expr , G::Log::Severity::s_InfoVerbose )
114#define G_LOG_ONCE( expr ) G_LOG_IMP_ONCE( expr , G::Log::Severity::s_InfoVerbose )
115#else
116#define G_LOG( expr )
117#define G_LOG_IF( cond , expr )
118#define G_LOG_ONCE( expr )
119#endif
120
121#if ! defined(G_NO_LOG_S)
122#define G_LOG_S( expr ) G_LOG_IMP( expr , G::Log::Severity::s_InfoSummary )
123#define G_LOG_S_IF( cond , expr ) G_LOG_IMP_IF( cond , expr , G::Log::Severity::s_InfoSummary )
124#define G_LOG_S_ONCE( expr ) G_LOG_IMP_ONCE( expr , G::Log::Severity::s_InfoSummary )
125#else
126#define G_LOG_S( expr )
127#define G_LOG_S_IF( cond , expr )
128#define G_LOG_S_ONCE( expr )
129#endif
130
131#if ! defined(G_NO_WARNING)
132#define G_WARNING( expr ) G_LOG_IMP( expr , G::Log::Severity::s_Warning )
133#define G_WARNING_ONCE( expr ) G_LOG_IMP_ONCE( expr , G::Log::Severity::s_Warning )
134#else
135#define G_WARNING( expr )
136#define G_WARNING_ONCE( expr )
137#endif
138
139#if ! defined(G_NO_ERROR)
140#define G_ERROR( expr ) G_LOG_IMP( expr , G::Log::Severity::s_Error )
141#else
142#define G_ERROR( expr )
143#endif
144
145#endif
A class for doing iostream-based logging.
Definition: glog.h:49
~Log()
Destructor. Writes the accumulated string to the log output.
Definition: glog.cpp:33
std::ostream & operator<<(const char *s)
Streams 's' and then returns a stream for streaming more stuff into.
Definition: glog.cpp:50
static bool at(Severity, const char *group)
An overload that adds a logging group name to the test.
Log(Severity, const char *file, int line)
Constructor.
Definition: glog.cpp:25
static bool at(Severity)
Returns true if G::LogOutput::output() would log at the given level.
Definition: glog.cpp:44
Low-level classes.
Definition: galign.h:28