[idd] debug: use better log path determination algorithm

This commit is contained in:
Quantum
2025-09-14 05:04:36 -04:00
parent daa78bcf47
commit e1a2fa790d
2 changed files with 25 additions and 6 deletions

View File

@@ -24,6 +24,7 @@
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <malloc.h>
#include <strsafe.h> #include <strsafe.h>
#include <shlobj.h>
#include "CDebug.h" #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); 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) void CDebug::Init(const wchar_t * name)
{ {
m_logDir = getLogPath();
// don't redirect the debug output if running under a debugger // don't redirect the debug output if running under a debugger
if (IsDebuggerPresent()) if (IsDebuggerPresent())
return; return;
std::wstring folder = L"C:\\ProgramData\\Looking Glass (IDD)\\";
std::wstring baseName = name; std::wstring baseName = name;
std::wstring ext = L".txt"; std::wstring ext = L".txt";
std::wstring logFile = folder + baseName + ext; std::wstring logFile = m_logDir + baseName + ext;
//rotate out old logs //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) for (int i = 3; i >= 0; --i)
{ {
std::wstring oldPath; std::wstring oldPath;
@@ -120,12 +137,12 @@ void CDebug::Init(const wchar_t * name)
if (i == 0) if (i == 0)
{ {
oldPath = logFile; oldPath = logFile;
newPath = folder + baseName + L".1" + ext; newPath = m_logDir + baseName + L".1" + ext;
} }
else else
{ {
oldPath = folder + baseName + L"." + std::to_wstring(i) + ext; oldPath = m_logDir + baseName + L"." + std::to_wstring(i) + ext;
newPath = folder + baseName + L"." + std::to_wstring(i + 1) + ext; newPath = m_logDir + baseName + L"." + std::to_wstring(i + 1) + ext;
} }
MoveFileW(oldPath.c_str(), newPath.c_str()); MoveFileW(oldPath.c_str(), newPath.c_str());

View File

@@ -27,6 +27,7 @@ class CDebug
{ {
private: private:
std::ofstream m_stream; std::ofstream m_stream;
std::wstring m_logDir;
void Write(const wchar_t *line); void Write(const wchar_t *line);
public: public:
@@ -44,6 +45,7 @@ class CDebug
LEVEL_MAX LEVEL_MAX
}; };
const wchar_t *logDir() { return m_logDir.c_str(); }
void Init(const wchar_t * name); 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_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, ...); void Log(CDebug::Level level, const char *function, int line, const wchar_t *fmt, ...);