fix: Show console on Windows for daemon -h and -f args

This commit is contained in:
Nick Bolton 2025-03-05 16:56:49 +00:00
parent cc821f750d
commit bd674f546b
2 changed files with 17 additions and 0 deletions

View file

@ -162,9 +162,11 @@ DaemonApp::InitResult DaemonApp::init(IEventQueue *events, int argc, char **argv
string arg(argv[i]);
if (arg == "-h" || arg == "--help") {
showConsole();
showHelp(argc, argv);
return ShowHelp;
} else if (arg == "-f" || arg == "--foreground") {
showConsole();
m_foreground = true;
}
#if SYSAPI_WIN32
@ -263,3 +265,16 @@ std::string DaemonApp::logFilename()
return logFilename;
}
void DaemonApp::showConsole()
{
#if SYSAPI_WIN32
// The daemon bin is compiled using the Win32 subsystem which works best for Windows services,
// so when running as a foreground process we need to allocate a console (or we won't see output).
// It is important to do this inside the arg check loop so that we can attach console ahead
// of log output generated by handling other args.
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#endif
}

View file

@ -69,6 +69,8 @@ private:
void handleError(const char *message);
void handleIpcMessage(const Event &e, void *);
static void showConsole();
#if SYSAPI_WIN32
std::unique_ptr<MSWindowsWatchdog> m_watchdog;
#endif