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 Library General Public
20 // License along with this library; if not, write to the
21 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 // Boston, MA 02111-1307, USA.
29 #include <simgear/compiler.h>
31 #ifdef SG_HAVE_STD_INCLUDES
35 # include <iostream.h>
36 # include <simgear/sg_traits.hxx>
39 #include <simgear/debug/debug_types.h>
41 SG_USING_STD(streambuf);
42 SG_USING_STD(ostream);
48 SG_USING_STD(iostream);
54 // 1. Change output destination. Done.
55 // 2. Make logbuf thread safe.
56 // 3. Read environment for default debugClass and debugPriority.
60 * logbuf is an output-only streambuf with the ability to disable sets of
61 * messages at runtime. Only messages with priority >= logbuf::logPriority
62 * and debugClass == logbuf::logClass are output.
64 #ifdef SG_NEED_STREAMBUF_HACK
65 class logbuf : public __streambuf
67 class logbuf : public streambuf
72 #ifndef SG_HAVE_STD_INCLUDES
73 typedef char_traits<char> traits_type;
74 typedef char_traits<char>::int_type int_type;
75 // typedef char_traits<char>::pos_type pos_type;
76 // typedef char_traits<char>::off_type off_type;
78 // logbuf( streambuf* sb ) : sbuf(sb) {}
87 * @return true or false*/
88 bool enabled() { return logging_enabled; }
91 * Set the logging level of subsequent messages.
92 * @param c debug class
95 void set_log_state( sgDebugClass c, sgDebugPriority p );
98 * Set the global logging level.
99 * @param c debug class
102 static void set_log_level( sgDebugClass c, sgDebugPriority p );
106 * Set the allowed logging classes.
107 * @param c All enabled logging classes anded together.
109 static void set_log_classes (sgDebugClass c);
113 * Get the logging classes currently enabled.
114 * @return All enabled debug logging anded together.
116 static sgDebugClass get_log_classes ();
120 * Set the logging priority.
121 * @param c The priority cutoff for logging messages.
123 static void set_log_priority (sgDebugPriority p);
127 * Get the current logging priority.
128 * @return The priority cutoff for logging messages.
130 static sgDebugPriority get_log_priority ();
134 * Set the stream buffer
135 * @param sb stream buffer
137 void set_sb( streambuf* sb );
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 streambuf* sbuf;
153 static bool logging_enabled;
154 static sgDebugClass logClass;
155 static sgDebugPriority logPriority;
160 logbuf( const logbuf& );
161 void operator= ( const logbuf& );
167 #ifdef SG_HAVE_STD_INCLUDES
168 return sbuf->pubsync();
175 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
177 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
180 inline logbuf::int_type
181 logbuf::overflow( int c )
183 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
187 * logstream manipulator for setting the log level of a message.
191 loglevel( sgDebugClass c, sgDebugPriority p )
192 : logClass(c), logPriority(p) {}
194 sgDebugClass logClass;
195 sgDebugPriority logPriority;
199 * A helper class that ensures a streambuf and ostream are constructed and
200 * destroyed in the correct order. The streambuf must be created before the
201 * ostream but bases are constructed before members. Thus, making this class
202 * a private base of logstream, declared to the left of ostream, we ensure the
203 * correct order of construction and destruction.
205 struct logstream_base
207 // logstream_base( streambuf* sb ) : lbuf(sb) {}
214 * Class to manage the debug logging stream.
216 class logstream : private logstream_base, public ostream
220 * The default is to send messages to cerr.
221 * @param out output stream
223 logstream( ostream& out )
224 // : logstream_base(out.rdbuf()),
226 ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
229 * Set the output stream
230 * @param out output stream
232 void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
235 * Set the global log class and priority level.
236 * @param c debug class
239 void setLogLevels( sgDebugClass c, sgDebugPriority p );
242 * Output operator to capture the debug level and priority of a message.
245 inline ostream& operator<< ( const loglevel& l );
249 logstream::operator<< ( const loglevel& l )
251 lbuf.set_log_state( l.logClass, l.logPriority );
255 extern logstream *global_logstream;
259 * Return the one and only logstream instance.
260 * We use a function instead of a global object so we are assured that cerr
261 * has been initialised.
262 * @return current logstream
267 if (global_logstream == NULL) {
271 * There appears to be a bug in the C++ runtime in Mac OS X that
272 * will crash if certain funtions are called (in this case
273 * cerr.rdbuf()) during static initialization of a class. This
274 * print statement is hack to kick the library in the pants so it
275 * won't crash when cerr.rdbuf() is first called -DW
277 cout << "Using Mac OS X hack for initializing C++ stdio..." << endl;
279 global_logstream = new logstream (cerr);
282 return *global_logstream;
286 /** \def SG_LOG(C,P,M)
288 * @param C debug class
293 # define SG_LOG(C,P,M)
294 #elif defined( __MWERKS__ )
295 # define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl
297 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << endl
301 #endif // _LOGSTREAM_H