st/oxr: Add oxr_sink_logger, the slog-inator!

This commit is contained in:
Jakob Bornecrantz 2019-09-05 23:15:25 +01:00
parent 81213f119c
commit 2db3df9027
2 changed files with 126 additions and 1 deletions

View file

@ -11,6 +11,7 @@
#include <stdarg.h>
#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;
}

View file

@ -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);
/*!
* @}
*/