summaryrefslogtreecommitdiff
path: root/kolabformat/errorhandler.cpp
blob: 91021f42380da5e17f18d111444f2cc115fdcf07 (plain)
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
/*
 * Copyright (C) 2012  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "errorhandler.h"

#include <qdebug.h>
#include <QTime>
#include <QStringList>
#include <qmutex.h>
#include <iostream>

#include <kolabformat.h>

QDebug operator<<(QDebug dbg, const std::string &s)
{
    dbg.nospace() << QString::fromStdString(s);
    return dbg.space();
}

namespace Kolab {

DebugStream::DebugStream()
:   QIODevice()
{
    open(WriteOnly);
}

DebugStream::~DebugStream(){}

qint64 DebugStream::writeData(const char *data, qint64 len) {
    const QByteArray buf = QByteArray::fromRawData(data, len);
//         qt_message_output(QtDebugMsg, buf.trimmed().constData());
    ErrorHandler::instance().addError(m_severity, buf, m_location);
    return len;
}


QMutex mutex;
    
void logMessage(const QString &message, const QString &file, int line, ErrorHandler::Severity s)
{
    ErrorHandler::instance().addError(s, message, file+" "+QString::number(line));
}

ErrorHandler::ErrorHandler()
:   m_worstError(Debug),
    m_debugStream(new DebugStream)
{

};

QDebug ErrorHandler::debugStream(ErrorHandler::Severity severity, int line, const char* file)
{
    QMutexLocker locker(&mutex);
    ErrorHandler::instance().m_debugStream->m_location = QString(QString(file) + "(" + QString::number(line)+")");
    ErrorHandler::instance().m_debugStream->m_severity = severity;
    return QDebug(ErrorHandler::instance().m_debugStream.data());
}

void ErrorHandler::addError(ErrorHandler::Severity s, const QString& message, const QString &location)
{
    QMutexLocker locker(&mutex);
    QString filename = location;
    if (!filename.split(QLatin1Char('/')).isEmpty()) {
       filename = filename.split(QLatin1Char('/')).last();
    }
    const QString output = QTime::currentTime().toString(QLatin1String("(hh:mm:ss) ")) + filename + QLatin1String(":\t") + message;
    std::cout << output.toStdString() << std::endl;
    if (s == Debug) {
        return;
    }
    if (s > m_worstError) {
        m_worstError = s;
        m_worstErrorMessage = message;
    }
    m_errorQueue.append(Err(s, message, location));
}

ErrorHandler::Severity ErrorHandler::error() const
{
    QMutexLocker locker(&mutex);
    return m_worstError;
}

QString ErrorHandler::errorMessage() const
{
    QMutexLocker locker(&mutex);
    return m_worstErrorMessage;
}

const QList< ErrorHandler::Err >& ErrorHandler::getErrors() const
{
    QMutexLocker locker(&mutex);
    return m_errorQueue;
}

void ErrorHandler::clear()
{
    QMutexLocker locker(&mutex);
    m_errorQueue.clear();
    m_worstError = Debug;
}

void ErrorHandler::handleLibkolabxmlErrors()
{
    switch (Kolab::error()) {
        case Kolab::Warning:
            instance().addError(ErrorHandler::Warning, QString::fromStdString(Kolab::errorMessage()), "libkolabxml");
            break;
        case Kolab::Error:
            instance().addError(ErrorHandler::Error, QString::fromStdString(Kolab::errorMessage()), "libkolabxml");
            break;
        case Kolab::Critical:
            instance().addError(ErrorHandler::Critical, QString::fromStdString(Kolab::errorMessage()), "libkolabxml");
            break;
        default:
            //Do nothing, there is no message available in this case
            break;
    }
}


}