ipc: Add ipc_print_result helper

This commit is contained in:
Jakob Bornecrantz 2023-11-13 17:23:00 +00:00
parent c188a69b14
commit 440b1f2660
2 changed files with 59 additions and 0 deletions

View file

@ -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)

View file

@ -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.