diff --git a/host/platform/Windows/CMakeLists.txt b/host/platform/Windows/CMakeLists.txt index 97dcb837..18fe746e 100644 --- a/host/platform/Windows/CMakeLists.txt +++ b/host/platform/Windows/CMakeLists.txt @@ -32,6 +32,7 @@ target_link_libraries(platform_Windows userenv wtsapi32 psapi + shlwapi ) target_include_directories(platform_Windows diff --git a/host/platform/Windows/src/platform.c b/host/platform/Windows/src/platform.c index c7f92d68..3ed3c74c 100644 --- a/host/platform/Windows/src/platform.c +++ b/host/platform/Windows/src/platform.c @@ -23,6 +23,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include +#include #include #include "interface/platform.h" @@ -34,6 +35,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #define ID_MENU_SHOW_LOG 3000 #define ID_MENU_EXIT 3001 +#define LOG_NAME "looking-glass-host.txt" struct AppState { @@ -44,6 +46,7 @@ struct AppState char ** argv; char executable[MAX_PATH + 1]; + char systemLogDir[MAX_PATH]; HWND messageWnd; NOTIFYICONDATA iconData; UINT trayRestartMsg; @@ -166,6 +169,29 @@ static BOOL WINAPI CtrlHandler(DWORD dwCtrlType) return FALSE; } +const char *getSystemLogDirectory(void) +{ + return app.systemLogDir; +} + +static void populateSystemLogDirectory() +{ + char programData[MAX_PATH]; + if (GetEnvironmentVariableA("ProgramData", programData, sizeof(programData)) && + PathIsDirectoryA(programData)) + { + if (!PathCombineA(app.systemLogDir, programData, "Looking Glass (host)")) + goto fail; + + if (!PathIsDirectoryA(app.systemLogDir) && !CreateDirectoryA(app.systemLogDir, NULL)) + goto fail; + + return; + } +fail: + strcpy(app.systemLogDir, ""); +} + int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // convert the command line to the standard argc and argv @@ -180,6 +206,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine LocalFree(wargv); GetModuleFileName(NULL, app.executable, sizeof(app.executable)); + populateSystemLogDirectory(); + if (HandleService(app.argc, app.argv)) return LG_HOST_EXIT_FAILED; @@ -201,11 +229,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine int result = 0; app.hInst = hInstance; - char tempPath[MAX_PATH+1]; - GetTempPathA(sizeof(tempPath), tempPath); - int len = snprintf(NULL, 0, "%slooking-glass-host.txt", tempPath); - char * logFilePath = malloc(len + 1); - sprintf(logFilePath, "%slooking-glass-host.txt", tempPath); + char logFilePath[MAX_PATH]; + if (!PathCombineA(logFilePath, app.systemLogDir, LOG_NAME)) + strcpy(logFilePath, LOG_NAME); struct Option options[] = { @@ -220,7 +246,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine }; option_register(options); - free(logFilePath); // setup a handler for ctrl+c SetConsoleCtrlHandler(CtrlHandler, TRUE); diff --git a/host/platform/Windows/src/platform.h b/host/platform/Windows/src/platform.h index 7290ccf2..489cd0d6 100644 --- a/host/platform/Windows/src/platform.h +++ b/host/platform/Windows/src/platform.h @@ -30,4 +30,5 @@ struct MSG_CALL_FUNCTION LPARAM lParam; }; +const char *getSystemLogDirectory(void); LRESULT sendAppMessage(UINT Msg, WPARAM wParam, LPARAM lParam); diff --git a/host/platform/Windows/src/service.c b/host/platform/Windows/src/service.c index a6b328cf..6cd59ba6 100644 --- a/host/platform/Windows/src/service.c +++ b/host/platform/Windows/src/service.c @@ -20,6 +20,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "interface/platform.h" #include "common/ivshmem.h" #include "service.h" +#include "platform.h" #include #include @@ -29,6 +30,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include +#include #include #include #include @@ -37,6 +39,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #define SVCNAME "Looking Glass (host)" #define SVC_ERROR ((DWORD)0xC0020001L) +#define LOG_NAME "looking-glass-host-service.txt" /* * Windows 10 provides this API via kernel32.dll as well as advapi32.dll and @@ -112,11 +115,9 @@ static bool setupAPI(void) static void setupLogging(void) { - char tempPath[MAX_PATH+1]; - GetTempPathA(sizeof(tempPath), tempPath); - int len = snprintf(NULL, 0, "%slooking-glass-host-service.txt", tempPath); - char * logFilePath = malloc(len + 1); - sprintf(logFilePath, "%slooking-glass-host-service.txt", tempPath); + char logFilePath[MAX_PATH]; + if (!PathCombineA(logFilePath, getSystemLogDirectory(), LOG_NAME)) + strcpy(logFilePath, LOG_NAME); service.logFile = fopen(logFilePath, "a+"); setbuf(service.logFile, NULL); doLog("Startup\n");