head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.38; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.54; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.01; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.57; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# logstream.hxx

Go to the documentation of this file.

    00001
    00005 // Written by Bernie Bright, 1998
    00006 //
    00007 // Copyright (C) 1998  Bernie Bright - bbright@@c031.aone.net.au
    00008 //
    00009 // This library is free software; you can redistribute it and/or
    00010 // modify it under the terms of the GNU Library General Public
    00011 // License as published by the Free Software Foundation; either
    00012 // version 2 of the License, or (at your option) any later version.
    00013 //
    00014 // This library is distributed in the hope that it will be useful,
    00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00017 // Library General Public License for more details.
    00018 //
    00019 // You should have received a copy of the GNU Library General Public
    00020 // License along with this library; if not, write to the
    00021 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00022 // Boston, MA  02111-1307, USA.
    00023 //
    00024 // $Id: logstream.hxx,v 1.13 2002/05/10 21:33:12 david Exp $
    00025
    00026 #ifndef _LOGSTREAM_H
    00027 #define _LOGSTREAM_H
    00028
    00029 #include <simgear/compiler.h>
    00030
    00031 // At least Irix needs this
    00032 #ifdef SG_HAVE_NATIVE_SGI_COMPILERS
    00033 #include <char_traits.h>
    00034 #endif
    00035
    00036 #ifdef SG_HAVE_STD_INCLUDES
    00037 # include <streambuf>
    00038 # include <iostream>
    00039 #else
    00040 # include <iostream.h>
    00041 # include <simgear/sg_traits.hxx>
    00042 #endif
    00043
    00044 #include <simgear/debug/debug_types.h>
    00045
    00046 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
    00047 SG_USING_STD(streambuf);
    00048 SG_USING_STD(ostream);
    00049 SG_USING_STD(cerr);
    00050 SG_USING_STD(endl);
    00051 #else
    00052 SG_USING_STD(char_traits);
    00053 #endif
    00054
    00055 #ifdef __MWERKS__
    00056 SG_USING_STD(iostream);
    00057 #endif
    00058
    00059 //
    00060 // TODO:
    00061 //
    00062 // 1. Change output destination. Done.
    00063 // 2. Make logbuf thread safe.
    00064 // 3. Read environment for default debugClass and debugPriority.
    00065 //
    00066
    00072 class logbuf : public streambuf
    00073 {
    00074 public:
    00075
    00076 #ifndef SG_HAVE_STD_INCLUDES
    00077     typedef char_traits<char>           traits_type;
    00078     typedef char_traits<char>::int_type int_type;
    00079     // typedef char_traits<char>::pos_type pos_type;
    00080     // typedef char_traits<char>::off_type off_type;
    00081 #endif
    00082     // logbuf( streambuf* sb ) : sbuf(sb) {}
    00084     logbuf();
    00085
    00087     ~logbuf();
    00088
    00092     bool enabled() { return logging_enabled; }
    00093
    00099     void set_log_state( sgDebugClass c, sgDebugPriority p );
    00100
    00106     static void set_log_level( sgDebugClass c, sgDebugPriority p );
    00107
    00108
    00113     static void set_log_classes (sgDebugClass c);
    00114
    00115
    00120     static sgDebugClass get_log_classes ();
    00121
    00122
    00127     static void set_log_priority (sgDebugPriority p);
    00128
    00129
    00134     static sgDebugPriority get_log_priority ();
    00135
    00136
    00141     void set_sb( streambuf* sb );
    00142
    00143 protected:
    00144
    00146     inline virtual int sync();
    00147
    00149     int_type overflow( int ch );
    00150     // int xsputn( const char* s, istreamsize n );
    00151
    00152 private:
    00153
    00154     // The streambuf used for actual output. Defaults to cerr.rdbuf().
    00155     static streambuf* sbuf;
    00156
    00157     static bool logging_enabled;
    00158     static sgDebugClass logClass;
    00159     static sgDebugPriority logPriority;
    00160
    00161 private:
    00162
    00163     // Not defined.
    00164     logbuf( const logbuf& );
    00165     void operator= ( const logbuf& );
    00166 };
    00167
    00168 inline int
    00169 logbuf::sync()
    00170 {
    00171 #ifdef SG_HAVE_STD_INCLUDES
    00172         return sbuf->pubsync();
    00173 #else
    00174         return sbuf->sync();
    00175 #endif
    00176 }
    00177
    00178 inline void
    00179 logbuf::set_log_state( sgDebugClass c, sgDebugPriority p )
    00180 {
    00181     logging_enabled = ((c & logClass) != 0 && p >= logPriority);
    00182 }
    00183
    00184 inline logbuf::int_type
    00185 logbuf::overflow( int c )
    00186 {
    00187     return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
    00188 }
    00189
    00193 struct loglevel
    00194 {
    00195     loglevel( sgDebugClass c, sgDebugPriority p )
    00196         : logClass(c), logPriority(p) {}
    00197
    00198     sgDebugClass logClass;
    00199     sgDebugPriority logPriority;
    00200 };
    00201
    00209 struct logstream_base
    00210 {
    00211     // logstream_base( streambuf* sb ) : lbuf(sb) {}
    00212     logstream_base() {}
    00213
    00214     logbuf lbuf;
    00215 };
    00216
    00220 class logstream : private logstream_base, public ostream
    00221 {
    00222 public:
    00227     logstream( ostream& out )
    00228         // : logstream_base(out.rdbuf()),
    00229         : logstream_base(),
    00230           ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
    00231
    00236     void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
    00237
    00243     void setLogLevels( sgDebugClass c, sgDebugPriority p );
    00244
    00249     inline ostream& operator<< ( const loglevel& l );
    00250 };
    00251
    00252 inline ostream&
    00253 logstream::operator<< ( const loglevel& l )
    00254 {
    00255     lbuf.set_log_state( l.logClass, l.logPriority );
    00256     return *this;
    00257 }
    00258
    00259 extern logstream *global_logstream;
    00260
    00268 inline logstream&
    00269 sglog()
    00270 {
    00271   if (global_logstream == NULL) {
    00272
    00273 #ifdef __APPLE__
    00274
    00281     cout << "Using Mac OS X hack for initializing C++ stdio..." << endl;
    00282 #endif
    00283     global_logstream = new logstream (cerr);
    00284   }
    00285
    00286   return *global_logstream;
    00287 }
    00288
    00289
    00296 #ifdef FG_NDEBUG
    00297 # define SG_LOG(C,P,M)
    00298 #elif defined( __MWERKS__ )
    00299 # define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl
    00300 #else
    00301 # define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << endl
    00302 #endif
    00303
    00304
    00305 #endif // _LOGSTREAM_H
    00306

@ 1.3 log @Oops, try again. @ text @d221 1 a221 1