mirror of
https://gitlab.com/pholy/OSCAR-code.git
synced 2025-04-08 04:00:44 +00:00
Seperated logger to separate class
This commit is contained in:
parent
56358c25c7
commit
c2ea072340
129
sleepyhead/logger.cpp
Normal file
129
sleepyhead/logger.cpp
Normal 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
44
sleepyhead/logger.h
Normal 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
|
@ -25,9 +25,8 @@
|
|||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QSysInfo>
|
#include <QSysInfo>
|
||||||
#include <QThreadPool>
|
|
||||||
|
|
||||||
|
|
||||||
|
#include "logger.h"
|
||||||
#include "SleepLib/schema.h"
|
#include "SleepLib/schema.h"
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "SleepLib/profiles.h"
|
#include "SleepLib/profiles.h"
|
||||||
@ -36,6 +35,7 @@
|
|||||||
#include "translation.h"
|
#include "translation.h"
|
||||||
#include "common_gui.h"
|
#include "common_gui.h"
|
||||||
|
|
||||||
|
|
||||||
// Gah! I must add the real darn plugin system one day.
|
// Gah! I must add the real darn plugin system one day.
|
||||||
#include "SleepLib/loader_plugins/prs1_loader.h"
|
#include "SleepLib/loader_plugins/prs1_loader.h"
|
||||||
#include "SleepLib/loader_plugins/cms50_loader.h"
|
#include "SleepLib/loader_plugins/cms50_loader.h"
|
||||||
@ -51,67 +51,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
MainWindow *mainwin = nullptr;
|
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()
|
void initialize()
|
||||||
{
|
{
|
||||||
@ -181,21 +120,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger = new LogThread();
|
initializeLogger();
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -33,7 +33,6 @@
|
|||||||
#include <QTranslator>
|
#include <QTranslator>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QCalendarWidget>
|
#include <QCalendarWidget>
|
||||||
#include <QThreadPool>
|
|
||||||
|
|
||||||
#include "common_gui.h"
|
#include "common_gui.h"
|
||||||
|
|
||||||
@ -47,6 +46,7 @@
|
|||||||
#include <SleepLib/loader_plugins/mseries_loader.h>
|
#include <SleepLib/loader_plugins/mseries_loader.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "logger.h"
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "ui_mainwindow.h"
|
#include "ui_mainwindow.h"
|
||||||
#include "newprofile.h"
|
#include "newprofile.h"
|
||||||
@ -116,47 +116,9 @@ QString getGraphicsEngine()
|
|||||||
return gfxEngine;
|
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)
|
void MainWindow::logMessage(QString msg)
|
||||||
{
|
{
|
||||||
ui->logText->appendPlainText(msg);
|
ui->logText->appendPlainText(msg);
|
||||||
fprintf(stderr, "%s\n", msg.toLocal8Bit().constData());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::MainWindow(QWidget *parent) :
|
MainWindow::MainWindow(QWidget *parent) :
|
||||||
@ -379,12 +341,8 @@ MainWindow::~MainWindow()
|
|||||||
// Trash anything allocated by the Graph objects
|
// Trash anything allocated by the Graph objects
|
||||||
DestroyGraphGlobals();
|
DestroyGraphGlobals();
|
||||||
|
|
||||||
logger->quit();
|
|
||||||
disconnect(logger, SIGNAL(outputLog(QString)), this, SLOT(logMessage(QString)));
|
disconnect(logger, SIGNAL(outputLog(QString)), this, SLOT(logMessage(QString)));
|
||||||
|
shutdownLogger();
|
||||||
otherThreadPool->waitForDone(-1);
|
|
||||||
delete logger;
|
|
||||||
logger = nullptr;
|
|
||||||
|
|
||||||
mainwin = nullptr;
|
mainwin = nullptr;
|
||||||
delete ui;
|
delete ui;
|
||||||
|
@ -17,8 +17,6 @@
|
|||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QSystemTrayIcon>
|
#include <QSystemTrayIcon>
|
||||||
#include <QRunnable>
|
|
||||||
#include <QThreadPool>
|
|
||||||
|
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
#include "daily.h"
|
#include "daily.h"
|
||||||
@ -62,37 +60,11 @@ class MainWindow;
|
|||||||
// * \section install_sec Installation
|
// * \section install_sec Installation
|
||||||
|
|
||||||
extern QStatusBar *qstatusbar;
|
extern QStatusBar *qstatusbar;
|
||||||
extern QThreadPool * otherThreadPool;
|
|
||||||
|
|
||||||
|
|
||||||
class Daily;
|
class Daily;
|
||||||
class Report;
|
class Report;
|
||||||
class Overview;
|
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
|
/*! \class MainWindow
|
||||||
\author Mark Watkins
|
\author Mark Watkins
|
||||||
|
@ -147,7 +147,8 @@ SOURCES += \
|
|||||||
oximeterimport.cpp \
|
oximeterimport.cpp \
|
||||||
SleepLib/serialoximeter.cpp \
|
SleepLib/serialoximeter.cpp \
|
||||||
SleepLib/loader_plugins/md300w1_loader.cpp \
|
SleepLib/loader_plugins/md300w1_loader.cpp \
|
||||||
Graphs/gSessionTimesChart.cpp
|
Graphs/gSessionTimesChart.cpp \
|
||||||
|
logger.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
common_gui.h \
|
common_gui.h \
|
||||||
@ -202,7 +203,8 @@ HEADERS += \
|
|||||||
oximeterimport.h \
|
oximeterimport.h \
|
||||||
SleepLib/serialoximeter.h \
|
SleepLib/serialoximeter.h \
|
||||||
SleepLib/loader_plugins/md300w1_loader.h \
|
SleepLib/loader_plugins/md300w1_loader.h \
|
||||||
Graphs/gSessionTimesChart.h
|
Graphs/gSessionTimesChart.h \
|
||||||
|
logger.h
|
||||||
|
|
||||||
FORMS += \
|
FORMS += \
|
||||||
daily.ui \
|
daily.ui \
|
||||||
|
Loading…
Reference in New Issue
Block a user