Skip to content

Commit 3011f69

Browse files
Windsland52MistEO
andauthored
fix: use _wfopen for non-ASCII log paths on Windows (#16)
* fix: use _wfopen for non-ASCII log paths on Windows When the log directory path contains non-ASCII characters (e.g. CJK), path.string() converts the internal wstring to narrow using the system active code page (ACP), not UTF-8. On Chinese Windows (ACP=936/GBK), this produces garbled bytes that cause fopen to throw an exception. Using _wfopen with the wide string path directly preserves the characters regardless of ACP setting. * fix: check _wfopen return value to avoid UB * fix(logger): report _wfopen failure instead of silent return * Update Logger.cpp --------- Co-authored-by: MistEO <mistereo@hotmail.com>
1 parent 72c94c9 commit 3011f69

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

source/Logger/Logger.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "MaaUtils/Logger.h"
44

5+
#include <cerrno>
6+
#include <iostream>
57
#include <thread>
68

79
#ifdef _WIN32
@@ -197,8 +199,10 @@ void Logger::open(bool append)
197199
#ifdef _WIN32
198200

199201
// https://stackoverflow.com/questions/55513974/controlling-inheritability-of-file-handles-created-by-c-stdfstream-in-window
200-
std::string str_log_path = log_path_.string();
201-
FILE* file_ptr = fopen(str_log_path.c_str(), append ? "a" : "w");
202+
FILE* file_ptr = _wfopen(log_path_.c_str(), append ? L"a" : L"w");
203+
if (!file_ptr) {
204+
return;
205+
}
202206
SetHandleInformation((HANDLE)_get_osfhandle(_fileno(file_ptr)), HANDLE_FLAG_INHERIT, 0);
203207
ofs_ = std::ofstream(file_ptr);
204208

0 commit comments

Comments
 (0)