1 //=============================================================================
2 //
3 // Adventure Game Studio (AGS)
4 //
5 // Copyright (C) 1999-2011 Chris Jones and 2011-20xx others
6 // The full list of copyright holders can be found in the Copyright.txt
7 // file, which is part of this source code distribution.
8 //
9 // The AGS source code is provided under the Artistic License 2.0.
10 // A copy of this license can be found in the file License.txt and at
11 // http://www.opensource.org/licenses/artistic-license-2.0.php
12 //
13 //=============================================================================
14 #include "debug/debugmanager.h"
15 #include "debug/messagebuffer.h"
16 
17 namespace AGS
18 {
19 namespace Engine
20 {
21 
22 using namespace Common;
23 
MessageBuffer(size_t buffer_limit)24 MessageBuffer::MessageBuffer(size_t buffer_limit)
25     : _bufferLimit(buffer_limit)
26     , _msgLost(0)
27 {
28 }
29 
PrintMessage(const DebugMessage & msg)30 void MessageBuffer::PrintMessage(const DebugMessage &msg)
31 {
32     if (_buffer.size() < _bufferLimit)
33         _buffer.push_back(msg);
34     else
35         _msgLost++;
36 }
37 
Clear()38 void MessageBuffer::Clear()
39 {
40     _buffer.clear();
41     _msgLost = 0;
42 }
43 
Send(const String & out_id)44 void MessageBuffer::Send(const String &out_id)
45 {
46     if (_buffer.empty())
47         return;
48     if (_msgLost > 0)
49     {
50         DebugGroup gr = DbgMgr.GetGroup(kDbgGroup_Main);
51         DbgMgr.SendMessage(out_id, DebugMessage(String::FromFormat("WARNING: output %s lost exceeding buffer: %u debug messages\n", out_id.GetCStr(), (unsigned)_msgLost),
52             gr.UID.ID, gr.OutputName, kDbgMsgSet_All));
53     }
54     for (std::vector<DebugMessage>::const_iterator it = _buffer.begin(); it != _buffer.end(); ++it)
55     {
56         DbgMgr.SendMessage(out_id, *it);
57     }
58 }
59 
Flush(const String & out_id)60 void MessageBuffer::Flush(const String &out_id)
61 {
62     Send(out_id);
63     Clear();
64 }
65 
66 } // namespace Engine
67 } // namespace AGS
68