How to write windows-936 to log When wchar_t in the code by boost.log

99 Views Asked by At

Follow the boost.log

, the sample.log was parsed as utf8. Now I want to write at windows.936, but the code below is useless.

void init_logging() {
    ...
    std::string strCodePage = boost::locale::util::get_system_locale();//strCodePage is "zh_CN.windows-936"
    std::locale loc = boost::locale::generator().generate(strCodePage);
    sink->imbue(loc);
    ...
}
void test_wide_char_logging() {
    ...
    const wchar_t national_chars[] = L"汉字";
    BOOST_LOG(lg) << national_chars;
    ...
}

For example, The "汉字" in the sample.log is 0xe6b189 0xe5ad97, which is parsed as utf8.But I want the "汉字" in the sample.log should be 0xbaba 0xd7d6, which is parsed as windows-936(the same as GBK, GB2312). Could you help me?

1

There are 1 best solutions below

0
On

Boost.Log relies on the locale you provide to perform character code conversion. So you either have to configure Boost.Locale properly or write the codecvt facet yourself. Specifically on Windows, Boost.Locale has to be configured with a backend other than WinAPI because according to the docs WinAPI backend does not support non-UTF8 multibyte encodings. The docs also contain a few examples of picking the backend, but you may need to build Boost.Locale with ICU, for example.