From c2ea07234017b3773c396122abb5d0e1760b13eb Mon Sep 17 00:00:00 2001 From: Mark Watkins Date: Fri, 20 Jun 2014 17:05:40 +1000 Subject: [PATCH] Seperated logger to separate class --- sleepyhead/logger.cpp | 129 ++++++++++++++++++++++++++++++++++++++ sleepyhead/logger.h | 44 +++++++++++++ sleepyhead/main.cpp | 81 +----------------------- sleepyhead/mainwindow.cpp | 46 +------------- sleepyhead/mainwindow.h | 28 --------- sleepyhead/sleepyhead.pro | 6 +- 6 files changed, 182 insertions(+), 152 deletions(-) create mode 100644 sleepyhead/logger.cpp create mode 100644 sleepyhead/logger.h diff --git a/sleepyhead/logger.cpp b/sleepyhead/logger.cpp new file mode 100644 index 00000000..2831cc63 --- /dev/null +++ b/sleepyhead/logger.cpp @@ -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); +} + + diff --git a/sleepyhead/logger.h b/sleepyhead/logger.h new file mode 100644 index 00000000..3addb091 --- /dev/null +++ b/sleepyhead/logger.h @@ -0,0 +1,44 @@ +#ifndef LOGGER_H +#define LOGGER_H + +#include +#include +#include +#include + +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 diff --git a/sleepyhead/main.cpp b/sleepyhead/main.cpp index 3ec10bc3..e04325d5 100644 --- a/sleepyhead/main.cpp +++ b/sleepyhead/main.cpp @@ -25,9 +25,8 @@ #include #include #include -#include - +#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(); //////////////////////////////////////////////////////////////////////////////////////////// diff --git a/sleepyhead/mainwindow.cpp b/sleepyhead/mainwindow.cpp index 384f496f..6ce17209 100644 --- a/sleepyhead/mainwindow.cpp +++ b/sleepyhead/mainwindow.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include "common_gui.h" @@ -47,6 +46,7 @@ #include #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; diff --git a/sleepyhead/mainwindow.h b/sleepyhead/mainwindow.h index 2fdd495c..95e781f6 100644 --- a/sleepyhead/mainwindow.h +++ b/sleepyhead/mainwindow.h @@ -17,8 +17,6 @@ #include #include #include -#include -#include #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 diff --git a/sleepyhead/sleepyhead.pro b/sleepyhead/sleepyhead.pro index b0ba508b..d7ca2889 100644 --- a/sleepyhead/sleepyhead.pro +++ b/sleepyhead/sleepyhead.pro @@ -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 \