From 440b1f2660415ca5edd15a8962551bcfde9f0ee8 Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Mon, 13 Nov 2023 17:23:00 +0000 Subject: [PATCH] ipc: Add ipc_print_result helper --- src/xrt/ipc/shared/ipc_utils.c | 34 ++++++++++++++++++++++++++++++++++ src/xrt/ipc/shared/ipc_utils.h | 25 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/xrt/ipc/shared/ipc_utils.c b/src/xrt/ipc/shared/ipc_utils.c index 6fdd5ae43..c370050e7 100644 --- a/src/xrt/ipc/shared/ipc_utils.c +++ b/src/xrt/ipc/shared/ipc_utils.c @@ -11,6 +11,9 @@ #include "ipc_utils.h" +#include "util/u_logging.h" +#include "util/u_pretty_print.h" + #ifdef XRT_OS_WINDOWS #include "util/u_windows.h" #endif @@ -22,6 +25,37 @@ * */ +void +ipc_print_result(enum u_logging_level cond_level, + const char *file, + int line, + const char *calling_fn, + xrt_result_t xret, + const char *called_fn) +{ + bool success = xret == XRT_SUCCESS; + enum u_logging_level level = success ? U_LOGGING_INFO : U_LOGGING_ERROR; + + // Should we be logging? + if (level < cond_level) { + return; + } + + struct u_pp_sink_stack_only sink; + u_pp_delegate_t dg = u_pp_sink_stack_only_init(&sink); + + if (success) { + u_pp(dg, "%s: ", called_fn); + } else { + u_pp(dg, "%s failed: ", called_fn); + } + + u_pp_xrt_result(dg, xret); + u_pp(dg, " [%s:%i]", file, line); + + u_log(file, line, calling_fn, level, "%s", sink.buffer); +} + #ifdef XRT_OS_WINDOWS const char * ipc_winerror(DWORD err) diff --git a/src/xrt/ipc/shared/ipc_utils.h b/src/xrt/ipc/shared/ipc_utils.h index e168f1de6..d792dafdb 100644 --- a/src/xrt/ipc/shared/ipc_utils.h +++ b/src/xrt/ipc/shared/ipc_utils.h @@ -12,6 +12,9 @@ #pragma once #include "xrt/xrt_config_os.h" +#include "xrt/xrt_results.h" + +#include "util/u_logging.h" #ifdef XRT_OS_WINDOWS #include "util/u_windows.h" @@ -29,6 +32,28 @@ extern "C" { * */ +/*! + * Helper to print the results of called functions that return xret results, if + * the result is @p XRT_SUCCESS will log with info, otherwise error. Will also + * check if logging should be done with @p cond_level. + * + * @param cond_level What the current logging level is. + * @param file Callee site (__FILE__). + * @param line Callee site (__LINE__). + * @param calling_fn Callee site (__func__). + * @param xret Result from the called function. + * @param called_fn Which function that this return is from. + * + * @ingroup ipc_shared + */ +void +ipc_print_result(enum u_logging_level cond_level, + const char *file, + int line, + const char *calling_func, + xrt_result_t xret, + const char *called_func); + #if defined(XRT_OS_WINDOWS) || defined(XRT_DOXYGEN) /*! * Helper to convert windows error codes to human readable strings for logging. -- 2.51.2