refactor: Common.h replace anon enum with set constexpr exit codes

This commit is contained in:
sithlord48 2025-07-08 08:58:27 -04:00 committed by Chris Rizzitello
parent 29e9b8ae3b
commit 9905f32738
7 changed files with 27 additions and 30 deletions

View file

@ -97,10 +97,10 @@ int main(int argc, char **argv)
if (parser.isSet(installOption)) { if (parser.isSet(installOption)) {
daemon.install(); daemon.install();
return kExitSuccess; return s_exitSuccess;
} else if (parser.isSet(uninstallOption)) { } else if (parser.isSet(uninstallOption)) {
daemon.uninstall(); daemon.uninstall();
return kExitSuccess; return s_exitSuccess;
} }
const auto ipcServer = const auto ipcServer =
@ -120,10 +120,10 @@ int main(int argc, char **argv)
} catch (std::exception &e) { } catch (std::exception &e) {
handleError(e.what()); handleError(e.what());
return kExitFailed; return s_exitFailed;
} catch (...) { } catch (...) {
handleError(); handleError();
return kExitFailed; return s_exitFailed;
} }
} }

View file

@ -33,11 +33,8 @@
#endif #endif
#endif #endif
enum static const int s_exitSuccess = 0; //!< App successfully completed
{ static const int s_exitFailed = 1; //!< App had a general failure
kExitSuccess = 0, // successful completion static const int s_exitTerminated = 2; //!< App was kill by a signal
kExitFailed = 1, // general failure static const int s_exitArgs = 3; //!< App was unable to run due to bad arguments being passed
kExitTerminated = 2, // killed by signal static const int s_exitConfig = 4; //!< App was unable to read the configuration
kExitArgs = 3, // bad arguments
kExitConfig = 4, // cannot read configuration
};

View file

@ -87,7 +87,7 @@ int App::run(int argc, char **argv)
appUtil().adoptApp(this); appUtil().adoptApp(this);
// HACK: fail by default (saves us setting result in each catch) // HACK: fail by default (saves us setting result in each catch)
int result = kExitFailed; int result = s_exitFailed;
try { try {
result = appUtil().run(argc, argv); result = appUtil().run(argc, argv);
@ -172,7 +172,7 @@ void App::initApp(int argc, const char **argv)
LOG(( LOG((
CLOG_CRIT "%s: unrecognized log level `%s'" BYE, argsBase().m_pname, argsBase().m_logFilter, argsBase().m_pname CLOG_CRIT "%s: unrecognized log level `%s'" BYE, argsBase().m_pname, argsBase().m_logFilter, argsBase().m_pname
)); ));
m_bye(kExitArgs); m_bye(s_exitArgs);
} }
loggingFilterWarning(); loggingFilterWarning();

View file

@ -72,9 +72,9 @@ void ClientApp::parseArgs(int argc, const char *const *argv)
if (!result || args().m_shouldExitOk || args().m_shouldExitFail) { if (!result || args().m_shouldExitOk || args().m_shouldExitFail) {
if (args().m_shouldExitOk) { if (args().m_shouldExitOk) {
m_bye(kExitSuccess); m_bye(s_exitSuccess);
} else { } else {
m_bye(kExitArgs); m_bye(s_exitArgs);
} }
} else { } else {
// save server address // save server address
@ -89,7 +89,7 @@ void ClientApp::parseArgs(int argc, const char *const *argv)
// Priddy. // Priddy.
if (!args().m_restartable || e.getError() == XSocketAddress::kBadPort) { if (!args().m_restartable || e.getError() == XSocketAddress::kBadPort) {
LOG((CLOG_CRIT "%s: %s" BYE, args().m_pname, e.what(), args().m_pname)); LOG((CLOG_CRIT "%s: %s" BYE, args().m_pname, e.what(), args().m_pname));
m_bye(kExitFailed); m_bye(s_exitFailed);
} }
} }
} }
@ -417,7 +417,7 @@ int ClientApp::mainLoop()
updateStatus(); updateStatus();
LOG((CLOG_NOTE "stopped client")); LOG((CLOG_NOTE "stopped client"));
return kExitSuccess; return s_exitSuccess;
} }
static int daemonMainLoopStatic(int argc, const char **argv) static int daemonMainLoopStatic(int argc, const char **argv)
@ -462,7 +462,7 @@ void ClientApp::startNode()
// we shouldn't retry. // we shouldn't retry.
LOG((CLOG_DEBUG1 "starting client")); LOG((CLOG_DEBUG1 "starting client"));
if (!startClient()) { if (!startClient()) {
m_bye(kExitFailed); m_bye(s_exitFailed);
} }
} }

