refactor: move daemon settings to Settings

fixes #8353
This commit is contained in:
sithlord48
2025-03-14 17:18:54 -04:00
committed by Nick Bolton
parent 0e3cee6287
commit 613f3651ea
5 changed files with 36 additions and 41 deletions

View File

@ -8,7 +8,7 @@
#include "arch/Arch.h"
#include "base/EventQueue.h"
#include "base/Log.h"
#include "common/constants.h"
#include "common/Settings.h"
#include "deskflow/DaemonApp.h"
#include "deskflow/ipc/DaemonIpcServer.h"
@ -80,7 +80,8 @@ int main(int argc, char **argv)
LOG_PRINT("%s v%s", QCoreApplication::applicationName().toStdString().c_str(), kDisplayVersion);
// Default log level to system setting (found in Registry).
if (std::string logLevel = ARCH->setting("LogLevel"); logLevel != "") {
auto logLevel = Settings::value(Settings::Daemon::LogLevel).toString().toStdString();
if (logLevel != "") {
CLOG->setFilter(logLevel.c_str());
LOG_DEBUG("log level: %s", logLevel.c_str());
}
@ -102,7 +103,8 @@ int main(int argc, char **argv)
return kExitSuccess;
}
const auto ipcServer = new ipc::DaemonIpcServer(&app, DaemonApp::logFilename().c_str()); // NOSONAR - Qt managed
const auto ipcServer =
new ipc::DaemonIpcServer(&app, DaemonApp::logFilename().toStdString().c_str()); // NOSONAR - Qt managed
ipcServer->listen();
daemon.connectIpcServer(ipcServer);

View File

@ -119,6 +119,13 @@ QVariant Settings::defaultValue(const QString &key)
if (key == Core::ProcessMode)
return defaultProcessMode;
if (key == Daemon::LogFile) {
return QStringLiteral("%1/%2").arg(instance()->settingsPath(), kDaemonLogFilename);
}
if (key == Daemon::Elevate)
return true;
return QVariant();
}

View File

@ -50,6 +50,13 @@ public:
inline static const auto ScreenName = QStringLiteral("core/screenName");
inline static const auto StartedBefore = QStringLiteral("core/startedBefore");
};
struct Daemon
{
inline static const auto Command = QStringLiteral("daemon/command");
inline static const auto Elevate = QStringLiteral("daemon/elevate");
inline static const auto LogFile = QStringLiteral("daemon/logFile");
inline static const auto LogLevel = QStringLiteral("daemon/logLevel");
};
struct Gui
{
inline static const auto Autohide = QStringLiteral("gui/autoHide");
@ -178,6 +185,9 @@ private:
, Settings::Core::ProcessMode
, Settings::Core::ScreenName
, Settings::Core::StartedBefore
, Settings::Daemon::Command
, Settings::Daemon::Elevate
, Settings::Daemon::LogFile
, Settings::Log::File
, Settings::Log::Level
, Settings::Log::ToFile

View File

@ -6,11 +6,10 @@
#include "deskflow/DaemonApp.h"
#include "arch/XArch.h"
#include "base/IEventQueue.h"
#include "base/Log.h"
#include "base/log_outputters.h"
#include "common/constants.h"
#include "common/Settings.h"
#include "deskflow/App.h"
#include "deskflow/ipc/DaemonIpcServer.h"
@ -50,39 +49,21 @@ void DaemonApp::saveLogLevel(const QString &logLevel) const
{
LOG_DEBUG("log level changed: %s", logLevel.toUtf8().constData());
CLOG->setFilter(logLevel.toUtf8().constData());
try {
// saves setting for next time the daemon starts.
ARCH->setting("LogLevel", logLevel.toStdString());
} catch (XArch &e) {
LOG_ERR("failed to save log level setting: %s", e.what());
}
Settings::setValue(Settings::Daemon::LogLevel, logLevel);
}
void DaemonApp::setElevate(bool elevate)
{
LOG_DEBUG("elevate value changed: %s", elevate ? "yes" : "no");
m_elevate = elevate;
try {
// saves setting for next time the daemon starts.
ARCH->setting("Elevate", std::string(elevate ? "1" : "0"));
} catch (XArch &e) {
LOG_ERR("failed to save elevate setting: %s", e.what());
}
Settings::setValue(Settings::Daemon::Elevate, m_elevate);
}
void DaemonApp::setCommand(const QString &command)
{
LOG_DEBUG("service command updated");
Settings::setValue(Settings::Daemon::Command, command);
m_command = command.toStdString();
try {
// saves setting for next time the daemon starts.
ARCH->setting("Command", command.toStdString());
} catch (XArch &e) {
LOG_ERR("failed to save command setting: %s", e.what());
}
}
void DaemonApp::applyWatchdogCommand() const
@ -113,7 +94,10 @@ void DaemonApp::clearWatchdogCommand()
void DaemonApp::clearSettings() const
{
LOG_INFO("clearing daemon settings");
ARCH->clearSettings();
Settings::setValue(Settings::Daemon::Command, QVariant());
Settings::setValue(Settings::Daemon::Elevate, QVariant());
Settings::setValue(Settings::Daemon::LogFile, QVariant());
Settings::setValue(Settings::Daemon::LogLevel, QVariant());
}
void DaemonApp::connectIpcServer(const ipc::DaemonIpcServer *ipcServer) const
@ -184,8 +168,8 @@ void DaemonApp::run(QThread &daemonThread)
#if SYSAPI_WIN32
m_pWatchdog = std::make_unique<MSWindowsWatchdog>(m_foreground, *m_pFileLogOutputter);
std::string command = ARCH->setting("Command");
bool elevate = ARCH->setting("Elevate") == "1";
auto command = Settings::value(Settings::Daemon::Command).toString().toStdString();
bool elevate = Settings::value(Settings::Daemon::Elevate).toBool();
if (!command.empty()) {
LOG_DEBUG("using last known command: %s", command.c_str());
m_pWatchdog->setProcessConfig(command, elevate);
@ -253,17 +237,9 @@ int DaemonApp::mainLoop()
return kExitSuccess;
}
std::string DaemonApp::logFilename()
QString DaemonApp::logFilename()
{
string logFilename;
logFilename = ARCH->setting("LogFilename");
if (logFilename.empty()) {
logFilename = ARCH->getLogDirectory();
logFilename.append("/");
logFilename.append(kDaemonLogFilename);
}
return logFilename;
return Settings::value(Settings::Daemon::LogFile).toString();
}
void DaemonApp::setForeground()
@ -282,7 +258,7 @@ void DaemonApp::initLogging()
}
#endif
m_pFileLogOutputter = new FileLogOutputter(logFilename().c_str()); // NOSONAR - Adopted by `Log`
m_pFileLogOutputter = new FileLogOutputter(logFilename().toStdString().c_str()); // NOSONAR - Adopted by `Log`
CLOG->insert(m_pFileLogOutputter);
}

View File

@ -55,7 +55,7 @@ public:
void initLogging();
void connectIpcServer(const deskflow::core::ipc::DaemonIpcServer *ipcServer) const;
static std::string logFilename();
static QString logFilename();
private:
void daemonize();