Seperated logger to separate class

This commit is contained in:
Mark Watkins 2014-06-20 17:05:40 +10:00
parent 56358c25c7
commit c2ea072340
6 changed files with 182 additions and 152 deletions

129
sleepyhead/logger.cpp Normal file
View File

@ -0,0 +1,129 @@
#include "logger.h"
QThreadPool * otherThreadPool = NULL;
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
void MyOutputHandler(QtMsgType type, const char *msgtxt)
{
#else
void MyOutputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgtxt)
{
Q_UNUSED(context)
#endif
if (!logger) {
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
fprintf(stderr, "Pre/Post: %s\n", msgtxt.toLocal8Bit().constData());
#else
fprintf(stderr, "Pre/Post: %s\n", msgtxt);
#endif
return;
}
QString msg, typestr;
switch (type) {
case QtWarningMsg:
typestr = QString("Warning: ");
break;
case QtFatalMsg:
typestr = QString("Fatal: ");
break;
case QtCriticalMsg:
typestr = QString("Critical: ");
break;
default:
typestr = QString("Debug: ");
break;
}
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
msg = typestr +
msgtxt; //+QString(" (%1:%2, %3)").arg(context.file).arg(context.line).arg(context.function);
#else
msg = typestr + msgtxt;
#endif
if (logger && logger->isRunning()) {
logger->append(msg);
} else {
fprintf(stderr, "%s\n", msg.toLocal8Bit().data());
}
if (type == QtFatalMsg) {
abort();
}
}
void initializeLogger()
{
logger = new LogThread();
otherThreadPool = new QThreadPool();
bool b = otherThreadPool->tryStart(logger);
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
qInstallMessageHandler(MyOutputHandler);
#else
qInstallMsgHandler(MyOutputHandler);
#endif
if (b) {
qWarning() << "Started logging thread";
} else {
qWarning() << "Logging thread did not start correctly";
}
}
void shutdownLogger()
{
if (logger) {
logger->quit();
otherThreadPool->waitForDone(-1);
logger = NULL;
}
delete otherThreadPool;
}
LogThread * logger = NULL;
void LogThread::append(QString msg)
{
QString tmp = QString("%1: %2").arg(logtime.elapsed(), 5, 10, QChar('0')).arg(msg);
//QStringList appears not to be threadsafe
strlock.lock();
buffer.append(tmp);
strlock.unlock();
}
void LogThread::quit() {
qDebug() << "Shutting down logging thread";
running = false;
strlock.lock();
while (!buffer.isEmpty()) {
QString msg = buffer.takeFirst();
fprintf(stderr, "%s\n", msg.toLocal8Bit().constData());
}
strlock.unlock();
}
void LogThread::run()
{
running = true;
do {
strlock.lock();
while (!buffer.isEmpty()) {
QString msg = buffer.takeFirst();
fprintf(stderr, "%s\n", msg.toLocal8Bit().data());
emit outputLog(msg);
}
strlock.unlock();
QThread::msleep(1000);
} while (running);
}

44
sleepyhead/logger.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef LOGGER_H
#define LOGGER_H
#include <QDebug>
#include <QRunnable>
#include <QThreadPool>
#include <QTime>
void initializeLogger();
void shutdownLogger();
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
void MyOutputHandler(QtMsgType type, const char *msgtxt);
#else
void MyOutputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgtxt);
#endif
class LogThread:public QObject, public QRunnable
{
Q_OBJECT
public:
explicit LogThread() : QRunnable() { running = false; logtime.start(); }
virtual ~LogThread() {}
void run();
void append(QString msg);
bool isRunning() { return running; }
void quit();
QStringList buffer;
QMutex strlock;
QThreadPool *threadpool;
signals:
void outputLog(QString);
protected:
volatile bool running;
QTime logtime;
};
extern LogThread * logger;
extern QThreadPool * otherThreadPool;
#endif // LOGGER_H

View File

