]> git.mxchange.org Git - simgear.git/blobdiff - simgear/debug/logstream.hxx
Merge branch 'next' of git.mxchange.org:/var/cache/git/repos/simgear into next
[simgear.git] / simgear / debug / logstream.hxx
index bc1a4fbf77f360c5adc74cb5906b1037a3e35708..4e2771b9989857923be633eaf92019f3234a0a05 100644 (file)
-// Stream based logging mechanism.
-//
+/** \file logstream.hxx
+ * Stream based logging mechanism.
+ */
+
 // Written by Bernie Bright, 1998
 //
 // Copyright (C) 1998  Bernie Bright - bbright@c031.aone.net.au
 //
-// This program is free software; you can redistribute it and/or
-// modify it under the terms of the GNU General Public License as
-// published by the Free Software Foundation; either version 2 of the
-// License, or (at your option) any later version.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
 //
-// This program is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-// General Public License for more details.
+// Library General Public License for more details.
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
 #ifndef _LOGSTREAM_H
 #define _LOGSTREAM_H
 
-#ifdef HAVE_CONFIG_H
-#  include <config.h>
-#endif
-
-
 #include <simgear/compiler.h>
-
-#ifdef FG_HAVE_STD_INCLUDES
-# include <streambuf>
-# include <iostream>
-#else
-# include <iostream.h>
-# include <simgear/fg_traits.hxx>
-#endif
-
-#include <simgear/debug_types.h>
-
-#ifndef FG_HAVE_NATIVE_SGI_COMPILERS
-FG_USING_STD(streambuf);
-FG_USING_STD(ostream);
-FG_USING_STD(cerr);
-FG_USING_STD(endl);
-#endif
-
-#ifdef __MWERKS__
-#  define cerr std::cerr
-#  define endl std::endl
-FG_USING_STD(iostream);
-#endif
-
-//
-// TODO:
-//
-// 1. Change output destination. Done.
-// 2. Make logbuf thread safe.
-// 3. Read environment for default debugClass and debugPriority.
-//
-
-//-----------------------------------------------------------------------------
-//
-// logbuf is an output-only streambuf with the ability to disable sets of
-// messages at runtime. Only messages with priority >= logbuf::logPriority
-// and debugClass == logbuf::logClass are output.
-//
-class logbuf : public streambuf
+#include <simgear/debug/debug_types.h>
+
+#include <sstream>
+#include <vector>
+// forward decls
+class SGPath;
+      
+namespace simgear
+{
+     
+class LogCallback
 {
 public:
+    virtual ~LogCallback() {}
+    virtual void operator()(sgDebugClass c, sgDebugPriority p, 
+        const char* file, int line, const std::string& aMessage) = 0;
 
-#ifndef FG_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) {}
-    logbuf();
-    ~logbuf();
-
-    // Is logging enabled?
-    bool enabled() { return logging_enabled; }
-
-    // Set the logging level of subsequent messages.
-    void set_log_state( fgDebugClass c, fgDebugPriority p );
-
-    // Set the global logging level.
-    static void set_log_level( fgDebugClass c, fgDebugPriority p );
-
-    //
-    void set_sb( streambuf* sb );
-
+       void setLogLevels(sgDebugClass c, sgDebugPriority p);
 protected:
+       LogCallback(sgDebugClass c, sgDebugPriority p);
 
-    inline virtual int sync();
-    int_type overflow( int ch );
-//     int xsputn( const char* s, istreamsize n );
-
-private:
-
-    // The streambuf used for actual output. Defaults to cerr.rdbuf().
-    static streambuf* sbuf;
-
-    static bool logging_enabled;
-    static fgDebugClass logClass;
-    static fgDebugPriority logPriority;
-
+       bool shouldLog(sgDebugClass c, sgDebugPriority p) const;
 private:
-
-    // Not defined.
-    logbuf( const logbuf& );
-    void operator= ( const logbuf& );
+       sgDebugClass m_class;
+       sgDebugPriority m_priority;
 };
 
-inline int
-logbuf::sync()
-{
-#ifdef FG_HAVE_STD_INCLUDES
-       return sbuf->pubsync();
-#else
-       return sbuf->sync();
-#endif
-}
-
-inline void
-logbuf::set_log_state( fgDebugClass c, fgDebugPriority p )
-{
-    logging_enabled = ((c & logClass) != 0 && p >= logPriority);
-}
-
-inline logbuf::int_type
-logbuf::overflow( int c )
-{
-    return logging_enabled ? sbuf->sputc(c) : (EOF == 0 ? 1: 0);
-}
-
-//-----------------------------------------------------------------------------
-//
-// logstream manipulator for setting the log level of a message.
-//
-struct loglevel
-{
-    loglevel( fgDebugClass c, fgDebugPriority p )
-       : logClass(c), logPriority(p) {}
-
-    fgDebugClass logClass;
-    fgDebugPriority logPriority;
-};
+/**
+ * Helper force a console on platforms where it might optional, when
+ * we need to show a console. This basically means Windows at the
+ * moment - on other plaforms it's a no-op
+ */
+void requestConsole();
 
-//-----------------------------------------------------------------------------
-//
-// A helper class that ensures a streambuf and ostream are constructed and
-// destroyed in the correct order.  The streambuf must be created before the
-// ostream but bases are constructed before members.  Thus, making this class
-// a private base of logstream, declared to the left of ostream, we ensure the
-// correct order of construction and destruction.
-//
-struct logstream_base
-{
-//     logstream_base( streambuf* sb ) : lbuf(sb) {}
-    logstream_base() {}
+void shutdownLogging();
 
-    logbuf lbuf;
-};
+} // of namespace simgear
 
