]> git.mxchange.org Git - simgear.git/commitdiff
Buffered log-callback.
authorJames Turner <zakalawe@mac.com>
Thu, 7 Feb 2013 11:57:15 +0000 (12:57 +0100)
committerJames Turner <zakalawe@mac.com>
Thu, 7 Feb 2013 12:01:56 +0000 (13:01 +0100)
Allow a particular set of log messages to be retained indefinitely, for (presumably) later display somehow, such as in the GUI.

simgear/debug/logstream.cxx
simgear/debug/logstream.hxx

index 9e4cafa8c5eea741db224897a32a2c832cfeee5f..91ee4ebe0b6b2bdfb1a34c0970668cb0a82f4519 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <boost/foreach.hpp>
 
+#include <simgear/sg_inlines.h>
 #include <simgear/threads/SGThread.hxx>
 #include <simgear/threads/SGQueue.hxx>
 #include <simgear/threads/SGGuard.hxx>
@@ -112,7 +113,55 @@ public:
 private:
     
 };
+
+namespace simgear
+{
+class BufferedLogCallback::BufferedLogCallbackPrivate
+{
+public:
+    SGMutex m_mutex;
+    sgDebugClass m_class;
+    sgDebugPriority m_priority;
+    string_list m_buffer;
+};
    
+BufferedLogCallback::BufferedLogCallback(sgDebugClass c, sgDebugPriority p) :
+    d(new BufferedLogCallbackPrivate)
+{
+    d->m_class = c;
+    d->m_priority = p;
+}
+
+BufferedLogCallback::~BufferedLogCallback()
+{
+}
+void BufferedLogCallback::operator()(sgDebugClass c, sgDebugPriority p, 
+        const char* file, int line, const std::string& aMessage)
+{
+    SG_UNUSED(file);
+    SG_UNUSED(line);
+    
+    if ((c & d->m_class) == 0 || p < d->m_priority) return;
+    
+    SGGuard<SGMutex> g(d->m_mutex);
+    d->m_buffer.push_back(aMessage);
+}
+void BufferedLogCallback::threadsafeCopy(string_list& aOutput)
+{
+    aOutput.clear();
+    SGGuard<SGMutex> g(d->m_mutex);
+    size_t sz = d->m_buffer.size();
+    aOutput.resize(sz);
+    for (unsigned int i=0; i<sz; ++i) {
+        aOutput[i] = d->m_buffer[i];
+    }
+} 
+
+} // of namespace simgear
+
 class LogStreamPrivate : public SGThread
 {
 private:
index 87021903a7b351ea40a9d8a565fa0e2ddcc9b8df..f50a4cc2124489ce204943993eb53ae5888b1d42 100644 (file)
 #include <simgear/compiler.h>
 #include <simgear/debug/debug_types.h>
 
+#include <vector>
 #include <sstream>
-
+#include <memory> // for std::auto_ptr
+     
+typedef std::vector<std::string> string_list;
 // forward decls
 class SGPath;
       
@@ -44,6 +48,27 @@ public:
         const char* file, int line, const std::string& aMessage) = 0;
 };
      
+     
+class BufferedLogCallback : public LogCallback
+{
+public:
+    BufferedLogCallback(sgDebugClass c, sgDebugPriority p);
+    virtual ~BufferedLogCallback();
+    
+    virtual void operator()(sgDebugClass c, sgDebugPriority p, 
+        const char* file, int line, const std::string& aMessage);
+    
+    /**
+     * copy the buffered log data into the provided output list
+     * (which will be cleared first). This method is safe to call from
+     * any thread.
+     */
+    void threadsafeCopy(string_list& aOutput);
+private:
+    class BufferedLogCallbackPrivate;
+    std::auto_ptr<BufferedLogCallbackPrivate> d;
+};
+     
 } // of namespace simgear
 
 /**