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 );
82 * Set the global logging level.
83 * @param c debug class
86 static void set_log_level( sgDebugClass c, sgDebugPriority p );
90 * Set the allowed logging classes.
91 * @param c All enabled logging classes anded together.
93 static void set_log_classes (sgDebugClass c);
97 * Get the logging classes currently enabled.
98 * @return All enabled debug logging anded together.
100 static sgDebugClass get_log_classes ();
104 * Set the logging priority.
105 * @param c The priority cutoff for logging messages.
107 static void set_log_priority (sgDebugPriority p);
111 * Get the current logging priority.
112 * @return The priority cutoff for logging messages.
114 static sgDebugPriority get_log_priority ();
118 * Set the stream buffer
119 * @param sb stream buffer
121 void set_sb( std::streambuf* sb );
124 static void has_no_console() { has_console = false; }
130 inline virtual int sync();
133 int_type overflow( int ch );
134 // int xsputn( const char* s, istreamsize n );
138 // The streambuf used for actual output. Defaults to cerr.rdbuf().
139 static std::streambuf* sbuf;
141 static bool logging_enabled;
143 static bool has_console;
145 static sgDebugClass logClass;
146 static sgDebugPriority logPriority;
151 logbuf( const logbuf& );
152 void operator= ( const logbuf& );
158 return sbuf->pubsync();
162 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
164 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
167 inline logbuf::int_type
168 logbuf::overflow( int c )
171 if ( logging_enabled ) {
172 if ( !has_console ) {
174 freopen("conin$", "r", stdin);
175 freopen("conout$", "w", stdout);
176 freopen("conout$", "w", stderr);
179 return sbuf->sputc(c);
182 return EOF == 0 ? 1: 0;
184 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
189 * logstream manipulator for setting the log level of a message.
193 loglevel( sgDebugClass c, sgDebugPriority p )
194 : logClass(c), logPriority(p) {}
196 sgDebugClass logClass;
197 sgDebugPriority logPriority;
201 * A helper class that ensures a streambuf and ostream are constructed and
202 * destroyed in the correct order. The streambuf must be created before the
203 * ostream but bases are constructed before members. Thus, making this class
204 * a private base of logstream, declared to the left of ostream, we ensure the
205 * correct order of construction and destruction.
207 struct logstream_base
209 // logstream_base( streambuf* sb ) : lbuf(sb) {}
216 * Class to manage the debug logging stream.
218 class logstream : private logstream_base, public std::ostream
222 * The default is to send messages to cerr.
223 * @param out output stream
225 logstream( std::ostream& out )
226 // : logstream_base(out.rdbuf()),
228 std::ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
231 * Set the output stream
232 * @param out output stream
234 void set_output( std::ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
237 * Set the global log class and priority level.
238 * @param c debug class
241 void setLogLevels( sgDebugClass c, sgDebugPriority p );
244 * Output operator to capture the debug level and priority of a message.
247 inline std::ostream& operator<< ( const loglevel& l );
248 friend logstream& sglog();
250 static logstream *global_logstream;
251 static void initGlobalLogstream();
255 logstream::operator<< ( const loglevel& l )
257 lbuf.set_log_state( l.logClass, l.logPriority );
263 * Return the one and only logstream instance.
264 * We use a function instead of a global object so we are assured that cerr
265 * has been initialised.
266 * @return current logstream
271 if (logstream::global_logstream == NULL) {
272 logstream::initGlobalLogstream();
274 return *logstream::global_logstream;
278 /** \def SG_LOG(C,P,M)
280 * @param C debug class
285 # define SG_LOG(C,P,M)
287 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << std::endl
290 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
292 #endif // _LOGSTREAM_H