Repository: gabime/spdlog
File: include/spdlog/sinks/win_eventlog_sink.h (C/C++)

Code before:
    HANDLE event_log_handle()
    {
        if (!hEventLog_)
        {
            hEventLog_ = ::RegisterEventSourceA(nullptr, source_.c_str());
            if (!hEventLog_ || hEventLog_ == (HANDLE)ERROR_ACCESS_DENIED)
            {
                SPDLOG_THROW(internal::win32_error("RegisterEventSource"));
            }
        }

        return hEventLog_;
    }

protected:
    void sink_it_(const details::log_msg &msg) override
    {
        using namespace internal;

        bool succeeded;
        memory_buf_t formatted;
        base_sink<Mutex>::formatter_->format(msg, formatted);
        formatted.push_back('\0');

#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
        auto buf = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(formatted.data());
        LPCWSTR lp_wstr = reinterpret_cast<LPCWSTR>(buf.c_str());

        succeeded = ::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
                current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr);
#else
        LPCSTR lp_str = reinterpret_cast<LPCSTR>(formatted.data());

        succeeded = ::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
                current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr);
#endif

        if (!succeeded)
        {
            SPDLOG_THROW(win32_error("ReportEvent"));
        }
    }

    void flush_() override {}

public:
    win_eventlog_sink(std::string const &source, WORD event_id = 1000 /* according to mscoree.dll */)
        : source_(source)
        , event_id_(event_id)
    {
        try

Review comment (suggestion):
```std::wstring_convert<std::codecvt_utf8>``` is deprecated. Please use  ```details::os::wstr_to_utf8buf(..)``` instead. See usage example [here](https://github.com/gabime/spdlog/blob/9ce39a470fa7adc8b101fdd5e4325e6c371f675c/include/spdlog/logger.h#L243).

Code after:
        if (!hEventLog_)
        {
            hEventLog_ = ::RegisterEventSourceA(nullptr, source_.c_str());
            if (!hEventLog_ || hEventLog_ == (HANDLE)ERROR_ACCESS_DENIED)
            {
                SPDLOG_THROW(internal::win32_error("RegisterEventSource"));
            }
        }

        return hEventLog_;
    }

protected:
    void sink_it_(const details::log_msg &msg) override
    {
        using namespace internal;

        bool succeeded;
        memory_buf_t formatted;
        base_sink<Mutex>::formatter_->format(msg, formatted);
        formatted.push_back('\0');

#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
        try
        {
            memory_buf_t buf;
            details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf);

            LPCWSTR lp_wstr = reinterpret_cast<LPCWSTR>(buf.data());
            succeeded = ::ReportEventW(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
                current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr);

            if (!succeeded)
            {
                SPDLOG_THROW(win32_error("ReportEvent"));
            }
        }
        catch (...)
        {
            // WCHAR string conversion can fail and if it does, we shouldn't call to report event function.
        }
#else
        LPCSTR lp_str = reinterpret_cast<LPCSTR>(formatted.data());

        succeeded = ::ReportEventA(event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), event_id_,
                current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr);

        if (!succeeded)
        {
            SPDLOG_THROW(win32_error("ReportEvent"));
        }