我正在开发一个国际插件,我遇到了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
。从std::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())
)
);
}