Update log_cleaner.cpp

This commit is contained in:
zsyg
2025-06-14 10:51:12 +08:00
committed by GitHub
parent 30d1c0c8c6
commit 9dfa777c6d

View File

@@ -1,18 +1,14 @@
#include <iostream> #include <iostream>
#include <filesystem> #include <filesystem>
#include <chrono> #include <chrono>
namespace fs = std::filesystem; namespace fs = std::filesystem;
int main() { int main() {
try { try {
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
// 定义日志目录路径 // 定义日志目录路径
fs::path logDir = "logs"; fs::path logDir = "logs";
size_t deletedCount = 0; size_t deletedCount = 0;
size_t errorCount = 0; size_t errorCount = 0;
// 检查目录是否存在 // 检查目录是否存在
if (fs::exists(logDir) && fs::is_directory(logDir)) { if (fs::exists(logDir) && fs::is_directory(logDir)) {
// 遍历并删除所有日志文件 // 遍历并删除所有日志文件
@@ -31,19 +27,15 @@ int main() {
std::cout << "日志目录不存在,无需清理" << std::endl; std::cout << "日志目录不存在,无需清理" << std::endl;
return 0; return 0;
} }
auto end = std::chrono::high_resolution_clock::now(); auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "日志清理完成: " << std::endl; std::cout << "日志清理完成: " << std::endl;
std::cout << "删除文件数: " << deletedCount << std::endl; std::cout << "删除文件数: " << deletedCount << std::endl;
std::cout << "错误数: " << errorCount << std::endl; std::cout << "错误数: " << errorCount << std::endl;
std::cout << "耗时: " << duration.count() << " 毫秒" << std::endl; std::cout << "耗时: " << duration.count() << " 毫秒" << std::endl;
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << "发生错误: " << e.what() << std::endl; std::cerr << "发生错误: " << e.what() << std::endl;
return 1; return 1;
} }
return 0; return 0;
} }