From 2db3df902789483a8ea8ee4e250409b116ccdf36 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Thu, 5 Sep 2019 23:15:25 +0100 Subject: [PATCH] st/oxr: Add oxr_sink_logger, the slog-inator! --- src/xrt/state_trackers/oxr/oxr_logger.cpp | 76 ++++++++++++++++++++++- src/xrt/state_trackers/oxr/oxr_logger.h | 51 +++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/src/xrt/state_trackers/oxr/oxr_logger.cpp b/src/xrt/state_trackers/oxr/oxr_logger.cpp index 25d8c647e..b0d9b11fd 100644 --- a/src/xrt/state_trackers/oxr/oxr_logger.cpp +++ b/src/xrt/state_trackers/oxr/oxr_logger.cpp @@ -11,6 +11,7 @@ #include #include "xrt/xrt_compiler.h" +#include "util/u_misc.h" #include "util/u_debug.h" #include "oxr_objects.h" @@ -25,7 +26,6 @@ DEBUG_GET_ONCE_BOOL_OPTION(break_on_error, "OXR_BREAK_ON_ERROR", false) static const char * oxr_result_to_string(XrResult result); - void oxr_log_init(struct oxr_logger *logger, const char *api_func_name) { @@ -119,3 +119,77 @@ oxr_result_to_string(XrResult result) } // clang-format on } + + +/* + * + * Sink logger. + * + */ + +static void +oxr_slog_ensure(struct oxr_sink_logger *slog, size_t extra) +{ + while (slog->store_size < extra + slog->length) { + slog->store_size += 1024; + } + + slog->store = (char *)realloc(slog->store, slog->store_size); +} + +extern "C" void +oxr_slog(struct oxr_sink_logger *slog, const char *fmt, ...) +{ + va_list args; + + int ret = 0; + + va_start(args, fmt); + ret = vsnprintf(NULL, 0, fmt, args); + va_end(args); + + if (ret <= 0) { + return; + } + + oxr_slog_ensure(slog, ret + 1); + + va_start(args, fmt); + ret = vsnprintf(slog->store + slog->length, + slog->store_size - slog->length, fmt, args); + va_end(args); + + slog->length += ret; +} + +extern "C" void +oxr_slog_free(struct oxr_sink_logger *slog) +{ + free(slog->store); + slog->length = 0; + slog->store_size = 0; +} + +extern "C" void +oxr_log_slog(struct oxr_logger *log, struct oxr_sink_logger *slog) +{ + oxr_log(log, "%s", slog->store); + oxr_slog_free(slog); +} + +extern "C" void +oxr_warn_slog(struct oxr_logger *log, struct oxr_sink_logger *slog) +{ + oxr_warn(log, "%s", slog->store); + oxr_slog_free(slog); +} + +extern "C" XrResult +oxr_error_slog(struct oxr_logger *log, + XrResult res, + struct oxr_sink_logger *slog) +{ + oxr_error(log, res, "%s", slog->store); + oxr_slog_free(slog); + return res; +} diff --git a/src/xrt/state_trackers/oxr/oxr_logger.h b/src/xrt/state_trackers/oxr/oxr_logger.h index 4e768a0df..5d2d4a3d5 100644 --- a/src/xrt/state_trackers/oxr/oxr_logger.h +++ b/src/xrt/state_trackers/oxr/oxr_logger.h @@ -73,6 +73,57 @@ XrResult oxr_error(struct oxr_logger *logger, XrResult result, const char *fmt, ...) XRT_PRINTF_FORMAT(3, 4); + + +/* + * + * Sink logger. + * + */ + +/*! + * Allocate on the stack, make sure to zero initialize. + */ +struct oxr_sink_logger +{ + char *store; + size_t store_size; + size_t length; +}; + +/*! + * Log string to sink logger. + */ +void +oxr_slog(struct oxr_sink_logger *slog, const char *fmt, ...); + +/*! + * Abort logging, frees all internal data. + */ +void +oxr_slog_abort(struct oxr_sink_logger *slog); + +/*! + * Flush sink as a log message, frees all internal data. + */ +void +oxr_log_slog(struct oxr_logger *log, struct oxr_sink_logger *slog); + +/*! + * Flush sink as a warning message, frees all internal data. + */ +void +oxr_warn_slog(struct oxr_logger *log, struct oxr_sink_logger *slog); + +/*! + * Flush sink as a error message, frees all internal data. + */ +XrResult +oxr_error_slog(struct oxr_logger *log, + XrResult res, + struct oxr_sink_logger *slog); + + /*! * @} */