-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlogging.h
78 lines (61 loc) · 2.35 KB
/
logging.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
#ifndef LOGGING_H
#define LOGGING_H
#include <QDebug>
/*
* This is how the variable printing works (examples below for debug mode)
*
* 1. A call to DEBUG() will translate to
* a. DBG() << ARGS
* b. DBG() << ARG0 // ARG0 is no macro but an enum
* c. DBG() << "" // Empty QDebug
*
* 2. A call to DEBUG()(var1) will translate to
* a. DBG() << ARGS()(var1)
* b. DBG() << ARG0(var1) // ARG0 is a macro
* c. DBG() << "(" #var1 "=" << #var1 << ARG1 // ARG1 is no macro but an enum
* d. DBG() << "(" #var1 "=" << #var1 << ")" // QDebug << ")"
*
* 3. A call to DEBUG()(var1)(var2) will translate to
* a. DBG() << ARGS()(var1)(var2)
* b. DBG() << ARG0(var1)(var2) // ARG0 is a macro
* c. DBG() << "(" #var1 "=" << #var1 << ARG1(var2) // ARG1 is a macro
* c. DBG() << "(" #var1 "=" << #var1 << ", " #var1 "=" << #var2 << ARG2 // ARG2 is no macro but an enum
* d. DBG() << "(" #var1 "=" << #var1 << ", " #var1 "=" << #var2 << ")" // QDebug << ")"
*/
enum argNone{ ARG0 };
enum argLast{ ARG1, ARG2 };
inline QDebug operator<<( QDebug debug, argNone )
{
return debug;
}
inline QDebug operator<<( QDebug debug, argLast )
{
return debug << ")";
}
#define ARG0(x) "(" #x "=" << x << ARG1
#define ARG1(x) ", " #x "=" << x << ARG2
#define ARG2(x) ", " #x "=" << x << ARG1
#define ARGS ARG0
//
// If debug mode is enabled, use advanced logging methods
//
#ifdef DEBUG_OUTPUT
#include <QFileInfo>
#define C(s) s.c_str()
#define STR(s) std::string(s)
#define TOS(i) std::to_string(i)
// Function name
#define FUNC C( STR(__func__).append("()") )
// Filename and position
#define POS C( QFileInfo(__FILE__).fileName().toStdString().append(":").append(TOS(__LINE__)).append(":") )
// Enter and return helpers with filename and position
#define DBG(...) qDebug().noquote().nospace() << POS << " " << QString(__VA_ARGS__) << " "
#define DEBUG(...) DBG(__VA_ARGS__) << ARGS
#define ENTER(...) DBG(__VA_ARGS__) << "Entering " << FUNC << ARGS
#define RETURN(x) do{ DBG() << "Leaving " << FUNC; return x; }while(0)
#else
#define DEBUG(...) qDebug() << QString(__VA_ARGS__) << ARGS
#define ENTER(...) DEBUG(__VA_ARGS__) << "Entering" << Q_FUNC_INFO << ARGS
#define RETURN(x) do{ DEBUG() << "Leaving" << Q_FUNC_INFO; return x; }while(0)
#endif
#endif // LOGGING_H