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
|
#include "log.h"
#include <QString>
#include <QIODevice>
#include <QCoreApplication>
#include <iostream>
#include <unistd.h>
using namespace Sink::Log;
class DebugStream: public QIODevice
{
public:
QString m_location;
DebugStream()
: QIODevice()
{
open(WriteOnly);
}
virtual ~DebugStream();
bool isSequential() const { return true; }
qint64 readData(char *, qint64) { return 0; /* eof */ }
qint64 readLineData(char *, qint64) { return 0; /* eof */ }
qint64 writeData(const char *data, qint64 len)
{
const QByteArray buf = QByteArray::fromRawData(data, len);
// if (!qgetenv("IMAP_TRACE").isEmpty()) {
// qt_message_output(QtDebugMsg, buf.trimmed().constData());
std::cout << buf.trimmed().constData() << std::endl;
// }
return len;
}
private:
Q_DISABLE_COPY(DebugStream)
};
//Virtual method anchor
DebugStream::~DebugStream()
{}
class NullStream: public QIODevice
{
public:
NullStream()
: QIODevice()
{
open(WriteOnly);
}
virtual ~NullStream();
bool isSequential() const { return true; }
qint64 readData(char *, qint64) { return 0; /* eof */ }
qint64 readLineData(char *, qint64) { return 0; /* eof */ }
qint64 writeData(const char *data, qint64 len)
{
return len;
}
private:
Q_DISABLE_COPY(NullStream)
};
//Virtual method anchor
NullStream::~NullStream()
{}
/*
* ANSI color codes:
* 0: reset colors/style
* 1: bold
* 4: underline
* 30 - 37: black, red, green, yellow, blue, magenta, cyan, and white text
* 40 - 47: black, red, green, yellow, blue, magenta, cyan, and white background
*/
enum ANSI_Colors {
DoNothing = -1,
Reset = 0,
Bold = 1,
Underline = 4,
Black = 30,
Red = 31,
Green = 32,
Yellow = 33,
Blue = 34
};
static QString colorCommand(int colorCode)
{
return QString("\x1b[%1m").arg(colorCode);
}
static QString colorCommand(QList<int> colorCodes)
{
colorCodes.removeAll(ANSI_Colors::DoNothing);
if (colorCodes.isEmpty()) {
return QString();
}
QString string("\x1b[");
for (int code : colorCodes) {
string += QString("%1;").arg(code);
}
string.chop(1);
string += "m";
return string;
}
QByteArray Sink::Log::debugLevelName(DebugLevel debugLevel)
{
switch (debugLevel) {
case DebugLevel::Trace:
return "Trace";
case DebugLevel::Log:
return "Log";
case DebugLevel::Warning:
return "Warning";
case DebugLevel::Error:
return "Error";
default:
break;
};
Q_ASSERT(false);
return QByteArray();
}
DebugLevel Sink::Log::debugLevelFromName(const QByteArray &name)
{
const QByteArray lowercaseName = name.toLower();
if (lowercaseName == "trace")
return DebugLevel::Trace;
if (lowercaseName == "log")
return DebugLevel::Log;
if (lowercaseName == "warning")
return DebugLevel::Warning;
if (lowercaseName == "error")
return DebugLevel::Error;
return DebugLevel::Log;
}
void Sink::Log::setDebugOutputLevel(DebugLevel debugLevel)
{
qputenv("SINKDEBUGLEVEL", debugLevelName(debugLevel));
}
Sink::Log::DebugLevel Sink::Log::debugOutputLevel()
{
return debugLevelFromName(qgetenv("SINKDEBUGLEVEL"));
}
QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea)
{
DebugLevel debugOutputLevel = debugLevelFromName(qgetenv("SINKDEBUGLEVEL"));
if (debugLevel < debugOutputLevel) {
static NullStream stream;
return QDebug(&stream);
}
static DebugStream stream;
QDebug debug(&stream);
static QByteArray programName;
if (programName.isEmpty()) {
if (QCoreApplication::instance())
programName = QCoreApplication::instance()->applicationName().toLocal8Bit();
else
programName = "<unknown program name>";
}
QString prefix;
int prefixColorCode = ANSI_Colors::DoNothing;
switch (debugLevel) {
case DebugLevel::Trace:
prefix = "Trace: ";
break;
case DebugLevel::Log:
prefix = "Log: ";
prefixColorCode = ANSI_Colors::Green;
break;
case DebugLevel::Warning:
prefix = "Warning:";
prefixColorCode = ANSI_Colors::Red;
break;
case DebugLevel::Error:
prefix = "Error: ";
prefixColorCode = ANSI_Colors::Red;
break;
};
bool showLocation = false;
bool showProgram = true;
const QString resetColor = colorCommand(ANSI_Colors::Reset);
QString output;
output += colorCommand(QList<int>() << ANSI_Colors::Bold << prefixColorCode) + prefix + resetColor;
if (showProgram) {
output += QString(" %1(%2)").arg(QString::fromLatin1(programName)).arg(unsigned(getpid()));
}
if (debugArea) {
output += colorCommand(QList<int>() << ANSI_Colors::Bold << prefixColorCode) + QString(" %1 ").arg(QString::fromLatin1(debugArea)) + resetColor;
}
if (showLocation) {
output += QString(" %3").arg(function);
output += QString("%1:%2").arg(file).arg(line);
}
output += ":";
debug << output;
return debug;
}
|