Disable logs by default in release mode

Enable logs if '-d' command-line option is given.

Additional command-line options -
-v : print version
-h : print usage

Drone only:
-p <port-number> : use given port number for the RPC service
This commit is contained in:
Srivats P 2019-07-02 18:15:26 +05:30
parent 3fca24396d
commit f24a6719fa
10 changed files with 206 additions and 19 deletions

View File

@ -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();
}
}

View File

@ -21,9 +21,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <unistd.h>
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();

View File

@ -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_;
};

View File

@ -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()

View File

@ -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,

View File

@ -20,13 +20,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "drone.h"
#include "myservice.h"
#include "params.h"
#include "rpcserver.h"
#include "settings.h"
#include "../common/updater.h"
#include <QMetaType>
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;

View File

@ -58,6 +58,7 @@ SOURCES += \
bsdport.cpp \
linuxhostdevice.cpp \
linuxport.cpp \
params.cpp \
winhostdevice.cpp \
winpcapport.cpp
SOURCES += myservice.cpp

View File

@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include "drone.h"
#include "../common/protocolmanager.h"
#include "params.h"
#include "settings.h"
#include <google/protobuf/stubs/common.h>
@ -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();
}
}

82
server/params.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>
*/
#include "params.h"
#include <unistd.h>
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 <port-number>]\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();
}

45
server/params.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>
*/
#ifndef _PARAMS_H
#define _PARAMS_H
#include <QStringList>
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