diff --git a/idd/LGCommon/CDebug.cpp b/idd/LGCommon/CDebug.cpp index 28c73b71..d70cf9d9 100644 --- a/idd/LGCommon/CDebug.cpp +++ b/idd/LGCommon/CDebug.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "CDebug.h" @@ -99,19 +100,35 @@ inline static void iso8601(wchar_t *buf, size_t count) wcsftime(buf, count, L"%Y-%m-%d %H:%M:%SZ", &utc); } +inline static std::wstring getLogPath() { + PWSTR pszPath; + if (FAILED(SHGetKnownFolderPath(FOLDERID_ProgramData, 0, NULL, &pszPath))) + { + DEBUG_ERROR("Failed to get ProgramData path"); + return L""; + } + + std::wstring result(pszPath); + CoTaskMemFree(pszPath); + + result += L"\\Looking Glass (IDD)\\"; + return result; +} + void CDebug::Init(const wchar_t * name) { + m_logDir = getLogPath(); + // don't redirect the debug output if running under a debugger if (IsDebuggerPresent()) return; - std::wstring folder = L"C:\\ProgramData\\Looking Glass (IDD)\\"; std::wstring baseName = name; std::wstring ext = L".txt"; - std::wstring logFile = folder + baseName + ext; + std::wstring logFile = m_logDir + baseName + ext; //rotate out old logs - DeleteFileW((folder + baseName + L".4" + ext).c_str()); + DeleteFileW((m_logDir + baseName + L".4" + ext).c_str()); for (int i = 3; i >= 0; --i) { std::wstring oldPath; @@ -120,12 +137,12 @@ void CDebug::Init(const wchar_t * name) if (i == 0) { oldPath = logFile; - newPath = folder + baseName + L".1" + ext; + newPath = m_logDir + baseName + L".1" + ext; } else { - oldPath = folder + baseName + L"." + std::to_wstring(i) + ext; - newPath = folder + baseName + L"." + std::to_wstring(i + 1) + ext; + oldPath = m_logDir + baseName + L"." + std::to_wstring(i) + ext; + newPath = m_logDir + baseName + L"." + std::to_wstring(i + 1) + ext; } MoveFileW(oldPath.c_str(), newPath.c_str()); diff --git a/idd/LGCommon/CDebug.h b/idd/LGCommon/CDebug.h index 61de5756..0ee95f22 100644 --- a/idd/LGCommon/CDebug.h +++ b/idd/LGCommon/CDebug.h @@ -27,6 +27,7 @@ class CDebug { private: std::ofstream m_stream; + std::wstring m_logDir; void Write(const wchar_t *line); public: @@ -44,6 +45,7 @@ class CDebug LEVEL_MAX }; + const wchar_t *logDir() { return m_logDir.c_str(); } void Init(const wchar_t * name); void Log_va(CDebug::Level level, const char *function, int line, const wchar_t *fmt, va_list args); void Log(CDebug::Level level, const char *function, int line, const wchar_t *fmt, ...);