Buffered log-callback.

Allow a particular set of log messages to be retained indefinitely, for (presumably) later display somehow, such as in the GUI.
This commit is contained in:
James Turner
2013-02-07 12:57:15 +01:00
parent ab8373b989
commit f983194d7e
2 changed files with 75 additions and 1 deletions

View 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:

View File

@@ -28,8 +28,12 @@
#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
/**