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.
22 // (Log is kept at end of this file)
32 #include "Include/compiler.h"
34 #ifdef FG_HAVE_STD_INCLUDES
39 # include <iostream.h>
40 # include "Include/fg_traits.hxx"
43 #include "debug_types.h"
45 FG_USING_STD(streambuf);
46 FG_USING_STD(ostream);
53 // 1. Change output destination. Done.
54 // 2. Make logbuf thread safe.
55 // 3. Read environment for default debugClass and debugPriority.
58 //-----------------------------------------------------------------------------
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 class logbuf : public streambuf
68 #ifndef FG_HAVE_STD_INCLUDES
69 typedef char_traits<char> traits_type;
70 typedef char_traits<char>::int_type int_type;
71 typedef char_traits<char>::pos_type pos_type;
72 typedef char_traits<char>::off_type off_type;
74 // logbuf( streambuf* sb ) : sbuf(sb) {}
78 // Is logging enabled?
79 bool enabled() { return logging_enabled; }
81 // Set the logging level of subsequent messages.
82 void set_log_state( fgDebugClass c, fgDebugPriority p );
84 // Set the global logging level.
85 static void set_log_level( fgDebugClass c, fgDebugPriority p );
88 void set_sb( streambuf* sb );
92 inline virtual int sync();
93 int_type overflow( int ch );
94 // int xsputn( const char* s, istreamsize n );
98 // The streambuf used for actual output. Defaults to cerr.rdbuf().
99 static streambuf* sbuf;
101 static bool logging_enabled;
102 static fgDebugClass logClass;
103 static fgDebugPriority logPriority;
108 logbuf( const logbuf& );
109 void operator= ( const logbuf& );
115 #ifdef FG_HAVE_STD_INCLUDES
116 return sbuf->pubsync();
123 logbuf::set_log_state( fgDebugClass c, fgDebugPriority p )
125 logging_enabled = ((c & logClass) != 0 && p >= logPriority);
128 inline logbuf::int_type
129 logbuf::overflow( int c )
131 return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
134 //-----------------------------------------------------------------------------
136 // logstream manipulator for setting the log level of a message.
140 loglevel( fgDebugClass c, fgDebugPriority p )
141 : logClass(c), logPriority(p) {}
143 fgDebugClass logClass;
144 fgDebugPriority logPriority;
147 //-----------------------------------------------------------------------------
149 // A helper class that ensures a streambuf and ostream are constructed and
150 // destroyed in the correct order. The streambuf must be created before the
151 // ostream but bases are constructed before members. Thus, making this class
152 // a private base of logstream, declared to the left of ostream, we ensure the
153 // correct order of construction and destruction.
155 struct logstream_base
157 // logstream_base( streambuf* sb ) : lbuf(sb) {}
163 //-----------------------------------------------------------------------------
167 class logstream : private logstream_base, public ostream
170 // The default is to send messages to cerr.
171 logstream( ostream& out )
172 // : logstream_base(out.rdbuf()),
174 ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
176 void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
178 // Set the global log class and priority level.
179 void setLogLevels( fgDebugClass c, fgDebugPriority p );
181 // Output operator to capture the debug level and priority of a message.
182 inline ostream& operator<< ( const loglevel& l );
186 logstream::operator<< ( const loglevel& l )
188 lbuf.set_log_state( l.logClass, l.logPriority );
192 //-----------------------------------------------------------------------------
194 // Return the one and only logstream instance.
195 // We use a function instead of a global object so we are assured that cerr
196 // has been initialised.
201 static logstream logstrm( cerr );
206 # define FG_LOG(C,P,M)
208 # define FG_LOG(C,P,M) fglog() << loglevel(C,P) << M << endl
211 #endif // _LOGSTREAM_H
214 // Revision 1.3 1999/01/19 20:53:35 curt
215 // Portability updates by Bernie Bright.
217 // Revision 1.2 1998/11/07 19:07:02 curt
218 // Enable release builds using the --without-logging option to the configure
219 // script. Also a couple log message cleanups, plus some C to C++ comment
222 // Revision 1.1 1998/11/06 21:20:42 curt