Fix log text encoding (UTF-16 w/o BOM -> UTF-8 w/o BOM)

This commit is contained in:
zeffy
2018-03-26 02:36:47 -07:00
parent 0523eb1b7b
commit c2069e3c69

View File

@@ -104,6 +104,8 @@ void log_trace_(const wchar_t *const format, ...)
wchar_t *buf1;
int ret;
wchar_t *buf2;
int size;
char *buf3;
DWORD written;
bStatus = InitOnceExecuteOnce(&InitOnce,
@@ -135,11 +137,24 @@ void log_trace_(const wchar_t *const format, ...)
buf2 = calloc(count + 1, sizeof *buf2);
if ( !buf2 ) goto free_buf1;
ret = swprintf_s(buf2, count + 1, fmt, datebuf, timebuf, data.pszExeName, data.dwProcessId, buf1);
if ( ret == -1 ) goto free_buf2;
count = swprintf_s(buf2, count + 1, fmt, datebuf, timebuf, data.pszExeName, data.dwProcessId, buf1);
if ( count == -1 ) goto free_buf2;
if ( !bStatus || !WriteFile(m_hFile, buf2, count * (sizeof *buf2), &written, NULL) )
if ( bStatus ) {
size = WideCharToMultiByte(CP_UTF8, 0, buf2, count, NULL, 0, NULL, NULL);
if ( !size ) goto fallback;
buf3 = malloc(size);
if ( !buf3 ) goto fallback;
ret = WideCharToMultiByte(CP_UTF8, 0, buf2, count, buf3, size, NULL, NULL)
&& WriteFile(m_hFile, buf3, size, &written, NULL);
free(buf3);
if ( !ret ) goto fallback;
} else {
fallback:
OutputDebugStringW(buf2);
}
free_buf2:
free(buf2);
free_buf1: