2022-01-18 09:03:41 +00:00
# ifdef BUILD_WFSERVER
2022-01-17 17:23:55 +00:00
# include <QtCore/QCoreApplication>
2022-04-13 11:55:59 +00:00
# include "keyboard.h"
2022-01-17 17:23:55 +00:00
# else
2018-06-19 19:58:52 +00:00
# include <QApplication>
2022-01-17 17:23:55 +00:00
# endif
2022-01-26 09:49:52 +00:00
# ifdef Q_OS_WIN
# include <windows.h>
# include <csignal>
# endif
2021-02-02 09:05:59 +00:00
# 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
2022-03-23 15:45:51 +00:00
// Copyright 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 ;
2021-05-15 17:53:16 +00:00
bool debugMode = false ;
2021-02-07 20:07:26 +00:00
2022-01-26 09:49:52 +00:00
# ifdef BUILD_WFSERVER
servermain * w = Q_NULLPTR ;
# ifdef Q_OS_WIN
bool __stdcall cleanup ( DWORD sig )
# else
static void cleanup ( int sig )
# endif
{
Q_UNUSED ( sig )
qDebug ( ) < < " Exiting via SIGNAL " ;
if ( w ! = Q_NULLPTR ) w - > deleteLater ( ) ;
2022-05-24 09:08:18 +00:00
QCoreApplication : : quit ( ) ;
2022-01-26 09:49:52 +00:00
# ifdef Q_OS_WIN
return true ;
# else
return ;
# endif
}
# endif
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 [ ] )
{
2022-04-13 11:55:59 +00:00
2022-01-18 09:03:41 +00:00
# ifdef BUILD_WFSERVER
2022-01-17 17:23:55 +00:00
QCoreApplication a ( argc , argv ) ;
2022-04-13 11:55:59 +00:00
a . setOrganizationName ( " wfview " ) ;
a . setOrganizationDomain ( " wfview.org " ) ;
a . setApplicationName ( " wfserver " ) ;
keyboard * kb = new keyboard ( ) ;
kb - > start ( ) ;
2022-01-17 17:23:55 +00:00
# else
2022-04-20 12:35:23 +00:00
QCoreApplication : : setAttribute ( Qt : : AA_EnableHighDpiScaling ) ;
2018-06-19 19:58:52 +00:00
QApplication a ( argc , argv ) ;
2021-05-01 07:58:30 +00:00
a . setOrganizationName ( " wfview " ) ;
a . setOrganizationDomain ( " wfview.org " ) ;
2020-01-05 01:12:35 +00:00
a . setApplicationName ( " wfview " ) ;
2022-04-13 11:55:59 +00:00
# endif
2018-11-24 08:10:05 +00:00
2021-05-15 17:53:16 +00:00
# ifdef QT_DEBUG
debugMode = true ;
# endif
2021-02-02 09:05:59 +00:00
QString serialPortCL ;
QString hostCL ;
QString civCL ;
2021-05-17 16:05:19 +00:00
# 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 " ;
2021-05-17 16:05:19 +00:00
# endif
2021-05-20 18:24:40 +00:00
QString settingsFile = NULL ;
2021-02-02 09:05:59 +00:00
QString currentArg ;
2021-02-11 19:47:29 +00:00
2021-11-19 17:52:18 +00:00
const QString helpText = QString ( " \n Usage: -p --port /dev/port, -h --host remotehostname, -c --civ 0xAddr, -l --logfile filename.log, -s --settings filename.ini, -d --debug, -v --version \n " ) ; // TODO...
2022-01-18 09:03:41 +00:00
# ifdef BUILD_WFSERVER
2022-01-17 17:23:55 +00:00
const QString version = QString ( " wfserver version: %1 (Git:%2 on %3 at %4 by %5@%6) \n Operating System: %7 (%8) \n Build Qt Version %9. Current Qt Version: %10 \n " )
. arg ( QString ( WFVIEW_VERSION ) )
. arg ( GITSHORT ) . arg ( __DATE__ ) . arg ( __TIME__ ) . arg ( UNAME ) . arg ( HOST )
. arg ( QSysInfo : : prettyProductName ( ) ) . arg ( QSysInfo : : buildCpuArchitecture ( ) )
. arg ( QT_VERSION_STR ) . arg ( qVersion ( ) ) ;
# else
2021-11-19 18:48:15 +00:00
const QString version = QString ( " wfview version: %1 (Git:%2 on %3 at %4 by %5@%6) \n Operating System: %7 (%8) \n Build Qt Version %9. Current Qt Version: %10 \n " )
2022-01-17 17:23:55 +00:00
. arg ( QString ( WFVIEW_VERSION ) )
. arg ( GITSHORT ) . arg ( __DATE__ ) . arg ( __TIME__ ) . arg ( UNAME ) . arg ( HOST )
. arg ( QSysInfo : : prettyProductName ( ) ) . arg ( QSysInfo : : buildCpuArchitecture ( ) )
. arg ( QT_VERSION_STR ) . arg ( qVersion ( ) ) ;
# endif
2021-02-02 09:05:59 +00:00
for ( int c = 1 ; c < argc ; c + + )
{
2021-05-15 17:53:16 +00:00
//qInfo() << "Argc: " << c << " argument: " << argv[c];
2021-02-02 09:05:59 +00:00
currentArg = QString ( argv [ c ] ) ;
2021-05-15 17:53:16 +00:00
if ( ( currentArg = = " -p " ) | | ( currentArg = = " --port " ) )
2021-02-02 09:05:59 +00:00
{
2021-05-15 17:53:16 +00:00
if ( argc > c )
2021-02-02 09:05:59 +00:00
{
2021-05-15 17:53:16 +00:00
serialPortCL = argv [ c + 1 ] ;
c + = 1 ;
2021-02-02 09:05:59 +00:00
}
2021-05-15 17:53:16 +00:00
}
2021-05-20 18:24:40 +00:00
else if ( ( currentArg = = " -d " ) | | ( currentArg = = " --debug " ) )
2021-05-15 17:53:16 +00:00
{
debugMode = true ;
}
else if ( ( currentArg = = " -h " ) | | ( currentArg = = " --host " ) )
2021-02-02 09:05:59 +00:00
{
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-02 09:05:59 +00:00
{
2021-02-07 20:07:26 +00:00
if ( argc > c )
2021-02-02 09:05:59 +00:00
{
2021-02-07 20:07:26 +00:00
civCL = argv [ c + 1 ] ;
c + = 1 ;
}
}
2021-02-08 09:20:06 +00:00
else if ( ( currentArg = = " -l " ) | | ( currentArg = = " --logfile " ) )
2021-02-07 20:07:26 +00:00
{
if ( argc > c )
{
logFilename = argv [ c + 1 ] ;
c + = 1 ;
2021-02-02 09:05:59 +00:00
}
2021-05-20 18:24:40 +00:00
}
else if ( ( currentArg = = " -s " ) | | ( currentArg = = " --settings " ) )
{
if ( argc > c )
{
settingsFile = argv [ c + 1 ] ;
c + = 1 ;
}
}
2021-11-19 17:52:18 +00:00
else if ( ( currentArg = = " -? " ) | | ( currentArg = = " --help " ) )
2021-02-02 09:05:59 +00:00
{
std : : cout < < helpText . toStdString ( ) ;
2021-11-19 17:52:18 +00:00
return 0 ;
}
else if ( ( currentArg = = " -v " ) | | ( currentArg = = " --version " ) )
{
2021-11-19 18:48:15 +00:00
std : : cout < < version . toStdString ( ) ;
2021-02-02 09:05:59 +00:00
return 0 ;
} else {
std : : cout < < " Unrecognized option: " < < currentArg . toStdString ( ) ;
std : : cout < < helpText . toStdString ( ) ;
2022-01-17 17:23:55 +00:00
return - 1 ;
2021-02-02 09:05:59 +00:00
}
}
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-11-19 18:48:15 +00:00
qInfo ( logSystem ( ) ) < < version ;
2021-05-28 17:33:40 +00:00
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 ) ;
2022-01-17 17:23:55 +00:00
2022-01-18 09:03:41 +00:00
# ifdef BUILD_WFSERVER
2022-01-26 09:49:52 +00:00
# ifdef Q_OS_WIN
SetConsoleCtrlHandler ( ( PHANDLER_ROUTINE ) cleanup , TRUE ) ;
# else
signal ( SIGINT , cleanup ) ;
2022-05-24 09:13:44 +00:00
signal ( SIGTERM , cleanup ) ;
signal ( SIGKILL , cleanup ) ;
2022-01-26 09:49:52 +00:00
# endif
w = new servermain ( serialPortCL , hostCL , settingsFile ) ;
2022-01-17 17:23:55 +00:00
# else
2018-11-25 06:21:36 +00:00
a . setWheelScrollLines ( 1 ) ; // one line per wheel click
2022-01-17 17:23:55 +00:00
wfmain w ( serialPortCL , hostCL , settingsFile ) ;
2018-06-19 19:58:52 +00:00
w . show ( ) ;
2022-01-18 00:11:15 +00:00
2022-01-17 17:23:55 +00:00
# endif
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
2021-05-15 17:53:16 +00:00
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 )
{
2021-05-15 17:53:16 +00:00
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 " ;
2022-01-18 09:03:41 +00:00
# ifdef BUILD_WFSERVER
2022-04-11 10:54:06 +00:00
std : : cout < < QDateTime : : currentDateTime ( ) . toString ( " yyyy-MM-dd hh:mm:ss.zzz " ) . toLocal8Bit ( ) . toStdString ( ) < < msg . toLocal8Bit ( ) . toStdString ( ) < < " \n " ;
2022-01-17 17:23:55 +00:00
# endif
2021-02-07 20:07:26 +00:00
out . flush ( ) ; // Clear the buffered data
2021-02-08 09:20:06 +00:00
}