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