feat: 整理日志,缓存等目录结构

This commit is contained in:
MistEO
2023-01-13 14:37:31 +08:00
parent 3d7e6f0eeb
commit 95f4b5f54c
15 changed files with 86 additions and 57 deletions

View File

@@ -46,7 +46,8 @@ bool inited()
AsstBool AsstSetUserDir(const char* path)
{
return asst::UserDir.set(path);
auto os_path = asst::utils::path(path);
return asst::UserDir.set(os_path);
}
AsstBool AsstLoadResource(const char* path)

View File

@@ -8,6 +8,8 @@ bool asst::AvatarCacheManager::load(const std::filesystem::path& path)
{
LogTraceFunction;
m_path = path;
if (!std::filesystem::exists(path)) {
return true;
}
@@ -33,7 +35,6 @@ bool asst::AvatarCacheManager::load(const std::filesystem::path& path)
m_avatars[role].emplace(name, std::move(avatar));
}
m_path = path;
return true;
}
@@ -61,7 +62,6 @@ void asst::AvatarCacheManager::set_avatar(const std::string& name, battle::Role
return;
}
std::filesystem::create_directories(m_path);
auto path = m_path / utils::path(name + CacheExtension);
Log.info(path);

View File

@@ -151,17 +151,15 @@ void asst::AbstractTask::click_return_button()
ProcessTask(*this, { "Return" }).run();
}
bool asst::AbstractTask::save_img(const std::string& dirname)
bool asst::AbstractTask::save_img(const std::filesystem::path& relative_dir)
{
auto image = ctrler()->get_image();
if (image.empty()) {
return false;
}
std::string stem = utils::get_format_time();
stem = utils::string_replace_all(stem, { { ":", "-" }, { " ", "_" } });
std::filesystem::create_directories(dirname);
std::string full_path = dirname + stem + "_raw.png";
Log.trace("Save image", full_path);
return asst::imwrite(full_path, image);
auto relative_path = relative_dir / (stem + "_raw.png");
Log.trace("Save image", relative_path);
return asst::imwrite(relative_path, image);
}

View File

@@ -7,6 +7,7 @@
#include "Common/AsstMsg.h"
#include "InstHelper.h"
#include "Utils/Platform.hpp"
namespace cv
{
@@ -61,7 +62,7 @@ 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::string& dirname = "debug/");
bool save_img(const std::filesystem::path& relative_dir = utils::path("debug"));
json::value basic_info_with_what(std::string what) const;

View File

@@ -470,11 +470,9 @@ void asst::BattleHelper::save_map(const cv::Mat& image)
LogTraceFunction;
using namespace asst::utils::path_literals;
const auto& MapDir = "map"_p;
const auto& MapRelativeDir = "debug"_p / "map"_p;
std::filesystem::create_directories(MapDir);
auto draw = image.clone();
for (const auto& [loc, info] : m_normal_tile_info) {
std::string text = "( " + std::to_string(loc.x) + ", " + std::to_string(loc.y) + " )";
cv::putText(draw, text, cv::Point(info.pos.x - 30, info.pos.y), 1, 1.2, cv::Scalar(0, 0, 255), 2);
@@ -484,7 +482,7 @@ void asst::BattleHelper::save_map(const cv::Mat& image)
if (++m_camera_count > 1) {
suffix = "-" + std::to_string(m_camera_count);
}
asst::imwrite(MapDir / asst::utils::path(m_stage_name + suffix + ".png"), draw);
asst::imwrite(MapRelativeDir / asst::utils::path(m_stage_name + suffix + ".png"), draw);
}
bool asst::BattleHelper::click_oper_on_deployment(const std::string& name)

View File

