From 719b201a43e9a082ebc19f8fe9d28cd10685cc06 Mon Sep 17 00:00:00 2001 From: Lveyou <1339484752@qq.com> Date: Thu, 3 Sep 2026 19:02:01 +0800 Subject: [PATCH] =?UTF-8?q?Output=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl/io/dl_log.cpp | 58 ++++++++++++++++++++++++++++++------------------ dl/io/dl_log.h | 3 ++- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/dl/io/dl_log.cpp b/dl/io/dl_log.cpp index 5711dea4..2f2c04c5 100644 --- a/dl/io/dl_log.cpp +++ b/dl/io/dl_log.cpp @@ -39,6 +39,33 @@ void log_std(std::string_view s) } #endif + +void LogFileOpen() +{ + // 打开日志文件 + std::string str_path = g_log->GetLogFile(); + if (!str_path.empty()) + { + g_log->GetMutex().lock(); + g_logOfs = File::Set(str_path, false, true); + if (!g_logOfs.is_open()) + { + log_easy(fmt::format("日志文件打开(写入)失败:{}", str_path)); + } + } +} + +void LogFileClose() +{ + // 关闭日志文件 + std::string str_path = g_log->GetLogFile(); + if (!str_path.empty()) + { + g_logOfs.close(); + g_log->GetMutex().unlock(); + } +} + void Output(std::string_view str) { log_std(str); @@ -47,8 +74,9 @@ void Output(std::string_view str) OutputDebugStringW(CodeCvt::Utf8ToWideChar(str).c_str()); #endif - if (g_logOfs.is_open()) - g_logOfs.write(str.data(), str.size()); + LogFileOpen(); + g_logOfs.write(str.data(), str.size()); + LogFileClose(); } #if (DL_PLATFORM == DL_PLATFORM_WIN32) @@ -233,28 +261,9 @@ void log_imp(std::string_view str, DL_SOURCE_LOCATION_IMP) #endif } - // 打开日志文件 - std::string str_path = g_log->GetLogFile(); - if (!str_path.empty()) - { - g_log->GetMutex().lock(); - g_logOfs = File::Set(str_path, false, true); - if (!g_logOfs.is_open()) - { - log_easy(fmt::format("日志文件打开(写入)失败:{}", str_path)); - } - } - Output(str_all); SetColorBlank(); - // 关闭日志文件 - if (!str_path.empty()) - { - g_logOfs.close(); - g_log->GetMutex().unlock(); - } - if (g_log->IsLevelMsgBox(setting._level)) { #if (DL_PLATFORM == DL_PLATFORM_WIN32) @@ -396,7 +405,7 @@ LogSystem::LogSystem() : #endif } -void LogSystem::SetLogFile(std::string_view path, std::string_view name /*= {}*/) +void LogSystem::SetLogFile(std::string_view path, std::string_view name /*= {}*/, bool clear) { _logFile = std::string{ path }; if (name.empty()) @@ -407,6 +416,11 @@ void LogSystem::SetLogFile(std::string_view path, std::string_view name /*= {}*/ else _logFile += name; File::CreateFolder(File::GetFilePath(_logFile)); + + if (clear) + { + File::Delete(_logFile); + } } std::string LogSystem::_logFile; diff --git a/dl/io/dl_log.h b/dl/io/dl_log.h index 30e9da8d..a756cb63 100644 --- a/dl/io/dl_log.h +++ b/dl/io/dl_log.h @@ -109,9 +109,10 @@ public: * @brief 设置日志文件路径(非线程安全) * @param[in] path 日志文件夹(为空时不生成) * @param[in] name 日志名(为空时自动取名) + * @param[in] clear 清空文件内容 * @note 允许在Engine::Init之前调用 */ - void SetLogFile(std::string_view path, std::string_view name = {}); + void SetLogFile(std::string_view path, std::string_view name = {}, bool clear = true); std::string GetLogFile() { return _logFile;