Commit 2aceb13f authored by gabime's avatar gabime

fix support for utf8 logging under win32

parent e9f34fbd
...@@ -14,5 +14,5 @@ spdlog::logger *get_logger(); ...@@ -14,5 +14,5 @@ spdlog::logger *get_logger();
int main(int, char *[]) int main(int, char *[])
{ {
auto *l = get_logger(); auto *l = get_logger();
l->info("HE LO ", "GA"); l->info(L"HEllo {}", L"HGFS");
} }
\ No newline at end of file
...@@ -79,6 +79,11 @@ bool is_color_terminal() SPDLOG_NOEXCEPT; ...@@ -79,6 +79,11 @@ bool is_color_terminal() SPDLOG_NOEXCEPT;
// Detrmine if the terminal attached // Detrmine if the terminal attached
// Source: https://github.com/agauniyal/rang/ // Source: https://github.com/agauniyal/rang/
bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) && defined(_WIN32)
void wbuf_to_utf8buf(const fmt::wmemory_buffer &wbuf, fmt::memory_buffer &target);
#endif
} // namespace os } // namespace os
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog
......
This diff is collapsed.
...@@ -172,4 +172,4 @@ void spdlog::logger::err_handler_(const std::string &msg) ...@@ -172,4 +172,4 @@ void spdlog::logger::err_handler_(const std::string &msg)
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time); std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg); fmt::print(stderr, "[*** LOG ERROR ***] [{}] [{}] {}\n", date_buf, name(), msg);
} }
} }
\ No newline at end of file
...@@ -397,6 +397,30 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT ...@@ -397,6 +397,30 @@ SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
return isatty(fileno(file)) != 0; return isatty(fileno(file)) != 0;
#endif #endif
} }
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) && defined(_WIN32)
SPDLOG_INLINE void wbuf_to_utf8buf(const fmt::wmemory_buffer &wbuf, fmt::memory_buffer &target)
{
int wbuf_size = static_cast<int>(wbuf.size());
if (wbuf_size == 0)
{
return;
}
auto result_size = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), wbuf_size, NULL, 0, NULL, NULL);
if (result_size > 0)
{
target.resize(result_size);
::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), wbuf_size, &target.data()[0], result_size, NULL, NULL);
}
else
{
throw spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
}
}
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT) && _WIN32
} // namespace os } // namespace os
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment