QtRedmine
logging.h
1 #ifndef LOGGING_H
2 #define LOGGING_H
3 
4 #include <QDebug>
5 
6 /*
7  * This is how the variable printing works (examples below for debug mode)
8  *
9  * 1. A call to DEBUG() will translate to
10  * a. DBG() << ARGS
11  * b. DBG() << ARG0 // ARG0 is no macro but an enum
12  * c. DBG() << "" // Empty QDebug
13  *
14  * 2. A call to DEBUG()(var1) will translate to
15  * a. DBG() << ARGS()(var1)
16  * b. DBG() << ARG0(var1) // ARG0 is a macro
17  * c. DBG() << "(" #var1 "=" << #var1 << ARG1 // ARG1 is no macro but an enum
18  * d. DBG() << "(" #var1 "=" << #var1 << ")" // QDebug << ")"
19  *
20  * 3. A call to DEBUG()(var1)(var2) will translate to
21  * a. DBG() << ARGS()(var1)(var2)
22  * b. DBG() << ARG0(var1)(var2) // ARG0 is a macro
23  * c. DBG() << "(" #var1 "=" << #var1 << ARG1(var2) // ARG1 is a macro
24  * c. DBG() << "(" #var1 "=" << #var1 << ", " #var1 "=" << #var2 << ARG2 // ARG2 is no macro but an enum
25  * d. DBG() << "(" #var1 "=" << #var1 << ", " #var1 "=" << #var2 << ")" // QDebug << ")"
26  */
27 
28 enum argNone{ ARG0 };
29 enum argLast{ ARG1, ARG2 };
30 
31 inline QDebug operator<<( QDebug debug, argNone )
32 {
33  return debug;
34 }
35 
36 inline QDebug operator<<( QDebug debug, argLast )
37 {
38  return debug << ")";
39 }
40 
41 #define ARG0(x) "(" #x "=" << x << ARG1
42 #define ARG1(x) ", " #x "=" << x << ARG2
43 #define ARG2(x) ", " #x "=" << x << ARG1
44 #define ARGS ARG0
45 
46 //
47 // If debug mode is enabled, use advanced logging methods
48 //
49 #ifdef DEBUG_OUTPUT
50 
51 #include <QFileInfo>
52 
53 #define C(s) s.c_str()
54 #define STR(s) std::string(s)
55 #define TOS(i) std::to_string(i)
56 
57 // Function name
58 #define FUNC C( STR(__func__).append("()") )
59 
60 // Filename and position
61 #define POS C( QFileInfo(__FILE__).fileName().toStdString().append(":").append(TOS(__LINE__)).append(":") )
62 
63 // Enter and return helpers with filename and position
64 #define DBG(...) qDebug().noquote().nospace() << POS << " " << QString(__VA_ARGS__) << " "
65 
66 #define DEBUG(...) DBG(__VA_ARGS__) << ARGS
67 #define ENTER(...) DBG(__VA_ARGS__) << "Entering " << FUNC << ARGS
68 #define RETURN(x) do{ DBG() << "Leaving " << FUNC; return x; }while(0)
69 
70 #else
71 
72 #define DEBUG(...) qDebug() << QString(__VA_ARGS__) << ARGS
73 #define ENTER(...) DEBUG(__VA_ARGS__) << "Entering" << Q_FUNC_INFO << ARGS
74 #define RETURN(x) do{ DEBUG() << "Leaving" << Q_FUNC_INFO; return x; }while(0)
75 
76 #endif
77 
78 #endif // LOGGING_H