@@ -36,6 +36,6 @@ bool asst::RoguelikeDebugTaskPlugin::verify(AsstMsg msg, const json::value& deta
bool asst::RoguelikeDebugTaskPlugin::_run()
{
save_img("debug/roguelike/");
save_img(utils::path("debug") / utils::path("roguelike"));
return true;
}

View File

@@ -37,6 +37,7 @@ namespace asst
else {
absolute_path = path;
}
std::filesystem::create_directories(absolute_path.parent_path());
std::ofstream of(absolute_path, std::ios::out | std::ios::binary);
std::vector<uint8_t> encoded;
auto ext = asst::utils::path_to_utf8_string(absolute_path.extension());

View File

@@ -32,21 +32,17 @@ namespace asst
// is_reference_wrapper
template <typename T>
struct is_reference_wrapper: std::false_type
{
};
struct is_reference_wrapper : std::false_type
{};
template <typename T>
struct is_reference_wrapper<std::reference_wrapper<T>>: std::true_type
{
};
struct is_reference_wrapper<std::reference_wrapper<T>> : std::true_type
{};
template <typename T>
struct is_reference_wrapper<std::reference_wrapper<T>&>: std::true_type
{
};
struct is_reference_wrapper<std::reference_wrapper<T>&> : std::true_type
{};
template <typename T>
struct is_reference_wrapper<std::reference_wrapper<T>&&>: std::true_type
{
};
struct is_reference_wrapper<std::reference_wrapper<T>&&> : std::true_type
{};
template <typename T>
inline constexpr bool is_reference_wrapper_v = is_reference_wrapper<T>::value;
@@ -127,12 +123,12 @@ namespace asst
toansi_ostream& operator=(toansi_ostream&&) = default;
toansi_ostream& operator=(const toansi_ostream&) = default;
toansi_ostream(std::ostream& stream): m_ofs(stream) {}
toansi_ostream(std::ostream& stream) : m_ofs(stream) {}
toansi_ostream(std::reference_wrapper<std::ostream> stream): m_ofs(stream) {}
toansi_ostream(std::reference_wrapper<std::ostream> stream) : m_ofs(stream) {}
template <typename T>
requires has_stream_insertion_operator<std::ostream, T>
requires has_stream_insertion_operator<std::ostream, T>
toansi_ostream& operator<<(T&& v)
{
if constexpr (std::convertible_to<T, std::string_view>) {
@@ -162,10 +158,10 @@ namespace asst
ostreams& operator=(ostreams&&) = default;
ostreams& operator=(const ostreams&) = default;
ostreams(Args&&... args): m_ofss(std::forward<Args>(args)...) {}
ostreams(Args&&... args) : m_ofss(std::forward<Args>(args)...) {}
template <typename T>
requires has_stream_insertion_operator<std::ostream, T>
requires has_stream_insertion_operator<std::ostream, T>
ostreams& operator<<(T&& x)
{
streams_put(m_ofss, x, std::index_sequence_for<Args...> {});
@@ -192,7 +188,7 @@ namespace asst
template <typename... Args>
ostreams(Args&&...) -> ostreams<to_reference_wrapper_t<Args>...>;
class Logger: public SingletonHolder<Logger>
class Logger : public SingletonHolder<Logger>
{
public:
struct separator
@@ -200,7 +196,7 @@ namespace asst
constexpr separator() = default;
constexpr separator(const separator&) = default;
constexpr separator(separator&&) noexcept = default;
constexpr explicit separator(std::string_view s) noexcept: str(s) {}
constexpr explicit separator(std::string_view s) noexcept : str(s) {}
constexpr separator& operator=(const separator&) = default;
constexpr separator& operator=(separator&&) noexcept = default;
constexpr separator& operator=(std::string_view s) noexcept
@@ -222,7 +218,7 @@ namespace asst
{
constexpr level(const level&) = default;
constexpr level(level&&) noexcept = default;
constexpr explicit level(std::string_view s) noexcept: str(s) {}
constexpr explicit level(std::string_view s) noexcept : str(s) {}
constexpr level& operator=(const level&) = default;
constexpr level& operator=(level&&) noexcept = default;
constexpr level& operator=(std::string_view s) noexcept
@@ -261,7 +257,7 @@ namespace asst
}
template <typename _stream_t = stream_t>
LogStream(std::mutex& mtx, _stream_t&& ofs, Logger::level lv): m_trace_lock(mtx), m_ofs(ofs)
LogStream(std::mutex& mtx, _stream_t&& ofs, Logger::level lv) : m_trace_lock(mtx), m_ofs(ofs)
{
*this << lv;
}
@@ -313,7 +309,7 @@ namespace asst
}
else if constexpr (ranges::input_range<T>) {
s << "[";
std::string_view comma_space{};
std::string_view comma_space {};
for (const auto& elem : std::forward<T>(v)) {
s << comma_space;
stream_put(s, elem);
@@ -370,14 +366,14 @@ namespace asst
}
if constexpr (std::same_as<level, remove_cvref_t<T>>) {
#ifdef ASST_DEBUG
return LogStream(m_trace_mutex, ostreams{ toansi_ostream(std::cout), m_ofs }, arg);
return LogStream(m_trace_mutex, ostreams { toansi_ostream(std::cout), m_ofs }, arg);
#else
return LogStream(m_trace_mutex, m_ofs, arg);
#endif
}
else {
#ifdef ASST_DEBUG
return LogStream(m_trace_mutex, ostreams{ toansi_ostream(std::cout), m_ofs }, level::trace, arg);
return LogStream(m_trace_mutex, ostreams { toansi_ostream(std::cout), m_ofs }, level::trace, arg);
#else
return LogStream(m_trace_mutex, m_ofs, level::trace, arg);
#endif
@@ -428,7 +424,7 @@ namespace asst
private:
friend class SingletonHolder<Logger>;
Logger(): m_directory(UserDir.get())
Logger() : m_directory(UserDir.get())
{
check_filesize_and_remove();
log_init_info();
@@ -444,6 +440,21 @@ namespace asst
std::filesystem::rename(m_log_path, m_log_bak_path);
}
}
// FIXME: 迁移以前文件路径,之后版本删了这段
{
using namespace asst::utils::path_literals;
if (std::filesystem::exists(m_directory / "asst.log")) {
std::filesystem::remove(m_directory / "asst.log");
}
if (std::filesystem::exists(m_directory / "asst.bak.log")) {
std::filesystem::remove(m_directory / "asst.bak.log");
}
const auto& MapDir = UserDir.get() / "debug"_p / "map"_p;
const auto& OldMapDir = "map"_p;
if (std::filesystem::exists(OldMapDir)) {
std::filesystem::rename(OldMapDir, MapDir);
}
}
}
catch (...) {
}
@@ -460,8 +471,8 @@ namespace asst
std::filesystem::path m_directory;
std::filesystem::path m_log_path = m_directory / "asst.log";
std::filesystem::path m_log_bak_path = m_directory / "asst.bak.log";
std::filesystem::path m_log_path = m_directory / "debug" / "asst.log";
std::filesystem::path m_log_bak_path = m_directory / "debug" / "asst.bak.log";
std::mutex m_trace_mutex;
std::ofstream m_ofs;
};

View File

@@ -77,18 +77,17 @@ asst::Rect asst::AbstractImageAnalyzer::correct_rect(const Rect& rect, const cv:
return res;
}
bool asst::AbstractImageAnalyzer::save_img(const std::string& dirname)
bool asst::AbstractImageAnalyzer::save_img(const std::filesystem::path& relative_dir)
{
std::string stem = utils::get_format_time();
stem = utils::string_replace_all(stem, { { ":", "-" }, { " ", "_" } });
std::filesystem::create_directories(dirname);
std::string full_path = dirname + stem + "_raw.png";
Log.trace("Save image", full_path);
bool ret = asst::imwrite(full_path, m_image);
auto relative_path = relative_dir / (stem + "_raw.png");
Log.trace("Save image", relative_path);
bool ret = asst::imwrite(relative_path, m_image);
#ifdef ASST_DEBUG
asst::imwrite(dirname + stem + "_draw.png", m_image_draw);
asst::imwrite(relative_dir / (stem + "_draw.png"), m_image_draw);
#endif
return ret;
}
}

View File

@@ -3,6 +3,7 @@
#include "Common/AsstTypes.h"
#include "InstHelper.h"
#include "Utils/NoWarningCVMat.h"
#include "Utils/Platform.hpp"
// #ifndef ASST_DEBUG
// #define ASST_DEBUG
@@ -32,7 +33,7 @@ namespace asst
AbstractImageAnalyzer& operator=(const AbstractImageAnalyzer&) = delete;
AbstractImageAnalyzer& operator=(AbstractImageAnalyzer&&) = delete;
bool save_img(const std::string& dirname = "debug/");
bool save_img(const std::filesystem::path& relative_dir = utils::path("debug"));
protected:
using InstHelper::status;

View File

@@ -30,7 +30,7 @@ bool asst::DepotImageAnalyzer::analyze()
#ifdef ASST_DEBUG
m_image_draw = m_image_draw_resized;
#endif
save_img("debug/depot/");
save_img(utils::path("debug") / utils::path("depot"));
return ret;
}

View File

@@ -27,7 +27,7 @@ bool asst::StageDropsImageAnalyzer::analyze()
#ifndef ASST_DEBUG
if (!ret)
#endif // ! ASST_DEBUG
save_img("debug/drops/");
save_img(utils::path("debug") / utils::path("drops"));
return ret;
}

View File

@@ -28,7 +28,7 @@ namespace MaaWpfGui
Log(function + " Error: " + message);
}
private static readonly string Filename = "gui.log";
private static readonly string Filename = "debug\\gui.log";
private static readonly object _lock = new object();
private static void Log(string message)

View File

@@ -24,8 +24,8 @@ namespace MaaWpfGui
/// </summary>
public class ViewStatusStorage
{
private static readonly string _configFilename = Environment.CurrentDirectory + "\\gui.json";
private static readonly string _configBakFilename = Environment.CurrentDirectory + "\\gui.json.bak";
private static readonly string _configFilename = Environment.CurrentDirectory + "\\config\\gui.json";
private static readonly string _configBakFilename = Environment.CurrentDirectory + "\\config\\gui.json.bak";
private static JObject _viewStatus = new JObject();
/// <summary>
@@ -64,6 +64,18 @@ namespace MaaWpfGui
/// <returns>Whether the operation is successful.</returns>
public static bool Load(bool withRestore = true)
{
// 2023-1-13 配置文件迁移
// FIXME: 之后的版本删了这段
Directory.CreateDirectory("config");
if (File.Exists("gui.json"))
{
File.Move("gui.json", _configFilename);
}
File.Delete("gui.json.bak");
File.Delete("gui.json.bak1");
File.Delete("gui.json.bak2");
if (File.Exists(_configFilename))
{
try

View File

@@ -60,10 +60,17 @@ namespace MaaWpfGui
protected override void OnStart()
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
Directory.CreateDirectory("debug");
{
// FIXME: 目录迁移,过几个版本删除这段
File.Delete("gui.log");
File.Delete("gui.bak.log");
}
// 清理日志文件
var log_path = "gui.log";
var backup_log_path = "gui.bak.log";
var log_path = "debug\\gui.log";
var backup_log_path = "debug\\gui.bak.log";
if (File.Exists(log_path))
{
var fileInfo = new FileInfo(log_path);