1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-22 17:49:43 +01:00

Add colors to logging

This includes the bold as well.

INFO is just default, WARN is yellow, ERROR is red.

We try to automatically detect if the output is a TTY (and thus supports
colors), and don't emit colors if so. Windows 10 supports ANSI color
codes starting with a specific build, but we don't care to emit whatever
garbage Microsoft invented for builds older than that.
This commit is contained in:
Misa 2021-02-23 15:22:28 -08:00 committed by Misa Elizabeth Kai
parent 96539f891c
commit ac85f57441
3 changed files with 62 additions and 3 deletions

View file

@ -1,12 +1,61 @@
#include <SDL_stdinc.h>
#include <stdarg.h>
#include <stdio.h>
#if defined(__unix__) || defined(__APPLE__)
#include <unistd.h>
#define STDOUT_IS_TTY isatty(STDOUT_FILENO)
#define STDERR_IS_TTY isatty(STDERR_FILENO)
#elif defined(_WIN32)
#include <io.h>
#include <windows.h>
#define STDOUT_IS_TTY _isatty(_fileno(stdout))
#define STDERR_IS_TTY _isatty(_fileno(stderr))
#else
#define STDOUT_IS_TTY 0
#define STDERR_IS_TTY 0
#endif
#define COLOR(EXPR) color_enabled ? EXPR : ""
#define Color_RESET COLOR("\x1b[0m")
#define Color_BOLD COLOR("\x1b[1m")
#define Color_BOLD_YELLOW COLOR("\x1b[1;33m")
#define Color_BOLD_RED COLOR("\x1b[1;31m")
static int color_enabled = 0;
void vlog_init(void)
{
#ifdef _WIN32
OSVERSIONINFO osvi;
SDL_zero(osvi);
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionExW(&osvi);
#endif
if (STDOUT_IS_TTY
&& STDERR_IS_TTY
#ifdef _WIN32
/* Windows supports ANSI escape sequences starting with Windows 10
* build 10586. We don't care to emit them on anything lower. */
&& osvi.dwMajorVersion >= 10
&& osvi.dwBuildNumber >= 10586
#endif
) {
color_enabled = 1;
}
}
int vlog_info(const char* text, ...)
{
va_list list;
int retval;
printf(Color_BOLD);
printf("[INFO]");
printf(Color_RESET);
printf(" ");
va_start(list, text);
retval = vprintf(text, list);
@ -22,7 +71,10 @@ int vlog_warn(const char* text, ...)
va_list list;
int retval;
fprintf(stderr, Color_BOLD_YELLOW);
fprintf(stderr, "[WARN]");
fprintf(stderr, Color_RESET);
fprintf(stderr, " ");
va_start(list, text);
retval = vfprintf(stderr, text, list);
@ -38,7 +90,10 @@ int vlog_error(const char* text, ...)
va_list list;
int retval;
fprintf(stderr, Color_BOLD_RED);
fprintf(stderr, "[ERROR]");
fprintf(stderr, Color_RESET);
fprintf(stderr, " ");
va_start(list, text);
retval = vfprintf(stderr, text, list);

View file

@ -6,6 +6,8 @@ extern "C"
{
#endif
void vlog_init(void);
int vlog_info(const char* text, ...);
int vlog_warn(const char* text, ...);

View file

@ -361,6 +361,8 @@ int main(int argc, char *argv[])
char* baseDir = NULL;
char* assetsPath = NULL;
vlog_init();
for (int i = 1; i < argc; ++i)
{
#define ARG(name) (SDL_strcmp(argv[i], name) == 0)