0
class SeverityLevelFormatter {
private:
    mutable boost::wformat format_;

public:
    SeverityLevelFormatter(const std::wstring& format) : format_(format) {
    }

    void operator() (logging::wformatting_ostream& strm, const logging::value_ref<logging::trivial::severity_level>& value) const {
        strm << boost::wformat(format_) % to_string(value.get());
    }
};

class ServerityFormatterFactory : public logging::basic_formatter_factory<wchar_t, logging::trivial::severity_level> {
public:
    formatter_type create_formatter(const logging::attribute_name& name, const args_map& args) {
        auto it = args.find(L"format");
        if (it != args.end()) {
            return boost::phoenix::bind(SeverityLevelFormatter(it->second), expr::stream, expr::attr<logging::trivial::severity_level>(name));
        }
        else {
            return expr::stream << expr::attr<logging::trivial::severity_level>(name);
        }
    }
};


logging::register_formatter_factory(logging::aux::default_attribute_names::severity().string(), boost::make_shared<ServerityFormatterFactory>());

register_formatter_factory seems doesn't work in this case. However it works if I use the 'char' based ServerityFormatterFactory.

I find it is probably because the text_file_backend defined in 'boost/log/sinks/text_file_backend.hpp' doesn't have 'wchar_t' version.

user1633272
  • 2,007
  • 5
  • 25
  • 48

1 Answers1

0

To answer your question in the title, text file sinks in Boost.Log don't have wchar_t-based implementations (as well as char16_t and char32_t-based ones) because they don't deal with character encodings. Regardless of the internal character type your application uses, the sinks always write bytes to the log files. It is the locale's job to perform conversion from internal strings to bytes (i.e. chars).

The locale can be configured on per-sink basis by calling imbue() on the sink. It can also be set globally by calling std::locale::global() before initializing Boost.Log. The locale will be used in the formatting stream that is supplied to the formatter you set. Effectively, this means the formatter will perform any necessary character code conversion, when needed.

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