1 /** \file logstream.hxx
2 * Stream based logging mechanism.
5 // Written by Bernie Bright, 1998
7 // Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Library General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <simgear/compiler.h>
37 #include <simgear/debug/debug_types.h>
45 // 1. Change output destination. Done.
46 // 2. Make logbuf thread safe.
47 // 3. Read environment for default debugClass and debugPriority.
51 * logbuf is an output-only streambuf with the ability to disable sets of
52 * messages at runtime. Only messages with priority >= logbuf::logPriority
53 * and debugClass == logbuf::logClass are output.
55 #ifdef SG_NEED_STREAMBUF_HACK
56 class logbuf : public __streambuf
58 class logbuf : public std::streambuf
62 // logbuf( streambuf* sb ) : sbuf(sb) {}
71 * @return true or false*/
72 bool enabled() { return logging_enabled; }
75 * Set the logging level of subsequent messages.
76 * @param c debug class
79 void set_log_state( sgDebugClass c, sgDebugPriority p );
81 bool would_log( sgDebugClass c, sgDebugPriority p ) const;
84 * Set the global logging level.
85 * @param c debug class
88 static void set_log_level( sgDebugClass c, sgDebugPriority p );
92 * Set the allowed logging classes.
93 * @param c All enabled logging classes anded together.
95 static void set_log_classes (sgDebugClass c);
99 * Get the logging classes currently enabled.
100 * @return All enabled debug logging anded together.
102 static sgDebugClass get_log_classes ();
106 * Set the logging priority.
107 * @param c The priority cutoff for logging messages.
109 static void set_log_priority (sgDebugPriority p);
113 * Get the current logging priority.
114 * @return The priority cutoff for logging messages.
116 static sgDebugPriority get_log_priority ();
120 * Set the stream buffer
121 * @param sb stream buffer
123 void set_sb( std::streambuf* sb );
126 static void has_no_console() { has_console = false; }
132 inline virtual int sync();
135 int_type overflow( int ch );
136 // int xsputn( const char* s, istreamsize n );
140 // The streambuf used for actual output. Defaults to cerr.rdbuf().
141 static std::streambuf* sbuf;
143 static bool logging_enabled;
145 static bool has_console;
147 static sgDebugClass logClass;
148 static sgDebugPriority logPriority;
153 logbuf( const logbuf& );
154 void operator= ( const logbuf& );
160 return sbuf->pubsync();
164 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
166 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
170 logbuf::would_log( sgDebugClass c, sgDebugPriority p ) const
172 return ((c & logClass) != 0 && p >= logPriority);
175 inline logbuf::int_type
176 logbuf::overflow( int c )
179 if ( logging_enabled ) {
180 if ( !has_console ) {
182 freopen("conin$", "r", stdin);
183 freopen("conout$", "w", stdout);
184 freopen("conout$", "w", stderr);
187 return sbuf->sputc(c);
190 return EOF == 0 ? 1: 0;
192 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
197 * logstream manipulator for setting the log level of a message.
201 loglevel( sgDebugClass c, sgDebugPriority p )
202 : logClass(c), logPriority(p) {}
204 sgDebugClass logClass;
205 sgDebugPriority logPriority;
209 * A helper class that ensures a streambuf and ostream are constructed and
210 * destroyed in the correct order. The streambuf must be created before the
211 * ostream but bases are constructed before members. Thus, making this class
212 * a private base of logstream, declared to the left of ostream, we ensure the
213 * correct order of construction and destruction.
215 struct logstream_base
217 // logstream_base( streambuf* sb ) : lbuf(sb) {}
224 * Class to manage the debug logging stream.
226 class logstream : private logstream_base, public std::ostream
230 * The default is to send messages to cerr.
231 * @param out output stream
233 logstream( std::ostream& out )
234 // : logstream_base(out.rdbuf()),
236 std::ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
239 * Set the output stream
240 * @param out output stream
242 void set_output( std::ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
245 * Set the global log class and priority level.
246 * @param c debug class
249 void setLogLevels( sgDebugClass c, sgDebugPriority p );
251 bool would_log( sgDebugClass c, sgDebugPriority p ) const
253 return lbuf.would_log( c, p );
257 * Output operator to capture the debug level and priority of a message.
260 inline std::ostream& operator<< ( const loglevel& l );
261 friend logstream& sglog();
262 static logstream *initGlobalLogstream();
264 static logstream *global_logstream;
268 logstream::operator<< ( const loglevel& l )
270 lbuf.set_log_state( l.logClass, l.logPriority );
276 * Return the one and only logstream instance.
277 * We use a function instead of a global object so we are assured that cerr
278 * has been initialised.
279 * @return current logstream
284 return *logstream::initGlobalLogstream();
288 /** \def SG_LOG(C,P,M)
290 * @param C debug class
295 # define SG_LOG(C,P,M)
297 # define SG_LOG(C,P,M) do { \
298 logstream& __tmplogstreamref(sglog()); \
299 if(__tmplogstreamref.would_log(C,P)) { \
300 __tmplogstreamref << loglevel(C,P) << M << std::endl; } \
304 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
306 #endif // _LOGSTREAM_H