]> git.mxchange.org Git - simgear.git/blob - simgear/debug/logstream.hxx
remove redundant #defines (they are already in compiler.h)
[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 General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23 // $Id$
24
25 #ifndef _LOGSTREAM_H
26 #define _LOGSTREAM_H
27
28 #include <simgear/compiler.h>
29
30 #ifdef _MSC_VER
31 #  include <windows.h>
32 #endif
33
34 #ifdef SG_HAVE_STD_INCLUDES
35 # include <streambuf>
36 # include <ostream>
37 #else
38 # include <iostream.h>
39 # include <simgear/sg_traits.hxx>
40 #endif
41
42 #include <simgear/debug/debug_types.h>
43
44 SG_USING_STD(streambuf);
45 SG_USING_STD(ostream);
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 std::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( std::streambuf* sb );
138
139 #ifdef _MSC_VER
140     static void has_no_console() { has_console = false; }
141 #endif
142
143 protected:
144
145     /** sync/flush */
146     inline virtual int sync();
147
148     /** overflow */
149     int_type overflow( int ch );
150     // int xsputn( const char* s, istreamsize n );
151
152 private:
153
154     // The streambuf used for actual output. Defaults to cerr.rdbuf().
155     static std::streambuf* sbuf;
156
157     static bool logging_enabled;
158 #ifdef _MSC_VER
159     static bool has_console;
160 #endif
161     static sgDebugClass logClass;
162     static sgDebugPriority logPriority;
163
164 private:
165
166     // Not defined.
167     logbuf( const logbuf& );
168     void operator= ( const logbuf& );
169 };
170
171 inline int
172 logbuf::sync()
173 {
174 #ifdef SG_HAVE_STD_INCLUDES
175         return sbuf->pubsync();
176 #else
177         return sbuf->sync();
178 #endif
179 }
180
181 inline void
182 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
183 {
184     logging_enabled = ((c & logClass) != 0 && p >= logPriority);
185 }
186
187 inline logbuf::int_type
188 logbuf::overflow( int c )
189 {
190 #ifdef _MSC_VER
191     if ( logging_enabled ) {
192         if ( !has_console ) {
193             AllocConsole();
194             freopen("conin$", "r", stdin);
195             freopen("conout$", "w", stdout);
196             freopen("conout$", "w", stderr);
197             has_console = true;
198         }
199         return sbuf->sputc(c);
200     }
201     else
202         return EOF == 0 ? 1: 0;
203 #else
204     return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
205 #endif
206 }
207
208 /**
209  * logstream manipulator for setting the log level of a message.
210  */
211 struct loglevel
212 {
213     loglevel( sgDebugClass c, sgDebugPriority p )
214         : logClass(c), logPriority(p) {}
215
216     sgDebugClass logClass;
217     sgDebugPriority logPriority;
218 };
219
220 /**
221  * A helper class that ensures a streambuf and ostream are constructed and
222  * destroyed in the correct order.  The streambuf must be created before the
223  * ostream but bases are constructed before members.  Thus, making this class
224  * a private base of logstream, declared to the left of ostream, we ensure the
225  * correct order of construction and destruction.
226  */
227 struct logstream_base
228 {
229     // logstream_base( streambuf* sb ) : lbuf(sb) {}
230     logstream_base() {}
231
232     logbuf lbuf;
233 };
234
235 /**
236  * Class to manage the debug logging stream.
237  */
238 class logstream : private logstream_base, public std::ostream
239 {
240 public:
241     /**
242      * The default is to send messages to cerr.
243      * @param out output stream
244      */
245     logstream( std::ostream& out )
246         // : logstream_base(out.rdbuf()),
247         : logstream_base(),
248           std::ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
249
250     /**
251      * Set the output stream
252      * @param out output stream
253      */
254     void set_output( std::ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
255
256     /**
257      * Set the global log class and priority level.
258      * @param c debug class
259      * @param p priority
260      */
261     void setLogLevels( sgDebugClass c, sgDebugPriority p );
262
263     /**
264      * Output operator to capture the debug level and priority of a message.
265      * @param l log level
266      */
267     inline std::ostream& operator<< ( const loglevel& l );
268     friend logstream& sglog();
269 protected:
270     static logstream *global_logstream;
271     static void initGlobalLogstream();
272 };
273
274 inline std::ostream&
275 logstream::operator<< ( const loglevel& l )
276 {
277     lbuf.set_log_state( l.logClass, l.logPriority );
278     return *this;
279 }
280
281 /**
282  * \relates logstream
283  * Return the one and only logstream instance.
284  * We use a function instead of a global object so we are assured that cerr
285  * has been initialised.
286  * @return current logstream
287  */
288 inline logstream&
289 sglog()
290 {
291   if (logstream::global_logstream == NULL) {
292       logstream::initGlobalLogstream();
293   }
294   return *logstream::global_logstream;
295 }
296
297
298 /** \def SG_LOG(C,P,M)
299  * Log a message.
300  * @param C debug class
301  * @param P priority
302  * @param M message
303  */
304 #ifdef FG_NDEBUG
305 # define SG_LOG(C,P,M)
306 #elif defined( __MWERKS__ )
307 # define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl
308 #else
309 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << std::endl
310 #endif
311
312 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
313
314 #endif // _LOGSTREAM_H
315