@ -25,9 +25,8 @@
#include <QSettings>
#include <QFileDialog>
#include <QSysInfo>
#include <QThreadPool>
#include "logger.h"
#include "SleepLib/schema.h"
#include "mainwindow.h"
#include "SleepLib/profiles.h"
@ -36,6 +35,7 @@
#include "translation.h"
#include "common_gui.h"
// Gah! I must add the real darn plugin system one day.
#include "SleepLib/loader_plugins/prs1_loader.h"
#include "SleepLib/loader_plugins/cms50_loader.h"
@ -51,67 +51,6 @@
#endif
MainWindow *mainwin = nullptr;
QThreadPool * otherThreadPool = nullptr;
QMutex mutex;
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
void MyOutputHandler(QtMsgType type, const char *msgtxt)
{
#else
void MyOutputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msgtxt)
{
Q_UNUSED(context)
#endif
if (!logger) {
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
fprintf(stderr, "Pre/Post: %s\n", msgtxt.toLocal8Bit().constData());
#else
fprintf(stderr, "Pre/Post: %s\n", msgtxt);
#endif
return;
}
QString msg, typestr;
switch (type) {
case QtWarningMsg:
typestr = QString("Warning: ");
break;
case QtFatalMsg:
typestr = QString("Fatal: ");
break;
case QtCriticalMsg:
typestr = QString("Critical: ");
break;
default:
typestr = QString("Debug: ");
break;
}
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
msg = typestr +
msgtxt; //+QString(" (%1:%2, %3)").arg(context.file).arg(context.line).arg(context.function);
#else
msg = typestr + msgtxt;
#endif
if (logger && logger->isRunning()) logger->append(msg);
else {
fprintf(stderr, "%s\n", msg.toLocal8Bit().data());
}
if (type == QtFatalMsg) {
abort();
}
}
void initialize()
{
@ -181,21 +120,7 @@ int main(int argc, char *argv[])
}
}
logger = new LogThread();
otherThreadPool = new QThreadPool();
bool b = otherThreadPool->tryStart(logger);
if (b) {
qWarning() << "Started logging thread";
} else {
qWarning() << "Logging thread did not start correctly";
}
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
qInstallMessageHandler(MyOutputHandler);
#else
qInstallMsgHandler(MyOutputHandler);
#endif
initializeLogger();
////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -33,7 +33,6 @@
#include <QTranslator>
#include <QPushButton>
#include <QCalendarWidget>
#include <QThreadPool>
#include "common_gui.h"
@ -47,6 +46,7 @@
#include <SleepLib/loader_plugins/mseries_loader.h>
#endif
#include "logger.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "newprofile.h"
@ -116,47 +116,9 @@ QString getGraphicsEngine()
return gfxEngine;
}
LogThread * logger = nullptr;
void LogThread::append(QString msg)
{
QString tmp = QString("%1: %2").arg(logtime.elapsed(), 5, 10, QChar('0')).arg(msg);
//QStringList appears not to be threadsafe
strlock.lock();
buffer.append(tmp);
strlock.unlock();
}
void LogThread::quit() {
qDebug() << "Shutting down logging thread";
running = false;
strlock.lock();
while (!buffer.isEmpty()) {
QString msg = buffer.takeFirst();
fprintf(stderr, "%s\n", msg.toLocal8Bit().constData());
}
strlock.unlock();
}
void LogThread::run()
{
running = true;
do {
strlock.lock();
while (!buffer.isEmpty()) {
QString msg = buffer.takeFirst();
emit outputLog(msg);
}
strlock.unlock();
QThread::msleep(1000);
} while (running);
}
void MainWindow::logMessage(QString msg)
{
ui->logText->appendPlainText(msg);
fprintf(stderr, "%s\n", msg.toLocal8Bit().constData());
}
MainWindow::MainWindow(QWidget *parent) :
@ -379,12 +341,8 @@ MainWindow::~MainWindow()
// Trash anything allocated by the Graph objects
DestroyGraphGlobals();
logger->quit();
disconnect(logger, SIGNAL(outputLog(QString)), this, SLOT(logMessage(QString)));
otherThreadPool->waitForDone(-1);
delete logger;
logger = nullptr;
shutdownLogger();
mainwin = nullptr;
delete ui;

View File

@ -17,8 +17,6 @@
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QSystemTrayIcon>
#include <QRunnable>
#include <QThreadPool>
#include "version.h"
#include "daily.h"
@ -62,37 +60,11 @@ class MainWindow;
// * \section install_sec Installation
extern QStatusBar *qstatusbar;
extern QThreadPool * otherThreadPool;
class Daily;
class Report;
class Overview;
class LogThread:public QObject, public QRunnable
{
Q_OBJECT
public:
explicit LogThread() { running = false; logtime.start(); }
virtual ~LogThread() {}
void run();
void append(QString msg);
bool isRunning() { return running; }
void quit();
QStringList buffer;
QMutex strlock;
QThreadPool *threadpool;
signals:
void outputLog(QString);
protected:
volatile bool running;
QTime logtime;
};
extern LogThread * logger;
/*! \class MainWindow
\author Mark Watkins

View File

@ -147,7 +147,8 @@ SOURCES += \
oximeterimport.cpp \
SleepLib/serialoximeter.cpp \
SleepLib/loader_plugins/md300w1_loader.cpp \
Graphs/gSessionTimesChart.cpp
Graphs/gSessionTimesChart.cpp \
logger.cpp
HEADERS += \
common_gui.h \
@ -202,7 +203,8 @@ HEADERS += \
oximeterimport.h \
SleepLib/serialoximeter.h \
SleepLib/loader_plugins/md300w1_loader.h \
Graphs/gSessionTimesChart.h
Graphs/gSessionTimesChart.h \
logger.h
FORMS += \
daily.ui \