25#if __cplusplus < 201103L && !defined(va_copy)
26# define va_copy(...) __va_copy(__VA_ARGS__)
45#if __cplusplus < 201103L
47#elif __cplusplus == 201103L
49#elif __cplusplus == 201402L
51#elif __cplusplus == 201703L
53#elif __cplusplus == 202002L
55#elif __cplusplus == 202302L
60#if !ZTH_HAVE_EXCEPTIONS
69#ifdef ZTH_OS_BAREMETAL
73 " newlib" _NEWLIB_VERSION
86# ifdef __ARM_ARCH_PROFILE
87# if __ARM_ARCH_PROFILE == 'A'
89# elif __ARM_ARCH_PROFILE == 'R'
91# elif __ARM_ARCH_PROFILE == 'M'
93# elif __ARM_ARCH_PROFILE == 'S'
101#ifdef ZTH_ARM_USE_PSP
113#ifdef ZTH_CONTEXT_UCONTEXT
116#ifdef ZTH_CONTEXT_SIGALTSTACK
119#ifdef ZTH_CONTEXT_SJLJ
122#ifdef ZTH_CONTEXT_WINFIBER
128#ifdef ZTH_ENABLE_ASAN
131#ifdef ZTH_ENABLE_LSAN
134#ifdef ZTH_ENABLE_UBSAN
145void abort(
char const* fmt, ...) noexcept
157void abortv(
char const* fmt, va_list args)
noexcept
159 static bool recurse =
false;
173#ifndef ZTH_OS_BAREMETAL
174static void log_init()
176# ifdef ZTH_OS_WINDOWS
182 (void)setvbuf(stdout,
nullptr, _IOLBF, 4096);
183 (void)setvbuf(stderr,
nullptr, _IOLBF, 4096);
184# ifdef ZTH_OS_WINDOWS
197 static int support = 0;
208 AttachConsole(ATTACH_PARENT_PROCESS);
211 if(!GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode))
213 if(!SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), mode | 0x0004))
217 if(GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE), &mode))
218 SetConsoleMode(GetStdHandle(STD_ERROR_HANDLE), mode | 0x0004);
238 static bool do_color =
241 if(do_color && color > 0)
242 log(
"\x1b[%d%sm", (color % 8) + 30, color >= 8 ?
";1" :
"");
246 if(do_color && color > 0)
253#if __cplusplus >= 201703L
259# if defined(__cpp_lib_string_resize_and_overwrite) \
260 && __cpp_lib_string_resize_and_overwrite >= 202110L
262 res.resize_and_overwrite(
263 res.capacity(), [](
char* ,
size_t size) ->
size_t { return size; });
266 res.resize(res.capacity());
270 va_copy(args2, args);
272 int c = vsnprintf(res.data(), res.size(), fmt, args2);
275 if(
unlikely((
size_t)c >= res.size())) {
277# if defined(__cpp_lib_string_resize_and_overwrite) \
278 && __cpp_lib_string_resize_and_overwrite >= 202110L
279 res.resize_and_overwrite(
280 (
size_t)c + 1U, [](
char* ,
size_t size) ->
size_t {
return size; });
282 res.resize((
size_t)c + 1U);
286 c = vsnprintf(res.data(), res.size(), fmt, args);
290 res.resize(c > 0 ? (
size_t)c : 0);
295string formatv(
char const* fmt, va_list args)
297 int const maxstack = (int)
sizeof(
void*) * 8;
300 char* hbuf =
nullptr;
301 size_t hbuf_size = 0;
305 va_copy(args2, args);
307 int c = vsnprintf(sbuf, maxstack, fmt, args2);
311 hbuf_size = (size_t)c + 1U;
312 hbuf = allocate_noexcept<char>(hbuf_size);
318 int c2 = vsprintf(hbuf, fmt, args);
325 string res(buf, (
size_t)(c < 0 ? 0 : c));
326 deallocate(hbuf, hbuf_size);
345#ifdef ZTH_OS_BAREMETAL
346__attribute__((weak))
int asprintf(
char** strp,
const char* fmt, ...)
350 int res = vasprintf(strp, fmt, args);
355__attribute__((weak))
int vasprintf(
char** strp,
const char* fmt, va_list ap)
359 int len = vsnprintf(
nullptr, 0, fmt, ap_dup);
362 if(strp && (*strp =
static_cast<char*
>(malloc(len + 1))))
363 vsnprintf(*strp, len + 1, fmt, ap);
void print(int color=-1) const
void zth_abort(char const *fmt,...)
Aborts the process after printing the given printf() formatted message.
#define zth_config(name)
Checks if the given zth::Config field is enabled.
void log_colorv(int color, char const *fmt, va_list args)
Logs a given printf()-like formatted string using an ANSI color code.
void abort(char const *fmt,...) noexcept
Aborts the process after printing the given printf() formatted message.
#define ZTH_VERSION
Zth version as string literal.
char const * banner() noexcept
Prints a banner line with version and configuration information.
void log(char const *fmt,...)
Logs a given printf()-like formatted string.
void logv(char const *fmt, va_list arg)
Logs a given printf()-like formatted string.
void abortv(char const *fmt, va_list args) noexcept
Aborts the process after printing the given printf() formatted message.
bool log_supports_ansi_colors() noexcept
Returns if the system supports ANSI colors.
string formatv(char const *fmt, va_list args)
Format like vsprintf(), but save the result in an zth::string.
static bool const EnableColorLog
Enable colored output.
#define zth_assert(expr)
assert(), but better integrated in Zth.
#define likely(expr)
Marks the given expression to likely be evaluated to true.
#define ZTH_STRINGIFY(x)
Converts the argument to a string literal.
#define unlikely(expr)
Marks the given expression to likely be evaluated to true.