From e3b8203187235dc4add8cbc08c220eac049fbffd Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 8 Dec 2019 14:43:30 -0800 Subject: [PATCH] Implement logger_t::log_fmt for narrow chars by trampolining to wide chars This satifies the glibc. --- src/flog.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/flog.cpp b/src/flog.cpp index f46f22f2a..477a295dd 100644 --- a/src/flog.cpp +++ b/src/flog.cpp @@ -56,13 +56,28 @@ void logger_t::log_fmt(const category_t &cat, const wchar_t *fmt, ...) { } void logger_t::log_fmt(const category_t &cat, const char *fmt, ...) { + // glibc dislikes mixing wide and narrow output functions. + // So construct a narrow string in-place and output that via wide functions. va_list va; va_start(va, fmt); - log1(cat.name); - log1(": "); - std::vfprintf(file_, fmt, va); - log1('\n'); + int ret = vsnprintf(nullptr, 0, fmt, va); va_end(va); + + if (ret < 0) { + perror("vsnprintf"); + return; + } + size_t len = static_cast(ret) + 1; + std::unique_ptr buff(new char[len]); + + va_start(va, fmt); + ret = vsnprintf(buff.get(), len, fmt, va); + va_end(va); + if (ret < 0) { + perror("vsnprintf"); + return; + } + log_fmt(cat, L"%s", buff.get()); } } // namespace flog_details