wfview/main.cpp

174 wiersze
4.9 KiB
C++
Czysty Zwykły widok Historia

2018-06-19 19:58:52 +00:00
#include <QApplication>
#include <iostream>
2021-02-07 20:07:26 +00:00
#include "wfmain.h"
2021-02-23 21:21:22 +00:00
#include "logcategories.h"
2018-06-19 19:58:52 +00:00
// Copytight 2017-2021 Elliott H. Liggett
2018-06-19 19:58:52 +00:00
2021-02-07 20:07:26 +00:00
// Smart pointer to log file
QScopedPointer<QFile> m_logFile;
QMutex logMutex;
bool debugMode=false;
2021-02-07 20:07:26 +00:00
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg);
2018-06-19 19:58:52 +00:00
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//a.setStyle( "Fusion" );
a.setOrganizationName("wfview");
a.setOrganizationDomain("wfview.org");
a.setApplicationName("wfview");
#ifdef QT_DEBUG
debugMode = true;
#endif
QString serialPortCL;
QString hostCL;
QString civCL;
#ifdef Q_OS_MAC
QString logFilename= QStandardPaths::standardLocations(QStandardPaths::DownloadLocation)[0] + "/wfview.log";
#else
2021-02-07 20:07:26 +00:00
QString logFilename= QStandardPaths::standardLocations(QStandardPaths::TempLocation)[0] + "/wfview.log";
#endif
QString settingsFile = NULL;
QString currentArg;
const QString helpText = QString("\nUsage: -p --port /dev/port, -h --host remotehostname, -c --civ 0xAddr, -l --logfile filename.log, -s --settings filename.ini, -d --debug\n"); // TODO...
for(int c=1; c<argc; c++)
{
//qInfo() << "Argc: " << c << " argument: " << argv[c];
currentArg = QString(argv[c]);
if ((currentArg == "-p") || (currentArg == "--port"))
{
if (argc > c)
{
serialPortCL = argv[c + 1];
c += 1;
}
}
else if ((currentArg == "-d") || (currentArg == "--debug"))
{
debugMode = true;
}
else if ((currentArg == "-h") || (currentArg == "--host"))
{
if(argc > c)
{
hostCL = argv[c+1];
c+=1;
}
2021-02-07 20:07:26 +00:00
}
else if ((currentArg == "-c") || (currentArg == "--civ"))
{
2021-02-07 20:07:26 +00:00
if (argc > c)
{
2021-02-07 20:07:26 +00:00
civCL = argv[c + 1];
c += 1;
}
}
else if ((currentArg == "-l") || (currentArg == "--logfile"))
2021-02-07 20:07:26 +00:00
{
if (argc > c)
{
logFilename = argv[c + 1];
c += 1;
}
}
else if ((currentArg == "-s") || (currentArg == "--settings"))
{
if (argc > c)
{
settingsFile = argv[c + 1];
c += 1;
}
}
else if ((currentArg == "--help"))
{
2021-05-20 19:01:30 +00:00
#ifdef Q_OS_WIN
QMessageBox::information(0, "wfview help", helpText);
#else
std::cout << helpText.toStdString();
2021-05-20 19:01:30 +00:00
#endif
return 0;
} else {
2021-05-20 19:01:30 +00:00
#ifdef Q_OS_WIN
QMessageBox::information(0, "wfview unrecognised argument", helpText);
#else
std::cout << "Unrecognized option: " << currentArg.toStdString();
std::cout << helpText.toStdString();
2021-05-20 19:01:30 +00:00
#endif
return -1;
}
}
2021-02-07 20:07:26 +00:00
// Set the logging file before doing anything else.
m_logFile.reset(new QFile(logFilename));
// Open the file logging
2021-06-08 18:12:01 +00:00
m_logFile.data()->open(QFile::WriteOnly | QFile::Truncate | QFile::Text);
2021-02-07 20:07:26 +00:00
// Set handler
qInstallMessageHandler(messageHandler);
2021-05-28 17:33:40 +00:00
qInfo(logSystem()) << QString("Starting wfview: build %1 on %2 at %3 by %5@%6").arg(GITSHORT).arg(__DATE__).arg(__TIME__).arg(UNAME).arg(HOST);
qInfo(logSystem()) << QString("Operating System: %1 (%2)").arg(QSysInfo::prettyProductName()).arg(QSysInfo::buildCpuArchitecture());
qInfo(logSystem()) << QString("Build Qt Version %1. Current Qt Version: %2").arg(QT_VERSION_STR).arg(qVersion());
qDebug(logSystem()) << QString("SerialPortCL as set by parser: %1").arg(serialPortCL);
qDebug(logSystem()) << QString("remote host as set by parser: %1").arg(hostCL);
qDebug(logSystem()) << QString("CIV as set by parser: %1").arg(civCL);
a.setWheelScrollLines(1); // one line per wheel click
wfmain w( serialPortCL, hostCL, settingsFile);
2018-06-19 19:58:52 +00:00
w.show();
2018-06-19 19:58:52 +00:00
return a.exec();
2021-02-07 20:07:26 +00:00
2018-06-19 19:58:52 +00:00
}
2021-02-07 20:07:26 +00:00
void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
// Open stream file writes
if (type == QtDebugMsg && !debugMode)
{
return;
}
2021-02-07 20:07:26 +00:00
QMutexLocker locker(&logMutex);
QTextStream out(m_logFile.data());
// Write the date of recording
out << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz ");
// By type determine to what level belongs message
switch (type)
{
case QtDebugMsg:
out << "DBG ";
break;
case QtInfoMsg:
out << "INF ";
break;
case QtWarningMsg:
out << "WRN ";
break;
case QtCriticalMsg:
out << "CRT ";
break;
case QtFatalMsg:
out << "FTL ";
break;
2021-02-07 20:07:26 +00:00
}
// Write to the output category of the message and the message itself
out << context.category << ": " << msg << "\n";
out.flush(); // Clear the buffered data
}