diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | common/log.cpp | 70 | ||||
-rw-r--r-- | common/log.h | 17 | ||||
-rw-r--r-- | common/resourceaccess.cpp | 5 |
4 files changed, 90 insertions, 3 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 429bad7..656f987 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt | |||
@@ -23,6 +23,7 @@ else (STORAGE_unqlite) | |||
23 | endif (STORAGE_unqlite) | 23 | endif (STORAGE_unqlite) |
24 | 24 | ||
25 | set(command_SRCS | 25 | set(command_SRCS |
26 | log.cpp | ||
26 | entitybuffer.cpp | 27 | entitybuffer.cpp |
27 | clientapi.cpp | 28 | clientapi.cpp |
28 | commands.cpp | 29 | commands.cpp |
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 | |||
9 | class DebugStream: public QIODevice | ||
10 | { | ||
11 | public: | ||
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 | } | ||
32 | private: | ||
33 | Q_DISABLE_COPY(DebugStream) | ||
34 | }; | ||
35 | |||
36 | QDebug 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 | } | ||
diff --git a/common/log.h b/common/log.h new file mode 100644 index 0000000..8d3dc75 --- /dev/null +++ b/common/log.h | |||
@@ -0,0 +1,17 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include <QDebug> | ||
4 | |||
5 | enum DebugLevel { | ||
6 | Trace, | ||
7 | Log, | ||
8 | Warning, | ||
9 | Error | ||
10 | }; | ||
11 | |||
12 | QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function); | ||
13 | |||
14 | #define Trace() debugStream(DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) | ||
15 | #define Log() debugStream(DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) | ||
16 | #define Warning() debugStream(DebugLevel::Warning, __LINE__, __FILE__, Q_FUNC_INFO) | ||
17 | #define Error() debugStream(DebugLevel::Error, __LINE__, __FILE__, Q_FUNC_INFO) | ||
diff --git a/common/resourceaccess.cpp b/common/resourceaccess.cpp index ffe716b..59cbece 100644 --- a/common/resourceaccess.cpp +++ b/common/resourceaccess.cpp | |||
@@ -20,12 +20,12 @@ | |||
20 | 20 | ||
21 | #include "resourceaccess.h" | 21 | #include "resourceaccess.h" |
22 | 22 | ||
23 | #include "common/console.h" | ||
24 | #include "common/commands.h" | 23 | #include "common/commands.h" |
25 | #include "common/commandcompletion_generated.h" | 24 | #include "common/commandcompletion_generated.h" |
26 | #include "common/handshake_generated.h" | 25 | #include "common/handshake_generated.h" |
27 | #include "common/revisionupdate_generated.h" | 26 | #include "common/revisionupdate_generated.h" |
28 | #include "common/synchronize_generated.h" | 27 | #include "common/synchronize_generated.h" |
28 | #include "log.h" | ||
29 | 29 | ||
30 | #include <QCoreApplication> | 30 | #include <QCoreApplication> |
31 | #include <QDebug> | 31 | #include <QDebug> |
@@ -343,8 +343,7 @@ void ResourceAccess::callCallbacks(int id) | |||
343 | 343 | ||
344 | void ResourceAccess::log(const QString &message) | 344 | void ResourceAccess::log(const QString &message) |
345 | { | 345 | { |
346 | qDebug() << d->resourceName + ": " + message; | 346 | Log() << d->resourceName + ": " + message; |
347 | // Console::main()->log(d->resourceName + ": " + message); | ||
348 | } | 347 | } |
349 | 348 | ||
350 | } | 349 | } |