Skip to content

Commit ba681ec

Browse files
authored
Refactor logging and GPU display handling
1 parent 9b22101 commit ba681ec

1 file changed

Lines changed: 40 additions & 29 deletions

File tree

src/main.cpp

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <dxgi.h>
44
#include <fstream>
55
#include <sstream>
6+
#include <filesystem>
67
#include "resource.h"
78

89
#pragma comment(lib, "dxgi.lib")
@@ -18,10 +19,10 @@ extern "C" {
1819
__declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 1;
1920
}
2021

21-
static wchar_t g_displayGpuName[128] = L"Unknown GPU";
22-
static UINT g_displayVendor = 0;
23-
static HINSTANCE g_hInst = nullptr;
24-
static HICON g_currentIcon = nullptr;
22+
static wchar_t g_displayGpuName[128] = L"Unknown GPU";
23+
static UINT g_displayVendor = 0;
24+
static HINSTANCE g_hInst = nullptr;
25+
static HICON g_currentIcon = nullptr;
2526

2627
// ───────────────────────────────────────────────────────────────
2728
// Logging (errors only, capped at ~10 KB)
@@ -30,30 +31,44 @@ void LogError(const std::wstring& msg)
3031
{
3132
const wchar_t* logFile = L"gpu_switcher.log";
3233

33-
std::wifstream in(logFile);
34-
std::wstringstream buffer;
35-
buffer << in.rdbuf();
36-
in.close();
34+
// Append new entry
35+
{
36+
std::wofstream out(logFile, std::ios::app);
37+
if (!out.is_open())
38+
return;
3739

38-
std::wstring existing = buffer.str();
40+
SYSTEMTIME st;
41+
GetLocalTime(&st);
42+
wchar_t timestamp[64];
43+
swprintf_s(timestamp, L"[%04d-%02d-%02d %02d:%02d:%02d] ",
44+
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
3945

40-
SYSTEMTIME st;
41-
GetLocalTime(&st);
42-
wchar_t timestamp[64];
43-
swprintf_s(timestamp, L"[%04d-%02d-%02d %02d:%02d:%02d] ",
44-
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
46+
out << timestamp << msg << L"\n";
47+
}
4548

46-
existing += timestamp;
47-
existing += msg + L"\n";
49+
// Truncate if file grows too large
50+
const std::uintmax_t MAX_SIZE = 10 * 1024; // ~10 KB
51+
std::error_code ec;
52+
auto size = std::filesystem::file_size(logFile, ec);
53+
if (ec || size <= MAX_SIZE)
54+
return;
4855

49-
const size_t MAX_SIZE = 10 * 1024;
50-
if (existing.size() > MAX_SIZE)
51-
{
52-
existing = existing.substr(existing.size() - MAX_SIZE / 2);
53-
}
56+
// Keep the last half
57+
std::wifstream in(logFile);
58+
if (!in.is_open())
59+
return;
60+
61+
std::wstring content((std::istreambuf_iterator<wchar_t>(in)),
62+
std::istreambuf_iterator<wchar_t>());
63+
64+
if (content.size() > MAX_SIZE / 2)
65+
content = content.substr(content.size() - MAX_SIZE / 2);
5466

5567
std::wofstream out(logFile, std::ios::trunc);
56-
out << existing;
68+
if (!out.is_open())
69+
return;
70+
71+
out << content;
5772
}
5873

5974
// ───────────────────────────────────────────────────────────────
@@ -147,14 +162,13 @@ bool DetectDisplayGPU()
147162
// ───────────────────────────────────────────────────────────────
148163
HICON LoadDisplayIcon(UINT displayVendor)
149164
{
150-
UINT vendor = displayVendor;
151-
if (vendor == 0)
165+
if (displayVendor == 0)
152166
{
153167
// Detection failed → warning icon
154168
return LoadIconW(g_hInst, MAKEINTRESOURCEW(IDI_ICON_WARNING));
155169
}
156170

157-
switch (vendor)
171+
switch (displayVendor)
158172
{
159173
case 0x8086: // Intel
160174
return LoadIconW(g_hInst, MAKEINTRESOURCEW(IDI_ICON_INTEL));
@@ -255,10 +269,7 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int)
255269
nid.hIcon = g_currentIcon;
256270

257271
wchar_t tip[256];
258-
swprintf_s(tip,
259-
L"Display GPU: %s",
260-
g_displayGpuName);
261-
272+
swprintf_s(tip, L"Display GPU: %s", g_displayGpuName);
262273
wcsncpy_s(nid.szTip, tip, _TRUNCATE);
263274

264275
Shell_NotifyIconW(NIM_ADD, &nid);

0 commit comments

Comments
 (0)