-
Notifications
You must be signed in to change notification settings - Fork 3
/
dbg_trace.cpp
323 lines (274 loc) · 7.1 KB
/
dbg_trace.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright (c) 2012-2023 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include <array>
#include <string>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <chrono>
#include "deconz/dbg_trace.h"
#ifdef __WIN32__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include <time.h>
#include <sys/timeb.h>
#define MAX_DBG_LINE 8192
#define MAX_BUFFERS 3
#define FLUSH_EVERY_N_LINES 10
/******************************************************************************
Local variables
******************************************************************************/
struct DbgContext
{
DbgContext()
{
buf.reserve(MAX_DBG_LINE * MAX_BUFFERS);
}
uint64_t lastFlush = 0;
std::string buf;
};
struct DbgItem
{
int item = 0;
std::string name;
};
static FILE *logFP = nullptr;
static char lookup[] = { '0', '1', '2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
static int dbgEnable = 0;
static char dbgLine[MAX_DBG_LINE];
static char dbgLine2[MAX_DBG_LINE];
static DbgContext *_dbg = nullptr;
static void (*dbgCallback)(int, const char*) = nullptr;
#define DBG_ITEM(x) #x
static const std::array<DbgItem, 24> levelStrings = {
DbgItem{DBG_INFO, "INFO"},
DbgItem{DBG_ERROR, "ERROR"},
DbgItem{DBG_PROT, "PROT"},
DbgItem{DBG_VFS, "VFS"},
DbgItem{DBG_WIRE, "WIRE"},
DbgItem{DBG_PROTBUF, "PROTBUF"},
DbgItem{DBG_ZDP, "ZDP"},
DbgItem{DBG_ZCL, "ZCL"},
DbgItem{DBG_APS, "APS"},
DbgItem{DBG_PROT_L2, "PROT_L2"},
DbgItem{DBG_ZCLDB, "ZCLDB"},
DbgItem{DBG_INFO_L2, "INFO_L2"},
DbgItem{DBG_HTTP, "HTTP"},
DbgItem{DBG_TLINK, "TLINK"},
DbgItem{DBG_ERROR_L2, "ERROR_L2"},
DbgItem{DBG_OTA, "OTA"},
DbgItem{DBG_APS_L2, "APS_L2"},
DbgItem{DBG_MEASURE, "MEASURE"},
DbgItem{DBG_ROUTING, "ROUTING"},
DbgItem{DBG_ZGP, "ZGP"},
DbgItem{DBG_IAS, "IAS"},
DbgItem{DBG_DDF, "DDF"},
DbgItem{DBG_DEV, "DEV"},
DbgItem{DBG_JS, "JS"}
};
/******************************************************************************
Implementation
******************************************************************************/
static uint64_t msSinceEpoch()
{
const auto now = std::chrono::system_clock::now();
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
return static_cast<uint64_t>(ms.count());
}
void DBG_WriteString(int level, const char *str)
{
if (dbgEnable & level && _dbg)
{
{
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
if (strftime(dbgLine2, sizeof(dbgLine2), "%H:%M:%S", timeinfo) != 8)
{
return;
}
const uint64_t msCount = msSinceEpoch();
if (snprintf(dbgLine2 + 8, sizeof(dbgLine2) - 16, ":%03ld ", static_cast<long int>(msCount % 1000)) != 5)
{
return;
}
}
// 13: length of timestamp incl. space
const size_t len = strlen(str);
if (len + 13 >= sizeof(dbgLine2))
{
return;
}
memcpy(dbgLine2 + 13, str, len);
dbgLine2[len + 13] = '\0';
_dbg->buf.append(dbgLine2);
if (_dbg->buf.capacity() < MAX_DBG_LINE)
{
DBG_FlushLazy();
}
if (dbgCallback)
{
dbgCallback(level, dbgLine2);
}
}
}
int DBG_Printf1(int level, const char *format, ...)
{
if (dbgEnable & level)
{
{
va_list args;
va_start (args, format);
vsnprintf(dbgLine, sizeof(dbgLine) - 1, format, args);
va_end (args);
}
DBG_WriteString(level, dbgLine);
return 0;
}
//assert(level == DBG_ERROR);
return -1;
}
void DBG_Init(void *logfile)
{
assert(_dbg == nullptr);
_dbg = new DbgContext;
logFP = (FILE*)logfile;
dbgEnable = 0;
dbgLine2[sizeof(dbgLine2) - 4] = '.';
dbgLine2[sizeof(dbgLine2) - 3] = '.';
dbgLine2[sizeof(dbgLine2) - 2] = '.';
dbgLine2[sizeof(dbgLine2) - 1] = '\0';
}
void DBG_Destroy()
{
dbgEnable = 0;
delete _dbg;
_dbg = nullptr;
}
void DBG_Flush()
{
if (!_dbg || _dbg->buf.empty())
{
return;
}
#ifdef __WIN32__
OutputDebugStringA(_dbg->buf.c_str());
#endif
#if defined(__linux__) || defined(__APPLE__)
fputs(_dbg->buf.c_str(), logFP);
#if !defined(__arm__) && !defined(__ARM_ARCH)// don't wait blocking
fflush(logFP);
#endif
#endif // linux, apple
_dbg->buf.clear();
assert(_dbg->buf.capacity() > MAX_DBG_LINE);
}
void DBG_FlushLazy()
{
if (!_dbg || _dbg->buf.empty())
{
return;
}
const uint64_t msCount = msSinceEpoch();
if (msCount < _dbg->lastFlush || // invalid
((msCount - _dbg->lastFlush) > 100) || _dbg->buf.capacity() < MAX_DBG_LINE)
{
_dbg->lastFlush = msCount;
DBG_Flush();
}
}
void DBG_Enable(int item)
{
dbgEnable |= item;
}
void DBG_Disable(int item)
{
dbgEnable &= ~item;
}
/*! Returns the item number of the string represention \p item.
\param item – the string representing an item, e.g. "DBG_INFO", etc.
\returns >0 - on sucess
0 - if not found
*/
int DBG_ItemFromString(const char *item)
{
for (const auto &i : levelStrings)
{
if (i.name == item)
{
return i.item;
}
}
return 0;
}
/*! Copies the string representation of \p item into \p buf.
\param buflen – should be at least \c 16.
\returns 0 - on sucess
-1 - on error
*/
int DBG_StringFromItem(const int item, char *buf, unsigned long buflen)
{
for (const auto &i : levelStrings)
{
if (i.item == item)
{
assert(i.name.size() + 1 < buflen);
if (i.name.size() + 1 > buflen)
{
break;
}
memcpy(buf, i.name.c_str(), i.name.size());
buf[i.name.size()] = '\0';
return 0;
}
}
return -1;
}
void DBG_RegisterCallback(void (*cb)(int, const char*))
{
dbgCallback = cb;
}
int DBG_IsEnabled(int item)
{
if ((dbgEnable & item) == item)
{
return 1;
}
return 0;
}
/*!
Converts data into hex-ascii string.
The memory area \p ascii must have at least the
size \p 2 * length + 1 bytes.
A terminating zero will be appended at the end.
\returns a pointer to the last byte (zero).
*/
unsigned char *DBG_HexToAscii(const void *hex, unsigned length, void *ascii)
{
unsigned i;
unsigned char *h;
unsigned char *a;
if (!hex || length == 0 || !ascii)
return 0;
h = (unsigned char*)hex;
a = (unsigned char*)ascii;
for (i = 0; i < length; i++)
{
*a++ = lookup[(h[i] & 0xf0) >> 4];
*a++ = lookup[h[i] & 0xf];
}
*a = '\0';
return a;
}