2012-06-10 16:50:54 +00:00
|
|
|
/*
|
|
|
|
* synergy -- mouse and keyboard sharing utility
|
|
|
|
* Copyright (C) 2012 Nick Bolton
|
2012-07-03 14:15:05 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* 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 COPYING that should have accompanied this file.
|
2012-07-03 14:15:05 +00:00
|
|
|
*
|
2012-06-10 16:50:54 +00:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2012-07-03 14:15:05 +00:00
|
|
|
#include "CIpcLogOutputter.h"
|
|
|
|
#include "CIpcServer.h"
|
|
|
|
#include "CIpcMessage.h"
|
|
|
|
#include "Ipc.h"
|
2012-07-06 12:27:22 +00:00
|
|
|
#include "CEvent.h"
|
|
|
|
#include "CEventQueue.h"
|
|
|
|
#include "TMethodEventJob.h"
|
|
|
|
#include "CIpcClientProxy.h"
|
2012-07-06 16:18:21 +00:00
|
|
|
#include "CArch.h"
|
2012-07-08 16:01:27 +00:00
|
|
|
#include "CThread.h"
|
|
|
|
#include "TMethodJob.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2012-07-03 14:15:05 +00:00
|
|
|
CIpcLogOutputter::CIpcLogOutputter(CIpcServer& ipcServer) :
|
2012-07-06 16:18:21 +00:00
|
|
|
m_ipcServer(ipcServer),
|
2012-07-08 16:01:27 +00:00
|
|
|
m_bufferMutex(ARCH->newMutex()),
|
|
|
|
m_sending(false),
|
|
|
|
m_running(true)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2012-07-08 16:01:27 +00:00
|
|
|
m_bufferThread = new CThread(new TMethodJob<CIpcLogOutputter>(
|
|
|
|
this, &CIpcLogOutputter::bufferThread));
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 14:15:05 +00:00
|
|
|
CIpcLogOutputter::~CIpcLogOutputter()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2012-07-08 16:01:27 +00:00
|
|
|
ARCH->closeMutex(m_bufferMutex);
|
|
|
|
delete m_bufferThread;
|
2012-07-06 12:27:22 +00:00
|
|
|
}
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
void
|
2012-07-03 14:15:05 +00:00
|
|
|
CIpcLogOutputter::open(const char* title)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-07-03 14:15:05 +00:00
|
|
|
CIpcLogOutputter::close()
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2012-07-08 16:01:27 +00:00
|
|
|
m_running = false;
|
|
|
|
m_bufferThread->wait(5);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-07-03 14:15:05 +00:00
|
|
|
CIpcLogOutputter::show(bool showIfEmpty)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-03 14:15:05 +00:00
|
|
|
bool
|
2012-07-08 16:27:28 +00:00
|
|
|
CIpcLogOutputter::write(ELevel level, const char* text)
|
|
|
|
{
|
|
|
|
return write(level, text, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CIpcLogOutputter::write(ELevel, const char* text, bool force)
|
2012-06-10 16:50:54 +00:00
|
|
|
{
|
2012-07-08 16:01:27 +00:00
|
|
|
// sending the buffer generates log messages, which we must throw
|
|
|
|
// away (otherwise this would cause recursion). this is just a drawback
|
|
|
|
// of logging this way. there is also the risk that this could throw
|
|
|
|
// away log messages not generated by the ipc, but it seems like it
|
|
|
|
// would be difficult to distinguish (other than looking at the stack
|
|
|
|
// trace somehow). perhaps a file stream might be a better option :-/
|
2012-07-08 16:27:28 +00:00
|
|
|
if (m_sending && !force) {
|
2012-07-08 16:01:27 +00:00
|
|
|
return true;
|
2012-07-06 12:27:22 +00:00
|
|
|
}
|
2012-07-06 16:18:21 +00:00
|
|
|
|
2012-07-08 16:01:27 +00:00
|
|
|
CArchMutexLock lock(m_bufferMutex);
|
|
|
|
m_buffer.append(text);
|
|
|
|
m_buffer.append("\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CIpcLogOutputter::bufferThread(void*)
|
|
|
|
{
|
|
|
|
while (m_running) {
|
|
|
|
while (m_running && m_buffer.size() == 0) {
|
|
|
|
ARCH->sleep(.1);
|
2012-07-06 16:18:21 +00:00
|
|
|
}
|
2012-07-08 16:01:27 +00:00
|
|
|
|
|
|
|
if (!m_running) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_ipcServer.hasClients(kIpcClientGui)) {
|
|
|
|
sendBuffer();
|
2012-07-06 16:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
2012-07-08 16:01:27 +00:00
|
|
|
|
|
|
|
CString*
|
|
|
|
CIpcLogOutputter::emptyBuffer()
|
|
|
|
{
|
|
|
|
CArchMutexLock lock(m_bufferMutex);
|
|
|
|
CString* copy = new CString(m_buffer);
|
|
|
|
m_buffer.clear();
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CIpcLogOutputter::sendBuffer()
|
|
|
|
{
|
|
|
|
CIpcMessage message;
|
|
|
|
message.m_type = kIpcLogLine;
|
|
|
|
message.m_data = emptyBuffer();
|
|
|
|
|
|
|
|
m_sending = true;
|
|
|
|
m_ipcServer.send(message, kIpcClientGui);
|
|
|
|
m_sending = false;
|
|
|
|
}
|