diff --git a/client/main.cpp b/client/main.cpp index 1507eef..35933e4 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -42,6 +42,9 @@ Params appParams; QSettings *appSettings; QMainWindow *mainWindow; +void NoMsgHandler(QtMsgType type, const QMessageLogContext &context, + const QString &msg); + int main(int argc, char* argv[]) { QApplication app(argc, argv); @@ -54,6 +57,11 @@ int main(int argc, char* argv[]) appParams.parseCommandLine(argc, argv); +#ifndef QT_DEBUG // Release mode + if (appParams.optLogsDisabled()) + qInstallMessageHandler(NoMsgHandler); +#endif + OstProtocolManager = new ProtocolManager(); OstProtocolWidgetFactory = new ProtocolWidgetFactory(); @@ -87,3 +95,14 @@ int main(int argc, char* argv[]) return exitCode; } + +void NoMsgHandler(QtMsgType type, const QMessageLogContext &/*context*/, + const QString &msg) +{ + if (type == QtFatalMsg) { + fprintf(stderr, qPrintable(msg)); + fflush(stderr); + abort(); + } +} + diff --git a/client/params.cpp b/client/params.cpp index 4e90581..991e3fe 100644 --- a/client/params.cpp +++ b/client/params.cpp @@ -21,9 +21,13 @@ along with this program. If not, see #include +extern char *version; +extern char *revision; + Params::Params() { localDrone_ = true; + logsDisabled_ = true; } int Params::parseCommandLine(int argc, char* argv[]) @@ -31,14 +35,22 @@ int Params::parseCommandLine(int argc, char* argv[]) int c, n = 0; opterr = 0; - while ((c = getopt (argc, argv, "c")) != -1) { + while ((c = getopt (argc, argv, "cdhv")) != -1) { switch (c) { case 'c': localDrone_ = false; break; + case 'd': + logsDisabled_ = false; + break; + case 'v': + qDebug("Ostinato %s rev %s\n", version, revision); + exit(0); + case 'h': default: - qDebug("ignoring unrecognized option (%c)", c); + qDebug("usage: %s [-cdhv]\n", argv[0]); + exit(1); } n++; } @@ -54,6 +66,11 @@ bool Params::optLocalDrone() return localDrone_; } +bool Params::optLogsDisabled() +{ + return logsDisabled_; +} + int Params::argumentCount() { return args_.size(); diff --git a/client/params.h b/client/params.h index a5ebc2f..ee5d0b6 100644 --- a/client/params.h +++ b/client/params.h @@ -28,12 +28,14 @@ public: int parseCommandLine(int argc, char* argv[]); bool optLocalDrone(); + bool optLogsDisabled(); int argumentCount(); QString argument(int index); private: bool localDrone_; + bool logsDisabled_; QStringList args_; }; diff --git a/rpc/rpcserver.cpp b/rpc/rpcserver.cpp index 2691153..597693c 100644 --- a/rpc/rpcserver.cpp +++ b/rpc/rpcserver.cpp @@ -32,11 +32,12 @@ protected: void run() { exec(); } }; -RpcServer::RpcServer() +RpcServer::RpcServer(bool perConnLogs) { service = NULL; - qInstallMessageHandler(RpcConnection::connIdMsgHandler); + if (perConnLogs) + qInstallMessageHandler(RpcConnection::connIdMsgHandler); } RpcServer::~RpcServer() diff --git a/rpc/rpcserver.h b/rpc/rpcserver.h index 59450f1..c664512 100644 --- a/rpc/rpcserver.h +++ b/rpc/rpcserver.h @@ -37,7 +37,7 @@ class RpcServer : public QTcpServer Q_OBJECT public: - RpcServer(); //! \todo (LOW) use 'parent' param + RpcServer(bool perConnLogs); //! \todo (LOW) use 'parent' param virtual ~RpcServer(); bool registerService(::google::protobuf::Service *service, diff --git a/server/drone.cpp b/server/drone.cpp index cdad7cd..888fbcc 100644 --- a/server/drone.cpp +++ b/server/drone.cpp @@ -20,13 +20,14 @@ along with this program. If not, see #include "drone.h" #include "myservice.h" +#include "params.h" #include "rpcserver.h" #include "settings.h" #include "../common/updater.h" #include -extern int myport; +extern Params appParams; extern const char* version; extern const char* revision; @@ -35,7 +36,13 @@ Drone::Drone(QObject *parent) { Updater *updater = new Updater(); - rpcServer = new RpcServer(); +#ifdef QT_DEBUG + bool enableLogs = true; +#else + bool enableLogs = !appParams.optLogsDisabled(); +#endif + + rpcServer = new RpcServer(enableLogs); service = new MyService(); connect(updater, SIGNAL(newVersionAvailable(QString)), @@ -66,7 +73,7 @@ bool Drone::init() address = QHostAddress::Any; } - if (!rpcServer->registerService(service, address, myport ? myport : 7878)) + if (!rpcServer->registerService(service, address, appParams.servicePortNumber())) { //qCritical(qPrintable(rpcServer->errorString())); return false; diff --git a/server/drone.pro b/server/drone.pro index 13cdea4..48f4064 100644 --- a/server/drone.pro +++ b/server/drone.pro @@ -58,6 +58,7 @@ SOURCES += \ bsdport.cpp \ linuxhostdevice.cpp \ linuxport.cpp \ + params.cpp \ winhostdevice.cpp \ winpcapport.cpp SOURCES += myservice.cpp diff --git a/server/drone_main.cpp b/server/drone_main.cpp index 0de51f5..e9c0f5a 100644 --- a/server/drone_main.cpp +++ b/server/drone_main.cpp @@ -20,6 +20,7 @@ along with this program. If not, see #include "drone.h" #include "../common/protocolmanager.h" +#include "params.h" #include "settings.h" #include @@ -37,7 +38,10 @@ extern char *revision; Drone *drone; QSettings *appSettings; -int myport; +Params appParams; + +void NoMsgHandler(QtMsgType type, const QMessageLogContext &context, + const QString &msg); void cleanup(int /*signum*/) { @@ -49,19 +53,19 @@ int main(int argc, char *argv[]) int exitCode = 0; QCoreApplication app(argc, argv); - qDebug("Version: %s", version); - qDebug("Revision: %s", revision); - - // TODO: command line options - // -v (--version) - // -h (--help) - // -p (--portnum) - if (argc > 1) - myport = atoi(argv[1]); - app.setApplicationName("Drone"); app.setOrganizationName("Ostinato"); + appParams.parseCommandLine(argc, argv); + +#ifdef QT_NO_DEBUG + if (appParams.optLogsDisabled()) + qInstallMessageHandler(NoMsgHandler); +#endif + + qDebug("Version: %s", version); + qDebug("Revision: %s", revision); + /* (Portable Mode) If we have a .ini file in the same directory as the executable, we use that instead of the platform specific location and format for the settings */ @@ -108,3 +112,12 @@ _exit: return exitCode; } +void NoMsgHandler(QtMsgType type, const QMessageLogContext &/*context*/, + const QString &msg) +{ + if (type == QtFatalMsg) { + fprintf(stderr, qPrintable(msg)); + fflush(stderr); + abort(); + } +} diff --git a/server/params.cpp b/server/params.cpp new file mode 100644 index 0000000..11541cb --- /dev/null +++ b/server/params.cpp @@ -0,0 +1,82 @@ +/* +Copyright (C) 2019 Srivats P. + +This file is part of "Ostinato" + +This is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +#include "params.h" + +#include + +extern char *version; +extern char *revision; + +Params::Params() +{ + logsDisabled_ = true; + myPort_ = 7878; +} + +int Params::parseCommandLine(int argc, char* argv[]) +{ + int c, n = 0; + + opterr = 0; + while ((c = getopt (argc, argv, "dhp:v")) != -1) { + switch (c) + { + case 'd': + logsDisabled_ = false; + break; + case 'p': + myPort_ = atoi(optarg); + break; + case 'v': + printf("Ostinato Drone %s rev %s\n", version, revision); + exit(0); + case 'h': + default: + printf("usage: %s [-dhv] [-p ]\n", argv[0]); + exit(1); + } + n++; + } + + for (int i = optind; i < argc; i++, n++) + args_ << argv[i]; + + return n; +} + +bool Params::optLogsDisabled() +{ + return logsDisabled_; +} + +int Params::servicePortNumber() +{ + return myPort_; +} + +int Params::argumentCount() +{ + return args_.size(); +} + +QString Params::argument(int index) +{ + return index < args_.size() ? args_.at(index) : QString(); +} diff --git a/server/params.h b/server/params.h new file mode 100644 index 0000000..67ad3fd --- /dev/null +++ b/server/params.h @@ -0,0 +1,45 @@ +/* +Copyright (C) 2019 Srivats P. + +This file is part of "Ostinato" + +This is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +#ifndef _PARAMS_H +#define _PARAMS_H + +#include + +class Params { +public: + Params(); + int parseCommandLine(int argc, char* argv[]); + + bool optLogsDisabled(); + int servicePortNumber(); + + int argumentCount(); + QString argument(int index); + +private: + bool logsDisabled_; + int myPort_; + QStringList args_; +}; + +extern Params appParams; + +#endif +