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);
51 SG_USING_STD(iostream);
57 // 1. Change output destination. Done.
58 // 2. Make logbuf thread safe.
59 // 3. Read environment for default debugClass and debugPriority.
63 * logbuf is an output-only streambuf with the ability to disable sets of
64 * messages at runtime. Only messages with priority >= logbuf::logPriority
65 * and debugClass == logbuf::logClass are output.
67 #ifdef SG_NEED_STREAMBUF_HACK
68 class logbuf : public __streambuf
70 class logbuf : public streambuf
75 #ifndef SG_HAVE_STD_INCLUDES
76 typedef char_traits<char> traits_type;
77 typedef char_traits<char>::int_type int_type;
78 // typedef char_traits<char>::pos_type pos_type;
79 // typedef char_traits<char>::off_type off_type;
81 // logbuf( streambuf* sb ) : sbuf(sb) {}
90 * @return true or false*/
91 bool enabled() { return logging_enabled; }
94 * Set the logging level of subsequent messages.
95 * @param c debug class
98 void set_log_state( sgDebugClass c, sgDebugPriority p );
101 * Set the global logging level.
102 * @param c debug class
105 static void set_log_level( sgDebugClass c, sgDebugPriority p );
109 * Set the allowed logging classes.
110 * @param c All enabled logging classes anded together.
112 static void set_log_classes (sgDebugClass c);
116 * Get the logging classes currently enabled.
117 * @return All enabled debug logging anded together.
119 static sgDebugClass get_log_classes ();
123 * Set the logging priority.
124 * @param c The priority cutoff for logging messages.
126 static void set_log_priority (sgDebugPriority p);
130 * Get the current logging priority.
131 * @return The priority cutoff for logging messages.
133 static sgDebugPriority get_log_priority ();
137 * Set the stream buffer
138 * @param sb stream buffer
140 void set_sb( streambuf* sb );
143 static void has_no_console() { has_console = false; }
149 inline virtual int sync();
152 int_type overflow( int ch );
153 // int xsputn( const char* s, istreamsize n );
157 // The streambuf used for actual output. Defaults to cerr.rdbuf().
158 static streambuf* sbuf;
160 static bool logging_enabled;
162 static bool has_console;
164 static sgDebugClass logClass;
165 static sgDebugPriority logPriority;
170 logbuf( const logbuf& );
171 void operator= ( const logbuf& );
177 #ifdef SG_HAVE_STD_INCLUDES
178 return sbuf->pubsync();
185 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
187 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
190 inline logbuf::int_type
191 logbuf::overflow( int c )
194 if ( logging_enabled ) {
195 if ( !has_console ) {
197 freopen("conin$", "r", stdin);
198 freopen("conout$", "w", stdout);
199 freopen("conout$", "w", stderr);
202 return sbuf->sputc(c);
205 return EOF == 0 ? 1: 0;
207 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
212 * logstream manipulator for setting the log level of a message.
216 loglevel( sgDebugClass c, sgDebugPriority p )
217 : logClass(c), logPriority(p) {}
219 sgDebugClass logClass;
220 sgDebugPriority logPriority;
224 * A helper class that ensures a streambuf and ostream are constructed and
225 * destroyed in the correct order. The streambuf must be created before the
226 * ostream but bases are constructed before members. Thus, making this class
227 * a private base of logstream, declared to the left of ostream, we ensure the
228 * correct order of construction and destruction.
230 struct logstream_base
232 // logstream_base( streambuf* sb ) : lbuf(sb) {}
239 * Class to manage the debug logging stream.
241 class logstream : private logstream_base, public ostream
245 * The default is to send messages to cerr.
246 * @param out output stream
248 logstream( ostream& out )
249 // : logstream_base(out.rdbuf()),
251 ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
254 * Set the output stream
255 * @param out output stream
257 void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
260 * Set the global log class and priority level.
261 * @param c debug class
264 void setLogLevels( sgDebugClass c, sgDebugPriority p );
267 * Output operator to capture the debug level and priority of a message.
270 inline ostream& operator<< ( const loglevel& l );
274 logstream::operator<< ( const loglevel& l )
276 lbuf.set_log_state( l.logClass, l.logPriority );
280 extern logstream *global_logstream;
284 * Return the one and only logstream instance.
285 * We use a function instead of a global object so we are assured that cerr
286 * has been initialised.
287 * @return current logstream
292 if (global_logstream == NULL) {
296 * There appears to be a bug in the C++ runtime in Mac OS X that
297 * will crash if certain funtions are called (in this case
298 * cerr.rdbuf()) during static initialization of a class. This
299 * print statement is hack to kick the library in the pants so it
300 * won't crash when cerr.rdbuf() is first called -DW
302 cout << "Using Mac OS X hack for initializing C++ stdio..." << endl;
304 global_logstream = new logstream (cerr);
307 return *global_logstream;
311 /** \def SG_LOG(C,P,M)
313 * @param C debug class
318 # define SG_LOG(C,P,M)
319 #elif defined( __MWERKS__ )
320 # define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl
322 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << endl
326 #endif // _LOGSTREAM_H