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