-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippets.cpp
More file actions
332 lines (261 loc) · 9.61 KB
/
snippets.cpp
File metadata and controls
332 lines (261 loc) · 9.61 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
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
324
325
326
327
328
329
330
331
// stacktrace.h (c) 2008, Timo Bingmann from http://idlebox.net/
// published under the WTFPL v2.0
#ifndef _STACKTRACE_H_
#define _STACKTRACE_H_
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
#include <cxxabi.h>
/** Print a demangled stack backtrace of the caller function to FILE* out. */
static inline void print_stacktrace(FILE *out = stderr, unsigned int max_frames = 63)
{
fprintf(out, "stack trace:\n");
// storage array for stack trace address data
void* addrlist[max_frames+1];
// retrieve current stack addresses
int addrlen = backtrace(addrlist, sizeof(addrlist) / sizeof(void*));
if (addrlen == 0) {
fprintf(out, " <empty, possibly corrupt>\n");
return;
}
// resolve addresses into strings containing "filename(function+address)",
// this array must be free()-ed
char** symbollist = backtrace_symbols(addrlist, addrlen);
// allocate string which will be filled with the demangled function name
size_t funcnamesize = 256;
char* funcname = (char*)malloc(funcnamesize);
// iterate over the returned symbol lines. skip the first, it is the
// address of this function.
for (int i = 1; i < addrlen; i++)
{
char *begin_name = 0, *begin_offset = 0, *end_offset = 0;
// find parentheses and +address offset surrounding the mangled name:
// ./module(function+0x15c) [0x8048a6d]
for (char *p = symbollist[i]; *p; ++p)
{
if (*p == '(')
begin_name = p;
else if (*p == '+')
begin_offset = p;
else if (*p == ')' && begin_offset) {
end_offset = p;
break;
}
}
if (begin_name && begin_offset && end_offset
&& begin_name < begin_offset)
{
*begin_name++ = '\0';
*begin_offset++ = '\0';
*end_offset = '\0';
// mangled name is now in [begin_name, begin_offset) and caller
// offset in [begin_offset, end_offset). now apply
// __cxa_demangle():
int status;
char* ret = abi::__cxa_demangle(begin_name,
funcname, &funcnamesize, &status);
if (status == 0) {
funcname = ret; // use possibly realloc()-ed string
fprintf(out, " %s : %s+%s\n",
symbollist[i], funcname, begin_offset);
}
else {
// demangling failed. Output function name as a C function with
// no arguments.
fprintf(out, " %s : %s()+%s\n",
symbollist[i], begin_name, begin_offset);
}
}
else
{
// couldn't parse the line? print the whole line.
fprintf(out, " %s\n", symbollist[i]);
}
}
free(funcname);
free(symbollist);
}
#endif // _STACKTRACE_H_
////////////////////////////////////////////////////////////////////////
QJsonDocument doc = QJsonDocument::fromJson(prop.toUtf8());
QJsonObject obj = doc.object();
int callID = obj["callID"].toInt(-1);
qDebug() << "callID: " << callID;
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
#include <execinfo.h>
#define PRINT_CALLSTACK \
do \
{ \
void *buffer[200]; \
int n = backtrace(buffer,20); \
char **str = backtrace_symbols(buffer, n); \
for (int i = 0; i < n; i++) \
{ \
printf("%d: %s\n", i, str[i]); \
} \
} while(0) \
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// possible implementation for QJsonValue::fromVariant
////////////////////////////////////////////////////////////////////////
QJsonValue QJsonValue::fromVariant(const QVariant& value)
{
QJsonValue retValue;
switch(value.type())
{
case QMetaType::QStringList:
{
QJsonArray result;
foreach(QString listItem, value.toStringList())
{
result.push_back(listItem);
}
retValue = QJsonValue(result);
break;
}
// recursevely unwrap nested lists
case QMetaType::QVariantList:
{
QJsonArray result;
foreach(QVariant listItem, value.toList())
{
result.push_back(QVariant2QJson(listItem));
}
retValue = QJsonValue(result);
break;
}
case QMetaType::QVariantHash:
{
QJsonObject result;
////result.fromVariantHash(value.toHash());
QVariantHash::const_iterator it = value.toHash().constBegin();
for (; it != value.toHash().constEnd(); ++it)
{
result.insert(it.key(), QJsonValue::fromVariant(it.value()));
}
retValue = QJsonValue(result);
break;
}
case QMetaType::QVariantMap:
{
QJsonObject result;
result.fromVariantMap(value.toMap());
retValue = QJsonValue(result);
break;
}
case QMetaType::Int:
case QMetaType::UInt:
case QMetaType::Long:
case QMetaType::LongLong:
case QMetaType::ULongLong:
case QMetaType::Double:
retValue = QJsonValue(value.toDouble());
break;
case QMetaType::Bool:
retValue = QJsonValue(value.toBool());
break;
case QMetaType::QString:
retValue = QJsonValue(value.toString());
break;
default:
LOG_ERROR("Unrecognized type: '%s'", value.typeName());
retValue = QJsonValue(value.toJsonValue());
}
return retValue;
}
////////////////////////////////////////////////////////////////////////
// serialize to/from JSON
////////////////////////////////////////////////////////////////////////
// serialize to JSON
QJsonArray arr0;
arr0.append(QJsonValue("xxx"));
QJsonArray arr;
arr.append(arr0);
QJsonObject jsonObj;
jsonObj.insert("args", arr);
QJsonDocument doc(jsonObj);
QString serializedDoc = doc.toJson(QJsonDocument::Compact);
qDebug() << "serializedDoc: " << serializedDoc;
// deserialize from JSON
QJsonObject objMessage = QJsonDocument::fromJson(serializedDoc.toUtf8()).object();
QVariantList args = objMessage["args"].toArray().toVariantList();
qDebug() << "list.at(0).type: " << args.at(0).type();
qDebug() << "list.at(0).typeName: " << args.at(0).typeName();
////////////////////////////////////////////////////////////////////////
// Qt calculate hash for image
////////////////////////////////////////////////////////////////////////
QImage image;
QCryptographicHash hash(QCryptographicHash::Md5);
for (int row = 0; row < image.height(); ++row)
{
hash.addData(reinterpret_cast<const char*>(image.scanLine(row)), image.width() * 4);
}
QString actualHash = hash.result().toHex();
////////////////////////////////////////////////////////////////////////
// Timestamp
////////////////////////////////////////////////////////////////////////
#include <sys/time.h>
long long current_timestamp()
{
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds = te.tv_sec*1000LL + te.tv_usec/1000; // caculate milliseconds
return milliseconds;
}
////////////////////////////////////////////////////////////////////////
// config.h
////////////////////////////////////////////////////////////////////////
static bool _DebugEnabled()
{
QByteArray loggingEnv = qgetenv("__DEBUG");
if (loggingEnv.isEmpty())
return false;
return true;
}
static bool _DEBUG = _DebugEnabled();
#define TRC_GROUP "MyGroup"
#define TRC_DEBUG_FUNC_ENTER(s, f, args...) do { if (_DEBUG) { printf( TRC_GROUP "\t" ); printf( "%s: ENTER: " f, __PRETTY_FUNCTION__, ##args); printf ("\n"); } } while(0)
#define TRC_DEBUG_FUNC_EXIT(s) do { if (_DEBUG) { printf( TRC_GROUP "\t" ); printf( "%s: EXIT", __PRETTY_FUNCTION__); printf ("\n"); } } while(0)
#define TRC_DEBUG(s, f, args...) do { if (_DEBUG) { printf( TRC_GROUP "\t" ); printf( "%s: " f, __PRETTY_FUNCTION__, ##args); printf ("\n"); } } while(0)
#define TRC_INFO( s, f, args...) do { if (_DEBUG) { printf( TRC_GROUP "\t" ); printf( "%s: " f, __PRETTY_FUNCTION__, ##args); printf ("\n"); } } while(0)
#define TRC_WARN( s, f, args...) do { if (_DEBUG) { printf( TRC_GROUP "\t" ); printf( "%s: " f, __PRETTY_FUNCTION__, ##args); printf ("\n"); } } while(0)
#define TRC_ERROR(s, f, args...) do { if (_DEBUG) { printf( TRC_GROUP "\t" ); printf( "%s: " f, __PRETTY_FUNCTION__, ##args); printf ("\n"); } } while(0)
#define TRC_FATAL(s, f, args...) do { if (_DEBUG) { printf( TRC_GROUP "\t" ); printf( "%s: " f, __PRETTY_FUNCTION__, ##args); printf ("\n"); } } while(0)
[...]
TRC_DEBUG_FUNC_ENTER(0U, "");
TRC_DEBUG_FUNC_EXIT (0U);
TRC_INFO(0U, "New request is received: request='%s'", request.getHeaderStr().c_str());
/////////////////////////////////////////////////////////////////////////////////////////////
#ifndef NDEBUG
#include <cassert>
#define UL_ASSERT(condition)
{
if(!(condition))
{
std::cerr << "Assertion failed at " << __FILE__ << ":" << __LINE__;
std::cerr << " inside " << __FUNCTION__ << std::endl;
std::cerr << "Condition: " << #condition;
abort();
}
}
#else
#define UL_ASSERT(condition) (condition)
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
// Read key-value pairs
struct Flag_name_value {
std::string name;
std::string value;
};
std::string input("ab=xy z,time a=1s");
std::istringstream iss(input);
std::vector<Flag_name_value> flags;
std::string token;
Flag_name_value flag;
while (std::getline(iss, token, ',')) {
size_t pos = token.find('=');
flag.name = token.substr(0, pos);
flag.value = token.substr(pos + 1);
flags.push_back(flag);
}