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 // At least Irix needs this
32 #ifdef SG_HAVE_NATIVE_SGI_COMPILERS
33 #include <char_traits.h>
36 #ifdef SG_HAVE_STD_INCLUDES
40 # include <iostream.h>
41 # include <simgear/sg_traits.hxx>
44 #include <simgear/debug/debug_types.h>
46 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
47 SG_USING_STD(streambuf);
48 SG_USING_STD(ostream);
52 SG_USING_STD(char_traits);
56 SG_USING_STD(iostream);
62 // 1. Change output destination. Done.
63 // 2. Make logbuf thread safe.
64 // 3. Read environment for default debugClass and debugPriority.
68 * logbuf is an output-only streambuf with the ability to disable sets of
69 * messages at runtime. Only messages with priority >= logbuf::logPriority
70 * and debugClass == logbuf::logClass are output.
72 class logbuf : public streambuf
76 #ifndef SG_HAVE_STD_INCLUDES
77 typedef char_traits<char> traits_type;
78 typedef char_traits<char>::int_type int_type;
79 // typedef char_traits<char>::pos_type pos_type;
80 // typedef char_traits<char>::off_type off_type;
82 // logbuf( streambuf* sb ) : sbuf(sb) {}
91 * @return true or false*/
92 bool enabled() { return logging_enabled; }
95 * Set the logging level of subsequent messages.
96 * @param c debug class
99 void set_log_state( sgDebugClass c, sgDebugPriority p );
102 * Set the global logging level.
103 * @param c debug class
106 static void set_log_level( sgDebugClass c, sgDebugPriority p );
110 * Set the allowed logging classes.
111 * @param c All enabled logging classes anded together.
113 static void set_log_classes (sgDebugClass c);
117 * Get the logging classes currently enabled.
118 * @return All enabled debug logging anded together.
120 static sgDebugClass get_log_classes ();
124 * Set the logging priority.
125 * @param c The priority cutoff for logging messages.
127 static void set_log_priority (sgDebugPriority p);
131 * Get the current logging priority.
132 * @return The priority cutoff for logging messages.
134 static sgDebugPriority get_log_priority ();
138 * Set the stream buffer
139 * @param sb stream buffer
141 void set_sb( streambuf* sb );
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 streambuf* sbuf;
157 static bool logging_enabled;
158 static sgDebugClass logClass;
159 static sgDebugPriority logPriority;
164 logbuf( const logbuf& );
165 void operator= ( const logbuf& );
171 #ifdef SG_HAVE_STD_INCLUDES
172 return sbuf->pubsync();
179 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
181 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
184 inline logbuf::int_type
185 logbuf::overflow( int c )
187 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
191 * logstream manipulator for setting the log level of a message.
195 loglevel( sgDebugClass c, sgDebugPriority p )
196 : logClass(c), logPriority(p) {}
198 sgDebugClass logClass;
199 sgDebugPriority logPriority;
203 * A helper class that ensures a streambuf and ostream are constructed and
204 * destroyed in the correct order. The streambuf must be created before the
205 * ostream but bases are constructed before members. Thus, making this class
206 * a private base of logstream, declared to the left of ostream, we ensure the
207 * correct order of construction and destruction.
209 struct logstream_base
211 // logstream_base( streambuf* sb ) : lbuf(sb) {}
218 * Class to manage the debug logging stream.
220 class logstream : private logstream_base, public ostream
224 * The default is to send messages to cerr.
225 * @param out output stream
227 logstream( ostream& out )
228 // : logstream_base(out.rdbuf()),
230 ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
233 * Set the output stream
234 * @param out output stream
236 void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
239 * Set the global log class and priority level.
240 * @param c debug class
243 void setLogLevels( sgDebugClass c, sgDebugPriority p );
246 * Output operator to capture the debug level and priority of a message.
249 inline ostream& operator<< ( const loglevel& l );
253 logstream::operator<< ( const loglevel& l )
255 lbuf.set_log_state( l.logClass, l.logPriority );
262 * Return the one and only logstream instance.
263 * We use a function instead of a global object so we are assured that cerr
264 * has been initialised.
265 * @return current logstream
270 static logstream logstrm( cerr );
275 /** \def SG_LOG(C,P,M)
277 * @param C debug class
282 # define SG_LOG(C,P,M)
283 #elif defined( __MWERKS__ )
284 # define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl
286 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << endl
290 #endif // _LOGSTREAM_H