#include "viewmodel/tree/util/Log2TreeItemConverter.h" #include /*************************************************************************************************/ std::unique_ptr SimpleLog2TreeItemConverter::convert(const Log_ptr log, const TMap& translatorMap) const { auto root = std::make_unique(QVariantList{QString("Field"), QString("Value")}); if(log) { addIdItem(log, translatorMap, root.get()); addLevelItem(log, translatorMap, root.get()); addSrcItem(log, translatorMap, root.get()); addMsgItem(log, translatorMap, root.get()); addScopeItem(log, translatorMap, root.get()); addThreadId(log, translatorMap, root.get()); addTimestamp(log, translatorMap, root.get()); } return root; } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addIdItem(const Log_ptr log, const ILog2TreeItemConvertor::TMap& translatorMap, TreeItem* parent) const { if(log->id.has_value()) { parent->appendChild(std::make_unique( QVariantList{"Logger Id", translatorMap[id]->provideString(log)})); } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addLevelItem(const Log_ptr log, const ILog2TreeItemConvertor::TMap& translatorMap, TreeItem* parent) const { if(log->level.has_value()) { parent->appendChild(std::make_unique( QVariantList{"Level", translatorMap[level]->provideString(log)})); } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addSrcItem(const Log_ptr log, const ILog2TreeItemConvertor::TMap& translatorMap, TreeItem* parent) const { if(log->src.has_value()) { auto srcRoot = std::make_unique(QVariantList{"Location", ""}); srcRoot->appendChild(std::make_unique(QVariantList{ "Filename", translatorMap[file]->provideString(log)})); srcRoot->appendChild(std::make_unique(QVariantList{ "Line Number", translatorMap[lineno]->provideString(log)})); srcRoot->appendChild(std::make_unique(QVariantList{ "Function Name", translatorMap[func]->provideString(log)})); parent->appendChild(std::move(srcRoot)); } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addMsgItem(const Log_ptr log, const ILog2TreeItemConvertor::TMap& translatorMap, TreeItem* parent) const { if(log->msg.has_value()) { auto msgRoot = std::make_unique(QVariantList{"Message", ""}); addMsgParts(log->msg.value(), msgRoot.get()); parent->appendChild(std::move(msgRoot)); } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addMsgParts(const QList& parts, TreeItem* parent) const { for(const auto& part : parts) { if(part.string.has_value()) { addStringMessage(part.string.value(), parent); } if(part.loggedObject.has_value()) { addObjectMessage(part.loggedObject.value(), parent); } } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addStringMessage(const QString& message, TreeItem* parent) const { parent->appendChild(std::make_unique(QVariantList{"[MSG]", message})); } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addObjectMessage(const LoggedObjectDto& obj, TreeItem* parent) const { auto objRoot = std::make_unique(QVariantList{"[" + obj.type + "]", obj.summary}); addJsonObject(obj.content, objRoot.get()); parent->appendChild(std::move(objRoot)); } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addJsonObject(const QJsonObject& obj, TreeItem* parent) const { for(auto& key : obj.keys()) { auto val = obj[key]; if(val.isObject()) { auto objRoot = std::make_unique(QVariantList{key, "[OBJ]"}); addJsonObject(val.toObject(), objRoot.get()); parent->appendChild(std::move(objRoot)); } else if(val.isArray()) { auto objRoot = std::make_unique(QVariantList{key, "[ARRAY]"}); addJsonArray(val.toArray(), objRoot.get()); parent->appendChild(std::move(objRoot)); } else { parent->appendChild(std::make_unique(QVariantList{key, val.toVariant()})); } } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addJsonArray(const QJsonArray& arr, TreeItem* parent) const { for(const auto& val : arr) { if(val.isObject()) { auto objRoot = std::make_unique(QVariantList{"[OBJ]", ""}); addJsonObject(val.toObject(), objRoot.get()); parent->appendChild(std::move(objRoot)); } else if(val.isArray()) { auto objRoot = std::make_unique(QVariantList{"[ARRAY]", ""}); addJsonArray(val.toArray(), objRoot.get()); parent->appendChild(std::move(objRoot)); } else { parent->appendChild(std::make_unique(QVariantList{val.toVariant(), ""})); } } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addScopeItem(const Log_ptr log, const ILog2TreeItemConvertor::TMap& translatorMap, TreeItem* parent) const { if(log->scope.has_value()) { parent->appendChild(std::make_unique( QVariantList{"Scope", translatorMap[scope]->provideString(log)})); } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addThreadId(const Log_ptr log, const ILog2TreeItemConvertor::TMap& translatorMap, TreeItem* parent) const { if(log->threadId.has_value()) { parent->appendChild(std::make_unique( QVariantList{"ThreadId", translatorMap[threadId]->provideString(log)})); } } /*************************************************************************************************/ void SimpleLog2TreeItemConverter::addTimestamp(const Log_ptr log, const ILog2TreeItemConvertor::TMap& translatorMap, TreeItem* parent) const { if(log->timestamp.has_value()) { parent->appendChild(std::make_unique( QVariantList{"Time", translatorMap[timestamp]->provideString(log)})); } }