summaryrefslogtreecommitdiffstats
path: root/common/log.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-03-26 09:44:00 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-03-31 11:11:08 +0200
commitc9aeb8896ae578515c217b9a08988156b4d62f1e (patch)
tree4735d7df7c16ee9556f80aba61a034ff48f1196a /common/log.cpp
parent8f2fed8d2a1b23a8f318047b6592ad64b6ecbd22 (diff)
downloadsink-c9aeb8896ae578515c217b9a08988156b4d62f1e.tar.gz
sink-c9aeb8896ae578515c217b9a08988156b4d62f1e.zip
Resource crashhandler and logging facilities.
Diffstat (limited to 'common/log.cpp')
-rw-r--r--common/log.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/common/log.cpp b/common/log.cpp
new file mode 100644
index 0000000..1d93404
--- /dev/null
+++ b/common/log.cpp
@@ -0,0 +1,70 @@
1#include "log.h"
2
3#include <QString>
4#include <QIODevice>
5#include <QCoreApplication>
6#include <iostream>
7#include <unistd.h>
8
9class DebugStream: public QIODevice
10{
11public:
12 QString m_location;
13 DebugStream()
14 : QIODevice()
15 {
16 open(WriteOnly);
17 }
18 virtual ~DebugStream(){};
19
20 bool isSequential() const { return true; }
21 qint64 readData(char *, qint64) { return 0; /* eof */ }
22 qint64 readLineData(char *, qint64) { return 0; /* eof */ }
23 qint64 writeData(const char *data, qint64 len)
24 {
25 const QByteArray buf = QByteArray::fromRawData(data, len);
26 // if (!qgetenv("IMAP_TRACE").isEmpty()) {
27 // qt_message_output(QtDebugMsg, buf.trimmed().constData());
28 std::cout << buf.trimmed().constData() << std::endl;
29 // }
30 return len;
31 }
32private:
33 Q_DISABLE_COPY(DebugStream)
34};
35
36QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function)
37{
38 static DebugStream stream;
39 QDebug debug(&stream);
40
41 static QByteArray programName;
42 if (programName.isEmpty()) {
43 if (QCoreApplication::instance())
44 programName = QCoreApplication::instance()->applicationName().toLocal8Bit();
45 else
46 programName = "<unknown program name>";
47 }
48
49 QString prefix;
50 switch (debugLevel) {
51 case DebugLevel::Trace:
52 prefix = "Trace:";
53 break;
54 case DebugLevel::Log:
55 prefix = "Log:";
56 break;
57 case DebugLevel::Warning:
58 prefix = "Warning:";
59 break;
60 case DebugLevel::Error:
61 prefix = "Error:";
62 break;
63 default:
64 break;
65 };
66
67 debug << prefix + QString(" %1(%2) %3:").arg(QString::fromLatin1(programName)).arg(unsigned(getpid())).arg(function) /* << file << ":" << line */;
68
69 return debug;
70}