View file

@ -196,7 +196,7 @@ int DaemonApp::mainLoop()
#if SYSAPI_WIN32 #if SYSAPI_WIN32
if (m_pWatchdog == nullptr) { if (m_pWatchdog == nullptr) {
LOG_ERR("watchdog not initialized"); LOG_ERR("watchdog not initialized");
return kExitFailed; return s_exitFailed;
} }
#endif #endif
@ -235,7 +235,7 @@ int DaemonApp::mainLoop()
#endif #endif
DAEMON_RUNNING(false); DAEMON_RUNNING(false);
return kExitSuccess; return s_exitSuccess;
} }
QString DaemonApp::logFilename() QString DaemonApp::logFilename()

View file

@ -80,9 +80,9 @@ void ServerApp::parseArgs(int argc, const char *const *argv)
if (!result || args().m_shouldExitOk || args().m_shouldExitFail) { if (!result || args().m_shouldExitOk || args().m_shouldExitFail) {
if (args().m_shouldExitOk) { if (args().m_shouldExitOk) {
m_bye(kExitSuccess); m_bye(s_exitSuccess);
} else { } else {
m_bye(kExitArgs); m_bye(s_exitArgs);
} }
} else { } else {
if (!args().m_deskflowAddress.empty()) { if (!args().m_deskflowAddress.empty()) {
@ -91,7 +91,7 @@ void ServerApp::parseArgs(int argc, const char *const *argv)
m_deskflowAddress->resolve(); m_deskflowAddress->resolve();
} catch (XSocketAddress &e) { } catch (XSocketAddress &e) {
LOG((CLOG_CRIT "%s: %s" BYE, args().m_pname, e.what(), args().m_pname)); LOG((CLOG_CRIT "%s: %s" BYE, args().m_pname, e.what(), args().m_pname));
m_bye(kExitArgs); m_bye(s_exitArgs);
} }
} }
} }
@ -162,12 +162,12 @@ void ServerApp::loadConfig()
const auto path = args().m_configFile; const auto path = args().m_configFile;
if (path.empty()) { if (path.empty()) {
LOG((CLOG_CRIT "no configuration path provided")); LOG((CLOG_CRIT "no configuration path provided"));
m_bye(kExitConfig); m_bye(s_exitConfig);
} }
if (!loadConfig(path)) { if (!loadConfig(path)) {
LOG((CLOG_CRIT "%s: failed to load config: %s", args().m_pname, path.c_str())); LOG((CLOG_CRIT "%s: failed to load config: %s", args().m_pname, path.c_str()));
m_bye(kExitConfig); m_bye(s_exitConfig);
} }
} }
@ -609,7 +609,7 @@ int ServerApp::mainLoop()
// canonicalize the primary screen name // canonicalize the primary screen name
if (std::string primaryName = args().m_config->getCanonicalName(args().m_name); primaryName.empty()) { if (std::string primaryName = args().m_config->getCanonicalName(args().m_name); primaryName.empty()) {
LOG((CLOG_CRIT "unknown screen name `%s'", args().m_name.c_str())); LOG((CLOG_CRIT "unknown screen name `%s'", args().m_name.c_str()));
return kExitFailed; return s_exitFailed;
} }
// start server, etc // start server, etc
@ -661,7 +661,7 @@ int ServerApp::mainLoop()
updateStatus(); updateStatus();
LOG((CLOG_NOTE "stopped server")); LOG((CLOG_NOTE "stopped server"));
return kExitSuccess; return s_exitSuccess;
} }
void ServerApp::resetServer() void ServerApp::resetServer()
@ -735,6 +735,6 @@ void ServerApp::startNode()
// we shouldn't retry. // we shouldn't retry.
LOG((CLOG_DEBUG1 "starting server")); LOG((CLOG_DEBUG1 "starting server"));
if (!startServer()) { if (!startServer()) {
m_bye(kExitFailed); m_bye(s_exitFailed);
} }
} }

View file

@ -181,7 +181,7 @@ void MSWindowsProcess::shutdown(HANDLE handle, DWORD pid, int timeout)
} }
// Last resort, terminate the process forcefully. // Last resort, terminate the process forcefully.
if (TerminateProcess(handle, kExitSuccess)) { if (TerminateProcess(handle, s_exitSuccess)) {
LOG_WARN("forcefully terminated process %d", pid); LOG_WARN("forcefully terminated process %d", pid);
} else { } else {
LOG_ERR("failed to terminate process %d, error: %s", pid, windowsErrorToString(GetLastError()).c_str()); LOG_ERR("failed to terminate process %d, error: %s", pid, windowsErrorToString(GetLastError()).c_str());