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);
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 std::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( std::streambuf* sb );
140 static void has_no_console() { has_console = false; }
146 inline virtual int sync();
149 int_type overflow( int ch );
150 // int xsputn( const char* s, istreamsize n );
154 // The streambuf used for actual output. Defaults to cerr.rdbuf().
155 static std::streambuf* sbuf;
157 static bool logging_enabled;
159 static bool has_console;
161 static sgDebugClass logClass;
162 static sgDebugPriority logPriority;
167 logbuf( const logbuf& );
168 void operator= ( const logbuf& );
174 #ifdef SG_HAVE_STD_INCLUDES
175 return sbuf->pubsync();
182 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
184 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
187 inline logbuf::int_type
188 logbuf::overflow( int c )
191 if ( logging_enabled ) {
192 if ( !has_console ) {
194 freopen("conin$", "r", stdin);
195 freopen("conout$", "w", stdout);
196 freopen("conout$", "w", stderr);
199 return sbuf->sputc(c);
202 return EOF == 0 ? 1: 0;
204 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
209 * logstream manipulator for setting the log level of a message.
213 loglevel( sgDebugClass c, sgDebugPriority p )
214 : logClass(c), logPriority(p) {}
216 sgDebugClass logClass;
217 sgDebugPriority logPriority;
221 * A helper class that ensures a streambuf and ostream are constructed and
222 * destroyed in the correct order. The streambuf must be created before the
223 * ostream but bases are constructed before members. Thus, making this class
224 * a private base of logstream, declared to the left of ostream, we ensure the
225 * correct order of construction and destruction.
227 struct logstream_base
229 // logstream_base( streambuf* sb ) : lbuf(sb) {}
236 * Class to manage the debug logging stream.
238 class logstream : private logstream_base, public std::ostream
242 * The default is to send messages to cerr.
243 * @param out output stream
245 logstream( std::ostream& out )
246 // : logstream_base(out.rdbuf()),
248 std::ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
251 * Set the output stream
252 * @param out output stream
254 void set_output( std::ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
257 * Set the global log class and priority level.
258 * @param c debug class
261 void setLogLevels( sgDebugClass c, sgDebugPriority p );
264 * Output operator to capture the debug level and priority of a message.
267 inline std::ostream& operator<< ( const loglevel& l );
268 friend logstream& sglog();
270 static logstream *global_logstream;
271 static void initGlobalLogstream();
275 logstream::operator<< ( const loglevel& l )
277 lbuf.set_log_state( l.logClass, l.logPriority );
283 * Return the one and only logstream instance.
284 * We use a function instead of a global object so we are assured that cerr
285 * has been initialised.
286 * @return current logstream
291 if (logstream::global_logstream == NULL) {
292 logstream::initGlobalLogstream();
294 return *logstream::global_logstream;
298 /** \def SG_LOG(C,P,M)
300 * @param C debug class
305 # define SG_LOG(C,P,M)
306 #elif defined( __MWERKS__ )
307 # define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl
309 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << std::endl
312 #define SG_STRINGIFY(x) #x
313 #define SG_TOSTRING(x) SG_STRINGIFY(x)
314 #define SG_ORIGIN __FILE__ ":" SG_TOSTRING(__LINE__)
316 #endif // _LOGSTREAM_H