1 #ifndef STRACE_XSTRING_H
2 #define STRACE_XSTRING_H
7 #include "error_prints.h"
8 #include "gcc_compat.h"
11 * Print to static buffer and die on (really unexpected) errors and overflows.
12 * Shouldn't be used directly; please refer to helper macros xsnprintf and
15 * @param str String buffer to print into.
16 * @param size Size of the string buffer in bytes.
17 * @param func Function name from which this function is called.
18 * @param argstr Stringified arguments (including format argument).
19 * @param format Format string.
20 * @param ... Format arguments.
21 * @return Number of characters printed, excluding terminating null byte
22 * (the same as s(n)printf).
24 static inline int ATTRIBUTE_FORMAT((printf, 5, 6))
25 xsnprintf_(char *str, size_t size, const char *func, const char *argstr,
26 const char *format, ...)
32 ret = vsnprintf(str, size, format, ap);
35 if (ret < 0 || (unsigned int) ret >= size)
36 error_msg_and_die("%s: got unexpected return value %d for "
37 "snprintf(buf, %zu, %s)",
38 func, ret, size, argstr);
44 * snprintf that dies on (really unexpected) errors and overflows.
46 * @param str_ String buffer to print into.
47 * @param size_ Size of the string buffer in bytes.
48 * @param fmt_ Format string.
49 * @param ... Format arguments.
51 #define xsnprintf(str_, size_, fmt_, ...) \
52 xsnprintf_((str_), (size_), __func__, #fmt_ ", " #__VA_ARGS__, \
56 * Print to a character array buffer and die on (really unexpected) errors and
57 * overflows. Buffer size is obtained with sizeof().
59 * @param str_ Character array buffer to print into.
60 * @param fmt_ Format string.
61 * @param ... Format arguments.
63 #define xsprintf(str_, fmt_, ...) \
64 xsnprintf((str_), sizeof(str_) + MUST_BE_ARRAY(str_), (fmt_), \
67 #endif /* !STRACE_XSTRING_H */