-1

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?

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
sculida
  • 11
  • 3
  • `L"汉字"` is UTF16 in Windows, and UTF32 in POSIX. ANSI uses `char` but it is outdated, specially for Chinese. Show more details in your question about what you are trying to do. You should probably keep using UTF8 for the file. – Barmak Shemirani May 24 '18 at 16:53

1 Answers1

1

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.

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27