E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

glogoutput.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 glogoutput.h
19 ///
20
21 #ifndef G_LOG_OUTPUT_H
22 #define G_LOG_OUTPUT_H
23
24 #include "gdef.h"
25 #include "glog.h"
26 #include <string>
27 #include <vector>
28 #include <set>
29 #include <ctime>
30
31 namespace G
32 {
33 class LogOutput ;
34 }
35
36 /// \class G::LogOutput
37 /// Controls and implements low-level logging output, as used by the Log
38 /// interface. Applications should instantiate a LogOutput object in main() to
39 /// enable log output.
40 /// \see G::Log
41 ///
42 class G::LogOutput
43 {
44 public:
45 enum SyslogFacility { User , Daemon , Mail , Cron } ; // etc.
46
47 LogOutput( const std::string & prefix , bool output , bool summary_logging ,
48 bool verbose_logging , bool with_debug , bool with_level ,
49 bool with_timestamp , bool strip_context , bool use_syslog ,
50 const std::string & stderr_replacement = std::string() ,
51 SyslogFacility syslog_facility = User ) ;
52 ///< Constructor. If there is no LogOutput object, or if 'output'
53 ///< is false, then there is no output of any sort. Otherwise at
54 ///< least warning and error messages are generated.
55 ///<
56 ///< If 'summary_logging' is true then log-summary messages
57 ///< are output. If 'verbose_logging' is true then log-verbose
58 ///< messages are output. If 'with_debug' is true then debug
59 ///< messages will also be generated (but only if compiled in).
60 ///<
61 ///< More than one LogOutput object may be created, but only
62 ///< the first one controls output.
63
64 explicit LogOutput( bool output_with_logging , bool verbose_and_debug = true ,
65 const std::string & stderr_replacement = std::string() ) ;
66 ///< Constructor for test programs. Only generates output if the
67 ///< first parameter is true. Never uses syslog.
68
69 virtual ~LogOutput() ;
70 ///< Destructor.
71
72 virtual void rawOutput( std::ostream & , G::Log::Severity , const std::string & ) ;
73 ///< Overridable. Used to do the final message
74 ///< output (with OutputDebugString() or stderr).
75
76 static LogOutput * instance() ;
77 ///< Returns a pointer to the controlling LogOutput object. Returns
78 ///< nullptr if none.
79
80 bool enable( bool enabled = true ) ;
81 ///< Enables or disables output. Returns the previous setting.
82
83 void quiet( bool quiet_stderr = true ) ;
84 ///< Enables or disables quiet-stderr mode, intended for daemons
85 ///< that log to syslog and only want startup errors and warnings
86 ///< on stderr.
87
88 void verbose( bool verbose_log = true ) ;
89 ///< Enables or disables verbose logging.
90
91 bool at( G::Log::Severity ) const ;
92 ///< Returns true if logging should occur for the given severity level.
93
94 bool at( G::Log::Severity , const std::string & group ) const ;
95 ///< Returns true if logging should occur for the given severity level
96 ///< and logging group.
97
98 static void groups( const std::string & ) ;
99 ///< Sets the list of active logging groups. The string is a comma-separated
100 ///< list of "log-whatever" names, with "log-all" having the obvious meaning.
101 ///< Names that do not start with "log-" are ignored. The default set of
102 ///< groups is taken from an environment variable on construction. Does
103 ///< nothing if there is no instance().
104
105 bool syslog() const ;
106 ///< Returns true if syslog output is enabled.
107
108 static void output( G::Log::Severity , const char * file , int line , const std::string & ) ;
109 ///< Generates output if there is an existing LogOutput object
110 ///< which is enabled. Uses rawOutput().
111
112 static void assertion( const char * file , int line , bool test , const char * ) ;
113 ///< Makes an assertion check (regardless of any LogOutput
114 ///< object). Calls output() if the 'file' parameter is not null.
115
116 virtual void onAssert() ;
117 ///< Called during an assertion failure. This allows Windows
118 ///< applications to stop timers etc. (Timers can cause reentrancy
119 ///< problems and infinitely recursive dialog box creation.)
120
121 static void register_( const std::string & exe ) ;
122 ///< Registers the given executable as a source of logging.
123 ///< This is called from init(), but it might also need to be
124 ///< done as an installation task with the necessary
125 ///< permissions.
126
127 private:
128 typedef size_t size_type ;
129 LogOutput( const LogOutput & ) ; // not implemented
130 void operator=( const LogOutput & ) ; // not implemented
131 void init() ;
132 void cleanup() ;
133 void appendTimestampStringTo( std::string & ) ;
134 static std::string dateString() ;
135 void doOutput( G::Log::Severity , const std::string & ) ;
136 void doOutput( G::Log::Severity s , const char * , int , const std::string & ) ;
137 void doAssertion( const char * , int , const std::string & ) ;
138 static const char * levelString( Log::Severity s ) ;
139 static std::string itoa( int ) ;
140 static std::string fileAndLine( const char * , int ) ;
141 static void halt() ;
142 static LogOutput * & pthis() ;
143 static std::ostream & err( const std::string & ) ;
144 static void getLocalTime( time_t , struct std::tm * ) ; // dont make logging dependent on G::DateTime
145
146 private:
147 std::string m_prefix ;
148 bool m_enabled ;
149 bool m_summary_log ;
150 bool m_verbose_log ;
151 bool m_quiet ;
152 bool m_debug ;
153 bool m_level ;
154 bool m_strip ;
155 bool m_syslog ;
156 std::set<std::string> m_groups ;
157 std::ostream & m_std_err ;
158 SyslogFacility m_facility ;
159 time_t m_time ;
160 std::vector<char> m_time_buffer ;
161 bool m_timestamp ;
162 HANDLE m_handle ; // windows
163 bool m_handle_set ;
164 std::string m_buffer ;
165 } ;
166
167 #endif
G::LogOutput::quiet
void quiet(bool quiet_stderr=true)
Enables or disables quiet-stderr mode, intended for daemons that log to syslog and only want startup ...
Definition: glogoutput.cpp:126
G::LogOutput::LogOutput
LogOutput(const std::string &prefix, bool output, bool summary_logging, bool verbose_logging, bool with_debug, bool with_level, bool with_timestamp, bool strip_context, bool use_syslog, const std::string &stderr_replacement=std::string(), SyslogFacility syslog_facility=User)
Constructor.
Definition: glogoutput.cpp:34
G::LogOutput::syslog
bool syslog() const
Returns true if syslog output is enabled.
Definition: glogoutput.cpp:336
G::LogOutput::enable
bool enable(bool enabled=true)
Enables or disables output. Returns the previous setting.
Definition: glogoutput.cpp:119
glog.h
G::LogOutput::instance
static LogOutput * instance()
Returns a pointer to the controlling LogOutput object.
Definition: glogoutput.cpp:114
G::Daemon
A static interface for daemonising the calling process.
Definition: gdaemon.h:42
G::LogOutput::onAssert
virtual void onAssert()
Called during an assertion failure.
Definition: glogoutput.cpp:227
G::LogOutput::rawOutput
virtual void rawOutput(std::ostream &, G::Log::Severity, const std::string &)
Overridable.
Definition: glogoutput_unix.cpp:51
G::LogOutput::groups
static void groups(const std::string &)
Sets the list of active logging groups.
Definition: glogoutput.cpp:154
G::LogOutput::~LogOutput
virtual ~LogOutput()
Destructor.
Definition: glogoutput.cpp:101
G::LogOutput::assertion
static void assertion(const char *file, int line, bool test, const char *)
Makes an assertion check (regardless of any LogOutput object).
Definition: glogoutput.cpp:283
G
Low-level classes.
Definition: garg.h:29
G::LogOutput::output
static void output(G::Log::Severity, const char *file, int line, const std::string &)
Generates output if there is an existing LogOutput object which is enabled.
Definition: glogoutput.cpp:136
G::LogOutput::register_
static void register_(const std::string &exe)
Registers the given executable as a source of logging.
Definition: glogoutput_unix.cpp:70
G::LogOutput::at
bool at(G::Log::Severity) const
Returns true if logging should occur for the given severity level.
Definition: glogoutput.cpp:142
G::LogOutput
Controls and implements low-level logging output, as used by the Log interface.
Definition: glogoutput.h:42
G::LogOutput::verbose
void verbose(bool verbose_log=true)
Enables or disables verbose logging.
Definition: glogoutput.cpp:131