VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
glogoutput.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 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 <ctime>
29 
30 namespace G
31 {
32  class LogOutput ;
33 }
34 
35 /// \class G::LogOutput
36 /// Controls and implements low-level logging output, as used by the Log
37 /// interface. Applications should instantiate a LogOutput object in main() to
38 /// enable log output.
39 /// \see G::Log
40 ///
42 {
43 public:
44  enum SyslogFacility { User , Daemon , Mail , Cron } ; // etc.
45 
46  LogOutput( const std::string & prefix , bool output , bool with_logging ,
47  bool with_verbose_logging , bool with_debug , bool with_level ,
48  bool with_timestamp , bool strip_context , bool use_syslog ,
49  const std::string & stderr_replacement = std::string() ,
50  SyslogFacility syslog_facility = User ) ;
51  ///< Constructor. If there is no LogOutput object, or if 'output'
52  ///< is false, then there is no output of any sort. Otherwise at
53  ///< least warning and error messages are generated.
54  ///<
55  ///< If 'with-logging' is true then log[summary] messages are
56  ///< output. If 'with-verbose-logging' is true then log[verbose]
57  ///< messages are output. If 'with_debug' is true then debug
58  ///< messages will also be generated (but only if compiled in).
59  ///<
60  ///< More than one LogOutput object may be created, but only
61  ///< the first one controls output.
62 
63  explicit LogOutput( bool output_with_logging , bool verbose_and_debug = true ,
64  const std::string & stderr_replacement = std::string() ) ;
65  ///< Constructor for test programs. Only generates output if the
66  ///< first parameter is true. Never uses syslog.
67 
68  virtual ~LogOutput() ;
69  ///< Destructor.
70 
71  virtual void rawOutput( std::ostream & , G::Log::Severity , const std::string & ) ;
72  ///< Overridable. Used to do the final message
73  ///< output (with OutputDebugString() or stderr).
74 
75  static LogOutput * instance() ;
76  ///< Returns a pointer to the controlling LogOutput object. Returns
77  ///< nullptr if none.
78 
79  bool enable( bool enabled = true ) ;
80  ///< Enables or disables output. Returns the previous setting.
81 
82  void verbose( bool verbose_log = true ) ;
83  ///< Enables or disables verbose logging.
84 
85  bool at( G::Log::Severity ) const ;
86  ///< Returns true if output() generates output at the given severity level.
87 
88  bool syslog() const ;
89  ///< Returns true if syslog output is enabled.
90 
91  static void output( G::Log::Severity , const char * file , int line , const std::string & ) ;
92  ///< Generates output if there is an existing
93  ///< LogOutput object which is enabled. Uses rawOutput().
94 
95  static void assertion( const char * file , int line , bool test , const char * ) ;
96  ///< Makes an assertion check (regardless of any LogOutput
97  ///< object). Calls output() if the 'file' parameter is not null.
98 
99  virtual void onAssert() ;
100  ///< Called during an assertion failure. This allows Windows
101  ///< applications to stop timers etc. (Timers can cause reentrancy
102  ///< problems and infinitely recursive dialog box creation.)
103 
104 private:
105  typedef size_t size_type ;
106  LogOutput( const LogOutput & ) ; // not implemented
107  void operator=( const LogOutput & ) ; // not implemented
108  void init() ;
109  void cleanup() ;
110  void appendTimestampStringTo( std::string & ) ;
111  static std::string dateString() ;
112  void doOutput( G::Log::Severity , const std::string & ) ;
113  void doOutput( G::Log::Severity s , const char * , int , const std::string & ) ;
114  void doAssertion( const char * , int , const std::string & ) ;
115  static const char * levelString( Log::Severity s ) ;
116  static std::string itoa( int ) ;
117  static std::string fileAndLine( const char * , int ) ;
118  static void halt() ;
119  static LogOutput * & pthis() ;
120  static std::ostream & err( const std::string & ) ;
121  static void getLocalTime( time_t , struct std::tm * ) ; // dont make logging dependent on G::DateTime
122 
123 private:
124  std::string m_prefix ;
125  bool m_enabled ;
126  bool m_summary_log ;
127  bool m_verbose_log ;
128  bool m_debug ;
129  bool m_level ;
130  bool m_strip ;
131  bool m_syslog ;
132  std::ostream & m_std_err ;
133  SyslogFacility m_facility ;
134  time_t m_time ;
135  std::vector<char> m_time_buffer ;
136  bool m_timestamp ;
137  HANDLE m_handle ; // windows
138  bool m_handle_set ;
139 } ;
140 
141 #endif
bool syslog() const
Returns true if syslog output is enabled.
Definition: glogoutput.cpp:302
bool enable(bool enabled=true)
Enables or disables output. Returns the previous setting.
Definition: glogoutput.cpp:114
static LogOutput * instance()
Returns a pointer to the controlling LogOutput object.
Definition: glogoutput.cpp:109
A static interface for daemonising the calling process.
Definition: gdaemon.h:42
LogOutput(const std::string &prefix, bool output, bool with_logging, bool with_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:33
virtual void onAssert()
Called during an assertion failure.
Definition: glogoutput.cpp:193
virtual void rawOutput(std::ostream &, G::Log::Severity, const std::string &)
Overridable.
virtual ~LogOutput()
Destructor.
Definition: glogoutput.cpp:96
static void assertion(const char *file, int line, bool test, const char *)
Makes an assertion check (regardless of any LogOutput object).
Definition: glogoutput.cpp:249
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:126
bool at(G::Log::Severity) const
Returns true if output() generates output at the given severity level.
Definition: glogoutput.cpp:132
Controls and implements low-level logging output, as used by the Log interface.
Definition: glogoutput.h:41
void verbose(bool verbose_log=true)
Enables or disables verbose logging.
Definition: glogoutput.cpp:121