refactor: move ArgsBase:m_pname to App
This commit is contained in:
committed by
Chris Rizzitello
parent
1884ab3555
commit
cfd580fb21
@ -19,6 +19,7 @@
|
||||
#include <QCoreApplication>
|
||||
#endif
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QSharedMemory>
|
||||
#include <QTextStream>
|
||||
#include <iostream>
|
||||
@ -89,12 +90,13 @@ int main(int argc, char **argv)
|
||||
#endif
|
||||
|
||||
EventQueue events;
|
||||
const auto processName = QFileInfo(argv[0]).fileName();
|
||||
|
||||
if (parser.serverMode()) {
|
||||
ServerApp app(&events);
|
||||
ServerApp app(&events, processName);
|
||||
return app.run(argc, argv);
|
||||
} else if (parser.clientMode()) {
|
||||
ClientApp app(&events);
|
||||
ClientApp app(&events, processName);
|
||||
return app.run(argc, argv);
|
||||
}
|
||||
|
||||
|
||||
@ -48,11 +48,12 @@ App *App::s_instance = nullptr;
|
||||
// App
|
||||
//
|
||||
|
||||
App::App(IEventQueue *events, deskflow::ArgsBase *args)
|
||||
App::App(IEventQueue *events, const QString &processName, deskflow::ArgsBase *args)
|
||||
: m_bye(&exit),
|
||||
m_events(events),
|
||||
m_args(args),
|
||||
m_appUtil(events)
|
||||
m_appUtil(events),
|
||||
m_pname(processName)
|
||||
{
|
||||
assert(s_instance == nullptr);
|
||||
s_instance = this;
|
||||
@ -170,7 +171,7 @@ void App::initApp(int argc, const char **argv)
|
||||
|
||||
// set log filter
|
||||
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);
|
||||
}
|
||||
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 &&) = delete;
|
||||
~App() override;
|
||||
@ -108,6 +108,11 @@ public:
|
||||
return *s_instance;
|
||||
}
|
||||
|
||||
QString processName() const
|
||||
{
|
||||
return m_pname;
|
||||
}
|
||||
|
||||
void handleScreenError() const;
|
||||
|
||||
protected:
|
||||
@ -121,6 +126,7 @@ private:
|
||||
FileLogOutputter *m_fileLog = nullptr;
|
||||
ARCH_APP_UTIL m_appUtil;
|
||||
std::unique_ptr<SocketMultiplexer> m_socketMultiplexer;
|
||||
QString m_pname;
|
||||
};
|
||||
|
||||
#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
|
||||
{
|
||||
setArgsBase(args);
|
||||
updateCommonArgs(argv);
|
||||
int i = 1;
|
||||
while (i < argc) {
|
||||
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")) {
|
||||
args.m_chkPeerCert = false;
|
||||
} 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;
|
||||
}
|
||||
++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
|
||||
{
|
||||
setArgsBase(args);
|
||||
updateCommonArgs(argv);
|
||||
|
||||
int i{1};
|
||||
while (i < argc) {
|
||||
@ -80,7 +78,7 @@ bool ArgParser::parseClientArgs(deskflow::ClientArgs &args, int argc, const char
|
||||
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;
|
||||
}
|
||||
++i;
|
||||
@ -88,7 +86,7 @@ bool ArgParser::parseClientArgs(deskflow::ClientArgs &args, int argc, const char
|
||||
|
||||
// exactly one non-option argument (server-address)
|
||||
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;
|
||||
}
|
||||
|
||||
@ -137,7 +135,7 @@ bool ArgParser::isArg(
|
||||
if ((name1 != nullptr && strcmp(argv[argi], name1) == 0) || (name2 != nullptr && strcmp(argv[argi], name2) == 0)) {
|
||||
// match. check args left.
|
||||
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;
|
||||
return false;
|
||||
}
|
||||
@ -270,11 +268,6 @@ std::string ArgParser::assembleCommand(
|
||||
return result;
|
||||
}
|
||||
|
||||
void ArgParser::updateCommonArgs(const char *const *argv) const
|
||||
{
|
||||
argsBase().m_pname = QFileInfo(argv[0]).fileName().toLocal8Bit().constData();
|
||||
}
|
||||
|
||||
bool ArgParser::checkUnexpectedArgs() const
|
||||
{
|
||||
return false;
|
||||
|
||||
@ -50,7 +50,6 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
void updateCommonArgs(const char *const *argv) const;
|
||||
bool checkUnexpectedArgs() const;
|
||||
|
||||
private:
|
||||
|
||||
@ -32,9 +32,6 @@ public:
|
||||
/// @brief Stores what type of object this is
|
||||
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
|
||||
bool m_shouldExitOk = false;
|
||||
|
||||
|
||||
@ -62,7 +62,8 @@
|
||||
|
||||
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
|
||||
}
|
||||
@ -91,7 +92,7 @@ void ClientApp::parseArgs(int argc, const char *const *argv)
|
||||
// Priddy.
|
||||
if (!Settings::value(Settings::Core::RestartOnFailure).toBool() ||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -409,7 +410,6 @@ int ClientApp::runInner(int argc, char **argv, StartupFunc startup)
|
||||
{
|
||||
// general initialization
|
||||
m_serverAddress = new NetworkAddress;
|
||||
args().m_pname = QFileInfo(argv[0]).fileName().toLocal8Bit().constData();
|
||||
|
||||
int result;
|
||||
try {
|
||||
|
||||
@ -24,7 +24,7 @@ class ISocketFactory;
|
||||
class ClientApp : public App
|
||||
{
|
||||
public:
|
||||
explicit ClientApp(IEventQueue *events);
|
||||
explicit ClientApp(IEventQueue *events, const QString &processName = QString());
|
||||
~ClientApp() override = default;
|
||||
|
||||
//
|
||||
|
||||
@ -69,7 +69,8 @@ using namespace deskflow::server;
|
||||
// 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();
|
||||
// do nothing
|
||||
@ -93,7 +94,7 @@ void ServerApp::parseArgs(int argc, const char *const *argv)
|
||||
*m_deskflowAddress = NetworkAddress(address.toStdString(), kDefaultPort);
|
||||
m_deskflowAddress->resolve();
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
@ -140,7 +141,7 @@ void ServerApp::loadConfig()
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -642,7 +643,6 @@ int ServerApp::runInner(int argc, char **argv, StartupFunc startup)
|
||||
// general initialization
|
||||
m_deskflowAddress = new NetworkAddress;
|
||||
args().m_config = std::make_shared<Config>(getEvents());
|
||||
args().m_pname = QFileInfo(argv[0]).fileName().toLocal8Bit().constData();
|
||||
|
||||
// run
|
||||
int result = startup(argc, argv);
|
||||
|
||||
@ -48,7 +48,7 @@ class ServerApp : public App
|
||||
using ServerConfig = deskflow::server::Config;
|
||||
|
||||
public:
|
||||
explicit ServerApp(IEventQueue *events);
|
||||
explicit ServerApp(IEventQueue *events, const QString &processName = QString());
|
||||
~ServerApp() override = default;
|
||||
|
||||
//
|
||||
|
||||
@ -173,15 +173,6 @@ void ArgParserTests::server_setConfigFile()
|
||||
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()
|
||||
{
|
||||
deskflow::ServerArgs args;
|
||||
@ -276,13 +267,4 @@ void ArgParserTests::deprecatedArg_crypoPass_false()
|
||||
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)
|
||||
|
||||
@ -23,7 +23,6 @@ private Q_SLOTS:
|
||||
void assembleCommand();
|
||||
void serverArgs();
|
||||
void server_setConfigFile();
|
||||
void server_unexpectedParam();
|
||||
void clientArgs();
|
||||
void client_yScroll();
|
||||
void client_setLangSync();
|
||||
@ -32,7 +31,6 @@ private Q_SLOTS:
|
||||
void client_badArgs();
|
||||
void deprecatedArg_crypoPass_true();
|
||||
void deprecatedArg_crypoPass_false();
|
||||
void generic_unknown();
|
||||
|
||||
private:
|
||||
Arch m_arch;
|
||||
|
||||
Reference in New Issue
Block a user