]> git.mxchange.org Git - simgear.git/blob - simgear/debug/logstream.hxx
1b97803ee41ee3cbcbf4bf4ee3a778179fc83867
[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 //
48 // TODO:
49 //
50 // 1. Change output destination. Done.
51 // 2. Make logbuf thread safe.
52 // 3. Read environment for default debugClass and debugPriority.
53 //
54
55 /**
56  * logbuf is an output-only streambuf with the ability to disable sets of
57  * messages at runtime. Only messages with priority >= logbuf::logPriority
58  * and debugClass == logbuf::logClass are output.
59  */
60 #ifdef SG_NEED_STREAMBUF_HACK
61 class logbuf : public __streambuf
62 #else
63 class logbuf : public std::streambuf
64 #endif
65 {
66 public:
67
68 #ifndef SG_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;
73 #endif
74     // logbuf( streambuf* sb ) : sbuf(sb) {}
75     /** Constructor */
76     logbuf();
77
78     /** Destructor */
79     ~logbuf();
80
81     /**
82      * Is logging enabled?
83      * @return true or false*/
84     bool enabled() { return logging_enabled; }
85
86     /**
87      * Set the logging level of subsequent messages.
88      * @param c debug class
89      * @param p priority
90      */
91     void set_log_state( sgDebugClass c, sgDebugPriority p );
92
93     /**
94      * Set the global logging level.
95      * @param c debug class
96      * @param p priority
97      */
98     static void set_log_level( sgDebugClass c, sgDebugPriority p );
99
100
101     /**
102      * Set the allowed logging classes.
103      * @param c All enabled logging classes anded together.
104      */
105     static void set_log_classes (sgDebugClass c);
106
107
108     /**
109      * Get the logging classes currently enabled.
110      * @return All enabled debug logging anded together.
111      */
112     static sgDebugClass get_log_classes ();
113
114
115     /**
116      * Set the logging priority.
117      * @param c The priority cutoff for logging messages.
118      */
119     static void set_log_priority (sgDebugPriority p);
120
121
122     /**
123      * Get the current logging priority.
124      * @return The priority cutoff for logging messages.
125      */
126     static sgDebugPriority get_log_priority ();
127
128
129     /**
130      * Set the stream buffer
131      * @param sb stream buffer
132      */
133     void set_sb( std::streambuf* sb );
134
135 #ifdef _MSC_VER
136     static void has_no_console() { has_console = false; }
137 #endif
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 std::streambuf* sbuf;
152
153     static bool logging_enabled;
154 #ifdef _MSC_VER
155     static bool has_console;
156 #endif
157     static sgDebugClass logClass;
158     static sgDebugPriority logPriority;
159
160 private:
161
162     // Not defined.
163     logbuf( const logbuf& );
164     void operator= ( const logbuf& );
165 };
166
167 inline int
168 logbuf::sync()
169 {
170 #ifdef SG_HAVE_STD_INCLUDES
171         return sbuf->pubsync();
172 #else
173         return sbuf->sync();
174 #endif
175 }
176
177 inline void
178 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
179 {
180     logging_enabled = ((c & logClass) != 0 && p >= logPriority);
181 }
182
183 inline logbuf::int_type
184 logbuf::overflow( int c )
185 {
186 #ifdef _MSC_VER
187     if ( logging_enabled ) {
188         if ( !has_console ) {
189             AllocConsole();
190             freopen("conin$", "r", stdin);
191             freopen("conout$", "w", stdout);
192             freopen("conout$", "w", stderr);
193             has_console = true;
194         }
195         return sbuf->sputc(c);
196     }
197     else
198         return EOF == 0 ? 1: 0;
199 #else
200     return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
201 #endif
202 }
203
204 /**
205  * logstream manipulator for setting the log level of a message.
206  */
207 struct loglevel
208 {
209     loglevel( sgDebugClass c, sgDebugPriority p )
210         : logClass(c), logPriority(p) {}
211
212     sgDebugClass logClass;
213     sgDebugPriority logPriority;
214 };
215
216 /**
217  * A helper class that ensures a streambuf and ostream are constructed and
218  * destroyed in the correct order.  The streambuf must be created before the
219  * ostream but bases are constructed before members.  Thus, making this class
220  * a private base of logstream, declared to the left of ostream, we ensure the
221  * correct order of construction and destruction.
222  */
223 struct logstream_base
224 {
225     // logstream_base( streambuf* sb ) : lbuf(sb) {}
226     logstream_base() {}
227
228     logbuf lbuf;
229 };
230
231 /**
232  * Class to manage the debug logging stream.
233  */
234 class logstream : private logstream_base, public std::ostream
235 {
236 public:
237     /**
238      * The default is to send messages to cerr.
239      * @param out output stream
240      */
241     logstream( std::ostream& out )
242         // : logstream_base(out.rdbuf()),
243         : logstream_base(),
244           std::ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
245
246     /**
247      * Set the output stream
248      * @param out output stream
249      */
250     void set_output( std::ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
251
252     /**
253      * Set the global log class and priority level.
254      * @param c debug class
255      * @param p priority
256      */
257     void setLogLevels( sgDebugClass c, sgDebugPriority p );
258
259     /**
260      * Output operator to capture the debug level and priority of a message.
261      * @param l log level
262      */
263     inline std::ostream& operator<< ( const loglevel& l );
264     friend logstream& sglog();
265 protected:
266     static logstream *global_logstream;
267     static void initGlobalLogstream();
268 };
269
270 inline std::ostream&
271 logstream::operator<< ( const loglevel& l )
272 {
273     lbuf.set_log_state( l.logClass, l.logPriority );
274     return *this;
275 }
276
277 /**
278  * \relates logstream
279  * Return the one and only logstream instance.
280  * We use a function instead of a global object so we are assured that cerr
281  * has been initialised.
282  * @return current logstream
283  */
284 inline logstream&
285 sglog()
286 {
287   if (logstream::global_logstream == NULL) {
288       logstream::initGlobalLogstream();
289   }
290   return *logstream::global_logstream;
291 }
292
293
294 /** \def SG_LOG(C,P,M)
295  * Log a message.
296  * @param C debug class
297  * @param P priority
298  * @param M message
299  */
300 #ifdef FG_NDEBUG
301 # define SG_LOG(C,P,M)
302 #else
303 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << std::endl
304 #endif
305
306 #define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
307
308 #endif // _LOGSTREAM_H
309