-//-----------------------------------------------------------------------------
-//
-// 
-//
-class logstream : private logstream_base, public ostream
+/**
+ * Class to manage the debug logging stream.
+ */
+class logstream
 {
 public:
-    // The default is to send messages to cerr.
-    logstream( ostream& out )
-//     : logstream_base(out.rdbuf()),
-       : logstream_base(),
-         ostream(&lbuf) { lbuf.set_sb(out.rdbuf());}
-
-    void set_output( ostream& out ) { lbuf.set_sb( out.rdbuf() ); }
+    ~logstream();
+    
+    static void initGlobalLogstream();
+    /**
+     * Set the global log class and priority level.
+     * @param c debug class
+     * @param p priority
+     */
+    void setLogLevels( sgDebugClass c, sgDebugPriority p );
+
+    bool would_log(  sgDebugClass c, sgDebugPriority p ) const;
+
+    void logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p );
+
+    void set_log_priority( sgDebugPriority p);
+    
+    void set_log_classes( sgDebugClass c);
+    
+    sgDebugClass get_log_classes() const;
+    
+    sgDebugPriority get_log_priority() const;
+
+    /**
+     * the core logging method
+     */
+    void log( sgDebugClass c, sgDebugPriority p,
+            const char* fileName, int line, const std::string& msg);
+
+    /**
+     * support for the SG_POPUP logging class
+     * set the content of the popup message
+     */
+    void popup( const std::string& msg);
+
+    /**
+     * retrieve the contents of the popup message and clear it's internal
+     * content. The return value may be an empty string.
+     */
+    std::string get_popup();
+
+    /**
+     * return true if a new popup message is available. false otherwise.
+     */
+    bool has_popup();
+
+   /**
+    * \relates logstream
+    * Return the one and only logstream instance.
+    * We use a function instead of a global object so we are assured that cerr
+    * has been initialised.
+    * @return current logstream
+    */
+    friend logstream& sglog();
+    
+    /**
+     * register a logging callback. Note callbacks are run in a
+     * dedicated thread, so callbacks which pass data to other threads
+     * must use appropriate locking.
+     */
+    void addCallback(simgear::LogCallback* cb);
+     
+    void removeCallback(simgear::LogCallback* cb);
 
-    // Set the global log class and priority level.
-     void setLogLevels( fgDebugClass c, fgDebugPriority p );
+private:
+    // constructor
+    logstream();
 
-    // Output operator to capture the debug level and priority of a message.
-    inline ostream& operator<< ( const loglevel& l );
+    std::vector<std::string> popup_msgs;
 };
 
-inline ostream&
-logstream::operator<< ( const loglevel& l )
-{
-    lbuf.set_log_state( l.logClass, l.logPriority );
-    return *this;
-}
+logstream& sglog();
+
 
-//-----------------------------------------------------------------------------
-//
-// Return the one and only logstream instance.
-// We use a function instead of a global object so we are assured that cerr
-// has been initialised.
-//
-inline logstream&
-fglog()
-{
-    static logstream logstrm( cerr );
-    return logstrm;
-}
 
+/** \def SG_LOG(C,P,M)
+ * Log a message.
+ * @param C debug class
+ * @param P priority
+ * @param M message
+ */
+# define SG_LOGX(C,P,M) \
+    do { if(sglog().would_log(C,P)) {                         \
+        std::ostringstream os; os << M;                  \
+        sglog().log(C, P, __FILE__, __LINE__, os.str()); \
+        if (P == SG_POPUP) sglog().popup(os.str());      \
+    } } while(0)
 #ifdef FG_NDEBUG
-# define FG_LOG(C,P,M)
-#elif defined( __MWERKS__ )
-# define FG_LOG(C,P,M) ::fglog() << ::loglevel(C,P) << M << std::endl
+# define SG_LOG(C,P,M) do { if(P == SG_POPUP) SG_LOGX(C,P,M) } while(0)
 #else
-# define FG_LOG(C,P,M) fglog() << loglevel(C,P) << M << endl
+# define SG_LOG(C,P,M) SG_LOGX(C,P,M)
 #endif
 
+#define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
+
 #endif // _LOGSTREAM_H