This repository was archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdbg.h
169 lines (148 loc) · 5.12 KB
/
dbg.h
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @file
* Debug and logging macros and functions
*/
/*
*******************************************************************
*
* Copyright 2016 Intel Corporation All rights reserved.
*
*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
#ifndef _DPS_DBG_H
#define _DPS_DBG_H
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @defgroup debug Debug
* Debug and logging macros and functions.
* @{
*/
/** Debug control value */
extern int DPS_Debug;
/** This can be redefined to include a custom tag in the log message */
#define DPS_DBG_TAG NULL
/**
* Debug logging levels
*/
typedef enum {
DPS_LOG_INFO,
DPS_LOG_ERROR,
DPS_LOG_WARNING,
DPS_LOG_PRINT,
DPS_LOG_PRINTT,
DPS_LOG_DBGTRACE,
DPS_LOG_DBGPRINT,
} DPS_LogLevel;
/**
* Log a message
*
* @param level the logging level
* @param file the file name of the message
* @param line the file line of the message
* @param function the function name of the message
* @param tag the custom tag of the message
* @param fmt the printf style format of the message
* @param ... the format parameters
*/
void DPS_Log(DPS_LogLevel level, const char* file, int line, const char *function, const char* tag, const char *fmt, ...);
/**
* Log an array of bytes
*
* @param level the logging level
* @param file the file name of the message
* @param line the file line of the message
* @param function the function name of the message
* @param bytes the array of bytes
* @param n the number of bytes in the array
*/
void DPS_LogBytes(DPS_LogLevel level, const char* file, int line, const char *function, const uint8_t *bytes, size_t n);
/**
* Log a message at ERROR level
*/
#define DPS_ERRPRINT(fmt, ...) DPS_Log(DPS_LOG_ERROR, __FILE__, __LINE__, __FUNCTION__, DPS_DBG_TAG, fmt, ##__VA_ARGS__)
/**
* Log a message at PRINT level
*/
#define DPS_PRINT(fmt, ...) DPS_Log(DPS_LOG_PRINT, __FILE__, __LINE__, __FUNCTION__, DPS_DBG_TAG, fmt, ##__VA_ARGS__)
/**
* Same as DPS_PRINT but prepends a system timestamp
*/
#define DPS_PRINTT(fmt, ...) DPS_Log(DPS_LOG_PRINTT, __FILE__, __LINE__, __FUNCTION__, DPS_DBG_TAG, fmt, ##__VA_ARGS__)
#define DPS_DEBUG_OFF 0 /**< Disable debug logging */
#define DPS_DEBUG_ON 1 /**< Enable debug logging */
#define DPS_DEBUG_FORCE 2 /**< Force debug logging */
#define DPS_DEBUG_INFO 3 /**< Enable only INFO messages */
/**
* True if DPS debug logging is enabled
*/
#define DPS_DEBUG_ENABLED() ((DPS_Debug && (__DPS_DebugControl == DPS_DEBUG_ON)) || (__DPS_DebugControl == DPS_DEBUG_FORCE))
/**
* True if DPS info logging is enabled
*/
#define DPS_INFO_ENABLED() ((__DPS_DebugControl == DPS_DEBUG_INFO) || DPS_DEBUG_ENABLED())
#ifdef DPS_DEBUG
/**
* Log a message at DBGINFO level - Like DPS_DBGPRINT but can be turned on in isolation with DPS_DEBUG_CONTROL(DPS_DEBUG_INFO)
*/
#define DPS_DBGINFO(fmt, ...) (DPS_INFO_ENABLED() ? DPS_Log(DPS_LOG_INFO, __FILE__, __LINE__, __FUNCTION__, DPS_DBG_TAG, fmt, ##__VA_ARGS__) : (void)0)
/**
* Log a function name at DBGTRACE level
*/
#define DPS_DBGTRACE() (DPS_DEBUG_ENABLED() ? DPS_Log(DPS_LOG_DBGTRACE, __FILE__, __LINE__, __FUNCTION__, DPS_DBG_TAG, "\n") : (void)0)
/**
* Log a function name and message at DBGTRACE level
*/
#define DPS_DBGTRACEA(fmt, ...) (DPS_DEBUG_ENABLED() ? DPS_Log(DPS_LOG_DBGTRACE, __FILE__, __LINE__, __FUNCTION__, DPS_DBG_TAG, fmt, ##__VA_ARGS__) : (void)0)
/**
* Log a message at DBGPRINT level
*/
#define DPS_DBGPRINT(fmt, ...) (DPS_DEBUG_ENABLED() ? DPS_Log(DPS_LOG_DBGPRINT, __FILE__, __LINE__, __FUNCTION__, DPS_DBG_TAG, fmt, ##__VA_ARGS__) : (void)0)
/**
* Log a message at WARNING level
*/
#define DPS_WARNPRINT(fmt, ...) (DPS_DEBUG_ENABLED() ? DPS_Log(DPS_LOG_WARNING, __FILE__, __LINE__, __FUNCTION__, DPS_DBG_TAG, fmt, ##__VA_ARGS__) : (void)0)
/**
* Log an array of bytes at DBGPRINT level
*/
#define DPS_DBGBYTES(bytes, n) (DPS_DEBUG_ENABLED() ? DPS_LogBytes(DPS_LOG_DBGPRINT, __FILE__, __LINE__, __FUNCTION__, bytes, n) : (void)0)
#else
#define DPS_DBGINFO(...)
#define DPS_DBGTRACE()
#define DPS_DBGTRACEA(...)
#define DPS_DBGPRINT(...)
#define DPS_WARNPRINT(...)
#define DPS_DBGBYTES(bytes, n)
#endif
/**
* Used at the top of a file to turn debugging on or off for that file
*/
#if defined(__GNUC__) || defined(__MINGW64__)
#define DPS_DEBUG_CONTROL(dbg) __attribute__((__unused__))static int __DPS_DebugControl = dbg
#elif defined(_WIN32)
#define DPS_DEBUG_CONTROL(dbg) static int __DPS_DebugControl = dbg
#endif
/** @} */
#ifdef __cplusplus
}
#endif
#endif