#ifndef TEEC_TRACE_H
#define TEEC_TRACE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#ifndef BINARY_PREFIX
#error "BINARY_PREFIX not defined"
#endif
#define TRACE_ERROR 1
#define TRACE_INFO 2
#define TRACE_DEBUG 3
#define TRACE_FLOW 4
#if defined(DEBUGLEVEL_0) && !defined(DEBUGLEVEL)
#define DEBUGLEVEL TRACE_ERROR
#endif
#if defined(DEBUGLEVEL_1) && !defined(DEBUGLEVEL)
#define DEBUGLEVEL TRACE_ERROR
#endif
#if defined(DEBUGLEVEL_2) && !defined(DEBUGLEVEL)
#define DEBUGLEVEL TRACE_INFO
#endif
#if defined(DEBUGLEVEL_3) && !defined(DEBUGLEVEL)
#define DEBUGLEVEL TRACE_DEBUG
#endif
#if defined(DEBUGLEVEL_4) && !defined(DEBUGLEVEL)
#define DEBUGLEVEL TRACE_FLOW
#endif
#ifndef DEBUGLEVEL
#define DEBUGLEVEL TRACE_INFO
#endif
#define __PRINTFLIKE(__fmt, __varargs) __attribute__\
((__format__(__printf__, __fmt, __varargs)))
extern void rust_dprintf(const char *function, int line, int level,
const char *prefix, const char *message);
static void _dprintf(const char *function, int line, int level,
const char *prefix, const char *fmt, ...) __PRINTFLIKE(5, 6);
static inline void _dprintf(const char *function, int line, int level,
const char *prefix, const char *fmt, ...)
{
char buf[4096] = {0};
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
rust_dprintf(function, line, level, prefix, buf);
}
#define dprintf(level, x...) do { \
if ((level) <= DEBUGLEVEL) { \
_dprintf(__func__, __LINE__, level, \
BINARY_PREFIX, x); \
} \
} while (0)
#define EMSG(fmt, ...) dprintf(TRACE_ERROR, fmt "\n", ##__VA_ARGS__)
#define IMSG(fmt, ...) dprintf(TRACE_INFO, fmt "\n", ##__VA_ARGS__)
#define DMSG(fmt, ...) dprintf(TRACE_DEBUG, fmt "\n", ##__VA_ARGS__)
#define FMSG(fmt, ...) dprintf(TRACE_FLOW, fmt "\n", ##__VA_ARGS__)
#define INMSG(fmt, ...) FMSG("> " fmt, ##__VA_ARGS__)
#define OUTMSG(fmt, ...) FMSG("< " fmt, ##__VA_ARGS__)
#define OUTRMSG(r) \
do { \
if (r) \
EMSG("Function returns with [%d]", r); \
OUTMSG("r=[%d]", r); \
return r; \
} while (0)
#define dprintf_raw(level, x...) do { \
if ((level) <= DEBUGLEVEL) \
_dprintf(0, 0, (level), BINARY_PREFIX, x); \
} while (0)
#define EMSG_RAW(fmt, ...) dprintf_raw(TRACE_ERROR, fmt, ##__VA_ARGS__)
#define IMSG_RAW(fmt, ...) dprintf_raw(TRACE_INFO, fmt, ##__VA_ARGS__)
#define DMSG_RAW(fmt, ...) dprintf_raw(TRACE_DEBUG, fmt, ##__VA_ARGS__)
#define FMSG_RAW(fmt, ...) dprintf_raw(TRACE_FLOW, fmt, ##__VA_ARGS__)
void dump_buffer(const char *bname, const uint8_t *buffer, size_t blen);
#ifdef __cplusplus
}
#endif
#endif