diff --git a/src/MaaCore/Assistant.cpp b/src/MaaCore/Assistant.cpp index 458cc993dd..901014964b 100644 --- a/src/MaaCore/Assistant.cpp +++ b/src/MaaCore/Assistant.cpp @@ -417,7 +417,7 @@ void Assistant::working_proc() finished_tasks.clear(); m_thread_idle = true; m_running = false; - Log.rotate_check(); + Log.flush(); m_condvar.wait(lock); continue; } diff --git a/src/MaaCore/Task/Roguelike/RoguelikeResetTaskPlugin.cpp b/src/MaaCore/Task/Roguelike/RoguelikeResetTaskPlugin.cpp index 6359763046..f083ab4060 100644 --- a/src/MaaCore/Task/Roguelike/RoguelikeResetTaskPlugin.cpp +++ b/src/MaaCore/Task/Roguelike/RoguelikeResetTaskPlugin.cpp @@ -28,7 +28,6 @@ bool asst::RoguelikeResetTaskPlugin::verify(AsstMsg msg, const json::value& deta bool asst::RoguelikeResetTaskPlugin::_run() { - Log.rotate_check(); for (const auto& plugin : m_task_ptr->get_plugins()) { if (auto ptr = std::dynamic_pointer_cast(plugin)) { ptr->reset_in_run_variables(); diff --git a/src/MaaCore/Utils/Logger.hpp b/src/MaaCore/Utils/Logger.hpp index 70c8df9eef..d0a5ff75a0 100644 --- a/src/MaaCore/Utils/Logger.hpp +++ b/src/MaaCore/Utils/Logger.hpp @@ -557,13 +557,7 @@ public: LogStream(std::unique_lock&&, stream_t&&, Args&&...) -> LogStream; public: - virtual ~Logger() override - { - std::unique_lock m_trace_lock(m_trace_mutex); - if (m_ofs.is_open()) { - m_ofs.close(); - } - } + virtual ~Logger() override { flush(false); } // static bool set_directory(const std::filesystem::path& dir) // { @@ -679,13 +673,15 @@ public: << ... << std::forward(args)); } - void rotate_check() + void flush(bool rorate_log_file = true) { - if (!m_ofs || !m_ofs.is_open() || m_ofs.tellp() <= MaxLogSize) { - return; + std::unique_lock m_trace_lock(m_trace_mutex); + if (m_ofs.is_open()) { + m_ofs.close(); + } + if (rorate_log_file) { + rotate(); } - std::unique_lock trace_lock(m_trace_mutex); - rotate(); } private: @@ -695,18 +691,20 @@ private: m_directory(UserDir.get()) { std::filesystem::create_directories(m_log_path.parent_path()); + rotate(); log_init_info(); } - void rotate() + void rotate() const { + constexpr uintmax_t MaxLogSize = 4ULL * 1024 * 1024; try { - if (!std::filesystem::exists(m_log_path) || !std::filesystem::is_regular_file(m_log_path)) { - return; + if (std::filesystem::exists(m_log_path) && std::filesystem::is_regular_file(m_log_path)) { + const uintmax_t log_size = std::filesystem::file_size(m_log_path); + if (log_size >= MaxLogSize) { + std::filesystem::rename(m_log_path, m_log_bak_path); + } } - m_ofs.close(); - std::filesystem::rename(m_log_path, m_log_bak_path); - m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app); } catch (std::filesystem::filesystem_error& e) { std::cerr << e.what() << std::endl; @@ -733,7 +731,6 @@ private: std::filesystem::path m_log_bak_path = m_directory / "debug" / "asst.bak.log"; std::mutex m_trace_mutex; std::ofstream m_ofs; - const long long MaxLogSize = 64LL * 1024 * 1024; }; inline constexpr Logger::separator Logger::separator::none;