]> git.mxchange.org Git - simgear.git/blob - Lib/Debug/logstream.hxx
Fixed an IRIX warning message where an inline function is referenced
[simgear.git] / Lib / Debug / logstream.hxx
1 // Stream based logging mechanism.
2 //
3 // Written by Bernie Bright, 1998
4 //
5 // Copyright (C) 1998  Bernie Bright - bbright@c031.aone.net.au
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifndef _LOGSTREAM_H
24 #define _LOGSTREAM_H
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30
31 #include <Include/compiler.h>
32
33 #ifdef FG_HAVE_STD_INCLUDES
34 # include <streambuf>
35 # include <iostream>
36 #else
37 # include <iostream.h>
38 # include "Include/fg_traits.hxx"
39 #endif
40
41 #include "debug_types.h"
42
43 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
44 FG_USING_STD(streambuf);
45 FG_USING_STD(ostream);
46 FG_USING_STD(cerr);
47 FG_USING_STD(endl);
48 #endif
49
50 #ifdef __MWERKS__
51 FG_USING_STD(iostream);
52 #endif
53
54 //
55 // TODO:
56 //
57 // 1. Change output destination. Done.
58 // 2. Make logbuf thread safe.
59 // 3. Read environment for default debugClass and debugPriority.
60 //
61
62 //-----------------------------------------------------------------------------
63 //
64 // logbuf is an output-only streambuf with the ability to disable sets of
65 // messages at runtime. Only messages with priority >= logbuf::logPriority
66 // and debugClass == logbuf::logClass are output.
67 //
68 class logbuf : public streambuf
69 {
70 public:
71
72 #ifndef FG_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;
77 #endif
78 //     logbuf( streambuf* sb ) : sbuf(sb) {}
79     logbuf();
80     ~logbuf();
81
82     // Is logging enabled?
83     bool enabled() { return logging_enabled; }
84
85     // Set the logging level of subsequent messages.
86     void set_log_state( fgDebugClass c, fgDebugPriority p );
87
88     // Set the global logging level.
89     static void set_log_level( fgDebugClass c, fgDebugPriority p );
90
91     //
92     void set_sb( streambuf* sb );
93
94 protected:
95
96     inline virtual int sync();
97     int_type overflow( int ch );
98 //     int xsputn( const char* s, istreamsize n );
99
100 private:
101
102     // The streambuf used for actual output. Defaults to cerr.rdbuf().
103     static streambuf* sbuf;
104
105     static bool logging_enabled;
106     static fgDebugClass logClass;
107     static fgDebugPriority logPriority;
108
109 private:
110
111     // Not defined.
112     logbuf( const logbuf& );
113     void operator= ( const logbuf& );
114 };
115
116 inline int
117 logbuf::sync()
118 {
119 #ifdef FG_HAVE_STD_INCLUDES
120         return sbuf->pubsync();
121 #else
122         return sbuf->sync();
123 #endif
124 }
125
126 inline void
127 logbuf::set_log_state( fgDebugClass c, fgDebugPriority p )
128 {
129     logging_enabled = ((c & logClass) != 0 && p >= logPriority);
130 }
131
132 inline logbuf::int_type
133 logbuf::overflow( int c )
134 {
135     return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
136 }
137
138 //-----------------------------------------------------------------------------
139 //
140 // logstream manipulator for setting the log level of a message.
141 //
142 struct loglevel
143 {
144     loglevel( fgDebugClass c, fgDebugPriority p )
145         : logClass(c), logPriority(p) {}
146
147     fgDebugClass logClass;
148     fgDebugPriority logPriority;
149 };
150
151 //-----------------------------------------------------------------------------
152 //
153 // A helper class that ensures a streambuf and ostream are constructed and
154 // destroyed in the correct order.  The streambuf must be created before the
155 // ostream but bases are constructed before members.  Thus, making this class
156 // a private base of logstream, declared to the left of ostream, we ensure the
157 // correct order of construction and destruction.
158 //
159 struct logstream_base
160 {
161 //     logstream_base( streambuf* sb ) : lbuf(sb) {}
162     logstream_base() {}
163
164     logbuf lbuf;
165 };
166
167 //-----------------------------------------------------------------------------
168 //
169 // 
170 //
171 class logstream : private logstream_base, public ostream
172 {
173 public:
174     // The default is to send messages to cerr.
175     logstream( ostream& out )
176 //      : logstream_base(out.rdbuf()),
177         : logstream_base(),
178           ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
179
180     void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
181
182     // Set the global log class and priority level.
183      void setLogLevels( fgDebugClass c, fgDebugPriority p );
184
185     // Output operator to capture the debug level and priority of a message.
186     inline ostream& operator<< ( const loglevel& l );
187 };
188
189 inline ostream&
190 logstream::operator<< ( const loglevel& l )
191 {
192     lbuf.set_log_state( l.logClass, l.logPriority );
193     return *this;
194 }
195
196 //-----------------------------------------------------------------------------
197 //
198 // Return the one and only logstream instance.
199 // We use a function instead of a global object so we are assured that cerr
200 // has been initialised.
201 //
202 inline logstream&
203 fglog()
204 {
205     static logstream logstrm( cerr );
206     return logstrm;
207 }
208
209 #ifdef FG_NDEBUG
210 # define FG_LOG(C,P,M)
211 #elif defined( __MWERKS__ )
212 # define FG_LOG(C,P,M) ::fglog() << ::loglevel(C,P) << M << std::endl
213 #else
214 # define FG_LOG(C,P,M) fglog() << loglevel(C,P) << M << endl
215 #endif
216
217 #endif // _LOGSTREAM_H
218