]> git.mxchange.org Git - simgear.git/blobdiff - simgear/debug/logstream.hxx
Merge branch 'fredb/winfix'
[simgear.git] / simgear / debug / logstream.hxx
index 1b97803ee41ee3cbcbf4bf4ee3a778179fc83867..d3649a4a21b3cf5912dd20778d9daf39abb37b5b 100644 (file)
 
 #include <simgear/compiler.h>
 
-#ifdef _MSC_VER
+#ifdef _WIN32
 #  include <windows.h>
 #endif
 
-#ifdef SG_HAVE_STD_INCLUDES
-# include <streambuf>
-# include <ostream>
-#else
-# include <iostream.h>
-# include <simgear/sg_traits.hxx>
-#endif
+#include <streambuf>
+#include <ostream>
+#include <cstdio>
 
 #include <simgear/debug/debug_types.h>
 
-SG_USING_STD(streambuf);
-SG_USING_STD(ostream);
+using std::streambuf;
+using std::ostream;
 
 //
 // TODO:
@@ -64,13 +60,6 @@ class logbuf : public std::streambuf
 #endif
 {
 public:
-
-#ifndef SG_HAVE_STD_INCLUDES
-    typedef char_traits<char>           traits_type;
-    typedef char_traits<char>::int_type int_type;
-    // typedef char_traits<char>::pos_type pos_type;
-    // typedef char_traits<char>::off_type off_type;
-#endif
     // logbuf( streambuf* sb ) : sbuf(sb) {}
     /** Constructor */
     logbuf();
@@ -90,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
@@ -132,7 +123,7 @@ public:
      */
     void set_sb( std::streambuf* sb );
 
-#ifdef _MSC_VER
+#ifdef _WIN32
     static void has_no_console() { has_console = false; }
 #endif
 
@@ -151,7 +142,7 @@ private:
     static std::streambuf* sbuf;
 
     static bool logging_enabled;
-#ifdef _MSC_VER
+#ifdef _WIN32
     static bool has_console;
 #endif
     static sgDebugClass logClass;
@@ -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,10 +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
+#ifdef _WIN32
     if ( logging_enabled ) {
         if ( !has_console ) {
             AllocConsole();
@@ -256,15 +249,20 @@ 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 std::ostream& operator<< ( const loglevel& l );
     friend logstream& sglog();
+    static logstream *initGlobalLogstream();
 protected:
     static logstream *global_logstream;
-    static void initGlobalLogstream();
 };
 
 inline std::ostream&
@@ -284,10 +282,7 @@ logstream::operator<< ( const loglevel& l )
 inline logstream&
 sglog()
 {
-  if (logstream::global_logstream == NULL) {
-      logstream::initGlobalLogstream();
-  }
-  return *logstream::global_logstream;
+    return *logstream::initGlobalLogstream();
 }
 
 
@@ -300,7 +295,11 @@ sglog()
 #ifdef FG_NDEBUG
 # define SG_LOG(C,P,M)
 #else
-# define SG_LOG(C,P,M) sglog() << loglevel(C,P) << M << std::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__)