refactor(daemon): Use functional type for daemonize func
This commit is contained in:
@ -8,6 +8,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/IInterface.h"
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
//! Interface for architecture dependent daemonizing
|
||||
@ -19,7 +21,7 @@ implement this interface.
|
||||
class IArchDaemon : public IInterface
|
||||
{
|
||||
public:
|
||||
typedef int (*DaemonFunc)(int argc, const char **argv);
|
||||
using DaemonFunc = std::function<int(int, const char **)>;
|
||||
|
||||
//! @name manipulators
|
||||
//@{
|
||||
|
||||
@ -10,19 +10,21 @@
|
||||
#include "arch/IArchDaemon.h"
|
||||
#include "arch/IArchMultithread.h"
|
||||
#include "common/constants.h"
|
||||
#include <string>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#include <tchar.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#define ARCH_DAEMON ArchDaemonWindows
|
||||
|
||||
//! Win32 implementation of IArchDaemon
|
||||
class ArchDaemonWindows : public IArchDaemon
|
||||
{
|
||||
public:
|
||||
typedef int (*RunFunc)(void);
|
||||
using RunFunc = std::function<int()>;
|
||||
|
||||
ArchDaemonWindows();
|
||||
virtual ~ArchDaemonWindows();
|
||||
|
||||
@ -15,6 +15,8 @@
|
||||
|
||||
#include <Tlhelp32.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
//! Miscellaneous win32 functions.
|
||||
class ArchMiscWindows
|
||||
{
|
||||
@ -34,7 +36,7 @@ public:
|
||||
kDISPLAY = 0x0002
|
||||
};
|
||||
|
||||
typedef int (*RunFunc)(void);
|
||||
using RunFunc = std::function<int(void)>;
|
||||
|
||||
//! Initialize
|
||||
static void init();
|
||||
|
||||
@ -47,24 +47,6 @@ DaemonApp::DaemonApp()
|
||||
|
||||
DaemonApp::~DaemonApp() = default;
|
||||
|
||||
int daemonLoop()
|
||||
{
|
||||
DaemonApp::instance().mainLoop();
|
||||
return kExitSuccess;
|
||||
}
|
||||
|
||||
#if SYSAPI_WIN32
|
||||
int daemonLoop(int, const char **)
|
||||
{
|
||||
return ArchMiscWindows::runDaemon(daemonLoop);
|
||||
}
|
||||
#elif SYSAPI_UNIX
|
||||
int daemonLoop(int, const char **)
|
||||
{
|
||||
return daemonLoop();
|
||||
}
|
||||
#endif
|
||||
|
||||
void DaemonApp::run()
|
||||
{
|
||||
if (m_foreground) {
|
||||
@ -72,10 +54,23 @@ void DaemonApp::run()
|
||||
mainLoop();
|
||||
} else {
|
||||
LOG_DEBUG("running daemon in background (daemonizing)");
|
||||
ARCH->daemonize(kAppName, daemonLoop);
|
||||
ARCH->daemonize(kAppName, [this](int, const char **) { return daemonLoop(); });
|
||||
}
|
||||
}
|
||||
|
||||
int DaemonApp::daemonLoop()
|
||||
{
|
||||
#if SYSAPI_WIN32
|
||||
return ArchMiscWindows::runDaemon([this]() {
|
||||
mainLoop();
|
||||
return kExitSuccess;
|
||||
});
|
||||
#elif SYSAPI_UNIX
|
||||
mainLoop();
|
||||
return kExitSuccess;
|
||||
#endif
|
||||
}
|
||||
|
||||
void DaemonApp::saveLogLevel(const QString &logLevel) const
|
||||
{
|
||||
LOG_DEBUG("log level changed: %s", logLevel.toUtf8().constData());
|
||||
|
||||
@ -54,6 +54,7 @@ public:
|
||||
void clearWatchdogCommand();
|
||||
void clearSettings() const;
|
||||
std::string logFilename();
|
||||
int daemonLoop();
|
||||
|
||||
static DaemonApp &instance()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user