Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tlogger.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <fstream></fstream>
Toshihiro Shimizu 890ddd
#include <set></set>
Toshihiro Shimizu 890ddd
#include <vector></vector>
Campbell Barton d0e335
#include <qtimer></qtimer>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using std::endl;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TLogger::Imp
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	std::vector<tlogger::message> m_messages;</tlogger::message>
Toshihiro Shimizu 890ddd
	std::set<tlogger::listener *=""> m_listeners;</tlogger::listener>
Toshihiro Shimizu 890ddd
	TThread::Mutex m_mutex;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	class ListenerNotifier : public TThread::Message
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		Imp *m_imp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		ListenerNotifier(Imp *imp) : m_imp(imp) {}
Toshihiro Shimizu 890ddd
		void onDeliver()
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			QMutexLocker sl(&m_imp->m_mutex);
Toshihiro Shimizu 890ddd
			std::set<tlogger::listener *="">::iterator it;</tlogger::listener>
Toshihiro Shimizu 890ddd
			for (it = m_imp->m_listeners.begin();
Toshihiro Shimizu 890ddd
				 it != m_imp->m_listeners.end(); ++it)
Toshihiro Shimizu 890ddd
				(*it)->onLogChanged();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		TThread::Message *clone() const { return new ListenerNotifier(*this); }
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void notify()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		//ListenerNotifier(this).send();//onDeliver()
Toshihiro Shimizu 890ddd
		std::set<tlogger::listener *="">::iterator it;</tlogger::listener>
Toshihiro Shimizu 890ddd
		for (it = m_listeners.begin();
Toshihiro Shimizu 890ddd
			 it != m_listeners.end(); ++it)
Toshihiro Shimizu 890ddd
			(*it)->onLogChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger::TLogger()
Toshihiro Shimizu 890ddd
	: m_imp(new Imp())
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger::~TLogger()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger *TLogger::instance()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static TLogger _instance;
Toshihiro Shimizu 890ddd
	return &_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TLogger::Message::Message(MessageType type, std::string text)
Toshihiro Shimizu 890ddd
	: m_type(type), m_text(text)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QTime t = QTime::currentTime();
Toshihiro Shimizu 890ddd
	m_timestamp = t.toString("hh:mm:ss.zzz").toStdString();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TLogger::addMessage(const Message &msg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_imp->m_mutex);
Toshihiro Shimizu 890ddd
	m_imp->m_messages.push_back(msg);
Toshihiro Shimizu 890ddd
	m_imp->notify();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TLogger::clearMessages()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_imp->m_mutex);
Toshihiro Shimizu 890ddd
	m_imp->m_messages.clear();
Toshihiro Shimizu 890ddd
	m_imp->notify();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TLogger::getMessageCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_imp->m_mutex);
Toshihiro Shimizu 890ddd
	return m_imp->m_messages.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger::Message TLogger::getMessage(int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker sl(&m_imp->m_mutex);
Toshihiro Shimizu 890ddd
	assert(0 <= index && index < getMessageCount());
Toshihiro Shimizu 890ddd
	return m_imp->m_messages[index];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TLogger::addListener(TLogger::Listener *listener)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_imp->m_listeners.insert(listener);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TLogger::removeListener(TLogger::Listener *listener)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_imp->m_listeners.erase(listener);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger::Stream::Stream(MessageType type)
Toshihiro Shimizu 890ddd
	: m_type(type), m_text()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger::Stream::~Stream()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TLogger::Message msg(m_type, m_text);
Toshihiro Shimizu 890ddd
		TLogger::instance()->addMessage(msg);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TLogger::Stream &TLogger::Stream::operator<<(std::string v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_text += v;
Toshihiro Shimizu 890ddd
	return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger::Stream &TLogger::Stream::operator<<(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_text += toString(v);
Toshihiro Shimizu 890ddd
	return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger::Stream &TLogger::Stream::operator<<(double v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_text += toString(v);
Toshihiro Shimizu 890ddd
	return *this;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLogger::Stream &TLogger::Stream::operator<<(const TFilePath &v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_text += toString(v.getWideString());
Toshihiro Shimizu 890ddd
	return *this;
Toshihiro Shimizu 890ddd
}