1 // Stream based logging mechanism.
3 // Written by Bernie Bright, 1998
5 // Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <simgear/compiler.h>
33 #ifdef FG_HAVE_STD_INCLUDES
37 # include <iostream.h>
38 # include <simgear/fg_traits.hxx>
41 #include <simgear/debug/debug_types.h>
43 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
44 FG_USING_STD(streambuf);
45 FG_USING_STD(ostream);
51 # define cerr std::cerr
52 # define endl std::endl
53 FG_USING_STD(iostream);
59 // 1. Change output destination. Done.
60 // 2. Make logbuf thread safe.
61 // 3. Read environment for default debugClass and debugPriority.
64 //-----------------------------------------------------------------------------
66 // logbuf is an output-only streambuf with the ability to disable sets of
67 // messages at runtime. Only messages with priority >= logbuf::logPriority
68 // and debugClass == logbuf::logClass are output.
70 class logbuf : public streambuf
74 #ifndef FG_HAVE_STD_INCLUDES
75 typedef char_traits<char> traits_type;
76 typedef char_traits<char>::int_type int_type;
77 typedef char_traits<char>::pos_type pos_type;
78 typedef char_traits<char>::off_type off_type;
80 // logbuf( streambuf* sb ) : sbuf(sb) {}
84 // Is logging enabled?
85 bool enabled() { return logging_enabled; }
87 // Set the logging level of subsequent messages.
88 void set_log_state( fgDebugClass c, fgDebugPriority p );
90 // Set the global logging level.
91 static void set_log_level( fgDebugClass c, fgDebugPriority p );
94 void set_sb( streambuf* sb );
98 inline virtual int sync();
99 int_type overflow( int ch );
100 // int xsputn( const char* s, istreamsize n );
104 // The streambuf used for actual output. Defaults to cerr.rdbuf().
105 static streambuf* sbuf;
107 static bool logging_enabled;
108 static fgDebugClass logClass;
109 static fgDebugPriority logPriority;
114 logbuf( const logbuf& );
115 void operator= ( const logbuf& );
121 #ifdef FG_HAVE_STD_INCLUDES
122 return sbuf->pubsync();
129 logbuf::set_log_state( fgDebugClass c, fgDebugPriority p )
131 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
134 inline logbuf::int_type
135 logbuf::overflow( int c )
137 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
140 //-----------------------------------------------------------------------------
142 // logstream manipulator for setting the log level of a message.
146 loglevel( fgDebugClass c, fgDebugPriority p )
147 : logClass(c), logPriority(p) {}
149 fgDebugClass logClass;
150 fgDebugPriority logPriority;
153 //-----------------------------------------------------------------------------
155 // A helper class that ensures a streambuf and ostream are constructed and
156 // destroyed in the correct order. The streambuf must be created before the
157 // ostream but bases are constructed before members. Thus, making this class
158 // a private base of logstream, declared to the left of ostream, we ensure the
159 // correct order of construction and destruction.
161 struct logstream_base
163 // logstream_base( streambuf* sb ) : lbuf(sb) {}
169 //-----------------------------------------------------------------------------
173 class logstream : private logstream_base, public ostream
176 // The default is to send messages to cerr.
177 logstream( ostream& out )
178 // : logstream_base(out.rdbuf()),
180 ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
182 void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
184 // Set the global log class and priority level.
185 void setLogLevels( fgDebugClass c, fgDebugPriority p );
187 // Output operator to capture the debug level and priority of a message.
188 inline ostream& operator<< ( const loglevel& l );
192 logstream::operator<< ( const loglevel& l )
194 lbuf.set_log_state( l.logClass, l.logPriority );
198 //-----------------------------------------------------------------------------
200 // Return the one and only logstream instance.
201 // We use a function instead of a global object so we are assured that cerr
202 // has been initialised.
207 static logstream logstrm( cerr );
212 # define FG_LOG(C,P,M)
213 #elif defined( __MWERKS__ )
214 # define FG_LOG(C,P,M) ::fglog() << ::loglevel(C,P) << M << std::endl
216 # define FG_LOG(C,P,M) fglog() << loglevel(C,P) << M << endl
219 #endif // _LOGSTREAM_H