]> git.mxchange.org Git - simgear.git/blob - simgear/debug/logstream.hxx
A couple more sanity checking tweaks for texture freeing.
[simgear.git] / simgear / debug / logstream.hxx
1 /** \file logstream.hxx
2  * Stream based logging mechanism.
3  */
4
5 // Written by Bernie Bright, 1998
6 //
7 // Copyright (C) 1998  Bernie Bright - bbright@c031.aone.net.au
8 //
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.
13 //
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.
18 //
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.
23 //
24 // $Id$
25
26 #ifndef _LOGSTREAM_H
27 #define _LOGSTREAM_H
28
29 #include <simgear/compiler.h>
30
31 #ifdef SG_HAVE_STD_INCLUDES
32 # include <streambuf>
33 # include <iostream>
34 #else
35 # include <iostream.h>
36 # include <simgear/sg_traits.hxx>
37 #endif
38
39 #include <simgear/debug/debug_types.h>
40
41 SG_USING_STD(streambuf);
42 SG_USING_STD(ostream);
43 SG_USING_STD(cout);
44 SG_USING_STD(cerr);
45 SG_USING_STD(endl);
46
47 #ifdef __MWERKS__
48 SG_USING_STD(iostream);
49 #endif
50
51 //
52 // TODO:
53 //
54 // 1. Change output destination. Done.
55 // 2. Make logbuf thread safe.
56 // 3. Read environment for default debugClass and debugPriority.
57 //
58
59 /**
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.
63  */
64 #ifdef SG_NEED_STREAMBUF_HACK
65 class logbuf : public __streambuf
66 #else
67 class logbuf : public streambuf
68 #endif
69 {
70 public:
71
72 #ifndef SG_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     /** Constructor */
80     logbuf();
81
82     /** Destructor */
83     ~logbuf();
84
85     /**
86      * Is logging enabled?
87      * @return true or false*/
88     bool enabled() { return logging_enabled; }
89
90     /**
91      * Set the logging level of subsequent messages.
92      * @param c debug class
93      * @param p priority
94      */
95     void set_log_state( sgDebugClass c, sgDebugPriority p );
96
97     /**
98      * Set the global logging level.
99      * @param c debug class
100      * @param p priority
101      */
102     static void set_log_level( sgDebugClass c, sgDebugPriority p );
103
104
105     /**
106      * Set the allowed logging classes.
107      * @param c All enabled logging classes anded together.
108      */
109     static void set_log_classes (sgDebugClass c);
110
111
112     /**
113      * Get the logging classes currently enabled.
114      * @return All enabled debug logging anded together.
115      */
116     static sgDebugClass get_log_classes ();
117
118
119     /**
120      * Set the logging priority.
121      * @param c The priority cutoff for logging messages.
122      */
123     static void set_log_priority (sgDebugPriority p);
124
125
126     /**
127      * Get the current logging priority.
128      * @return The priority cutoff for logging messages.
129      */
130     static sgDebugPriority get_log_priority ();
131
132
133     /**
134      * Set the stream buffer
135      * @param sb stream buffer
136      */
137     void set_sb( streambuf* sb );
138
139 protected:
140
141     /** sync/flush */
142     inline virtual int sync();
143
144     /** overflow */
145     int_type overflow( int ch );
146     // int xsputn( const char* s, istreamsize n );
147
148 private:
149
150     // The streambuf used for actual output. Defaults to cerr.rdbuf().
151     static streambuf* sbuf;
152
153     static bool logging_enabled;
154     static sgDebugClass logClass;
155     static sgDebugPriority logPriority;
156
157 private:
158
159     // Not defined.
160     logbuf( const logbuf& );
161     void operator= ( const logbuf& );
162 };
163
164 inline int
165 logbuf::sync()
166 {
167 #ifdef SG_HAVE_STD_INCLUDES
168         return sbuf->pubsync();
169 #else
170         return sbuf->sync();
171 #endif
172 }
173
174 inline void
175 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
176 {
177     logging_enabled = ((c & logClass) != 0 && p >= logPriority);
178 }
179
180 inline logbuf::int_type
181 logbuf::overflow( int c )
182 {
183     return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
184 }
185
186 /**
187  * logstream manipulator for setting the log level of a message.
188  */
189 struct loglevel
190 {
191     loglevel( sgDebugClass c, sgDebugPriority p )
192         : logClass(c), logPriority(p) {}
193
194     sgDebugClass logClass;
195     sgDebugPriority logPriority;
196 };
197
198 /**
199  * A helper class that ensures a streambuf and ostream are constructed and
200  * destroyed in the correct order.  The streambuf must be created before the
201  * ostream but bases are constructed before members.  Thus, making this class
202  * a private base of logstream, declared to the left of ostream, we ensure the
203  * correct order of construction and destruction.
204  */
205 struct logstream_base
206 {
207     // logstream_base( streambuf* sb ) : lbuf(sb) {}
208     logstream_base() {}
209
210     logbuf lbuf;
211 };
212
213 /**
214  * Class to manage the debug logging stream.
215  */
216 class logstream : private logstream_base, public ostream
217 {
218 public:
219     /**
220      * The default is to send messages to cerr.
221      * @param out output stream
222      */
223     logstream( ostream& out )
224         // : logstream_base(out.rdbuf()),
225         : logstream_base(),
226           ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
227
228     /**
229      * Set the output stream
230      * @param out output stream
231      */
232     void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
233
234     /**
235      * Set the global log class and priority level.
236      * @param c debug class
237      * @param p priority
238      */
239     void setLogLevels( sgDebugClass c, sgDebugPriority p );
240
241     /**
242      * Output operator to capture the debug level and priority of a message.
243      * @param l log level
244      */
245     inline ostream& operator<< ( const loglevel& l );
246 };
247
248 inline ostream&
249 logstream::operator<< ( const loglevel& l )
250 {
251     lbuf.set_log_state( l.logClass, l.logPriority );
252     return *this;
253 }
254
255 extern logstream *global_logstream;
256
257 /**
258  * \relates logstream
259  * Return the one and only logstream instance.
260  * We use a function instead of a global object so we are assured that cerr
261  * has been initialised.
262  * @return current logstream
263  */
264 inline logstream&
265 sglog()
266 {
267   if (global_logstream == NULL) {
268
269 #ifdef __APPLE__
270     /**
271      * There appears to be a bug in the C++ runtime in Mac OS X that
272      * will crash if certain funtions are called (in this case
273      * cerr.rdbuf()) during static initialization of a class. This
274      * print statement is hack to kick the library in the pants so it
275      * won't crash when cerr.rdbuf() is first called -DW 
276      **/
277     cout << "Using Mac OS X hack for initializing C++ stdio..." << endl;
278 #endif    
279     global_logstream = new logstream (cerr);
280   }
281     
282   return *global_logstream;
283 }
284
285
286 /** \def SG_LOG(C,P,M)
287  * Log a message.
288  * @param C debug class
289  * @param P priority
290  * @param M message
291  */
292 #ifdef FG_NDEBUG
293 # define SG_LOG(C,P,M)
294 #elif defined( __MWERKS__ )
295 # define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl
296 #else
297 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << endl
298 #endif
299
300
301 #endif // _LOGSTREAM_H
302