|
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 |
|
|
Shinya Kitaoka |
120a6e |
class TLogger::Imp {
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
std::vector<tlogger::message> m_messages;</tlogger::message>
|
|
Shinya Kitaoka |
120a6e |
std::set<tlogger::listener *=""> m_listeners;</tlogger::listener>
|
|
Shinya Kitaoka |
120a6e |
TThread::Mutex m_mutex;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
d1f6c4 |
class ListenerNotifier final : public TThread::Message {
|
|
Shinya Kitaoka |
120a6e |
Imp *m_imp;
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
public:
|
|
Shinya Kitaoka |
120a6e |
ListenerNotifier(Imp *imp) : m_imp(imp) {}
|
|
Shinya Kitaoka |
473e70 |
void onDeliver() override {
|
|
Shinya Kitaoka |
120a6e |
QMutexLocker sl(&m_imp->m_mutex);
|
|
Shinya Kitaoka |
120a6e |
std::set<tlogger::listener *="">::iterator it;</tlogger::listener>
|
|
Shinya Kitaoka |
120a6e |
for (it = m_imp->m_listeners.begin(); it != m_imp->m_listeners.end();
|
|
Shinya Kitaoka |
120a6e |
++it)
|
|
Shinya Kitaoka |
120a6e |
(*it)->onLogChanged();
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
38fd86 |
TThread::Message *clone() const override {
|
|
Shinya Kitaoka |
38fd86 |
return new ListenerNotifier(*this);
|
|
Shinya Kitaoka |
38fd86 |
}
|
|
Shinya Kitaoka |
120a6e |
};
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
void notify() {
|
|
Shinya Kitaoka |
120a6e |
// ListenerNotifier(this).send();//onDeliver()
|
|
Shinya Kitaoka |
120a6e |
std::set<tlogger::listener *="">::iterator it;</tlogger::listener>
|
|
Shinya Kitaoka |
120a6e |
for (it = m_listeners.begin(); it != m_listeners.end(); ++it)
|
|
Shinya Kitaoka |
120a6e |
(*it)->onLogChanged();
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::TLogger() : m_imp(new Imp()) {}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::~TLogger() {}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger *TLogger::instance() {
|
|
Shinya Kitaoka |
120a6e |
static TLogger _instance;
|
|
Shinya Kitaoka |
120a6e |
return &_instance;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
3bfa54 |
TLogger::Message::Message(MessageType type, std::string text)
|
|
Shinya Kitaoka |
120a6e |
: m_type(type), m_text(text) {
|
|
Shinya Kitaoka |
120a6e |
QTime t = QTime::currentTime();
|
|
Shinya Kitaoka |
120a6e |
m_timestamp = t.toString("hh:mm:ss.zzz").toStdString();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TLogger::addMessage(const Message &msg) {
|
|
Shinya Kitaoka |
120a6e |
QMutexLocker sl(&m_imp->m_mutex);
|
|
Shinya Kitaoka |
120a6e |
m_imp->m_messages.push_back(msg);
|
|
Shinya Kitaoka |
120a6e |
m_imp->notify();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TLogger::clearMessages() {
|
|
Shinya Kitaoka |
120a6e |
QMutexLocker sl(&m_imp->m_mutex);
|
|
Shinya Kitaoka |
120a6e |
m_imp->m_messages.clear();
|
|
Shinya Kitaoka |
120a6e |
m_imp->notify();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
int TLogger::getMessageCount() const {
|
|
Shinya Kitaoka |
120a6e |
QMutexLocker sl(&m_imp->m_mutex);
|
|
Shinya Kitaoka |
120a6e |
return m_imp->m_messages.size();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::Message TLogger::getMessage(int index) const {
|
|
Shinya Kitaoka |
120a6e |
QMutexLocker sl(&m_imp->m_mutex);
|
|
Shinya Kitaoka |
120a6e |
assert(0 <= index && index < getMessageCount());
|
|
Shinya Kitaoka |
120a6e |
return m_imp->m_messages[index];
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TLogger::addListener(TLogger::Listener *listener) {
|
|
Shinya Kitaoka |
120a6e |
m_imp->m_listeners.insert(listener);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
void TLogger::removeListener(TLogger::Listener *listener) {
|
|
Shinya Kitaoka |
120a6e |
m_imp->m_listeners.erase(listener);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::Stream::Stream(MessageType type) : m_type(type), m_text() {}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::Stream::~Stream() {
|
|
Shinya Kitaoka |
120a6e |
try {
|
|
Shinya Kitaoka |
120a6e |
TLogger::Message msg(m_type, m_text);
|
|
Shinya Kitaoka |
120a6e |
TLogger::instance()->addMessage(msg);
|
|
Shinya Kitaoka |
120a6e |
} catch (...) {
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::Stream &TLogger::Stream::operator<<(std::string v) {
|
|
Shinya Kitaoka |
120a6e |
m_text += v;
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::Stream &TLogger::Stream::operator<<(int v) {
|
|
Shinya Kitaoka |
120a6e |
m_text += std::to_string(v);
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::Stream &TLogger::Stream::operator<<(double v) {
|
|
Shinya Kitaoka |
120a6e |
m_text += std::to_string(v);
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
TLogger::Stream &TLogger::Stream::operator<<(const TFilePath &v) {
|
|
Shinya Kitaoka |
120a6e |
m_text += v.getQString().toStdString();
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Toshihiro Shimizu |
890ddd |
}
|