1 /** \file BufferedLogCallback.cxx
2 * Buffer certain log messages permanently for later retrieval and display
5 // Copyright (C) 2013 James Turner zakalawe@mac.com
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Library General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <simgear/debug/BufferedLogCallback.hxx>
24 #include <boost/foreach.hpp>
26 #include <simgear/sg_inlines.h>
27 #include <simgear/threads/SGThread.hxx>
28 #include <simgear/threads/SGGuard.hxx>
33 class BufferedLogCallback::BufferedLogCallbackPrivate
37 vector_cstring m_buffer;
39 unsigned int m_maxLength;
42 BufferedLogCallback::BufferedLogCallback(sgDebugClass c, sgDebugPriority p) :
43 simgear::LogCallback(c,p),
44 d(new BufferedLogCallbackPrivate)
47 d->m_maxLength = 0xffff;
50 BufferedLogCallback::~BufferedLogCallback()
52 BOOST_FOREACH(unsigned char* msg, d->m_buffer) {
57 void BufferedLogCallback::operator()(sgDebugClass c, sgDebugPriority p,
58 const char* file, int line, const std::string& aMessage)
63 if (!shouldLog(c, p)) return;
65 vector_cstring::value_type msg;
66 if (aMessage.size() >= d->m_maxLength) {
67 msg = (vector_cstring::value_type) malloc(d->m_maxLength);
68 strncpy((char*) msg, aMessage.c_str(), d->m_maxLength - 1);
69 msg[d->m_maxLength - 1] = 0; // add final NULL byte
71 msg = (vector_cstring::value_type) strdup(aMessage.c_str());
74 SGGuard<SGMutex> g(d->m_mutex);
75 d->m_buffer.push_back(msg);
79 unsigned int BufferedLogCallback::stamp() const
84 unsigned int BufferedLogCallback::threadsafeCopy(vector_cstring& aOutput)
86 SGGuard<SGMutex> g(d->m_mutex);
87 size_t sz = d->m_buffer.size();
89 memcpy(aOutput.data(), d->m_buffer.data(), sz * sizeof(vector_cstring::value_type));
93 void BufferedLogCallback::truncateAt(unsigned int t)
98 } // of namespace simgear