]> git.mxchange.org Git - simgear.git/blob - simgear/debug/BufferedLogCallback.cxx
Merge branch 'next' of git.mxchange.org:/var/cache/git/repos/simgear into next
[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 #include <cstdlib> // for malloc
31      
32 namespace simgear
33 {
34
35 class BufferedLogCallback::BufferedLogCallbackPrivate
36 {
37 public:
38     SGMutex m_mutex;
39     vector_cstring m_buffer;
40     unsigned int m_stamp;
41     unsigned int m_maxLength;
42 };
43    
44 BufferedLogCallback::BufferedLogCallback(sgDebugClass c, sgDebugPriority p) :
45         simgear::LogCallback(c,p),
46     d(new BufferedLogCallbackPrivate)
47 {
48     d->m_stamp = 0;
49     d->m_maxLength = 0xffff;
50 }
51
52 BufferedLogCallback::~BufferedLogCallback()
53 {
54     BOOST_FOREACH(unsigned char* msg, d->m_buffer) {
55         free(msg);
56     }
57 }
58  
59 void BufferedLogCallback::operator()(sgDebugClass c, sgDebugPriority p, 
60         const char* file, int line, const std::string& aMessage)
61 {
62     SG_UNUSED(file);
63     SG_UNUSED(line);
64     
65     if (!shouldLog(c, p)) return;
66     
67     vector_cstring::value_type msg;
68     if (aMessage.size() >= d->m_maxLength) {
69         msg = (vector_cstring::value_type) malloc(d->m_maxLength);
70         strncpy((char*) msg, aMessage.c_str(), d->m_maxLength - 1);
71         msg[d->m_maxLength - 1] = 0; // add final NULL byte
72     } else {
73         msg = (vector_cstring::value_type) strdup(aMessage.c_str());
74     }
75     
76     SGGuard<SGMutex> g(d->m_mutex);
77     d->m_buffer.push_back(msg);
78     d->m_stamp++;
79 }
80  
81 unsigned int BufferedLogCallback::stamp() const
82 {
83     return d->m_stamp;
84 }
85  
86 unsigned int BufferedLogCallback::threadsafeCopy(vector_cstring& aOutput)
87 {
88     SGGuard<SGMutex> g(d->m_mutex);
89     size_t sz = d->m_buffer.size();
90     aOutput.resize(sz);
91     memcpy(aOutput.data(), d->m_buffer.data(), sz * sizeof(vector_cstring::value_type));
92     return d->m_stamp;
93
94  
95 void BufferedLogCallback::truncateAt(unsigned int t)
96 {
97     d->m_maxLength = t;
98 }
99  
100 } // of namespace simgear