X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fdebug%2Flogstream.hxx;h=c2a5edc5b6d7110b5fc68462ae75f602c8384e78;hb=6a7c2000027cd22eea603e936ddbad1a5bfc8b04;hp=c4ea7834538ca3db47421f826abf66c8c6142e69;hpb=a25eebef9b4cd549da9417029f2f18996f26afbf;p=simgear.git diff --git a/simgear/debug/logstream.hxx b/simgear/debug/logstream.hxx index c4ea7834..c2a5edc5 100644 --- a/simgear/debug/logstream.hxx +++ b/simgear/debug/logstream.hxx @@ -31,25 +31,14 @@ # include #endif -#ifdef SG_HAVE_STD_INCLUDES -# include -# include -#else -# include -# include -#endif +#include +#include +#include #include -SG_USING_STD(streambuf); -SG_USING_STD(ostream); -SG_USING_STD(cout); -SG_USING_STD(cerr); -SG_USING_STD(endl); - -#ifdef __MWERKS__ -SG_USING_STD(iostream); -#endif +using std::streambuf; +using std::ostream; // // TODO: @@ -67,17 +56,10 @@ SG_USING_STD(iostream); #ifdef SG_NEED_STREAMBUF_HACK class logbuf : public __streambuf #else -class logbuf : public streambuf +class logbuf : public std::streambuf #endif { public: - -#ifndef SG_HAVE_STD_INCLUDES - typedef char_traits traits_type; - typedef char_traits::int_type int_type; - // typedef char_traits::pos_type pos_type; - // typedef char_traits::off_type off_type; -#endif // logbuf( streambuf* sb ) : sbuf(sb) {} /** Constructor */ logbuf(); @@ -97,6 +79,8 @@ public: */ void set_log_state( sgDebugClass c, sgDebugPriority p ); + bool would_log( sgDebugClass c, sgDebugPriority p ) const; + /** * Set the global logging level. * @param c debug class @@ -137,7 +121,7 @@ public: * Set the stream buffer * @param sb stream buffer */ - void set_sb( streambuf* sb ); + void set_sb( std::streambuf* sb ); #ifdef _MSC_VER static void has_no_console() { has_console = false; } @@ -155,7 +139,7 @@ protected: private: // The streambuf used for actual output. Defaults to cerr.rdbuf(). - static streambuf* sbuf; + static std::streambuf* sbuf; static bool logging_enabled; #ifdef _MSC_VER @@ -174,11 +158,7 @@ private: inline int logbuf::sync() { -#ifdef SG_HAVE_STD_INCLUDES return sbuf->pubsync(); -#else - return sbuf->sync(); -#endif } inline void @@ -187,6 +167,12 @@ logbuf::set_log_state( sgDebugClass c, sgDebugPriority p ) logging_enabled = ((c & logClass) != 0 && p >= logPriority); } +inline bool +logbuf::would_log( sgDebugClass c, sgDebugPriority p ) const +{ + return ((c & logClass) != 0 && p >= logPriority); +} + inline logbuf::int_type logbuf::overflow( int c ) { @@ -238,23 +224,23 @@ struct logstream_base /** * Class to manage the debug logging stream. */ -class logstream : private logstream_base, public ostream +class logstream : private logstream_base, public std::ostream { public: /** * The default is to send messages to cerr. * @param out output stream */ - logstream( ostream& out ) + logstream( std::ostream& out ) // : logstream_base(out.rdbuf()), : logstream_base(), - ostream(&lbuf) { lbuf.set_sb(out.rdbuf());} + std::ostream(&lbuf) { lbuf.set_sb(out.rdbuf());} /** * Set the output stream * @param out output stream */ - void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); } + void set_output( std::ostream& out ) { lbuf.set_sb( out.rdbuf() ); } /** * Set the global log class and priority level. @@ -263,22 +249,29 @@ public: */ void setLogLevels( sgDebugClass c, sgDebugPriority p ); + bool would_log( sgDebugClass c, sgDebugPriority p ) const + { + return lbuf.would_log( c, p ); + }; + /** * Output operator to capture the debug level and priority of a message. * @param l log level */ - inline ostream& operator<< ( const loglevel& l ); + inline std::ostream& operator<< ( const loglevel& l ); + friend logstream& sglog(); + static logstream *initGlobalLogstream(); +protected: + static logstream *global_logstream; }; -inline ostream& +inline std::ostream& logstream::operator<< ( const loglevel& l ) { lbuf.set_log_state( l.logClass, l.logPriority ); return *this; } -extern logstream *global_logstream; - /** * \relates logstream * Return the one and only logstream instance. @@ -289,22 +282,7 @@ extern logstream *global_logstream; inline logstream& sglog() { - if (global_logstream == NULL) { - -#ifdef __APPLE__ - /** - * There appears to be a bug in the C++ runtime in Mac OS X that - * will crash if certain funtions are called (in this case - * cerr.rdbuf()) during static initialization of a class. This - * print statement is hack to kick the library in the pants so it - * won't crash when cerr.rdbuf() is first called -DW - **/ - cout << "Using Mac OS X hack for initializing C++ stdio..." << endl; -#endif - global_logstream = new logstream (cerr); - } - - return *global_logstream; + return *logstream::initGlobalLogstream(); } @@ -316,15 +294,15 @@ sglog() */ #ifdef FG_NDEBUG # define SG_LOG(C,P,M) -#elif defined( __MWERKS__ ) -# define SG_LOG(C,P,M) ::sglog() << ::loglevel(C,P) << M << std::endl #else -# define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << endl +# define SG_LOG(C,P,M) do { \ + logstream& __tmplogstreamref(sglog()); \ + if(__tmplogstreamref.would_log(C,P)) { \ + __tmplogstreamref << loglevel(C,P) << M << std::endl; } \ + } while(0) #endif -#define SG_STRINGIFY(x) #x -#define SG_TOSTRING(x) SG_STRINGIFY(x) -#define SG_ORIGIN __FILE__ ":" SG_TOSTRING(__LINE__) +#define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__) #endif // _LOGSTREAM_H