summaryrefslogtreecommitdiff
path: root/libmisc/include/libmisc/log.h
blob: 121b0e14e5df6a916895d4595f377076ff830cff (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
/* 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 */

#include <libmisc/macro.h>
#include <libmisc/_intercept.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 n_errorf(nam, fmt, ...) do { __lm_printf("error: " LM_STR_(nam) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0)
#define n_infof(nam, fmt, ...)  do { __lm_printf("info : " LM_STR_(nam) ": " fmt "\n" __VA_OPT__(,) __VA_ARGS__); } while (0)
#define n_debugf(nam, fmt, ...) do { if (LM_CAT3_(CONFIG_, nam, _DEBUG) && !_LOG_NDEBUG) \
                                     __lm_printf("debug: " LM_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_ */