Merge pull request #2226 from Yundi339/fix_log

feat: 重构流量历史记录优化性能
This commit is contained in:
zhongyang219
2026-07-10 16:17:47 +08:00
committed by GitHub
5 changed files with 422 additions and 97 deletions
+323 -67
View File
@@ -11,65 +11,190 @@ CHistoryTrafficFile::~CHistoryTrafficFile()
{
}
HistoryTraffic CHistoryTrafficFile::CreateTodayTraffic() const
{
SYSTEMTIME current_time;
GetLocalTime(&current_time);
HistoryTraffic today_traffic;
today_traffic.year = current_time.wYear;
today_traffic.month = current_time.wMonth;
today_traffic.day = current_time.wDay;
today_traffic.up_kBytes = 0;
today_traffic.down_kBytes = 0;
today_traffic.mixed = false;
return today_traffic;
}
void CHistoryTrafficFile::WriteTrafficRecord(ofstream& file, const HistoryTraffic& traffic) const
{
char buff[64];
if (traffic.mixed)
{
sprintf_s(buff, "%.4d/%.2d/%.2d %llu", traffic.year, traffic.month,
traffic.day, traffic.down_kBytes);
}
else
{
sprintf_s(buff, "%.4d/%.2d/%.2d %llu/%llu", traffic.year, traffic.month,
traffic.day, traffic.up_kBytes, traffic.down_kBytes);
}
file << buff << "\n";
}
void CHistoryTrafficFile::UpdateCache() const
{
// 更新缓存:合并今天的记录和历史记录链表
m_traffics_cache.clear();
m_traffics_cache.push_front(m_today_traffic);
m_traffics_cache.insert(m_traffics_cache.end(), m_history_traffics.begin(), m_history_traffics.end());
m_cache_dirty = false; // 标记缓存已更新
}
void CHistoryTrafficFile::Save() const
{
ofstream file{ m_file_path };
if (!file.is_open())
{
return;
}
char buff[64];
sprintf_s(buff, "lines: \"%u\"", static_cast<unsigned int>(m_history_traffics.size())); //在第一行写入总行数
file << buff << std::endl;
// 第一行:总记录数(今天的记录 + 历史记录)
size_t total_size = 1 + m_history_traffics.size();
sprintf_s(buff, "lines: \"%u\"", static_cast<unsigned int>(total_size));
file << buff << "\n";
// 第二行:今天的记录
WriteTrafficRecord(file, m_today_traffic);
// 第三行及之后:历史记录链表
for (const auto& history_traffic : m_history_traffics)
{
if (history_traffic.mixed)
sprintf_s(buff, "%.4d/%.2d/%.2d %llu", history_traffic.year, history_traffic.month, history_traffic.day, history_traffic.down_kBytes);
else
sprintf_s(buff, "%.4d/%.2d/%.2d %llu/%llu", history_traffic.year, history_traffic.month, history_traffic.day, history_traffic.up_kBytes, history_traffic.down_kBytes);
file << buff << std::endl;
WriteTrafficRecord(file, history_traffic);
}
file.close();
}
bool CHistoryTrafficFile::IsTodayRecord() const
{
// 检查今天的记录日期是否正确
SYSTEMTIME current_time;
GetLocalTime(&current_time);
return (m_today_traffic.year == current_time.wYear &&
m_today_traffic.month == current_time.wMonth &&
m_today_traffic.day == current_time.wDay);
}
void CHistoryTrafficFile::SaveTodayOnly() const
{
// 增量保存:只更新第一行(lines计数)和第二行(今天的记录)
// 前提:今天的记录日期正确(程序自己维护,无需读取文件判断)
// 如果今天的记录日期不正确,说明日期刚改变,使用完整保存
if (!IsTodayRecord())
{
Save();
return;
}
// 文件不存在时,使用完整保存(首次保存)
if (!CCommon::FileExist(m_file_path.c_str()))
{
Save();
return;
}
// 读取文件剩余行(第3行及之后),用于增量更新
vector<string> remaining_lines;
ifstream in_file{ m_file_path };
if (in_file.is_open())
{
string line;
int line_num = 0;
while (std::getline(in_file, line))
{
line_num++;
if (line_num > 2) // 跳过前两行
{
remaining_lines.push_back(line);
}
}
in_file.close();
}
// 写入更新后的文件
ofstream out_file{ m_file_path };
if (!out_file.is_open())
{
return;
}
// 第一行:lines计数(今天的记录 + 历史记录)
size_t total_size = 1 + m_history_traffics.size();
char buff[64];
sprintf_s(buff, "lines: \"%u\"", static_cast<unsigned int>(total_size));
out_file << buff << "\n";
// 第二行:今天的记录
WriteTrafficRecord(out_file, m_today_traffic);
// 剩余行:从文件读取,直接写入(不格式化)
for (const auto& line : remaining_lines)
{
out_file << line << "\n";
}
out_file.close();
}
void CHistoryTrafficFile::Load()
{
m_today_traffic = HistoryTraffic{}; // 初始化今天的记录
m_history_traffics.clear(); // 清空历史记录链表
InvalidateCache(); // 标记缓存过期
ifstream file{ m_file_path };
string current_line, temp;
HistoryTraffic traffic;
//bool first_line{ true };
bool is_first_data_line = true; // 标记是否是第一条数据行(今天的记录)
if (CCommon::FileExist(m_file_path.c_str()))
{
// 跳过第一行(lines:)
std::getline(file, current_line);
while (!file.eof())
{
if (m_history_traffics.size() > 9999) break; //最多读取10000天的历史记录
std::getline(file, current_line);
//if (first_line)
//{
// first_line = false;
// size_t index = current_line.find("lines:");
// if(index != wstring::npos)
// {
// index = current_line.find("\"", index + 6);
// size_t index1 = current_line.find("\"", index + 1);
// temp = current_line.substr(index + 1, index1 - index - 1);
// m_size = atoll(temp.c_str());
// continue;
// }
//}
if (current_line.size() < 12) continue;
if (current_line.size() < 12)
{
continue;
}
temp = current_line.substr(0, 4);
traffic.year = atoi(temp.c_str());
if (traffic.year < 1900 || traffic.year > 3000)
{
continue;
}
temp = current_line.substr(5, 2);
traffic.month = atoi(temp.c_str());
if (traffic.month < 1 || traffic.month > 12)
{
continue;
}
temp = current_line.substr(8, 2);
traffic.day = atoi(temp.c_str());
if (traffic.day < 1 || traffic.day > 31)
{
continue;
}
int index = current_line.find(L'/', 11);
traffic.mixed = (index == wstring::npos);
size_t separator_index = current_line.find(L'/', 11);
traffic.mixed = (separator_index == wstring::npos);
if (traffic.mixed)
{
temp = current_line.substr(11);
@@ -78,13 +203,26 @@ void CHistoryTrafficFile::Load()
}
else
{
temp = current_line.substr(11, index - 11);
temp = current_line.substr(11, separator_index - 11);
traffic.up_kBytes = atoll(temp.c_str());
temp = current_line.substr(index + 1);
temp = current_line.substr(separator_index + 1);
traffic.down_kBytes = atoll(temp.c_str());
}
if (traffic.year > 0 && traffic.month > 0 && traffic.day > 0 && traffic.kBytes() > 0)
m_history_traffics.push_back(traffic);
{
if (is_first_data_line)
{
// 第一条数据行是今天的记录
m_today_traffic = traffic;
is_first_data_line = false;
}
else
{
// 其余是历史记录,插入到链表
m_history_traffics.push_back(traffic);
}
}
}
}
@@ -97,7 +235,7 @@ void CHistoryTrafficFile::LoadSize()
string current_line, temp;
if (CCommon::FileExist(m_file_path.c_str()))
{
std::getline(file, current_line); //读取第一行
std::getline(file, current_line); // 读取第一行
size_t index = current_line.find("lines:");
if (index != wstring::npos)
{
@@ -111,69 +249,187 @@ void CHistoryTrafficFile::LoadSize()
void CHistoryTrafficFile::Merge(const CHistoryTrafficFile& history_traffic, bool ignore_same_data)
{
HistoryTraffic today_traffic = CreateTodayTraffic();
// 合并今天的记录(只合并日期相同的)
// 注意:如果 ignore_same_data=true,说明是从备份恢复,应该取较大的值而不是累加,避免重复累加
if (HistoryTraffic::DateEqual(m_today_traffic, history_traffic.m_today_traffic))
{
if (ignore_same_data)
{
// 从备份恢复时,取较大的值(避免重复累加)
// 备份文件通常是程序退出时的完整数据,当前文件可能是程序启动后的不完整数据
if (history_traffic.m_today_traffic.up_kBytes > m_today_traffic.up_kBytes)
{
m_today_traffic.up_kBytes = history_traffic.m_today_traffic.up_kBytes;
}
if (history_traffic.m_today_traffic.down_kBytes > m_today_traffic.down_kBytes)
{
m_today_traffic.down_kBytes = history_traffic.m_today_traffic.down_kBytes;
}
}
else
{
// 正常合并时,累加数据
m_today_traffic.up_kBytes += history_traffic.m_today_traffic.up_kBytes;
m_today_traffic.down_kBytes += history_traffic.m_today_traffic.down_kBytes;
}
}
// 合并历史记录链表
for (const HistoryTraffic& traffic : history_traffic.m_history_traffics)
{
if(ignore_same_data)
// 跳过"未来"的记录(系统时间可能被调整了)
if (HistoryTraffic::DateGreater(traffic, today_traffic))
{
//如果要忽略相同日期的项,则使用二分法查找日期相同的项,如果找到了,则跳过它
if (std::binary_search(m_history_traffics.begin(), m_history_traffics.end(), traffic, HistoryTraffic::DateGreater))
continue; // 跳过"未来"的记录
}
if (ignore_same_data)
{
// 如果要忽略相同日期的项,使用线性查找(list不支持随机访问)
auto it = std::find_if(m_history_traffics.begin(), m_history_traffics.end(),
[&traffic](const HistoryTraffic& existing) {
return HistoryTraffic::DateEqual(existing, traffic);
});
if (it != m_history_traffics.end())
{
auto iter = std::lower_bound(m_history_traffics.begin(), m_history_traffics.end(), traffic, HistoryTraffic::DateGreater);
if (iter != m_history_traffics.end())
{
continue;
}
continue; // 找到相同日期的记录,跳过
}
}
m_history_traffics.push_back(traffic);
}
MormalizeData();
InvalidateCache(); // 标记缓存过期
}
void CHistoryTrafficFile::OnDateChanged()
{
// 日期改变时,将今天的记录移到历史记录链表的前面,然后创建新的今天的记录
// 如果今天的记录有数据,将其移到历史记录链表
if (m_today_traffic.kBytes() > 0)
{
m_history_traffics.push_front(m_today_traffic);
// 立即排序,确保数据一致性(按日期从大到小)
if (m_history_traffics.size() >= 2)
{
m_history_traffics.sort(HistoryTraffic::DateGreater);
}
}
// 创建新的今天的记录
m_today_traffic = CreateTodayTraffic();
// 更新统计
m_today_up_traffic = 0;
m_today_down_traffic = 0;
m_size = 1 + m_history_traffics.size();
InvalidateCache(); // 标记缓存过期
}
void CHistoryTrafficFile::MormalizeData()
{
SYSTEMTIME current_time;
GetLocalTime(&current_time);
HistoryTraffic traffic;
traffic.year = current_time.wYear;
traffic.month = current_time.wMonth;
traffic.day = current_time.wDay;
traffic.up_kBytes = 0;
traffic.down_kBytes = 0;
traffic.mixed = false;
if (m_history_traffics.empty())
{
m_history_traffics.push_front(traffic);
}
HistoryTraffic today_traffic = CreateTodayTraffic();
// 先对历史记录链表排序(按日期从大到小),以便后续查找和合并
if (m_history_traffics.size() >= 2)
{
//将读取到的历史流量列表按日期从大到小排序
std::sort(m_history_traffics.begin(), m_history_traffics.end(), HistoryTraffic::DateGreater);
m_history_traffics.sort(HistoryTraffic::DateGreater);
//如果列表中有相同日期的项目,则将它合并
for (int i{}; i < static_cast<int>(m_history_traffics.size() - 1); i++)
// 合并相同日期的记录
auto it = m_history_traffics.begin();
while (it != m_history_traffics.end())
{
if (HistoryTraffic::DateEqual(m_history_traffics[i], m_history_traffics[i + 1]))
auto next_it = it;
++next_it;
if (next_it != m_history_traffics.end() && HistoryTraffic::DateEqual(*it, *next_it))
{
m_history_traffics[i].up_kBytes += m_history_traffics[i + 1].up_kBytes;
m_history_traffics[i].down_kBytes += m_history_traffics[i + 1].down_kBytes;
m_history_traffics.erase(m_history_traffics.begin() + i + 1);
it->up_kBytes += next_it->up_kBytes;
it->down_kBytes += next_it->down_kBytes;
m_history_traffics.erase(next_it);
}
else
{
++it;
}
}
}
//如果列表第一个项目的日期是今天,则将第一个项目统计的流量作为今天使用的流量,否则,在列表的前面插入一个日期为今天的项目
if (HistoryTraffic::DateEqual(m_history_traffics[0], traffic))
// 清理日期晚于当前日期的历史记录(系统时间可能被调整了)
// 历史记录应该都是过去的日期,不应该有未来的日期
if (!m_history_traffics.empty())
{
m_today_up_traffic = static_cast<__int64>(m_history_traffics[0].up_kBytes) * 1024;
m_today_down_traffic = static_cast<__int64>(m_history_traffics[0].down_kBytes) * 1024;
m_history_traffics[0].mixed = false;
auto it = m_history_traffics.begin();
while (it != m_history_traffics.end())
{
// 如果历史记录的日期晚于今天,说明是"未来"的记录,应该删除
if (HistoryTraffic::DateGreater(*it, today_traffic))
{
it = m_history_traffics.erase(it);
}
else
{
++it;
}
}
}
else
// 如果 m_today_traffic 的日期也晚于当前日期,说明系统时间被调整了,应该重置
if (HistoryTraffic::DateGreater(m_today_traffic, today_traffic))
{
m_history_traffics.push_front(traffic);
// 如果今天的记录有数据,应该将其移到历史记录(但日期晚于今天,会被上面的清理逻辑删除)
// 直接重置为今天的记录
m_today_traffic = today_traffic;
}
m_size = m_history_traffics.size();
// 在历史记录中查找今天的记录(可能历史记录中包含了今天的数据)
auto it = std::find_if(m_history_traffics.begin(), m_history_traffics.end(),
[&today_traffic](const HistoryTraffic& traffic) {
return HistoryTraffic::DateEqual(traffic, today_traffic);
});
if (it != m_history_traffics.end())
{
// 历史记录中找到了今天的记录
if (HistoryTraffic::DateEqual(m_today_traffic, today_traffic))
{
// 如果 m_today_traffic 也是今天的,合并数据(避免数据丢失)
m_today_traffic.up_kBytes += it->up_kBytes;
m_today_traffic.down_kBytes += it->down_kBytes;
}
else
{
// 如果 m_today_traffic 不是今天的,用历史记录中的替换
m_today_traffic = *it;
}
// 从历史记录中删除今天的记录(因为应该只在 m_today_traffic 中)
m_history_traffics.erase(it);
}
else if (!HistoryTraffic::DateEqual(m_today_traffic, today_traffic))
{
// 历史记录中没有今天的记录,且 m_today_traffic 也不是今天的
// 如果 m_today_traffic 有数据,应该将其移到历史记录链表
if (m_today_traffic.kBytes() > 0)
{
m_history_traffics.push_front(m_today_traffic);
// 重新排序(因为插入了新记录)
if (m_history_traffics.size() >= 2)
{
m_history_traffics.sort(HistoryTraffic::DateGreater);
}
}
// 创建新的今天的记录
m_today_traffic = today_traffic;
}
// 更新今天的流量统计
m_today_up_traffic = static_cast<__int64>(m_today_traffic.up_kBytes) * 1024;
m_today_down_traffic = static_cast<__int64>(m_today_traffic.down_kBytes) * 1024;
m_today_traffic.mixed = false;
// 更新总记录数
m_size = 1 + m_history_traffics.size();
InvalidateCache(); // 标记缓存过期
}
+24 -3
View File
@@ -7,23 +7,44 @@ public:
~CHistoryTrafficFile();
void Save() const;
void SaveTodayOnly() const; // 仅更新第一行(lines计数)和今天的记录(第二行),用于频繁保存时减少I/O
void Load();
void LoadSize(); //仅读取文件的大小
void Merge(const CHistoryTrafficFile& history_traffic, bool ignore_same_data = false); //合并另一个CHistoryTrafficFile对象。如果ignore_same_data为true,则忽略相同日期的项,否则将相同日期的流量数据相加
void OnDateChanged(); //日期改变时调用,将今天的记录移到历史记录,创建新的今天的记录
const wstring& GetFilePath() const { return m_file_path; }
const void SetFilePath(const wstring& file_path) { m_file_path = file_path; }
deque<HistoryTraffic>& GetTraffics() { return m_history_traffics; }
// 获取完整流量列表(用于统计功能),返回deque引用以兼容现有代码
deque<HistoryTraffic>& GetTraffics() { if (m_cache_dirty) UpdateCache(); return m_traffics_cache; }
const deque<HistoryTraffic>& GetTraffics() const { if (m_cache_dirty) UpdateCache(); return m_traffics_cache; }
// 获取今天的记录(第一条)
HistoryTraffic& GetTodayTraffic() { return m_today_traffic; }
const HistoryTraffic& GetTodayTraffic() const { return m_today_traffic; }
// 获取历史记录链表
const list<HistoryTraffic>& GetHistoryTraffics() const { return m_history_traffics; }
__int64 GetTodayUpTraffic() const { return m_today_up_traffic; }
__int64 GetTodayDownTraffic() const { return m_today_down_traffic; }
size_t Size() { return m_size; }
size_t Size() const { return m_size; }
private:
void MormalizeData(); //将历史流量数据排序并合并相同项
bool IsTodayRecord() const; //检查今天的记录日期是否正确
void UpdateCache() const; //更新缓存(用于统计功能)
void WriteTrafficRecord(ofstream& file, const HistoryTraffic& traffic) const; //写入一条流量记录到文件
HistoryTraffic CreateTodayTraffic() const; //创建今天的记录(日期为当前日期,流量为0)
void InvalidateCache() const { m_cache_dirty = true; } //标记缓存过期
private:
wstring m_file_path;
deque<HistoryTraffic> m_history_traffics; //储存历史流量
HistoryTraffic m_today_traffic; // 今天的记录(单独存储,频繁更新)
list<HistoryTraffic> m_history_traffics; // 历史记录链表(按日期从大到小排序)
mutable deque<HistoryTraffic> m_traffics_cache; // 缓存:合并后的完整列表(用于统计功能,按需更新)
mutable bool m_cache_dirty{ true }; // 缓存是否过期(需要更新)
__int64 m_today_up_traffic{}; //今天已使用的上传流量
__int64 m_today_down_traffic{}; //今天已使用的下载流量
size_t m_size{}; //流量数据的数量
+71 -26
View File
@@ -681,6 +681,13 @@ void CTrafficMonitorDlg::UpdateNotifyIconTip()
void CTrafficMonitorDlg::SaveHistoryTraffic()
{
// 使用增量保存,只更新第一行和今天的记录,减少I/O操作
m_history_traffic.SaveTodayOnly();
}
void CTrafficMonitorDlg::SaveHistoryTrafficFull()
{
// 完整保存,用于程序退出时确保所有数据都保存
m_history_traffic.Save();
}
@@ -689,13 +696,28 @@ void CTrafficMonitorDlg::LoadHistoryTraffic()
m_history_traffic.Load();
CHistoryTrafficFile backup_file(theApp.m_history_traffic_path + L".bak");
backup_file.LoadSize(); //读取备份文件中流量记录的数量
if (backup_file.Size() > m_history_traffic.Size()) //如果备份文件中流量记录的数量大于当前的数量,则从备份文件中恢复
// 如果备份文件中流量记录的数量大于当前的数量,尝试从备份文件中恢复
if (backup_file.Size() > m_history_traffic.Size())
{
backup_file.Load();
size_t size_before = m_history_traffic.Size();
m_history_traffic.Merge(backup_file, true);
CString log_info = CCommon::LoadTextFormat(IDS_HISTORY_TRAFFIC_LOST_ERROR_LOG, { size_before, backup_file.Size() });
CCommon::WriteLog(log_info, theApp.m_log_path.c_str());
backup_file.Load(); //加载备份文件(会清理"未来"的记录)
size_t backup_size_after_load = backup_file.Size(); //加载后实际的记录数(可能因为清理"未来"记录而减少)
// 加载后,如果备份文件的记录数仍然大于当前文件,才进行恢复
if (backup_size_after_load > m_history_traffic.Size())
{
m_history_traffic.Merge(backup_file, true);
size_t size_after = m_history_traffic.Size();
size_t recovered_count = size_after - size_before; //实际恢复的记录数
// 只有当实际恢复了记录时才记录日志
if (recovered_count > 0)
{
CString log_info = CCommon::LoadTextFormat(IDS_HISTORY_TRAFFIC_LOST_ERROR_LOG, { size_before, recovered_count });
CCommon::WriteLog(log_info, theApp.m_log_path.c_str());
}
}
}
theApp.m_today_up_traffic = m_history_traffic.GetTodayUpTraffic();
@@ -704,14 +726,19 @@ void CTrafficMonitorDlg::LoadHistoryTraffic()
void CTrafficMonitorDlg::BackupHistoryTrafficFile()
{
CHistoryTrafficFile backup_file(theApp.m_history_traffic_path + L".bak");
CHistoryTrafficFile latest_file(theApp.m_history_traffic_path);
backup_file.LoadSize();
latest_file.LoadSize();
if (backup_file.Size() < latest_file.Size())
// 确保文件已保存到磁盘
wstring latest_file_path = theApp.m_history_traffic_path;
wstring backup_file_path = latest_file_path + L".bak";
// 检查当前文件是否存在
if (!CCommon::FileExist(latest_file_path.c_str()))
{
CopyFile(latest_file.GetFilePath().c_str(), backup_file.GetFilePath().c_str(), FALSE);
return; // 当前文件不存在,无需备份
}
// 直接备份当前文件(当前文件是最新的,包含最新的数据)
// 备份文件可能包含"未来"的记录,但恢复时会自动清理,所以总是备份当前文件即可
CopyFile(latest_file_path.c_str(), backup_file_path.c_str(), FALSE);
}
void CTrafficMonitorDlg::_OnOptions(int tab, CWnd* pParent)
@@ -1262,31 +1289,49 @@ void CTrafficMonitorDlg::DoMonitorAcquisition()
//检测当前日期是否改变,如果已改变,就向历史流量列表插入一个新的日期
SYSTEMTIME current_time;
GetLocalTime(&current_time);
if (m_history_traffic.GetTraffics()[0].day != current_time.wDay)
static int last_check_day = -1; //用于检测日期变化,重置保存状态
if (m_history_traffic.GetTodayTraffic().day != current_time.wDay)
{
HistoryTraffic traffic;
traffic.year = current_time.wYear;
traffic.month = current_time.wMonth;
traffic.day = current_time.wDay;
traffic.mixed = false;
m_history_traffic.GetTraffics().push_front(traffic);
m_history_traffic.OnDateChanged();
theApp.m_today_up_traffic = 0;
theApp.m_today_down_traffic = 0;
last_check_day = -1; //重置日期标记,下次检查时会重新初始化保存状态
}
//统计今天已使用的流量
theApp.m_today_up_traffic += cur_out_speed;
theApp.m_today_down_traffic += cur_in_speed;
m_history_traffic.GetTraffics()[0].up_kBytes = theApp.m_today_up_traffic / 1024u;
m_history_traffic.GetTraffics()[0].down_kBytes = theApp.m_today_down_traffic / 1024u;
m_history_traffic.GetTodayTraffic().up_kBytes = theApp.m_today_up_traffic / 1024u;
m_history_traffic.GetTodayTraffic().down_kBytes = theApp.m_today_down_traffic / 1024u;
//每隔30秒保存一次流量历史记录
if (m_monitor_time_cnt % GetMonitorTimerCount(30) == GetMonitorTimerCount(30) - 1)
{
static unsigned __int64 last_today_kbytes;
if (m_history_traffic.GetTraffics()[0].kBytes() - last_today_kbytes >= 100u) //只有当流量变化超过100KB时才保存历史流量记录,防止磁盘写入过于频繁
static unsigned __int64 last_today_kbytes = 0;
static bool last_today_kbytes_initialized = false;
unsigned __int64 current_kbytes = m_history_traffic.GetTodayTraffic().kBytes();
//如果日期改变了,重置初始化状态
if (last_check_day != current_time.wDay)
{
SaveHistoryTraffic();
last_today_kbytes = m_history_traffic.GetTraffics()[0].kBytes();
last_today_kbytes_initialized = false;
last_check_day = current_time.wDay;
}
//首次检查时初始化,不保存
if (!last_today_kbytes_initialized)
{
last_today_kbytes = current_kbytes;
last_today_kbytes_initialized = true;
}
else
{
//只有当30秒内流量变化超过10MB时才保存历史流量记录,防止磁盘写入过于频繁
unsigned __int64 change_kbytes = current_kbytes - last_today_kbytes;
if (change_kbytes >= 10240u) // 10MB = 10240KB
{
SaveHistoryTraffic();
last_today_kbytes = current_kbytes;
}
}
}
@@ -2134,7 +2179,7 @@ void CTrafficMonitorDlg::OnClose()
theApp.m_cannot_save_global_config_warning = true;
theApp.SaveConfig(); //退出前保存设置到ini文件
theApp.SaveGlobalConfig();
SaveHistoryTraffic();
SaveHistoryTrafficFull(); // 退出时使用完整保存,确保所有数据都保存
BackupHistoryTrafficFile();
if (IsTaskbarWndValid())
@@ -2753,7 +2798,7 @@ BOOL CTrafficMonitorDlg::OnQueryEndSession()
// TODO: 在此添加专用的查询结束会话代码
theApp.SaveConfig();
theApp.SaveGlobalConfig();
SaveHistoryTraffic();
SaveHistoryTrafficFull(); // 系统关机时使用完整保存,确保所有数据都保存
BackupHistoryTrafficFile();
if (theApp.m_debug_log)
+2 -1
View File
@@ -171,7 +171,8 @@ public:
protected:
void UpdateNotifyIconTip(); //更新通知区图标的鼠标提示
void SaveHistoryTraffic();
void SaveHistoryTraffic(); // 增量保存,只更新第一行和今天的记录
void SaveHistoryTrafficFull(); // 完整保存,用于程序退出时确保所有数据都保存
void LoadHistoryTraffic();
void BackupHistoryTrafficFile();
+2
View File
@@ -41,6 +41,8 @@ using std::wstring;
using std::vector;
#include <deque>
using std::deque;
#include <list>
using std::list;
#include<iostream>
#include<fstream>
using std::ifstream;