1616namespace 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