File indexing completed on 2024-11-24 04:44:12

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include "errorhandler.h"
0008 
0009 #include <QMutex>
0010 #include <QTime>
0011 #include <iostream>
0012 
0013 #include <kolabformat.h>
0014 
0015 QDebug operator<<(QDebug dbg, const std::string &s)
0016 {
0017     dbg.nospace() << QString::fromStdString(s);
0018     return dbg.space();
0019 }
0020 
0021 namespace Kolab
0022 {
0023 DebugStream::DebugStream()
0024     : QIODevice()
0025 {
0026     open(WriteOnly);
0027 }
0028 
0029 DebugStream::~DebugStream() = default;
0030 
0031 qint64 DebugStream::writeData(const char *data, qint64 len)
0032 {
0033     const QByteArray buf = QByteArray::fromRawData(data, len);
0034     //         qt_message_output(QtDebugMsg, buf.trimmed().constData());
0035     ErrorHandler::instance().addError(m_severity, QString::fromLatin1(buf), m_location);
0036     return len;
0037 }
0038 
0039 QMutex mutex;
0040 
0041 void logMessage(const QString &message, const QString &file, int line, ErrorHandler::Severity s)
0042 {
0043     ErrorHandler::instance().addError(s, message, file + QLatin1Char(' ') + QString::number(line));
0044 }
0045 
0046 ErrorHandler::ErrorHandler()
0047     : m_worstError(Debug)
0048     , m_debugStream(new DebugStream)
0049 {
0050 }
0051 
0052 QDebug ErrorHandler::debugStream(ErrorHandler::Severity severity, int line, const char *file)
0053 {
0054     QMutexLocker locker(&mutex);
0055     ErrorHandler::instance().m_debugStream->m_location = QString(QLatin1StringView(file) + QLatin1Char('(') + QString::number(line) + QLatin1Char(')'));
0056     ErrorHandler::instance().m_debugStream->m_severity = severity;
0057     return QDebug(ErrorHandler::instance().m_debugStream.data());
0058 }
0059 
0060 void ErrorHandler::addError(ErrorHandler::Severity s, const QString &message, const QString &location)
0061 {
0062     QMutexLocker locker(&mutex);
0063     QString filename = location;
0064     const QStringList lst = filename.split(QLatin1Char('/'));
0065     if (!lst.isEmpty()) {
0066         filename = lst.last();
0067     }
0068     const QString output = QTime::currentTime().toString(QStringLiteral("(hh:mm:ss) ")) + filename + QLatin1StringView(":\t") + message;
0069     std::cout << output.toStdString() << std::endl;
0070     if (s == Debug) {
0071         return;
0072     }
0073     if (s > m_worstError) {
0074         m_worstError = s;
0075         m_worstErrorMessage = message;
0076     }
0077     m_errorQueue.append(Err(s, message, location));
0078 }
0079 
0080 ErrorHandler::Severity ErrorHandler::error() const
0081 {
0082     QMutexLocker locker(&mutex);
0083     return m_worstError;
0084 }
0085 
0086 QString ErrorHandler::errorMessage() const
0087 {
0088     QMutexLocker locker(&mutex);
0089     return m_worstErrorMessage;
0090 }
0091 
0092 const QList<ErrorHandler::Err> &ErrorHandler::getErrors() const
0093 {
0094     QMutexLocker locker(&mutex);
0095     return m_errorQueue;
0096 }
0097 
0098 void ErrorHandler::clear()
0099 {
0100     QMutexLocker locker(&mutex);
0101     m_errorQueue.clear();
0102     m_worstError = Debug;
0103 }
0104 
0105 void ErrorHandler::handleLibkolabxmlErrors()
0106 {
0107     switch (Kolab::error()) {
0108     case Kolab::Warning:
0109         instance().addError(ErrorHandler::Warning, QString::fromStdString(Kolab::errorMessage()), QStringLiteral("libkolabxml"));
0110         break;
0111     case Kolab::Error:
0112         instance().addError(ErrorHandler::Error, QString::fromStdString(Kolab::errorMessage()), QStringLiteral("libkolabxml"));
0113         break;
0114     case Kolab::Critical:
0115         instance().addError(ErrorHandler::Critical, QString::fromStdString(Kolab::errorMessage()), QStringLiteral("libkolabxml"));
0116         break;
0117     default:
0118         // Do nothing, there is no message available in this case
0119         break;
0120     }
0121 }
0122 }
0123 
0124 #include "moc_errorhandler.cpp"