]> git.mxchange.org Git - simgear.git/blob - simgear/debug/BufferedLogCallback.cxx
Move BufferedLogCallback to its own class.
[simgear.git] / simgear / debug / BufferedLogCallback.cxx
1 /** \file BufferedLogCallback.cxx
2  * Buffer certain log messages permanently for later retrieval and display
3  */
4
5 // Copyright (C) 2013  James Turner  zakalawe@mac.com
6 //
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.
11 //
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.
16 //
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.
20 //
21      
22 #include <simgear/debug/BufferedLogCallback.hxx>
23      
24 #include <boost/foreach.hpp>
25      
26 #include <simgear/sg_inlines.h>
27 #include <simgear/threads/SGThread.hxx>
28 #include <simgear/threads/SGGuard.hxx>
29      
30 namespace simgear
31 {
32
33 class BufferedLogCallback::BufferedLogCallbackPrivate
34 {
35 public:
36     SGMutex m_mutex;
37     sgDebugClass m_class;
38     sgDebugPriority m_priority;
39     vector_cstring m_buffer;
40 };
41    
42 BufferedLogCallback::BufferedLogCallback(sgDebugClass c, sgDebugPriority p) :
43     d(new BufferedLogCallbackPrivate)
44 {
45     d->m_class = c;
46     d->m_priority = p;
47 }
48
49 BufferedLogCallback::~BufferedLogCallback()
50 {
51     BOOST_FOREACH(unsigned char* msg, d->m_buffer) {
52         free(msg);
53     }
54 }
55  
56 void BufferedLogCallback::operator()(sgDebugClass c, sgDebugPriority p, 
57         const char* file, int line, const std::string& aMessage)
58 {
59     SG_UNUSED(file);
60     SG_UNUSED(line);
61     
62     if ((c & d->m_class) == 0 || p < d->m_priority) return;
63     
64     vector_cstring::value_type msg = (vector_cstring::value_type) strdup(aMessage.c_str());
65     SGGuard<SGMutex> g(d->m_mutex);
66     d->m_buffer.push_back(msg);
67 }
68  
69 void BufferedLogCallback::threadsafeCopy(vector_cstring& aOutput)
70 {
71     SGGuard<SGMutex> g(d->m_mutex);
72     size_t sz = d->m_buffer.size();
73     aOutput.resize(sz);
74     memcpy(aOutput.data(), d->m_buffer.data(), sz * sizeof(vector_cstring::value_type));
75
76  
77 } // of namespace simgear