aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ipc/IpcLogOutputter.cpp
blob: 5118a0a631746be10c38a86e03def99dcfcc522a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2012-2016 Symless Ltd.
 * Copyright (C) 2012 Nick Bolton
 *
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 *
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ipc/IpcLogOutputter.h"

#include "ipc/IpcServer.h"
#include "ipc/IpcMessage.h"
#include "ipc/Ipc.h"
#include "ipc/IpcClientProxy.h"
#include "mt/Thread.h"
#include "arch/Arch.h"
#include "arch/XArch.h"
#include "base/Event.h"
#include "base/EventQueue.h"
#include "base/TMethodEventJob.h"

enum EIpcLogOutputter {
    kBufferMaxSize = 1000,
    kMaxSendLines = 100,
    kBufferRateWriteLimit = 1000, // writes per kBufferRateTime
    kBufferRateTimeLimit = 1 // seconds
};

IpcLogOutputter::IpcLogOutputter(IpcServer& ipcServer, EIpcClientType clientType, bool useThread) :
    m_ipcServer(ipcServer),
    m_sending(false),
    m_bufferThread(nullptr),
    m_running(false),
    m_notifyCond(ARCH->newCondVar()),
    m_notifyMutex(ARCH->newMutex()),
    m_bufferWaiting(false),
    m_bufferThreadId(0),
    m_bufferMaxSize(kBufferMaxSize),
    m_bufferRateWriteLimit(kBufferRateWriteLimit),
    m_bufferRateTimeLimit(kBufferRateTimeLimit),
    m_bufferWriteCount(0),
    m_bufferRateStart(ARCH->time()),
    m_clientType(clientType)
{
    if (useThread) {
        m_bufferThread = new Thread([this](){ buffer_thread(); });
    }
}

IpcLogOutputter::~IpcLogOutputter()
{
    close();

    if (m_bufferThread != nullptr) {
        m_bufferThread->cancel();
        m_bufferThread->wait();
        delete m_bufferThread;
    }

    ARCH->closeCondVar(m_notifyCond);
    ARCH->closeMutex(m_notifyMutex);
}

void
IpcLogOutputter::open(const char* title)
{
}

void
IpcLogOutputter::close()
{
    if (m_bufferThread != nullptr) {
        std::lock_guard<std::mutex> lock(m_runningMutex);
        m_running = false;
        notifyBuffer();
        m_bufferThread->wait(5);
    }
}

void
IpcLogOutputter::show(bool showIfEmpty)
{
}

bool
IpcLogOutputter::write(ELevel, const char* text)
{
    // ignore events from the buffer thread (would cause recursion).
    if (m_bufferThread != nullptr &&
        Thread::getCurrentThread().getID() == m_bufferThreadId) {
        return true;
    }

    appendBuffer(text);
    notifyBuffer();

    return true;
}

void IpcLogOutputter::appendBuffer(const std::string& text)
{
    std::lock_guard<std::mutex> lock(m_bufferMutex);

    double elapsed = ARCH->time() - m_bufferRateStart;
    if (elapsed < m_bufferRateTimeLimit) {
        if (m_bufferWriteCount >= m_bufferRateWriteLimit) {
            // discard the log line if we've logged too much.
            return;
        }
    }
    else {
        m_bufferWriteCount = 0;
        m_bufferRateStart = ARCH->time();
    }

    if (m_buffer.size() >= m_bufferMaxSize) {
        // if the queue is exceeds size limit,
        // throw away the oldest item
        m_buffer.pop_front();
    }

    m_buffer.push_back(text);
    m_bufferWriteCount++;
}

bool
IpcLogOutputter::isRunning()
{
    std::lock_guard<std::mutex> lock(m_runningMutex);
    return m_running;
}

void IpcLogOutputter::buffer_thread()
{
    m_bufferThreadId = m_bufferThread->getID();
    m_running = true;

    try {
        while (isRunning()) {
            if (m_buffer.empty() || !m_ipcServer.hasClients(m_clientType)) {
                ArchMutexLock lock(m_notifyMutex);
                ARCH->waitCondVar(m_notifyCond, m_notifyMutex, -1);
            }

            sendBuffer();
        }
    }
    catch (XArch& e) {
        LOG((CLOG_ERR "ipc log buffer thread error, %s", e.what()));
    }

    LOG((CLOG_DEBUG "ipc log buffer thread finished"));
}

void
IpcLogOutputter::notifyBuffer()
{
    ArchMutexLock lock(m_notifyMutex);
    ARCH->broadcastCondVar(m_notifyCond);
}

std::string IpcLogOutputter::getChunk(size_t count)
{
    std::lock_guard<std::mutex> lock(m_bufferMutex);

    if (m_buffer.size() < count) {
        count = m_buffer.size();
    }

    std::string chunk;
    for (size_t i = 0; i < count; i++) {
        chunk.append(m_buffer.front());
        chunk.append("\n");
        m_buffer.pop_front();
    }
    return chunk;
}

void
IpcLogOutputter::sendBuffer()
{
    if (m_buffer.empty() || !m_ipcServer.hasClients(m_clientType)) {
        return;
    }

    IpcLogLineMessage message(getChunk(kMaxSendLines));
    m_sending = true;
    m_ipcServer.send(message, kIpcClientGui);
    m_sending = false;
}

void
IpcLogOutputter::bufferMaxSize(UInt16 bufferMaxSize)
{
    m_bufferMaxSize = bufferMaxSize;
}

UInt16
IpcLogOutputter::bufferMaxSize() const
{
    return m_bufferMaxSize;
}

void
IpcLogOutputter::bufferRateLimit(UInt16 writeLimit, double timeLimit)
{
    m_bufferRateWriteLimit = writeLimit;
    m_bufferRateTimeLimit = timeLimit;
}