Skip to content

Commit

Permalink
Added global debug to file functions to SPIERSview
Browse files Browse the repository at this point in the history
The standard QT debug, warning, info, and fatal macro are caught by the new function in main.cpp. These are either feed only to the console, or to the console and a file named SPIERSView_Debug.log in the users HOME folder. There is a bool flag on line 36 of the main.ccp file that swtiches this behaviour. Also to get this to work in release mode (for debuging release macOS applications) lines 12-14 in the .pro fiule need to be uncommented. Both this and the writting to log file should be turned off in real releases.
  • Loading branch information
alanspencer committed May 28, 2020
1 parent 1ddf1ae commit fbef3e0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
8 changes: 4 additions & 4 deletions SPIERSview/SPIERSview.pro
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ TEMPLATE = app
QT += network xml gui core widgets opengl

# Allow debug in release
#QMAKE_CXXFLAGS_RELEASE += -g
#QMAKE_CFLAGS_RELEASE += -g
#QMAKE_LFLAGS_RELEASE =
QMAKE_CXXFLAGS_RELEASE += -g
QMAKE_CFLAGS_RELEASE += -g
QMAKE_LFLAGS_RELEASE =

CONFIG += qt \
release \
Expand Down Expand Up @@ -216,7 +216,7 @@ unix:!macx {
-lz \
}

# MacOS common build here (not currently tested or supported)
# MacOS common build here
macx {

#RJG - Below vtk installation achieved using installation via homebrew
Expand Down
45 changes: 26 additions & 19 deletions SPIERSview/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QTimer>
#include <QDesktopWidget>
#include <QScreen>
#include <QFileOpenEvent>

#include "main.h"
#include "mainwindow.h"
Expand All @@ -32,6 +33,7 @@ void logMessageOutput(QtMsgType type, const QMessageLogContext &context, const Q
{
QByteArray localMsg = msg.toLocal8Bit();
QString txt;
bool logToFile = true;

switch (type)
{
Expand All @@ -51,24 +53,22 @@ void logMessageOutput(QtMsgType type, const QMessageLogContext &context, const Q
txt = QString("Info: %1 (%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function);
break;
}

#ifndef __APPLE__
#ifdef QT_DEBUG
// Save to debug.log
QFile outFile("debug.log");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream log(&outFile);
log << txt << endl;

// Now print to stout too
QTextStream console(stdout);
console << txt << endl;
#else
// Print to stout only
QTextStream console(stdout);
console << txt << endl;
#endif
#endif
if (logToFile) {
// Save to debug.log
QString path = QString("%1/SPIERSView_debug.log").arg(QDir::homePath());
QFile outFile(path);
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream log(&outFile);
log << txt << endl;

// Now print to stout too
QTextStream console(stdout);
console << txt << endl;
} else {
// Print to stout only
QTextStream console(stdout);
console << txt << endl;
}
}

#ifndef __APPLE__
Expand Down Expand Up @@ -183,8 +183,10 @@ main::main(int &argc, char *argv[]) : QApplication(argc, argv)
bool main::event(QEvent *event)
{
//we don't do anything if we were passed and argv1 - i.e. if we are a child process of first one
if (donthandlefileevent == true)
if (donthandlefileevent == true) {
qDebug() << "Don't handle file open event";
return QApplication::event(event);
}

switch (event->type())
{
Expand All @@ -194,6 +196,8 @@ bool main::event(QEvent *event)

case QEvent::FileOpen:

qDebug() << "File Open Event";

QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(event);
fn = openEvent->file();

Expand Down Expand Up @@ -239,6 +243,9 @@ int main(int argc, char *argv[])
// Set to allow the OpenGL context (ie. the same threads) to be shared between normal and full screen mode
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);

// Install the message handler to log to file
qInstallMessageHandler(logMessageOutput);

if (argc == 2) {
// Check that the passed file name has at least 2 characters
if (QString(argv[1]).length() < 2)
Expand Down

0 comments on commit fbef3e0

Please sign in to comment.