]> git.mxchange.org Git - simgear.git/blobdiff - simgear/debug/logstream.hxx
Tweak HTTP code to always sleep.
[simgear.git] / simgear / debug / logstream.hxx
index ea35ee956c66d935ba575a23c8feab1feb35f060..d4fa0e48431ac4ba1e8824a895f033455aeaba92 100644 (file)
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Library General Public License for more details.
 //
-// You should have received a copy of the GNU Library General Public
-// License along with this library; if not, write to the
-// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-// Boston, MA  02111-1307, USA.
+// 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
 #define _LOGSTREAM_H
 
 #include <simgear/compiler.h>
-
-// At least Irix needs this
-#ifdef SG_HAVE_NATIVE_SGI_COMPILERS
-#include <char_traits.h>
-#endif
-
-#ifdef SG_HAVE_STD_INCLUDES
-# include <streambuf>
-# include <iostream>
-#else
-# include <iostream.h>
-# include <simgear/sg_traits.hxx>
-#endif
-
 #include <simgear/debug/debug_types.h>
 
-#ifndef SG_HAVE_NATIVE_SGI_COMPILERS
-SG_USING_STD(streambuf);
-SG_USING_STD(ostream);
-SG_USING_STD(cerr);
-SG_USING_STD(endl);
-#else
-SG_USING_STD(char_traits);
-#endif
-
-#ifdef __MWERKS__
-SG_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 <sstream>
+// 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 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();
-
-    /** Destructor */
-    ~logbuf();
-
-    /**
-     * Is logging enabled?
-     * @return true or false*/
-    bool enabled() { return logging_enabled; }
-
-    /**
-     * Set the logging level of subsequent messages.
-     * @param c debug class
-     * @param p priority
-     */
-    void set_log_state( sgDebugClass c, sgDebugPriority p );
-
-    /**
-     * Set the global logging level.
-     * @param c debug class
-     * @param p priority
-     */
-    static void set_log_level( sgDebugClass c, sgDebugPriority p );
-
-
-    /**
-     * Set the allowed logging classes.
-     * @param c All enabled logging classes anded together.
-     */
-    static void set_log_classes (sgDebugClass c);
-
-
-    /**
-     * Get the logging classes currently enabled.
-     * @return All enabled debug logging anded together.
-     */
-    static sgDebugClass get_log_classes ();
-
-
-    /**
-     * Set the logging priority.
-     * @param c The priority cutoff for logging messages.
-     */
-    static void set_log_priority (sgDebugPriority p);
-
-
-    /**
-     * Get the current logging priority.
-     * @return The priority cutoff for logging messages.
-     */
-    static sgDebugPriority get_log_priority ();
-
-
-    /**
-     * Set the stream buffer
-     * @param sb stream buffer
-     */
-    void set_sb( streambuf* sb );
-
+       void setLogLevels(sgDebugClass c, sgDebugPriority p);
 protected:
+       LogCallback(sgDebugClass c, sgDebugPriority p);
 
-    /** sync/flush */
-    inline virtual int sync();
-
-    /** overflow */
-    int_type overflow( int ch );
-    // int xsputn( const char* s, istreamsize n );
-
+       bool shouldLog(sgDebugClass c, sgDebugPriority p) const;
 private:
-
-    // The streambuf used for actual output. Defaults to cerr.rdbuf().
-    static streambuf* sbuf;
-
-    static bool logging_enabled;
-    static sgDebugClass logClass;
-    static sgDebugPriority logPriority;
-
-private:
-
-    // Not defined.
-    logbuf( const logbuf& );
-    void operator= ( const logbuf& );
-};
-
-inline int
-logbuf::sync()
-{
-#ifdef SG_HAVE_STD_INCLUDES
-       return sbuf->pubsync();
-#else
-       return sbuf->sync();
-#endif
-}
-
-inline void
-logbuf::set_log_state( sgDebugClass c, sgDebugPriority 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( sgDebugClass c, sgDebugPriority p )
-       : logClass(c), logPriority(p) {}
-
-    sgDebugClass logClass;
-    sgDebugPriority logPriority;
+       sgDebugClass m_class;
+       sgDebugPriority m_priority;
 };
 
 /**
- * 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.
+ * 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
  */
-struct logstream_base
-{
-    // logstream_base( streambuf* sb ) : lbuf(sb) {}
-    logstream_base() {}
-
-    logbuf lbuf;
-};
+void requestConsole();
+     
+} // of namespace simgear
 
 /**
  * Class to manage the debug logging stream.
  */
-class logstream : private logstream_base, public ostream
+class logstream
 {
 public:
-    /**
-     * The default is to send messages to cerr.
-     * @param out output stream
-     */
-    logstream( ostream& out )
-       // : logstream_base(out.rdbuf()),
-       : logstream_base(),
-         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() ); }
-
+    static void initGlobalLogstream();
     /**
      * Set the global log class and priority level.
      * @param c debug class
@@ -242,49 +76,49 @@ public:
      */
     void setLogLevels( sgDebugClass c, sgDebugPriority p );
 
-    /**
-     * Output operator to capture the debug level and priority of a message.
-     * @param l log level
-     */
-    inline ostream& operator<< ( const loglevel& l );
-};
-
-inline ostream&
-logstream::operator<< ( const loglevel& l )
-{
-    lbuf.set_log_state( l.logClass, l.logPriority );
-    return *this;
-}
+    bool would_log(  sgDebugClass c, sgDebugPriority p ) const;
 
-extern logstream *global_logstream;
+    void logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p );
 
-/**
- * \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
- */
-inline logstream&
-sglog()
-{
-  if (global_logstream == NULL) {
+    void set_log_priority( sgDebugPriority p);
+    
+    void set_log_classes( sgDebugClass c);
+    
+    sgDebugClass get_log_classes() const;
+    
+    sgDebugPriority get_log_priority() const;
 
-#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);
-  }
+     * the core logging method
+     */
+    void log( sgDebugClass c, sgDebugPriority p,
+            const char* fileName, int line, const std::string& msg);
+
+   /**
+    * \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();
     
-  return *global_logstream;
-}
+    /**
+     * 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);
+
+private:
+    // constructor
+    logstream();
+};
+
+logstream& sglog();
+
 
 
 /** \def SG_LOG(C,P,M)
@@ -295,12 +129,17 @@ 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 {                     \
+       if(sglog().would_log(C,P)) {                      \
+        std::ostringstream os;                             \
+        os << M;                                       \
+        sglog().log(C, P, __FILE__, __LINE__, os.str()); \
+    } \
+} while(0)
 #endif
 
+#define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
 
 #endif // _LOGSTREAM_H