From 24cd8ca4ee1ea08526eb82e6c122870c86da5603 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 19 Nov 2024 21:20:13 -0700 Subject: Add libmisc/log.h to remove stdio.h --- libmisc/include/libmisc/log.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 libmisc/include/libmisc/log.h (limited to 'libmisc/include') diff --git a/libmisc/include/libmisc/log.h b/libmisc/include/libmisc/log.h new file mode 100644 index 0000000..eb9db3b --- /dev/null +++ b/libmisc/include/libmisc/log.h @@ -0,0 +1,31 @@ +/* libmisc/log.h - stdio logging + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBMISC_LOG_H_ +#define _LIBMISC_LOG_H_ + +#ifndef LOG_NAME + #error "each compilation unit that includes must define LOG_NAME" +#endif + +#ifdef NDEBUG + #define _LOG_NDEBUG 1 +#else + #define _LOG_NDEBUG 0 +#endif +#define _LOG_STR(x) #x +#define __LOG_CAT3(a, b, c) a ## b ## c +#define _LOG_CAT3(a, b, c) __LOG_CAT3(a, b, c) + +__attribute__((format(printf, 1, 2))) +int _log_printf(const char *format, ...); + +#define errorf(fmt, ...) do { _log_printf("error: " _LOG_STR(LOG_NAME) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0) +#define infof(fmt, ...) do { _log_printf("info : " _LOG_STR(LOG_NAME) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0) +#define debugf(fmt, ...) do { if (_LOG_CAT3(CONFIG_, LOG_NAME, _DEBUG) && !_LOG_NDEBUG) \ + _log_printf("debug: " _LOG_STR(LOG_NAME) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0) + +#endif /* _LIBMISC_LOG_H_ */ -- cgit v1.2.3-2-g168b