Add wide character formatting and output support to wincolor_sink. (#3092)
Fixes printing of unicode characters to the windows console such as microsecond suffix for std::chrono types.
This commit is contained in:
		@@ -108,9 +108,11 @@ endif()
 | 
				
			|||||||
if(WIN32)
 | 
					if(WIN32)
 | 
				
			||||||
    option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF)
 | 
					    option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF)
 | 
				
			||||||
    option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF)
 | 
					    option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF)
 | 
				
			||||||
 | 
					    option(SPDLOG_WCHAR_CONSOLE "Support wchar output to console" OFF)
 | 
				
			||||||
else()
 | 
					else()
 | 
				
			||||||
    set(SPDLOG_WCHAR_SUPPORT OFF CACHE BOOL "non supported option" FORCE)
 | 
					    set(SPDLOG_WCHAR_SUPPORT OFF CACHE BOOL "non supported option" FORCE)
 | 
				
			||||||
    set(SPDLOG_WCHAR_FILENAMES OFF CACHE BOOL "non supported option" FORCE)
 | 
					    set(SPDLOG_WCHAR_FILENAMES OFF CACHE BOOL "non supported option" FORCE)
 | 
				
			||||||
 | 
					    set(SPDLOG_WCHAR_CONSOLE OFF CACHE BOOL "non supported option" FORCE)
 | 
				
			||||||
endif()
 | 
					endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
 | 
					if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
 | 
				
			||||||
@@ -237,9 +239,11 @@ endif()
 | 
				
			|||||||
# Misc definitions according to tweak options
 | 
					# Misc definitions according to tweak options
 | 
				
			||||||
# ---------------------------------------------------------------------------------------
 | 
					# ---------------------------------------------------------------------------------------
 | 
				
			||||||
set(SPDLOG_WCHAR_TO_UTF8_SUPPORT ${SPDLOG_WCHAR_SUPPORT})
 | 
					set(SPDLOG_WCHAR_TO_UTF8_SUPPORT ${SPDLOG_WCHAR_SUPPORT})
 | 
				
			||||||
 | 
					set(SPDLOG_UTF8_TO_WCHAR_CONSOLE ${SPDLOG_WCHAR_CONSOLE})
 | 
				
			||||||
foreach(
 | 
					foreach(
 | 
				
			||||||
    SPDLOG_OPTION
 | 
					    SPDLOG_OPTION
 | 
				
			||||||
    SPDLOG_WCHAR_TO_UTF8_SUPPORT
 | 
					    SPDLOG_WCHAR_TO_UTF8_SUPPORT
 | 
				
			||||||
 | 
					    SPDLOG_UTF8_TO_WCHAR_CONSOLE
 | 
				
			||||||
    SPDLOG_WCHAR_FILENAMES
 | 
					    SPDLOG_WCHAR_FILENAMES
 | 
				
			||||||
    SPDLOG_NO_EXCEPTIONS
 | 
					    SPDLOG_NO_EXCEPTIONS
 | 
				
			||||||
    SPDLOG_CLOCK_COARSE
 | 
					    SPDLOG_CLOCK_COARSE
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -134,9 +134,18 @@ void SPDLOG_INLINE wincolor_sink<ConsoleMutex>::print_range_(const memory_buf_t
 | 
				
			|||||||
                                                             size_t start,
 | 
					                                                             size_t start,
 | 
				
			||||||
                                                             size_t end) {
 | 
					                                                             size_t end) {
 | 
				
			||||||
    if (end > start) {
 | 
					    if (end > start) {
 | 
				
			||||||
 | 
					#if defined(SPDLOG_UTF8_TO_WCHAR_CONSOLE)
 | 
				
			||||||
 | 
					        wmemory_buf_t wformatted;
 | 
				
			||||||
 | 
					        details::os::utf8_to_wstrbuf(string_view_t(formatted.data() + start, end - start),
 | 
				
			||||||
 | 
					            wformatted);
 | 
				
			||||||
 | 
					        auto size = static_cast<DWORD>(wformatted.size());
 | 
				
			||||||
 | 
					        auto ignored = ::WriteConsoleW(static_cast<HANDLE>(out_handle_), wformatted.data(), size,
 | 
				
			||||||
 | 
					            nullptr, nullptr);
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
        auto size = static_cast<DWORD>(end - start);
 | 
					        auto size = static_cast<DWORD>(end - start);
 | 
				
			||||||
        auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start,
 | 
					        auto ignored = ::WriteConsoleA(static_cast<HANDLE>(out_handle_), formatted.data() + start,
 | 
				
			||||||
                                       size, nullptr, nullptr);
 | 
					                                       size, nullptr, nullptr);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
        (void)(ignored);
 | 
					        (void)(ignored);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user