summaryrefslogtreecommitdiff
path: root/libmisc/include/libmisc/log.h
blob: b4f546169952be339ee10528124eb6a32cd00862 (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
32
33
34
35
36
37
38
39
/* 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_

#include <stdint.h> /* for uint8_t */

#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 n_errorf(nam, fmt, ...) do { _log_printf("error: " _LOG_STR(nam) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0)
#define n_infof(nam, fmt, ...)  do { _log_printf("info : " _LOG_STR(nam) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0)
#define n_debugf(nam, fmt, ...) do { if (_LOG_CAT3(CONFIG_, nam, _DEBUG) && !_LOG_NDEBUG) \
                                     _log_printf("debug: " _LOG_STR(nam) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0)

#define errorf(fmt, ...) n_errorf(LOG_NAME, fmt, __VA_ARGS__)
#define infof(fmt, ...)  n_infof(LOG_NAME, fmt, __VA_ARGS__)
#define debugf(fmt, ...) n_debugf(LOG_NAME, fmt, __VA_ARGS__)

const char *const_byte_str(uint8_t b);

#endif /* _LIBMISC_LOG_H_ */