From: James Turner Date: Thu, 7 Feb 2013 11:57:15 +0000 (+0100) Subject: Buffered log-callback. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f983194d7ea9e8c9e2bb37b3bd604ccd3133795b;p=simgear.git Buffered log-callback. Allow a particular set of log messages to be retained indefinitely, for (presumably) later display somehow, such as in the GUI. --- diff --git a/simgear/debug/logstream.cxx b/simgear/debug/logstream.cxx index 9e4cafa8..91ee4ebe 100644 --- a/simgear/debug/logstream.cxx +++ b/simgear/debug/logstream.cxx @@ -27,6 +27,7 @@ #include +#include #include #include #include @@ -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 g(d->m_mutex); + d->m_buffer.push_back(aMessage); +} + +void BufferedLogCallback::threadsafeCopy(string_list& aOutput) +{ + aOutput.clear(); + SGGuard g(d->m_mutex); + size_t sz = d->m_buffer.size(); + aOutput.resize(sz); + for (unsigned int i=0; im_buffer[i]; + } +} + +} // of namespace simgear + class LogStreamPrivate : public SGThread { private: diff --git a/simgear/debug/logstream.hxx b/simgear/debug/logstream.hxx index 87021903..f50a4cc2 100644 --- a/simgear/debug/logstream.hxx +++ b/simgear/debug/logstream.hxx @@ -28,8 +28,12 @@ #include #include +#include #include - +#include // for std::auto_ptr + +typedef std::vector 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 d; +}; + } // of namespace simgear /**