diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-19 21:20:13 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-11-19 22:05:53 -0700 |
commit | 24cd8ca4ee1ea08526eb82e6c122870c86da5603 (patch) | |
tree | 25e467df0236b8a514bbc328299f8500277f9512 /libmisc/include | |
parent | 1cc68fb6f72ada5446a9914cc3bf97db9259a880 (diff) |
Add libmisc/log.h to remove stdio.h
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_ */ |