refactor: move ArgsBase:m_pname to App
This commit is contained in:
parent
1884ab3555
commit
cfd580fb21
12 changed files with 28 additions and 50 deletions
|
|
@ -19,6 +19,7 @@
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <QFileInfo>
|
||||||
#include <QSharedMemory>
|
#include <QSharedMemory>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@ -89,12 +90,13 @@ int main(int argc, char **argv)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
EventQueue events;
|
EventQueue events;
|
||||||
|
const auto processName = QFileInfo(argv[0]).fileName();
|
||||||
|
|
||||||
if (parser.serverMode()) {
|
if (parser.serverMode()) {
|
||||||
ServerApp app(&events);
|
ServerApp app(&events, processName);
|
||||||
return app.run(argc, argv);
|
return app.run(argc, argv);
|
||||||
} else if (parser.clientMode()) {
|
} else if (parser.clientMode()) {
|
||||||
ClientApp app(&events);
|
ClientApp app(&events, processName);
|
||||||
return app.run(argc, argv);
|
return app.run(argc, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,11 +48,12 @@ App *App::s_instance = nullptr;
|
||||||
// App
|
// App
|
||||||
//
|
//
|
||||||
|
|
||||||
App::App(IEventQueue *events, deskflow::ArgsBase *args)
|
App::App(IEventQueue *events, const QString &processName, deskflow::ArgsBase *args)
|
||||||
: m_bye(&exit),
|
: m_bye(&exit),
|
||||||
m_events(events),
|
m_events(events),
|
||||||
m_args(args),
|
m_args(args),
|
||||||
m_appUtil(events)
|
m_appUtil(events),
|
||||||
|
m_pname(processName)
|
||||||
{
|
{
|
||||||
assert(s_instance == nullptr);
|
assert(s_instance == nullptr);
|
||||||
s_instance = this;
|
s_instance = this;
|
||||||
|
|
@ -170,7 +171,7 @@ void App::initApp(int argc, const char **argv)
|
||||||
|
|
||||||
// set log filter
|
// set log filter
|
||||||
if (const auto logLevel = qPrintable(Settings::logLevelText()); !CLOG->setFilter(logLevel)) {
|
if (const auto logLevel = qPrintable(Settings::logLevelText()); !CLOG->setFilter(logLevel)) {
|
||||||
LOG_CRIT("%s: unrecognized log level `%s'" BYE, argsBase().m_pname, logLevel, argsBase().m_pname);
|
LOG_CRIT("%s: unrecognized log level `%s'" BYE, qPrintable(processName()), logLevel, qPrintable(processName()));
|
||||||
m_bye(s_exitArgs);
|
m_bye(s_exitArgs);
|
||||||
}
|
}
|
||||||
loggingFilterWarning();
|
loggingFilterWarning();
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
App(IEventQueue *events, deskflow::ArgsBase *args);
|
App(IEventQueue *events, const QString &processName, deskflow::ArgsBase *args);
|
||||||
App(App const &) = delete;
|
App(App const &) = delete;
|
||||||
App(App &&) = delete;
|
App(App &&) = delete;
|
||||||
~App() override;
|
~App() override;
|
||||||
|
|
@ -108,6 +108,11 @@ public:
|
||||||
return *s_instance;
|
return *s_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString processName() const
|
||||||
|
{
|
||||||
|
return m_pname;
|
||||||
|
}
|
||||||
|
|
||||||
void handleScreenError() const;
|
void handleScreenError() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
@ -121,6 +126,7 @@ private:
|
||||||
FileLogOutputter *m_fileLog = nullptr;
|
FileLogOutputter *m_fileLog = nullptr;
|
||||||
ARCH_APP_UTIL m_appUtil;
|
ARCH_APP_UTIL m_appUtil;
|
||||||
std::unique_ptr<SocketMultiplexer> m_socketMultiplexer;
|
std::unique_ptr<SocketMultiplexer> m_socketMultiplexer;
|
||||||
|
QString m_pname;
|
||||||
};
|
};
|
||||||
|
|
||||||
#if WINAPI_MSWINDOWS
|
#if WINAPI_MSWINDOWS
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,6 @@ ArgParser::ArgParser(App *app) : m_app(app)
|
||||||
bool ArgParser::parseServerArgs(deskflow::ServerArgs &args, int argc, const char *const *argv) const
|
bool ArgParser::parseServerArgs(deskflow::ServerArgs &args, int argc, const char *const *argv) const
|
||||||
{
|
{
|
||||||
setArgsBase(args);
|
setArgsBase(args);
|
||||||
updateCommonArgs(argv);
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
while (i < argc) {
|
while (i < argc) {
|
||||||
if (parseGenericArgs(argc, argv, i) || parseDeprecatedArgs(argc, argv, i) ||
|
if (parseGenericArgs(argc, argv, i) || parseDeprecatedArgs(argc, argv, i) ||
|
||||||
|
|
@ -41,7 +40,7 @@ bool ArgParser::parseServerArgs(deskflow::ServerArgs &args, int argc, const char
|
||||||
} else if (isArg(i, argc, argv, nullptr, "--disable-client-cert-check")) {
|
} else if (isArg(i, argc, argv, nullptr, "--disable-client-cert-check")) {
|
||||||
args.m_chkPeerCert = false;
|
args.m_chkPeerCert = false;
|
||||||
} else {
|
} else {
|
||||||
LOG_CRIT("%s: unrecognized option `%s'" BYE, args.m_pname, argv[i], args.m_pname);
|
LOG_CRIT("%s: unrecognized option `%s'" BYE, "deskflow-core", argv[i], "deskflow-core");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
|
|
@ -57,7 +56,6 @@ bool ArgParser::parseServerArgs(deskflow::ServerArgs &args, int argc, const char
|
||||||
bool ArgParser::parseClientArgs(deskflow::ClientArgs &args, int argc, const char *const *argv) const
|
bool ArgParser::parseClientArgs(deskflow::ClientArgs &args, int argc, const char *const *argv) const
|
||||||
{
|
{
|
||||||
setArgsBase(args);
|
setArgsBase(args);
|
||||||
updateCommonArgs(argv);
|
|
||||||
|
|
||||||
int i{1};
|
int i{1};
|
||||||
while (i < argc) {
|
while (i < argc) {
|
||||||
|
|
@ -80,7 +78,7 @@ bool ArgParser::parseClientArgs(deskflow::ClientArgs &args, int argc, const char
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_CRIT("%s: unrecognized option `%s'" BYE, args.m_pname, argv[i], args.m_pname);
|
LOG_CRIT("%s: unrecognized option `%s'" BYE, "deskflow-core", argv[i], "deskflow-core");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
|
|
@ -88,7 +86,7 @@ bool ArgParser::parseClientArgs(deskflow::ClientArgs &args, int argc, const char
|
||||||
|
|
||||||
// exactly one non-option argument (server-address)
|
// exactly one non-option argument (server-address)
|
||||||
if (i == argc && !args.m_shouldExitFail && !args.m_shouldExitOk) {
|
if (i == argc && !args.m_shouldExitFail && !args.m_shouldExitOk) {
|
||||||
LOG_CRIT("%s: a server address or name is required" BYE, args.m_pname, args.m_pname);
|
LOG_CRIT("%s: a server address or name is required" BYE, "deskflow-core", "deskflow-core");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,7 +135,7 @@ bool ArgParser::isArg(
|
||||||
if ((name1 != nullptr && strcmp(argv[argi], name1) == 0) || (name2 != nullptr && strcmp(argv[argi], name2) == 0)) {
|
if ((name1 != nullptr && strcmp(argv[argi], name1) == 0) || (name2 != nullptr && strcmp(argv[argi], name2) == 0)) {
|
||||||
// match. check args left.
|
// match. check args left.
|
||||||
if (argi + minRequiredParameters >= argc) {
|
if (argi + minRequiredParameters >= argc) {
|
||||||
LOG_PRINT("%s: missing arguments for `%s'" BYE, argsBase().m_pname, argv[argi], argsBase().m_pname);
|
LOG_PRINT("%s: missing arguments for `%s'" BYE, "deskflow-core", argv[argi], "deskflow-core");
|
||||||
argsBase().m_shouldExitFail = true;
|
argsBase().m_shouldExitFail = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -270,11 +268,6 @@ std::string ArgParser::assembleCommand(
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArgParser::updateCommonArgs(const char *const *argv) const
|
|
||||||
{
|
|
||||||
argsBase().m_pname = QFileInfo(argv[0]).fileName().toLocal8Bit().constData();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ArgParser::checkUnexpectedArgs() const
|
bool ArgParser::checkUnexpectedArgs() const
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,6 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateCommonArgs(const char *const *argv) const;
|
|
||||||
bool checkUnexpectedArgs() const;
|
bool checkUnexpectedArgs() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -32,9 +32,6 @@ public:
|
||||||
/// @brief Stores what type of object this is
|
/// @brief Stores what type of object this is
|
||||||
ClassType m_classType = ClassType::Base;
|
ClassType m_classType = ClassType::Base;
|
||||||
|
|
||||||
/// @brief The filename of the running process
|
|
||||||
const char *m_pname = nullptr;
|
|
||||||
|
|
||||||
/// @brief Will cause the application to exit with OK code when set to true
|
/// @brief Will cause the application to exit with OK code when set to true
|
||||||
bool m_shouldExitOk = false;
|
bool m_shouldExitOk = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,8 @@
|
||||||
|
|
||||||
constexpr static auto s_retryTime = 1.0;
|
constexpr static auto s_retryTime = 1.0;
|
||||||
|
|
||||||
ClientApp::ClientApp(IEventQueue *events) : App(events, new deskflow::ClientArgs())
|
ClientApp::ClientApp(IEventQueue *events, const QString &processName)
|
||||||
|
: App(events, processName, new deskflow::ClientArgs())
|
||||||
{
|
{
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +92,7 @@ void ClientApp::parseArgs(int argc, const char *const *argv)
|
||||||
// Priddy.
|
// Priddy.
|
||||||
if (!Settings::value(Settings::Core::RestartOnFailure).toBool() ||
|
if (!Settings::value(Settings::Core::RestartOnFailure).toBool() ||
|
||||||
e.getError() == SocketAddressException::SocketError::BadPort) {
|
e.getError() == SocketAddressException::SocketError::BadPort) {
|
||||||
LOG_CRIT("%s: %s" BYE, args().m_pname, e.what(), args().m_pname);
|
LOG_CRIT("%s: %s" BYE, qPrintable(processName()), e.what(), qPrintable(processName()));
|
||||||
bye(s_exitFailed);
|
bye(s_exitFailed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -409,7 +410,6 @@ int ClientApp::runInner(int argc, char **argv, StartupFunc startup)
|
||||||
{
|
{
|
||||||
// general initialization
|
// general initialization
|
||||||
m_serverAddress = new NetworkAddress;
|
m_serverAddress = new NetworkAddress;
|
||||||
args().m_pname = QFileInfo(argv[0]).fileName().toLocal8Bit().constData();
|
|
||||||
|
|
||||||
int result;
|
int result;
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ class ISocketFactory;
|
||||||
class ClientApp : public App
|
class ClientApp : public App
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit ClientApp(IEventQueue *events);
|
explicit ClientApp(IEventQueue *events, const QString &processName = QString());
|
||||||
~ClientApp() override = default;
|
~ClientApp() override = default;
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,8 @@ using namespace deskflow::server;
|
||||||
// ServerApp
|
// ServerApp
|
||||||
//
|
//
|
||||||
|
|
||||||
ServerApp::ServerApp(IEventQueue *events) : App(events, new deskflow::ServerArgs())
|
ServerApp::ServerApp(IEventQueue *events, const QString &processName)
|
||||||
|
: App(events, processName, new deskflow::ServerArgs())
|
||||||
{
|
{
|
||||||
m_name = Settings::value(Settings::Core::ScreenName).toString().toStdString();
|
m_name = Settings::value(Settings::Core::ScreenName).toString().toStdString();
|
||||||
// do nothing
|
// do nothing
|
||||||
|
|
@ -93,7 +94,7 @@ void ServerApp::parseArgs(int argc, const char *const *argv)
|
||||||
*m_deskflowAddress = NetworkAddress(address.toStdString(), kDefaultPort);
|
*m_deskflowAddress = NetworkAddress(address.toStdString(), kDefaultPort);
|
||||||
m_deskflowAddress->resolve();
|
m_deskflowAddress->resolve();
|
||||||
} catch (SocketAddressException &e) {
|
} catch (SocketAddressException &e) {
|
||||||
LOG_CRIT("%s: %s" BYE, args().m_pname, e.what(), args().m_pname);
|
LOG_CRIT("%s: %s" BYE, qPrintable(processName()), e.what(), qPrintable(processName()));
|
||||||
bye(s_exitArgs);
|
bye(s_exitArgs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -140,7 +141,7 @@ void ServerApp::loadConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!loadConfig(path)) {
|
if (!loadConfig(path)) {
|
||||||
LOG_CRIT("%s: failed to load config: %s", args().m_pname, path.c_str());
|
LOG_CRIT("%s: failed to load config: %s", qPrintable(processName()), path.c_str());
|
||||||
bye(s_exitConfig);
|
bye(s_exitConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -642,7 +643,6 @@ int ServerApp::runInner(int argc, char **argv, StartupFunc startup)
|
||||||
// general initialization
|
// general initialization
|
||||||
m_deskflowAddress = new NetworkAddress;
|
m_deskflowAddress = new NetworkAddress;
|
||||||
args().m_config = std::make_shared<Config>(getEvents());
|
args().m_config = std::make_shared<Config>(getEvents());
|
||||||
args().m_pname = QFileInfo(argv[0]).fileName().toLocal8Bit().constData();
|
|
||||||
|
|
||||||
// run
|
// run
|
||||||
int result = startup(argc, argv);
|
int result = startup(argc, argv);
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ class ServerApp : public App
|
||||||
using ServerConfig = deskflow::server::Config;
|
using ServerConfig = deskflow::server::Config;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ServerApp(IEventQueue *events);
|
explicit ServerApp(IEventQueue *events, const QString &processName = QString());
|
||||||
~ServerApp() override = default;
|
~ServerApp() override = default;
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -173,15 +173,6 @@ void ArgParserTests::server_setConfigFile()
|
||||||
QCOMPARE(serverArgs.m_configFile, "mock_configFile");
|
QCOMPARE(serverArgs.m_configFile, "mock_configFile");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArgParserTests::server_unexpectedParam()
|
|
||||||
{
|
|
||||||
deskflow::ServerArgs serverArgs;
|
|
||||||
const int argc = 2;
|
|
||||||
std::array<const char *, argc> kUnknownCmd = {"stub", "--unknown"};
|
|
||||||
|
|
||||||
QVERIFY(!m_parser.parseServerArgs(serverArgs, argc, kUnknownCmd.data()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ArgParserTests::serverArgs()
|
void ArgParserTests::serverArgs()
|
||||||
{
|
{
|
||||||
deskflow::ServerArgs args;
|
deskflow::ServerArgs args;
|
||||||
|
|
@ -276,13 +267,4 @@ void ArgParserTests::deprecatedArg_crypoPass_false()
|
||||||
QCOMPARE(i, 1);
|
QCOMPARE(i, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ArgParserTests::generic_unknown()
|
|
||||||
{
|
|
||||||
int i = 1;
|
|
||||||
const int argc = 2;
|
|
||||||
const char *kBackendCmd[argc] = {"stub", "-z"};
|
|
||||||
|
|
||||||
QVERIFY(!m_parser.parseGenericArgs(argc, kBackendCmd, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
QTEST_MAIN(ArgParserTests)
|
QTEST_MAIN(ArgParserTests)
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ private Q_SLOTS:
|
||||||
void assembleCommand();
|
void assembleCommand();
|
||||||
void serverArgs();
|
void serverArgs();
|
||||||
void server_setConfigFile();
|
void server_setConfigFile();
|
||||||
void server_unexpectedParam();
|
|
||||||
void clientArgs();
|
void clientArgs();
|
||||||
void client_yScroll();
|
void client_yScroll();
|
||||||
void client_setLangSync();
|
void client_setLangSync();
|
||||||
|
|
@ -32,7 +31,6 @@ private Q_SLOTS:
|
||||||
void client_badArgs();
|
void client_badArgs();
|
||||||
void deprecatedArg_crypoPass_true();
|
void deprecatedArg_crypoPass_true();
|
||||||
void deprecatedArg_crypoPass_false();
|
void deprecatedArg_crypoPass_false();
|
||||||
void generic_unknown();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Arch m_arch;
|
Arch m_arch;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue