u/logging: Implement optional color logging.

Check if stderr is a tty.
This commit is contained in:
Lubosz Sarnecki 2020-12-18 11:17:48 +01:00 committed by Jakob Bornecrantz
parent 9ce9fa9238
commit 8bdff9a61a
5 changed files with 66 additions and 1 deletions

View file

@ -115,6 +115,8 @@ endif()
# This one is named differently because that's what CTest uses
option(BUILD_TESTING "Enable building of the test suite?" ON)
option(XRT_FEATURE_COLOR_LOG "Enable logging in color on supported platforms" ON)
cmake_dependent_option(CMAKE_INTERPROCEDURAL_OPTIMIZATION "Enable inter-procedural (link-time) optimization" OFF "HAS_IPO" OFF)
cmake_dependent_option(XRT_HAVE_WAYLAND "Enable Wayland support" ON "WAYLAND_FOUND AND WAYLAND_SCANNER_FOUND AND WAYLAND_PROTOCOLS_FOUND" OFF)
cmake_dependent_option(XRT_HAVE_XLIB "Enable xlib support" ON "X11_FOUND" OFF)

View file

@ -114,3 +114,8 @@ option('steamvr_plugin',
description: 'Enable SteamVR Plugin'
)
option('color_log',
type: 'boolean',
value: true,
description: 'Log in color'
)

View file

@ -9,6 +9,7 @@
#include "util/u_logging.h"
#include "xrt/xrt_config_os.h"
#include "xrt/xrt_config_build.h"
#include <assert.h>
#include <stdio.h>
@ -149,14 +150,51 @@ u_log_xdev(const char *file,
#else
#include <unistd.h>
/*
*
* Helper functions.
*
*/
#ifdef XRT_FEATURE_COLOR_LOG
#define COLOR_TRACE "\033[2m"
#define COLOR_DEBUG "\033[36m"
#define COLOR_INFO "\033[32m"
#define COLOR_WARN "\033[33m"
#define COLOR_ERROR "\033[31m"
#define COLOR_RESET "\033[0m"
static void
print_prefix(const char *func, enum u_logging_level level)
print_prefix_color(const char *func, enum u_logging_level level)
{
switch (level) {
case U_LOGGING_TRACE:
fprintf(stderr, COLOR_TRACE "TRACE " COLOR_RESET);
break;
case U_LOGGING_DEBUG:
fprintf(stderr, COLOR_DEBUG "DEBUG " COLOR_RESET);
break;
case U_LOGGING_INFO:
fprintf(stderr, COLOR_INFO " INFO " COLOR_RESET);
break;
case U_LOGGING_WARN:
fprintf(stderr, COLOR_WARN " WARN " COLOR_RESET);
break;
case U_LOGGING_ERROR:
fprintf(stderr, COLOR_ERROR "ERROR " COLOR_RESET);
break;
case U_LOGGING_RAW: break;
default: break;
}
}
#endif
static void
print_prefix_mono(const char *func, enum u_logging_level level)
{
switch (level) {
case U_LOGGING_TRACE: fprintf(stderr, "TRACE "); break;
@ -167,6 +205,20 @@ print_prefix(const char *func, enum u_logging_level level)
case U_LOGGING_RAW: break;
default: break;
}
}
static void
print_prefix(const char *func, enum u_logging_level level)
{
#ifdef XRT_FEATURE_COLOR_LOG
if (isatty(STDERR_FILENO)) {
print_prefix_color(func, level);
} else {
print_prefix_mono(func, level);
}
#else
print_prefix_mono(func, level);
#endif
if (level != U_LOGGING_RAW && func != NULL) {
fprintf(stderr, "[%s] ", func);

View file

@ -121,6 +121,10 @@ if get_option('service')
build_conf.set('XRT_FEATURE_SERVICE', true)
endif
if get_option('color_log')
build_conf.set('XRT_FEATURE_COLOR_LOG', true)
endif
xrt_config_build_h = configure_file(
output: 'xrt_config_build.h',
configuration: build_conf,

View file

@ -24,3 +24,5 @@
#cmakedefine XRT_FEATURE_OPENXR_LAYER_EQUIRECT2
#cmakedefine XRT_FEATURE_OPENXR_LAYER_EQUIRECT1
#cmakedefine XRT_FEATURE_COLOR_LOG