aboutsummaryrefslogtreecommitdiff
path: root/example/example_debug.h
blob: dd3aa7f3d27119fe66fa8b6911be1234cc5a6a6c (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Copyright (c) 2014, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */
/**
 * @file
 *
 * example debug
 */

#ifndef EXAMPLE_DEBUG_H_
#define EXAMPLE_DEBUG_H_

#include <stdio.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef EXAMPLE_DEBUG_PRINT
#define EXAMPLE_DEBUG_PRINT 1
#endif

/**
 * log level.
 */
typedef enum example_log_level {
	EXAMPLE_LOG_DBG,
	EXAMPLE_LOG_ERR,
	EXAMPLE_LOG_ABORT
} example_log_level_e;

/**
 * default LOG macro.
 */
#define EXAMPLE_LOG(level, fmt, ...) \
do { \
	switch (level) { \
	case EXAMPLE_LOG_ERR: \
		fprintf(stderr, "%s:%d:%s():" fmt, __FILE__, \
		__LINE__, __func__, ##__VA_ARGS__); \
		break; \
	case EXAMPLE_LOG_DBG: \
		if (EXAMPLE_DEBUG_PRINT == 1) \
			fprintf(stderr, "%s:%d:%s():" fmt, __FILE__, \
			__LINE__, __func__, ##__VA_ARGS__); \
		break; \
	case EXAMPLE_LOG_ABORT: \
		fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, \
		__LINE__, __func__, ##__VA_ARGS__); \
		abort(); \
		break; \
	default: \
		fprintf(stderr, "Unknown LOG level"); \
		break;\
	} \
} while (0)

/**
 * Debug printing macro, which prints output when DEBUG flag is set.
 */
#define EXAMPLE_DBG(fmt, ...) \
		EXAMPLE_LOG(EXAMPLE_LOG_DBG, fmt, ##__VA_ARGS__)

/**
 * Print output to stderr (file, line and function).
 */
#define EXAMPLE_ERR(fmt, ...) \
		EXAMPLE_LOG(EXAMPLE_LOG_ERR, fmt, ##__VA_ARGS__)

/**
 * Print output to stderr (file, line and function),
 * then abort.
 */
#define EXAMPLE_ABORT(fmt, ...) \
		EXAMPLE_LOG(EXAMPLE_LOG_ABORT, fmt, ##__VA_ARGS__)

/**
 * Intentionally unused variables to functions
 */
#define EXAMPLE_UNUSED     __attribute__((__unused__))

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#endif