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