Output修正

This commit is contained in:
2026-09-03 19:02:01 +08:00
parent a45313be08
commit 719b201a43
2 changed files with 38 additions and 23 deletions
+36 -22
View File
@@ -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;
+2 -1
View File
@@ -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;