diff options
Diffstat (limited to 'libmisc/include')
-rw-r--r-- | libmisc/include/libmisc/log.h | 31 |
1 files changed, 31 insertions, 0 deletions
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 <lukeshu@lukeshu.com> + * 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 <libmisc/log.h> 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_ */ |