Search code examples
c++loggingpoco-libraries

How to log unicode character string with Poco::Logger?


I'm using POCO C++ libraries version 1.7.5 for loading a few records from database and logging them in file with POCO logger utility. One of the string records is in unicode format which I saved in std::wstring. I can not find how to log std::wstring with POCO logger.

Poco::Logger logger;
std::wstring gameName;
...
logger.information("GameName: %s", gameName.c_str());

The result is:

2017-04-27 11:47:28.438 - GameName: [ERRFMT]

How to log std::wstring properly?


Solution

  • Poco does not support wstring logging directly and Poco's format functions use type safe formatting.

    See format.

    However Poco has a UnicodeConverter class.

    So you can convert the std::wstring to std::string via Poco::UnicodeConverter::toUTF16 like:

    std::string str;
    Poco::UnicodeConverter::toUTF16(str, gameName);
    logger.information("GameName: %s", str);
    

    I hope this helps.