添加日志清理功能
This commit is contained in:
parent
ff56839334
commit
8d9cfe0125
@ -20,7 +20,7 @@ enum class LogLevel
|
|||||||
class Logger
|
class Logger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static void init(const std::string& log_dir);
|
static void init(const std::string& log_dir, int keep_days = 30);
|
||||||
|
|
||||||
static void log(LogLevel level, const std::string& msg);
|
static void log(LogLevel level, const std::string& msg);
|
||||||
|
|
||||||
@ -31,11 +31,13 @@ class Logger
|
|||||||
static std::string log_directory;
|
static std::string log_directory;
|
||||||
static std::string current_date;
|
static std::string current_date;
|
||||||
static std::string current_filename;
|
static std::string current_filename;
|
||||||
|
static int keep_days_;
|
||||||
|
|
||||||
static std::string get_time_string();
|
static std::string get_time_string();
|
||||||
static std::string get_today_date();
|
static std::string get_today_date();
|
||||||
static void open_log_file();
|
static void open_log_file();
|
||||||
static void check_date_rollover();
|
static void check_date_rollover();
|
||||||
|
static void cleanup_old_logs();
|
||||||
};
|
};
|
||||||
|
|
||||||
// 简化宏
|
// 简化宏
|
||||||
|
|||||||
@ -4,9 +4,11 @@ std::ofstream Logger::log_file;
|
|||||||
std::string Logger::log_directory;
|
std::string Logger::log_directory;
|
||||||
std::string Logger::current_date;
|
std::string Logger::current_date;
|
||||||
std::string Logger::current_filename;
|
std::string Logger::current_filename;
|
||||||
|
int Logger::keep_days_ = 30;
|
||||||
|
|
||||||
void Logger::init(const std::string& log_dir)
|
void Logger::init(const std::string& log_dir, int keep_days)
|
||||||
{
|
{
|
||||||
|
keep_days_ = keep_days;
|
||||||
log_directory = log_dir;
|
log_directory = log_dir;
|
||||||
|
|
||||||
if (!std::filesystem::exists(log_directory)) std::filesystem::create_directories(log_directory);
|
if (!std::filesystem::exists(log_directory)) std::filesystem::create_directories(log_directory);
|
||||||
@ -36,6 +38,39 @@ void Logger::check_date_rollover()
|
|||||||
{
|
{
|
||||||
current_date = today;
|
current_date = today;
|
||||||
open_log_file();
|
open_log_file();
|
||||||
|
cleanup_old_logs();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::cleanup_old_logs()
|
||||||
|
{
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
for (auto& entry : fs::directory_iterator(log_directory))
|
||||||
|
{
|
||||||
|
if (!entry.is_regular_file()) continue;
|
||||||
|
|
||||||
|
auto path = entry.path();
|
||||||
|
auto filename = path.filename().string();
|
||||||
|
|
||||||
|
if (filename.size() != 14 || filename.substr(4, 1) != "-" || filename.substr(7, 1) != "-")
|
||||||
|
continue; // 跳过不符合 YYYY-MM-DD.log 的文件
|
||||||
|
|
||||||
|
std::tm file_tm{};
|
||||||
|
std::istringstream ss(filename.substr(0, 10)); // YYYY-MM-DD
|
||||||
|
ss >> std::get_time(&file_tm, "%Y-%m-%d");
|
||||||
|
|
||||||
|
if (ss.fail()) continue;
|
||||||
|
|
||||||
|
auto file_time = std::chrono::system_clock::from_time_t(std::mktime(&file_tm));
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
|
||||||
|
auto age = std::chrono::duration_cast<std::chrono::hours>(now - file_time).count() / 24;
|
||||||
|
|
||||||
|
if (age > keep_days_)
|
||||||
|
{
|
||||||
|
fs::remove(path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,6 +110,7 @@ std::string Logger::get_current_time_utc8()
|
|||||||
|
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << ms.count();
|
ss << std::put_time(&tm_now, "%Y%m%d%H%M%S") << std::setw(3) << std::setfill('0') << ms.count();
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user