]> git.mxchange.org Git - simgear.git/blob - simgear/debug/logstream.hxx
Fix a build order problem.
[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 library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24 #ifndef _LOGSTREAM_H
25 #define _LOGSTREAM_H
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31
32 #include <simgear/compiler.h>
33
34 #ifdef FG_HAVE_STD_INCLUDES
35 # include <streambuf>
36 # include <iostream>
37 #else
38 # include <iostream.h>
39 # include <simgear/fg_traits.hxx>
40 #endif
41
42 #include <simgear/debug/debug_types.h>
43
44 #ifndef FG_HAVE_NATIVE_SGI_COMPILERS
45 FG_USING_STD(streambuf);
46 FG_USING_STD(ostream);
47 FG_USING_STD(cerr);
48 FG_USING_STD(endl);
49 #endif
50
51 #ifdef __MWERKS__
52 FG_USING_STD(iostream);
53 #endif
54
55 //
56 // TODO:
57 //
58 // 1. Change output destination. Done.
59 // 2. Make logbuf thread safe.
60 // 3. Read environment for default debugClass and debugPriority.
61 //
62
63 //-----------------------------------------------------------------------------
64 //
65 // logbuf is an output-only streambuf with the ability to disable sets of
66 // messages at runtime. Only messages with priority >= logbuf::logPriority
67 // and debugClass == logbuf::logClass are output.
68 //
69 class logbuf : public streambuf
70 {
71 public:
72
73 #ifndef FG_HAVE_STD_INCLUDES
74     typedef char_traits<char>           traits_type;
75     typedef char_traits<char>::int_type int_type;
76     typedef char_traits<char>::pos_type pos_type;
77     typedef char_traits<char>::off_type off_type;
78 #endif
79 //     logbuf( streambuf* sb ) : sbuf(sb) {}
80     logbuf();
81     ~logbuf();
82
83     // Is logging enabled?
84     bool enabled() { return logging_enabled; }
85
86     // Set the logging level of subsequent messages.
87     void set_log_state( fgDebugClass c, fgDebugPriority p );
88
89     // Set the global logging level.
90     static void set_log_level( fgDebugClass c, fgDebugPriority p );
91
92     //
93     void set_sb( streambuf* sb );
94
95 protected:
96
97     inline virtual int sync();
98     int_type overflow( int ch );
99 //     int xsputn( const char* s, istreamsize n );
100
101 private:
102
103     // The streambuf used for actual output. Defaults to cerr.rdbuf().
104     static streambuf* sbuf;
105
106     static bool logging_enabled;
107     static fgDebugClass logClass;
108     static fgDebugPriority logPriority;
109
110 private:
111
112     // Not defined.
113     logbuf( const logbuf& );
114     void operator= ( const logbuf& );
115 };
116
117 inline int
118 logbuf::sync()
119 {
120 #ifdef FG_HAVE_STD_INCLUDES
121         return sbuf->pubsync();
122 #else
123         return sbuf->sync();
124 #endif
125 }
126
127 inline void
128 logbuf::set_log_state( fgDebugClass c, fgDebugPriority p )
129 {
130     logging_enabled = ((c & logClass) != 0 && p >= logPriority);
131 }
132
133 inline logbuf::int_type
134 logbuf::overflow( int c )
135 {
136     return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
137 }
138
139 //-----------------------------------------------------------------------------
140 //
141 // logstream manipulator for setting the log level of a message.
142 //
143 struct loglevel
144 {
145     loglevel( fgDebugClass c, fgDebugPriority p )
146         : logClass(c), logPriority(p) {}
147
148     fgDebugClass logClass;
149     fgDebugPriority logPriority;
150 };
151
152 //-----------------------------------------------------------------------------
153 //
154 // A helper class that ensures a streambuf and ostream are constructed and
155 // destroyed in the correct order.  The streambuf must be created before the
156 // ostream but bases are constructed before members.  Thus, making this class
157 // a private base of logstream, declared to the left of ostream, we ensure the
158 // correct order of construction and destruction.
159 //
160 struct logstream_base
161 {
162 //     logstream_base( streambuf* sb ) : lbuf(sb) {}
163     logstream_base() {}
164
165     logbuf lbuf;
166 };
167
168 //-----------------------------------------------------------------------------
169 //
170 // 
171 //
172 class logstream : private logstream_base, public ostream
173 {
174 public:
175     // The default is to send messages to cerr.
176     logstream( ostream& out )
177 //      : logstream_base(out.rdbuf()),
178         : logstream_base(),
179           ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
180
181     void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
182
183     // Set the global log class and priority level.
184      void setLogLevels( fgDebugClass c, fgDebugPriority p );
185
186     // Output operator to capture the debug level and priority of a message.
187     inline ostream& operator<< ( const loglevel& l );
188 };
189
190 inline ostream&
191 logstream::operator<< ( const loglevel& l )
192 {
193     lbuf.set_log_state( l.logClass, l.logPriority );
194     return *this;
195 }
196
197 //-----------------------------------------------------------------------------
198 //
199 // Return the one and only logstream instance.
200 // We use a function instead of a global object so we are assured that cerr
201 // has been initialised.
202 //
203 inline logstream&
204 fglog()
205 {
206     static logstream logstrm( cerr );
207     return logstrm;
208 }
209
210 #ifdef FG_NDEBUG
211 # define FG_LOG(C,P,M)
212 #elif defined( __MWERKS__ )
213 # define FG_LOG(C,P,M) ::fglog() << ::loglevel(C,P) << M << std::endl
214 #else
215 # define FG_LOG(C,P,M) fglog() << loglevel(C,P) << M << endl
216 #endif
217
218 #endif // _LOGSTREAM_H
219