Skip to content

Commit c11ce6c

Browse files
committed
```
feat(core): 实现日志目录智能管理功能 - 添加日志目录相关函数,支持DLL目录和系统TEMP目录的智能选择 - 实现日志文件路径动态构建,格式为proxy-YYYYMMDD.log - 添加旧日志清理功能,只保留当天日志文件 - 更新README文档,详细说明日志文件位置和验证方法 - 修复metadata.json中的时间戳更新问题 ```
1 parent e1a7745 commit c11ce6c

File tree

3 files changed

+125
-9
lines changed

3 files changed

+125
-9
lines changed

.sanshu-memory/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"project_path": "\\\\?\\E:\\ProjectCode\\C++Code\\antigravity-proxy",
3-
"last_organized": "2026-01-09T20:49:23.286768500Z",
3+
"last_organized": "2026-01-10T16:47:10.839367Z",
44
"total_entries": 9,
55
"version": "1.0.0"
66
}

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,23 @@ target_link_libraries(version PRIVATE ws2_32)
496496

497497
### 验证是否生效 / Verification
498498

499-
1. **检查日志**: 查看目标程序目录下是否生成 `proxy.log`
499+
1. **检查日志**: 查看是否生成日志文件(格式:`proxy-YYYYMMDD.log`
500500
2. **查看代理软件**: 观察代理软件的连接日志
501501
3. **使用抓包工具**: 使用 Wireshark 确认流量走向
502502

503+
#### 📂 日志文件位置 / Log File Locations
504+
505+
日志文件按以下优先级存放:
506+
507+
| 优先级 | 位置 | 说明 |
508+
|--------|------|------|
509+
| 1️⃣ | `<DLL所在目录>\logs\` |`version.dll` 同级的 `logs` 子目录 |
510+
| 2️⃣ | `%TEMP%\antigravity-proxy-logs\` | 系统临时目录(通常为 `C:\Users\<用户名>\AppData\Local\Temp\antigravity-proxy-logs\`|
511+
512+
> 💡 **提示**:如果在 DLL 目录无法创建 `logs` 文件夹(例如权限不足),日志会自动回退到系统 TEMP 目录。
513+
>
514+
> 快速打开 TEMP 目录:按 `Win+R`,输入 `%TEMP%`,回车即可。
515+
503516
---
504517

505518
## 🚀 进阶玩法 / Advanced Usage

src/core/Logger.hpp

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,88 @@
1616
namespace Core {
1717
class Logger {
1818
private:
19+
// ========== 日志目录相关函数 ==========
20+
21+
// 获取 DLL 所在目录(用于定位日志目录)
22+
static std::string GetDllDirectory() {
23+
char modulePath[MAX_PATH] = {0};
24+
HMODULE hModule = NULL;
25+
// 通过函数地址获取当前 DLL 的模块句柄
26+
if (!GetModuleHandleExA(
27+
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
28+
reinterpret_cast<LPCSTR>(&GetDllDirectory),
29+
&hModule)) {
30+
return "";
31+
}
32+
DWORD len = GetModuleFileNameA(hModule, modulePath, MAX_PATH);
33+
if (len == 0 || len >= MAX_PATH) {
34+
return "";
35+
}
36+
// 截取路径,去掉文件名部分
37+
for (int i = static_cast<int>(len) - 1; i >= 0; --i) {
38+
if (modulePath[i] == '\\' || modulePath[i] == '/') {
39+
modulePath[i] = '\0';
40+
break;
41+
}
42+
}
43+
return std::string(modulePath);
44+
}
45+
46+
// 确保目录存在,不存在则创建
47+
static bool EnsureLogDirectory(const std::string& dirPath) {
48+
DWORD attr = GetFileAttributesA(dirPath.c_str());
49+
if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY)) {
50+
return true; // 目录已存在
51+
}
52+
// 尝试创建目录
53+
return CreateDirectoryA(dirPath.c_str(), NULL) != 0;
54+
}
55+
56+
// 获取系统临时目录路径
57+
static std::string GetSystemTempDirectory() {
58+
char tempPath[MAX_PATH] = {0};
59+
DWORD len = GetTempPathA(MAX_PATH, tempPath);
60+
if (len == 0 || len >= MAX_PATH) {
61+
return "";
62+
}
63+
// 去掉末尾的反斜杠(GetTempPathA 返回的路径末尾带 \)
64+
if (len > 0 && (tempPath[len - 1] == '\\' || tempPath[len - 1] == '/')) {
65+
tempPath[len - 1] = '\0';
66+
}
67+
return std::string(tempPath);
68+
}
69+
70+
// 获取日志目录路径,首次调用时初始化
71+
// 优先级:DLL目录/logs/ → 系统TEMP目录/antigravity-proxy-logs/
72+
static std::string GetLogDirectory() {
73+
static std::string s_logDir;
74+
static bool s_initialized = false;
75+
if (!s_initialized) {
76+
s_initialized = true;
77+
// 优先尝试 DLL 目录下的 logs 子目录
78+
std::string dllDir = GetDllDirectory();
79+
if (!dllDir.empty()) {
80+
std::string dllLogs = dllDir + "\\logs";
81+
if (EnsureLogDirectory(dllLogs)) {
82+
s_logDir = dllLogs;
83+
return s_logDir;
84+
}
85+
}
86+
// 回退到系统 TEMP 目录
87+
std::string tempDir = GetSystemTempDirectory();
88+
if (!tempDir.empty()) {
89+
std::string tempLogs = tempDir + "\\antigravity-proxy-logs";
90+
if (EnsureLogDirectory(tempLogs)) {
91+
s_logDir = tempLogs;
92+
}
93+
}
94+
// 如果都失败,s_logDir 保持为空,日志将写入当前目录(最后手段)
95+
}
96+
return s_logDir;
97+
}
98+
99+
// ========== 原有辅助函数 ==========
100+
19101
static std::string GetTimestamp() {
20102
auto now = std::time(nullptr);
21103
struct tm tm;
@@ -32,11 +114,17 @@ namespace Core {
32114
return "[PID:" + std::to_string(pid) + "][TID:" + std::to_string(tid) + "]";
33115
}
34116

117+
// 获取今日日志文件完整路径(如:C:\xxx\logs\proxy-20260111.log)
35118
static std::string GetTodayLogName() {
36119
auto now = std::time(nullptr);
37120
struct tm tm;
38121
localtime_s(&tm, &now);
39122
std::ostringstream oss;
123+
// 优先使用 DLL 目录下的 logs 子目录
124+
std::string logDir = GetLogDirectory();
125+
if (!logDir.empty()) {
126+
oss << logDir << "\\";
127+
}
40128
oss << "proxy-" << std::put_time(&tm, "%Y%m%d") << ".log";
41129
return oss.str();
42130
}
@@ -50,25 +138,40 @@ namespace Core {
50138
return size >= maxBytes;
51139
}
52140

141+
// 清理旧日志文件,只保留当天的日志
53142
static void CleanupOldLogs(const std::string& todayLog) {
54-
// 删除非当天日志,只保留当前日期的日志文件
143+
std::string logDir = GetLogDirectory();
144+
// 构建搜索模式(支持有/无日志目录两种情况)
145+
std::string searchPattern = logDir.empty()
146+
? "proxy-*.log"
147+
: (logDir + "\\proxy-*.log");
148+
55149
WIN32_FIND_DATAA findData{};
56-
HANDLE hFind = FindFirstFileA("proxy-*.log", &findData);
150+
HANDLE hFind = FindFirstFileA(searchPattern.c_str(), &findData);
57151
if (hFind != INVALID_HANDLE_VALUE) {
58152
do {
59153
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
60154
continue;
61155
}
62-
if (todayLog != findData.cFileName) {
63-
DeleteFileA(findData.cFileName);
156+
// 构建找到文件的完整路径
157+
std::string fullPath = logDir.empty()
158+
? std::string(findData.cFileName)
159+
: (logDir + "\\" + findData.cFileName);
160+
161+
// 保留今天的日志,删除其他日期的
162+
if (todayLog != fullPath) {
163+
DeleteFileA(fullPath.c_str());
64164
}
65165
} while (FindNextFileA(hFind, &findData));
66166
FindClose(hFind);
67167
}
68-
if (todayLog != "proxy.log") {
69-
DeleteFileA("proxy.log");
168+
// 清理旧版日志文件(无日期后缀的遗留文件)
169+
std::string oldLog = logDir.empty() ? "proxy.log" : (logDir + "\\proxy.log");
170+
std::string oldLog1 = logDir.empty() ? "proxy.log.1" : (logDir + "\\proxy.log.1");
171+
if (todayLog != oldLog) {
172+
DeleteFileA(oldLog.c_str());
70173
}
71-
DeleteFileA("proxy.log.1");
174+
DeleteFileA(oldLog1.c_str());
72175
}
73176

74177
static void WriteToFile(const std::string& message) {

0 commit comments

Comments
 (0)