X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fdebug%2Flogstream.hxx;h=d3649a4a21b3cf5912dd20778d9daf39abb37b5b;hb=a668e47fa180ca6efb592a9406dcbe9909d69adf;hp=98e5e3269e37ed3057aed7c6192670b71e86f9a1;hpb=dcb95d131bc6aef1abe25d1f415e309f06e52436;p=simgear.git diff --git a/simgear/debug/logstream.hxx b/simgear/debug/logstream.hxx index 98e5e326..d3649a4a 100644 --- a/simgear/debug/logstream.hxx +++ b/simgear/debug/logstream.hxx @@ -27,29 +27,18 @@ #include -#ifdef _MSC_VER +#ifdef _WIN32 # 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,11 @@ public: * Set the stream buffer * @param sb stream buffer */ - void set_sb( streambuf* sb ); + void set_sb( std::streambuf* sb ); + +#ifdef _WIN32 + static void has_no_console() { has_console = false; } +#endif protected: @@ -151,9 +139,12 @@ protected: private: // The streambuf used for actual output. Defaults to cerr.rdbuf(). - static streambuf* sbuf; + static std::streambuf* sbuf; static bool logging_enabled; +#ifdef _WIN32 + static bool has_console; +#endif static sgDebugClass logClass; static sgDebugPriority logPriority; @@ -167,11 +158,7 @@ private: inline int logbuf::sync() { -#ifdef SG_HAVE_STD_INCLUDES return sbuf->pubsync(); -#else - return sbuf->sync(); -#endif } inline void @@ -180,11 +167,16 @@ 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 ) { -#ifdef _MSC_VER - static bool has_console = false; +#ifdef _WIN32 if ( logging_enabled ) { if ( !has_console ) { AllocConsole(); @@ -232,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. @@ -257,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. @@ -283,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(); } @@ -310,12 +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_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__) #endif // _LOGSTREAM_H