diff --git a/src/lib/deskflow/DaemonApp.cpp b/src/lib/deskflow/DaemonApp.cpp index dc1b93f81..66760bdc5 100644 --- a/src/lib/deskflow/DaemonApp.cpp +++ b/src/lib/deskflow/DaemonApp.cpp @@ -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 +} diff --git a/src/lib/deskflow/DaemonApp.h b/src/lib/deskflow/DaemonApp.h index 7ace0af46..850087989 100644 --- a/src/lib/deskflow/DaemonApp.h +++ b/src/lib/deskflow/DaemonApp.h @@ -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 m_watchdog; #endif