File indexing completed on 2025-01-05 05:23:46
0001 /* 0002 This file is part of the Okteta Kasten Framework, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2010, 2011, 2012 Alex Richardson <alex.richardson@gmx.de> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "scriptlogger.hpp" 0010 #include "../datatypes/datainformation.hpp" 0011 0012 #include <KLocalizedString> 0013 #include <QIcon> 0014 0015 QIcon ScriptLogger::iconForLevel(ScriptLogger::LogLevel level) 0016 { 0017 switch (level) 0018 { 0019 case LogInfo: 0020 return QIcon::fromTheme(QStringLiteral("dialog-information")); 0021 case LogWarning: 0022 return QIcon::fromTheme(QStringLiteral("dialog-warning")); 0023 case LogError: 0024 return QIcon::fromTheme(QStringLiteral("dialog-error")); 0025 default: 0026 Q_ASSERT_X(false, "ScriptLogger::iconForLevel", "Invalid log level passed"); 0027 return {}; 0028 } 0029 } 0030 0031 ScriptLogger::ScriptLogger() = default; 0032 0033 ScriptLogger::~ScriptLogger() = default; 0034 0035 QVariant ScriptLogger::data(const QModelIndex& index, int role) const 0036 { 0037 if (!index.isValid()) { 0038 return {}; 0039 } 0040 int row = index.row(); 0041 Q_ASSERT(row < mData.size()); 0042 Q_ASSERT(!index.parent().isValid()); 0043 if (role == Qt::DisplayRole) { 0044 const Data& data = mData.at(row); 0045 switch (index.column()) 0046 { 0047 case ColumnTime: 0048 return data.time.toString(QStringLiteral("hh:mm:ss.zzz")); 0049 case ColumnOrigin: 0050 return data.origin; 0051 case ColumnMessage: 0052 return data.message; 0053 default: 0054 return {}; 0055 } 0056 } 0057 if (role == Qt::DecorationRole && index.column() == ColumnTime) { 0058 return iconForLevel(mData.at(row).level); 0059 } 0060 return {}; 0061 } 0062 0063 int ScriptLogger::rowCount(const QModelIndex& parent) const 0064 { 0065 if (parent.isValid()) { 0066 return 0; 0067 } 0068 return mData.size(); 0069 } 0070 0071 int ScriptLogger::columnCount(const QModelIndex& parent) const 0072 { 0073 Q_UNUSED(parent) 0074 return COLUMN_COUNT; 0075 } 0076 0077 QVariant ScriptLogger::headerData(int section, Qt::Orientation orientation, int role) const 0078 { 0079 if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { 0080 switch (section) 0081 { 0082 case ColumnTime: 0083 return i18nc("@title:column", "Time"); 0084 case ColumnOrigin: 0085 return i18nc("@title:column", "Origin"); 0086 case ColumnMessage: 0087 return i18nc("@title:column", "Message"); 0088 default: 0089 return {}; 0090 } 0091 } 0092 return {}; 0093 } 0094 0095 QDebug ScriptLogger::log(LogLevel level, const DataInformation* origin) 0096 { 0097 Q_CHECK_PTR(origin); 0098 if (origin->loggedData() < level) { 0099 origin->setLoggedData(level); 0100 } 0101 return log(level, origin->fullObjectPath()); 0102 } 0103 0104 QDebug ScriptLogger::log(LogLevel level, const QString& origin) 0105 { 0106 Q_ASSERT(level != LogInvalid); 0107 if (mLogToStdOut) { 0108 return (level == LogInvalid || level == LogInfo) ? qDebug() : qWarning(); 0109 } 0110 0111 beginInsertRows(QModelIndex(), mData.size(), mData.size()); 0112 mData.append(Data(level, origin)); 0113 endInsertRows(); 0114 return QDebug(&mData.last().message); 0115 } 0116 0117 void ScriptLogger::clear() 0118 { 0119 if (mData.isEmpty()) { 0120 return; 0121 } 0122 0123 beginRemoveRows(QModelIndex(), 0, mData.size() - 1); 0124 mData.clear(); 0125 endRemoveRows(); 0126 } 0127 0128 QStringList ScriptLogger::messages(LogLevel minLevel) const 0129 { 0130 QStringList ret; 0131 for (const auto& d : mData) { 0132 if (d.level >= minLevel) { 0133 ret << d.message; 0134 } 0135 } 0136 0137 return ret; 0138 } 0139 0140 #include "moc_scriptlogger.cpp"