/* libmisc/tests/test_log.c - Tests for * * Copyright (C) 2024 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ #define _GNU_SOURCE /* for vasprintf() */ #include /* for va_list */ #include /* for vasprintf() */ #include /* for free() */ #include /* for strcmp() */ #define LOG_NAME FROBNICATE #include #include #include "test.h" /* Intercept output ***********************************************************/ static char *log_output = NULL; int __lm_printf(const char *format, ...) { va_list va; va_start(va, format); int ret = vasprintf(&log_output, format, va); va_end(va); return ret; } /* Actual tests ***************************************************************/ #define should_print(_exp, cmd) do { \ char *exp = _exp; \ test_assert(!log_output); \ cmd; \ if (!exp) \ test_assert(!log_output); \ else { \ test_assert(log_output); \ if (strcmp(log_output, exp)) { \ printf("exp = \"%s\"\n" \ "act = \"%s\"\n", \ exp, log_output); \ test_assert(0); \ } \ } \ if (log_output) { \ free(log_output); \ log_output = NULL; \ } \ } while (0) int main() { should_print("error: FROBNICATE: val=42\n", errorf("val=%d", 42)); should_print("info : FROBNICATE: val=0\n", infof("val=%d", 0)); #ifndef NDEBUG #define CONFIG_FROBNICATE_DEBUG 1 should_print("debug: FROBNICATE: val=-2\n", debugf("val=%d", -2)); #undef CONFIG_FROBNICATE_DEBUG #define CONFIG_FROBNICATE_DEBUG 0 should_print(NULL, debugf("val=%d", -2)); #endif return 0; }