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