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>
34 #ifdef SG_HAVE_STD_INCLUDES
38 # include <iostream.h>
39 # include <simgear/sg_traits.hxx>
42 #include <simgear/debug/debug_types.h>
44 SG_USING_STD(streambuf);
45 SG_USING_STD(ostream);
50 // 1. Change output destination. Done.
51 // 2. Make logbuf thread safe.
52 // 3. Read environment for default debugClass and debugPriority.
56 * logbuf is an output-only streambuf with the ability to disable sets of
57 * messages at runtime. Only messages with priority >= logbuf::logPriority
58 * and debugClass == logbuf::logClass are output.
60 #ifdef SG_NEED_STREAMBUF_HACK
61 class logbuf : public __streambuf
63 class logbuf : public std::streambuf
68 #ifndef SG_HAVE_STD_INCLUDES
69 typedef char_traits<char> traits_type;
70 typedef char_traits<char>::int_type int_type;
71 // typedef char_traits<char>::pos_type pos_type;
72 // typedef char_traits<char>::off_type off_type;
74 // logbuf( streambuf* sb ) : sbuf(sb) {}
83 * @return true or false*/
84 bool enabled() { return logging_enabled; }
87 * Set the logging level of subsequent messages.
88 * @param c debug class
91 void set_log_state( sgDebugClass c, sgDebugPriority p );
94 * Set the global logging level.
95 * @param c debug class
98 static void set_log_level( sgDebugClass c, sgDebugPriority p );
102 * Set the allowed logging classes.
103 * @param c All enabled logging classes anded together.
105 static void set_log_classes (sgDebugClass c);
109 * Get the logging classes currently enabled.
110 * @return All enabled debug logging anded together.
112 static sgDebugClass get_log_classes ();
116 * Set the logging priority.
117 * @param c The priority cutoff for logging messages.
119 static void set_log_priority (sgDebugPriority p);
123 * Get the current logging priority.
124 * @return The priority cutoff for logging messages.
126 static sgDebugPriority get_log_priority ();
130 * Set the stream buffer
131 * @param sb stream buffer
133 void set_sb( std::streambuf* sb );
136 static void has_no_console() { has_console = false; }
142 inline virtual int sync();
145 int_type overflow( int ch );
146 // int xsputn( const char* s, istreamsize n );
150 // The streambuf used for actual output. Defaults to cerr.rdbuf().
151 static std::streambuf* sbuf;
153 static bool logging_enabled;
155 static bool has_console;
157 static sgDebugClass logClass;
158 static sgDebugPriority logPriority;
163 logbuf( const logbuf& );
164 void operator= ( const logbuf& );
170 #ifdef SG_HAVE_STD_INCLUDES
171 return sbuf->pubsync();
178 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
180 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
183 inline logbuf::int_type
184 logbuf::overflow( int c )
187 if ( logging_enabled ) {
188 if ( !has_console ) {
190 freopen("conin$", "r", stdin);
191 freopen("conout$", "w", stdout);
192 freopen("conout$", "w", stderr);
195 return sbuf->sputc(c);
198 return EOF == 0 ? 1: 0;
200 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
205 * logstream manipulator for setting the log level of a message.
209 loglevel( sgDebugClass c, sgDebugPriority p )
210 : logClass(c), logPriority(p) {}
212 sgDebugClass logClass;
213 sgDebugPriority logPriority;
217 * A helper class that ensures a streambuf and ostream are constructed and
218 * destroyed in the correct order. The streambuf must be created before the
219 * ostream but bases are constructed before members. Thus, making this class
220 * a private base of logstream, declared to the left of ostream, we ensure the
221 * correct order of construction and destruction.
223 struct logstream_base
225 // logstream_base( streambuf* sb ) : lbuf(sb) {}
232 * Class to manage the debug logging stream.
234 class logstream : private logstream_base, public std::ostream
238 * The default is to send messages to cerr.
239 * @param out output stream
241 logstream( std::ostream& out )
242 // : logstream_base(out.rdbuf()),
244 std::ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
247 * Set the output stream
248 * @param out output stream
250 void set_output( std::ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
253 * Set the global log class and priority level.
254 * @param c debug class
257 void setLogLevels( sgDebugClass c, sgDebugPriority p );
260 * Output operator to capture the debug level and priority of a message.
263 inline std::ostream& operator<< ( const loglevel& l );
264 friend logstream& sglog();
266 static logstream *global_logstream;
267 static void initGlobalLogstream();
271 logstream::operator<< ( const loglevel& l )
273 lbuf.set_log_state( l.logClass, l.logPriority );
279 * Return the one and only logstream instance.
280 * We use a function instead of a global object so we are assured that cerr
281 * has been initialised.
282 * @return current logstream
287 if (logstream::global_logstream == NULL) {
288 logstream::initGlobalLogstream();
290 return *logstream::global_logstream;
294 /** \def SG_LOG(C,P,M)
296 * @param C debug class
301 # define SG_LOG(C,P,M)
303 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << std::endl
306 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
308 #endif // _LOGSTREAM_H