mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-20 10:57:45 +08:00
feat: 肉鸽debug日志写入图片前限制文件夹内图片数量 (#5325)
This commit is contained in:
@@ -49,6 +49,10 @@
|
||||
"Doc": "仓库识别导出结果模板",
|
||||
"arkPlanner": "{\"@type\": \"@penguin-statistics/depot\",\"items\": []}",
|
||||
"arkPlanner_Doc": "https://penguin-stats.cn/planner"
|
||||
},
|
||||
"debug": {
|
||||
"cleanFilesFreq": 100,
|
||||
"maxDebugFileNum": 1000
|
||||
}
|
||||
},
|
||||
"intent": {
|
||||
|
||||
@@ -53,6 +53,8 @@ bool asst::GeneralConfig::parse(const json::value& json)
|
||||
}
|
||||
m_options.depot_export_template.ark_planner =
|
||||
options_json.get("depotExportTemplate", "arkPlanner", std::string());
|
||||
m_options.debug.clean_files_freq = options_json.get("debug", "cleanFilesFreq", 100);
|
||||
m_options.debug.max_debug_file_num = options_json.get("debug", "maxDebugFileNum", 1000);
|
||||
}
|
||||
|
||||
for (const auto& [client_type, intent_name] : json.at("intent").as_object()) {
|
||||
|
||||
@@ -23,6 +23,12 @@ namespace asst
|
||||
std::string ark_planner;
|
||||
};
|
||||
|
||||
struct DebugConf
|
||||
{
|
||||
int clean_files_freq = 100;
|
||||
int max_debug_file_num = 1000;
|
||||
};
|
||||
|
||||
struct Options
|
||||
{
|
||||
int task_delay = 0; // 任务间延时:越快操作越快,但会增加CPU消耗
|
||||
@@ -48,6 +54,7 @@ namespace asst
|
||||
// 每次到结算界面,汇报掉落数据至企鹅物流 https://penguin-stats.cn/
|
||||
DepotExportTemplate depot_export_template; // 仓库识别结果导出模板
|
||||
RequestInfo yituliu_report; // 一图流大数据汇报:目前只有公招功能,https://yituliu.site/maarecruitdata
|
||||
DebugConf debug;
|
||||
};
|
||||
|
||||
struct AdbCfg
|
||||
|
||||
@@ -151,14 +151,70 @@ void asst::AbstractTask::click_return_button()
|
||||
ProcessTask(*this, { "Return" }).run();
|
||||
}
|
||||
|
||||
bool asst::AbstractTask::save_img(const std::filesystem::path& relative_dir)
|
||||
bool asst::AbstractTask::save_img(const std::filesystem::path& relative_dir, bool auto_clean)
|
||||
{
|
||||
auto image = ctrler()->get_image();
|
||||
if (image.empty()) {
|
||||
return false;
|
||||
}
|
||||
std::string stem = utils::get_time_filestem();
|
||||
|
||||
if (auto_clean) {
|
||||
// 第1次或每执行 debug.clean_files_freq(100) 次后执行清理
|
||||
// 限制文件数量 debug.max_debug_file_num
|
||||
if (m_save_file_cnt[relative_dir] == 0) {
|
||||
filenum_ctrl(relative_dir, Config.get_options().debug.max_debug_file_num);
|
||||
m_save_file_cnt[relative_dir] = 0;
|
||||
}
|
||||
m_save_file_cnt[relative_dir] =
|
||||
(m_save_file_cnt[relative_dir] + 1) % Config.get_options().debug.clean_files_freq;
|
||||
}
|
||||
|
||||
auto relative_path = relative_dir / (stem + "_raw.png");
|
||||
Log.trace("Save image", relative_path);
|
||||
return asst::imwrite(relative_path, image);
|
||||
}
|
||||
}
|
||||
|
||||
size_t asst::AbstractTask::filenum_ctrl(const std::filesystem::path& relative_dir, size_t max_files)
|
||||
{
|
||||
std::filesystem::path absolute_path;
|
||||
if (relative_dir.is_relative()) [[likely]] {
|
||||
const auto& user_dir = UserDir.get();
|
||||
absolute_path = user_dir / relative_dir;
|
||||
}
|
||||
else {
|
||||
absolute_path = relative_dir;
|
||||
}
|
||||
if (!std::filesystem::exists(absolute_path)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t file_nums = 0;
|
||||
std::vector<std::pair<std::filesystem::file_time_type, std::filesystem::path>> filepaths;
|
||||
std::filesystem::directory_iterator iter(absolute_path);
|
||||
for (auto& file : iter) {
|
||||
if (file.is_regular_file()) {
|
||||
++file_nums;
|
||||
filepaths.emplace_back(last_write_time(file.path()), file.path());
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(filepaths.begin(), filepaths.end(),
|
||||
[](const std::pair<std::filesystem::file_time_type, std::filesystem::path>& a,
|
||||
const std::pair<std::filesystem::file_time_type, std::filesystem::path>& b) {
|
||||
if (a.first != b.first) return a.first < b.first;
|
||||
return a.second < b.second;
|
||||
});
|
||||
|
||||
long long to_del = file_nums - max_files;
|
||||
size_t deleted = 0;
|
||||
|
||||
for (int i = 0; i < to_del; ++i) {
|
||||
auto path = filepaths[i].second;
|
||||
if (std::filesystem::remove(path)) {
|
||||
deleted++;
|
||||
}
|
||||
}
|
||||
LogTrace << "Finish folder cleanup delete " << deleted << " files from " << absolute_path;
|
||||
return deleted;
|
||||
}
|
||||
|
||||
@@ -62,7 +62,8 @@ namespace asst
|
||||
virtual bool on_run_fails() { return true; }
|
||||
virtual void callback(AsstMsg msg, const json::value& detail);
|
||||
virtual void click_return_button();
|
||||
bool save_img(const std::filesystem::path& relative_dir = utils::path("debug"));
|
||||
bool save_img(const std::filesystem::path& relative_dir = utils::path("debug"), bool auto_clean = true);
|
||||
size_t filenum_ctrl(const std::filesystem::path& relative_dir, size_t max_files = 1000);
|
||||
|
||||
json::value basic_info_with_what(std::string what) const;
|
||||
|
||||
@@ -76,5 +77,6 @@ namespace asst
|
||||
mutable json::value m_basic_info_cache;
|
||||
int m_task_id = 0;
|
||||
std::set<TaskPluginPtr> m_plugins;
|
||||
std::map<std::filesystem::path, size_t> m_save_file_cnt;
|
||||
};
|
||||
} // namespace asst
|
||||
|
||||
Reference in New Issue
Block a user