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.
33 #include <simgear/compiler.h>
35 // At least Irix needs this
36 #ifdef SG_HAVE_NATIVE_SGI_COMPILERS
37 #include <char_traits.h>
40 #ifdef SG_HAVE_STD_INCLUDES
44 # include <iostream.h>
45 # include <simgear/fg_traits.hxx>
48 #include <simgear/debug/debug_types.h>
50 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
51 SG_USING_STD(streambuf);
52 SG_USING_STD(ostream);
56 SG_USING_STD(char_traits);
60 SG_USING_STD(iostream);
66 // 1. Change output destination. Done.
67 // 2. Make logbuf thread safe.
68 // 3. Read environment for default debugClass and debugPriority.
72 * logbuf is an output-only streambuf with the ability to disable sets of
73 * messages at runtime. Only messages with priority >= logbuf::logPriority
74 * and debugClass == logbuf::logClass are output.
76 class logbuf : public streambuf
80 #ifndef SG_HAVE_STD_INCLUDES
81 typedef char_traits<char> traits_type;
82 typedef char_traits<char>::int_type int_type;
83 // typedef char_traits<char>::pos_type pos_type;
84 // typedef char_traits<char>::off_type off_type;
86 // logbuf( streambuf* sb ) : sbuf(sb) {}
92 /** Is logging enabled? */
93 bool enabled() { return logging_enabled; }
95 /** Set the logging level of subsequent messages. */
96 void set_log_state( sgDebugClass c, sgDebugPriority p );
98 /** Set the global logging level. */
99 static void set_log_level( sgDebugClass c, sgDebugPriority p );
101 /** Set the stream buffer */
102 void set_sb( streambuf* sb );
107 inline virtual int sync();
110 int_type overflow( int ch );
111 // int xsputn( const char* s, istreamsize n );
115 // The streambuf used for actual output. Defaults to cerr.rdbuf().
116 static streambuf* sbuf;
118 static bool logging_enabled;
119 static sgDebugClass logClass;
120 static sgDebugPriority logPriority;
125 logbuf( const logbuf& );
126 void operator= ( const logbuf& );
132 #ifdef SG_HAVE_STD_INCLUDES
133 return sbuf->pubsync();
140 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
142 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
145 inline logbuf::int_type
146 logbuf::overflow( int c )
148 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
152 * logstream manipulator for setting the log level of a message.
156 loglevel( sgDebugClass c, sgDebugPriority p )
157 : logClass(c), logPriority(p) {}
159 sgDebugClass logClass;
160 sgDebugPriority logPriority;
164 * A helper class that ensures a streambuf and ostream are constructed and
165 * destroyed in the correct order. The streambuf must be created before the
166 * ostream but bases are constructed before members. Thus, making this class
167 * a private base of logstream, declared to the left of ostream, we ensure the
168 * correct order of construction and destruction.
170 struct logstream_base
172 // logstream_base( streambuf* sb ) : lbuf(sb) {}
179 * Class to manage the debug logging stream.
181 class logstream : private logstream_base, public ostream
184 /** The default is to send messages to cerr. */
185 logstream( ostream& out )
186 // : logstream_base(out.rdbuf()),
188 ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
190 /** Set the output stream */
191 void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
193 /** Set the global log class and priority level. */
194 void setLogLevels( sgDebugClass c, sgDebugPriority p );
196 /** Output operator to capture the debug level and priority of a message. */
197 inline ostream& operator<< ( const loglevel& l );
201 logstream::operator<< ( const loglevel& l )
203 lbuf.set_log_state( l.logClass, l.logPriority );
209 * Return the one and only logstream instance.
210 * We use a function instead of a global object so we are assured that cerr
211 * has been initialised.
216 static logstream logstrm( cerr );
221 /** \def SG_LOG(C,P,M)
222 * Log a message = M of class = C and priority = P
225 # define SG_LOG(C,P,M)
226 #elif defined( __MWERKS__ )
227 # define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl
229 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << endl
233 #endif // _LOGSTREAM_H