chore: Improve logging related to Windows daemon start/stop/install/uninstall

This commit is contained in:
Nick Bolton 2025-02-24 16:37:21 +00:00
parent 19c41e2ac5
commit a99d408be1
5 changed files with 30 additions and 10 deletions

View file

@ -69,8 +69,10 @@ int main(int argc, char **argv)
daemon.moveToThread(&daemonThread);
QObject::connect(&daemonThread, &QThread::started, [&daemon, &daemonThread]() {
LOG_DEBUG("daemon thread started");
daemon.run();
daemonThread.quit();
LOG_DEBUG("daemon thread finished");
});
QObject::connect(&daemonThread, &QThread::finished, &app, &QCoreApplication::quit);

View file

@ -6,6 +6,7 @@
*/
#include "arch/win32/ArchDaemonWindows.h"
#include "arch/Arch.h"
#include "arch/win32/ArchMiscWindows.h"
#include "arch/win32/XArchWindows.h"
@ -59,6 +60,8 @@ void ArchDaemonWindows::installDaemon(
const char *name, const char *description, const char *pathname, const char *commandLine, const char *dependencies
)
{
LOG_DEBUG("installing windows service: %s", name);
// open service manager
SC_HANDLE mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
if (mgr == NULL) {
@ -125,6 +128,8 @@ void ArchDaemonWindows::installDaemon(
void ArchDaemonWindows::uninstallDaemon(const char *name)
{
LOG_DEBUG("uninstalling windows service: %s", name);
// remove parameters for this service. ignore failures.
HKEY key = openNTServicesKey();
key = ArchMiscWindows::openKey(key, name);

View file

@ -80,17 +80,17 @@ DaemonApp::~DaemonApp() = default;
void DaemonApp::run()
{
if (m_foreground) {
LOG_INFO("starting daemon in foreground");
LOG_DEBUG("starting daemon in foreground");
// run process in foreground instead of daemonizing.
// useful for debugging.
mainLoop(m_foreground);
} else {
#if SYSAPI_WIN32
LOG_INFO("daemonizing windows service");
LOG_DEBUG("daemonizing windows service");
ARCH->daemonize(kAppName, winMainLoopStatic);
#elif SYSAPI_UNIX
LOG_INFO("daemonizing unix service");
LOG_DEBUG("daemonizing unix service");
ARCH->daemonize(kAppName, unixMainLoopStatic);
#endif
}
@ -253,9 +253,12 @@ void DaemonApp::mainLoop(bool foreground)
m_watchdog->startAsync();
#endif
LOG_INFO("daemon is running");
m_events->loop();
LOG_INFO("daemon is stopping");
#if SYSAPI_WIN32
LOG_DEBUG("stopping process watchdog");
m_watchdog->stop();
#endif

View file

@ -127,12 +127,16 @@ void MSWindowsProcess::shutdown(HANDLE handle, DWORD pid, int timeout)
SetEvent(hCloseEvent);
CloseHandle(hCloseEvent);
} else {
LOG((CLOG_WARN "could not send close event to process"));
LOG_WARN("could not send close event to process");
throw XArch(new XArchEvalWindows);
}
DWORD exitCode;
GetExitCodeProcess(handle, &exitCode);
if (!GetExitCodeProcess(handle, &exitCode)) {
LOG_ERR("failed to get process exit code for process %d", pid);
throw XArch(new XArchEvalWindows);
}
if (exitCode != STILL_ACTIVE) {
LOG_DEBUG("process %d is already shutdown", pid);
return;

View file

@ -93,10 +93,14 @@ void MSWindowsWatchdog::stop()
{
m_monitoring = false;
m_thread->wait(5);
if (!m_thread->wait(5)) {
LOG((CLOG_WARN "could not stop main thread"));
}
delete m_thread;
m_outputThread->wait(5);
if (!m_outputThread->wait(5)) {
LOG((CLOG_WARN "could not stop output thread"));
}
delete m_outputThread;
}
@ -161,7 +165,7 @@ MSWindowsWatchdog::getUserToken(LPSECURITY_ATTRIBUTES security, bool elevatedTok
void MSWindowsWatchdog::mainLoop(void *)
{
LOG_DEBUG("starting main loop");
LOG_DEBUG("starting watchdog main loop");
shutdownExistingProcesses();
@ -277,7 +281,7 @@ void MSWindowsWatchdog::mainLoop(void *)
m_processStarted = false;
}
LOG((CLOG_DEBUG "watchdog main thread finished"));
LOG((CLOG_DEBUG "watchdog main loop finished"));
}
bool MSWindowsWatchdog::isProcessRunning()
@ -347,8 +351,10 @@ void MSWindowsWatchdog::startProcess()
LOG((CLOG_ERR "exit code: %d", exitCode));
throw XArch(new XArchEvalWindows);
} else {
// wait for program to fail.
// Wait for program to fail. This needs to be 1 second, as the process may take some time to fail.
LOG_DEBUG("waiting for process start result");
ARCH->sleep(1);
if (!isProcessRunning()) {
m_process.reset();
throw XMSWindowsWatchdogError("process immediately stopped");