我正在开发一个国际插件,但遇到了 UTF-16 字符串的问题。我正在尝试使用 FB::FactoryBase::GetLoggingMethods 将日志存储到 %APPDATA%。目前它是这样定义的:
void getLoggingMethods(FB::Log::LogMethodList& outMethods) {
try {
boost::filesystem::path appDataPath = FB::System::getLocalAppDataPath("AppName");
boost::filesystem::path logDirPath = appDataPath / "Logs";
if(!exists(logDirPath)) {
// create the directory
boost::filesystem::create_directories(logDirPath);
}
if (exists(logDirPath) && is_directory(logDirPath)) {
boost::filesystem::path logPath = logDirPath / "app_name.log";
outMethods.push_back(std::make_pair(FB::Log::LogMethod_File, logPath.string()));
}
} catch(...) { /* safely fail here */ }
}
当用户的用户名中包含 UTF-16 字符时,就会出现此问题。这会引发异常。
但是,outMethods.push_back
需要一个std::string
for 输入。转换 fromstd::wstring
会std::string
丢失 UTF-16 字符(这会使路径无效。)
有任何想法吗?
编辑:通过为 boost::filesystem::path 为其构造函数提供一个 LPCWSTR 来设法使其工作。
boost::filesystem::path appDataPath =
FB::utf8_to_wstring(FB::System::getLocalAppDataPath("AppName")).c_str();
boost::filesystem::path logDirPath = appDataPath / "Logs";
if(!exists(logDirPath)) {
// create the directory
boost::filesystem::create_directories(logDirPath);
}
if (exists(logDirPath) && is_directory(logDirPath)) {
boost::filesystem::path logPath = logDirPath / "app_name.log";
outMethods.push_back(
std::make_pair(
FB::Log::LogMethod_File, FB::wstring_to_utf8(logPath.wstring())
)
);
}