summaryrefslogtreecommitdiff
path: root/libmisc/include/libmisc/log.h
blob: 37da20b3e99ed60686ec233c3e1e5472f6275414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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_STR(x) __LOG_STR(x)
#define __LOG_CAT3(a, b, c) a ## b ## c
#define _LOG_CAT3(a, b, c) __LOG_CAT3(a, b, c)

[[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_ */