Slightly improve logging on Windows

The characters won't display properly if the system codepage isn't UTF-8, but
at least it shouldn't cut the line off prematurely.
master
Chris Robinson 2021-11-21 21:21:46 -08:00
parent 834800c817
commit 3ef4bffaf9
2 changed files with 7 additions and 66 deletions

View File

@ -35,10 +35,9 @@ void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
va_end(args2);
va_end(args);
std::wstring wstr{utf8_to_wstr(str)};
if(gLogLevel >= level)
{
fputws(wstr.c_str(), logfile);
fputs(str, logfile);
fflush(logfile);
}
/* OutputDebugStringW has no 'level' property to distinguish between
@ -46,6 +45,7 @@ void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
* non-Release builds.
*/
#ifndef NDEBUG
std::wstring wstr{utf8_to_wstr(str)};
OutputDebugStringW(wstr.c_str());
#endif
}

View File

@ -46,70 +46,6 @@
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static WCHAR *FromUTF8(const char *str)
{
WCHAR *out = NULL;
int len;
if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
{
out = calloc(sizeof(WCHAR), (unsigned int)(len));
MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
}
return out;
}
/* Override printf, fprintf, and fwrite so we can print UTF-8 strings. */
static void al_fprintf(FILE *file, const char *fmt, ...)
{
char str[1024];
WCHAR *wstr;
va_list ap;
va_start(ap, fmt);
vsnprintf(str, sizeof(str), fmt, ap);
va_end(ap);
str[sizeof(str)-1] = 0;
wstr = FromUTF8(str);
if(!wstr)
fprintf(file, "<UTF-8 error> %s", str);
else
fprintf(file, "%ls", wstr);
free(wstr);
}
#define fprintf al_fprintf
#define printf(...) al_fprintf(stdout, __VA_ARGS__)
static size_t al_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *file)
{
char str[1024];
WCHAR *wstr;
size_t len;
len = size * nmemb;
if(len > sizeof(str)-1)
len = sizeof(str)-1;
memcpy(str, ptr, len);
str[len] = 0;
wstr = FromUTF8(str);
if(!wstr)
fprintf(file, "<UTF-8 error> %s", str);
else
fprintf(file, "%ls", wstr);
free(wstr);
return len / size;
}
#define fwrite al_fwrite
#endif
#define MAX_WIDTH 80
static void printList(const char *list, char separator)
@ -384,6 +320,11 @@ int main(int argc, char *argv[])
ALCdevice *device;
ALCcontext *context;
#ifdef _WIN32
/* OpenAL Soft gives UTF-8 strings, so set the console to expect that. */
SetConsoleOutputCP(CP_UTF8);
#endif
if(argc > 1 && (strcmp(argv[1], "--help") == 0 ||
strcmp(argv[1], "-h") == 0))
{