-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdebug.h
More file actions
151 lines (132 loc) · 6.48 KB
/
debug.h
File metadata and controls
151 lines (132 loc) · 6.48 KB
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
/*******************************************************************************
* This file is part of the Incubed project.
* Sources: https://github.com/blockchainsllc/in3
*
* Copyright (C) 2018-2020 slock.it GmbH, Blockchains LLC
*
*
* COMMERCIAL LICENSE USAGE
*
* Licensees holding a valid commercial license may use this file in accordance
* with the commercial license agreement provided with the Software or, alternatively,
* in accordance with the terms contained in a written agreement between you and
* slock.it GmbH/Blockchains LLC. For licensing terms and conditions or further
* information please contact slock.it at in3@slock.it.
*
* Alternatively, this file may be used under the AGPL license as follows:
*
* AGPL LICENSE USAGE
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Affero General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
* [Permissions of this strong copyleft license are conditioned on making available
* complete source code of licensed works and modifications, which include larger
* works using a licensed work, under the same license. Copyright and license notices
* must be preserved. Contributors provide an express grant of patent rights.]
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <https://www.gnu.org/licenses/>.
*******************************************************************************/
/** @file
* logs debug data only if the DEBUG-flag is set.
* */
#ifndef IN3_DEBUG_H
#define IN3_DEBUG_H
#include "params.h"
#include "stringbuilder.h"
#include <assert.h>
#include <stdbool.h>
#ifdef DEBUG
#define dbg_log(msg, ...) __dbg_log(0, __FILE__, __func__, __LINE__, msg, ##__VA_ARGS__)
#define dbg_log_raw(msg, ...) __dbg_log(1, __FILE__, __func__, __LINE__, msg, ##__VA_ARGS__)
void __dbg_log(int raw, char* file, const char* func, int line, char* fmt, ...);
#else
/** logs a debug-message including file and linenumber*/
#define dbg_log(msg, ...)
/** logs a debug-message without the filename */
#define dbg_log_raw(msg, ...)
#endif
#ifdef DBG_FNCTRACE
#define DBG_FNCTRACE_ENTER \
{ printk("ENTER %s::%s\n", __FILE__, __func__); }
#define DBG_FNCTRACE_LEAVE \
{ printk("LEAVE %s::%s\n", __FILE__, __func__); }
#endif // DBG_FNCTRACE
/** dumps the given data as hex coded bytes to stdout */
extern void msg_dump(const char* s, const unsigned char* data, unsigned len);
#if defined(ASSERTIONS) || defined(DEBUG)
#define _assert(exp) assert(exp)
#else
#define _assert(exp)
#endif
#define EXPECT(cond, exit) \
do { \
if (!(cond)) \
exit; \
} while (0)
#define EXPECT_CFG(cond, err) EXPECT(cond, { \
res = malloc(strlen(err) + 1); \
if (res) strcpy(res, err); \
goto cleanup; \
})
#define EXPECT_CFG_NCP_ERR(cond, err) EXPECT(cond, { res = err; goto cleanup; })
#define EXPECT_TOK(token, cond, err) EXPECT_CFG_NCP_ERR(cond, config_err(d_get_keystr(json, d_get_key(token)), err))
#define EXPECT_TOK_BOOL(token) EXPECT_TOK(token, d_type(token) == T_BOOLEAN, "expected boolean value")
#define EXPECT_TOK_STR(token) EXPECT_TOK(token, d_type(token) == T_STRING, "expected string value")
#define EXPECT_TOK_ARR(token) EXPECT_TOK(token, d_type(token) == T_ARRAY, "expected array")
#define EXPECT_TOK_OBJ(token) EXPECT_TOK(token, d_type(token) == T_OBJECT, "expected object")
#define EXPECT_TOK_ADDR(token) EXPECT_TOK(token, d_is_bytes(token) && d_bytes(token).len == 20, "expected address")
#define EXPECT_TOK_B256(token) EXPECT_TOK(token, d_is_bytes(token) && d_bytes(token).len == 32, "expected 256 bit data")
#define IS_D_UINT64(token) ((d_type(token) == T_INTEGER || (d_is_bytes(token) && d_len(token) <= 8)) && d_long(token) <= UINT64_MAX)
#define IS_D_UINT32(token) ((d_type(token) == T_INTEGER || d_is_bytes(token)) && d_long(token) <= UINT32_MAX)
#define IS_D_UINT16(token) (d_type(token) == T_INTEGER && d_int(token) >= 0 && d_int(token) <= UINT16_MAX)
#define IS_D_UINT8(token) (d_type(token) == T_INTEGER && d_int(token) >= 0 && d_int(token) <= UINT8_MAX)
#define EXPECT_TOK_U8(token) EXPECT_TOK(token, IS_D_UINT8(token), "expected uint8 value")
#define EXPECT_TOK_U16(token) EXPECT_TOK(token, IS_D_UINT16(token), "expected uint16 value")
#define EXPECT_TOK_U32(token) EXPECT_TOK(token, IS_D_UINT32(token), "expected uint32 value")
#define EXPECT_TOK_U64(token) EXPECT_TOK(token, IS_D_UINT64(token), "expected uint64 value")
#define EXPECT_TOK_KEY_HEXSTR(token) EXPECT_TOK(token, is_hex_str(d_get_keystr(json, d_get_key(token))), "expected hex str")
static inline char* config_err(const char* keyname, const char* err) {
const char* k = keyname ? keyname : "";
char* s = _malloc(strlen(k) + strlen(err) + 4);
sprintf(s, "%s: %s!", k, err);
return s;
}
static inline bool is_hex_str(const char* str) {
if (!str) return false;
if (str[0] == '0' && str[1] == 'x')
str += 2;
return str[strspn(str, "0123456789abcdefABCDEF")] == 0;
}
static inline void add_prop(sb_t* sb, char prefix, const char* property) {
sb_add_char(sb, prefix);
sb_add_char(sb, '"');
sb_add_chars(sb, property);
sb_add_chars(sb, "\":");
}
static inline void add_bool(sb_t* sb, char prefix, const char* property, bool value) {
add_prop(sb, prefix, property);
sb_add_chars(sb, value ? "true" : "false");
}
static inline void add_string(sb_t* sb, char prefix, const char* property, const char* value) {
add_prop(sb, prefix, property);
sb_add_char(sb, '"');
sb_add_chars(sb, value);
sb_add_char(sb, '"');
}
static inline void add_uint(sb_t* sb, char prefix, const char* property, uint64_t value) {
add_prop(sb, prefix, property);
char tmp[16];
sprintf(tmp, "%u", (uint32_t) value);
sb_add_chars(sb, tmp);
}
static inline void add_hex(sb_t* sb, char prefix, const char* property, bytes_t value) {
add_prop(sb, prefix, property);
sb_add_bytes(sb, NULL, &value, 1, false);
}
/** used for exeuting a function based on the name. This macro will return if the name matches. */
#endif /* DEBUG_H */