From 5e6a0b961d1fc2162c5a7498f10bbd1a477f1ece Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 27 May 2016 16:51:13 +0900 Subject: Use nullptr --- events/roommessageevent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'events') diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp index 99d17e5c..b7459054 100644 --- a/events/roommessageevent.cpp +++ b/events/roommessageevent.cpp @@ -39,7 +39,7 @@ RoomMessageEvent::RoomMessageEvent() : Event(EventType::RoomMessage) , d(new Private) { - d->content = 0; + d->content = nullptr; } RoomMessageEvent::~RoomMessageEvent() -- cgit v1.2.3 From 028ed5afc18209064002c9f6aa34e08a228d471f Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 11 Apr 2016 11:17:48 +0900 Subject: Enhance logging 1. Introduce QDebug manipulators and formatJson manipulator in particular - this allows to accommodate some changes in Qt's debug printing behaviour between versions. 2. Show JSON for some questionable objects (UnknownEvents, events with no event_id etc.) 3. Log the list of typing users and the user id when getting an avatar. --- events/event.cpp | 37 ++++++++++++++------------ events/roomaliasesevent.cpp | 3 +-- events/typingevent.cpp | 2 ++ events/unknownevent.cpp | 7 ++++- logging_util.h | 63 +++++++++++++++++++++++++++++++++++++++++++++ user.cpp | 2 +- 6 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 logging_util.h (limited to 'events') diff --git a/events/event.cpp b/events/event.cpp index 9a48108c..d05d666f 100644 --- a/events/event.cpp +++ b/events/event.cpp @@ -23,6 +23,7 @@ #include #include +#include "../logging_util.h" #include "roommessageevent.h" #include "roomnameevent.h" #include "roomaliasesevent.h" @@ -110,36 +111,38 @@ Event* Event::fromJson(const QJsonObject& obj) } if( obj.value("type").toString() == "m.typing" ) { - qDebug() << "m.typing..."; return TypingEvent::fromJson(obj); } if( obj.value("type").toString() == "m.receipt" ) { return ReceiptEvent::fromJson(obj); } - //qDebug() << "Unknown event"; return UnknownEvent::fromJson(obj); } bool Event::parseJson(const QJsonObject& obj) { d->originalJson = QString::fromUtf8(QJsonDocument(obj).toJson()); - bool correct = true; - if( obj.contains("event_id") ) + bool correct = (d->type != EventType::Unknown); + if ( d->type != EventType::Unknown && d->type != EventType::Typing ) { - d->id = obj.value("event_id").toString(); - } else { - correct = false; - qDebug() << "Event: can't find event_id"; - } - if( obj.contains("origin_server_ts") ) - { - d->timestamp = QDateTime::fromMSecsSinceEpoch( - static_cast(obj.value("origin_server_ts").toDouble()), Qt::UTC ); - } else { - correct = false; - qDebug() << "Event: can't find ts"; - //qDebug() << obj; + if( obj.contains("event_id") ) + { + d->id = obj.value("event_id").toString(); + } else { + correct = false; + qDebug() << "Event: can't find event_id"; + qDebug() << formatJson << obj; + } + if( obj.contains("origin_server_ts") ) + { + d->timestamp = QDateTime::fromMSecsSinceEpoch( + static_cast(obj.value("origin_server_ts").toDouble()), Qt::UTC ); + } else { + correct = false; + qDebug() << "Event: can't find ts"; + qDebug() << formatJson << obj; + } } if( obj.contains("room_id") ) { diff --git a/events/roomaliasesevent.cpp b/events/roomaliasesevent.cpp index 54fb5682..15b4c54e 100644 --- a/events/roomaliasesevent.cpp +++ b/events/roomaliasesevent.cpp @@ -64,7 +64,6 @@ QStringList RoomAliasesEvent::aliases() const RoomAliasesEvent* RoomAliasesEvent::fromJson(const QJsonObject& obj) { - qDebug() << "RoomAliasesEvent::fromJson"; RoomAliasesEvent* e = new RoomAliasesEvent(); e->parseJson(obj); const QJsonObject contents = obj.value("content").toObject(); @@ -73,6 +72,6 @@ RoomAliasesEvent* RoomAliasesEvent::fromJson(const QJsonObject& obj) { e->d->aliases << alias.toString(); } - qDebug() << e->d->aliases; + qDebug() << "RoomAliasesEvent:" << e->d->aliases; return e; } diff --git a/events/typingevent.cpp b/events/typingevent.cpp index 242094e1..36a7e693 100644 --- a/events/typingevent.cpp +++ b/events/typingevent.cpp @@ -19,6 +19,7 @@ #include "typingevent.h" #include +#include using namespace QMatrixClient; @@ -53,5 +54,6 @@ TypingEvent* TypingEvent::fromJson(const QJsonObject& obj) { e->d->users << user.toString(); } + qDebug() << "Typing:" << e->d->users; return e; } diff --git a/events/unknownevent.cpp b/events/unknownevent.cpp index 758d0ec8..90551409 100644 --- a/events/unknownevent.cpp +++ b/events/unknownevent.cpp @@ -19,6 +19,9 @@ #include "unknownevent.h" #include +#include + +#include "../logging_util.h" using namespace QMatrixClient; @@ -56,5 +59,7 @@ UnknownEvent* UnknownEvent::fromJson(const QJsonObject& obj) e->parseJson(obj); e->d->type = obj.value("type").toString(); e->d->content = QString::fromUtf8(QJsonDocument(obj).toJson()); + qDebug() << "UnknownEvent, JSON follows:"; + qDebug() << formatJson << obj; return e; -} \ No newline at end of file +} diff --git a/logging_util.h b/logging_util.h new file mode 100644 index 00000000..47b2e062 --- /dev/null +++ b/logging_util.h @@ -0,0 +1,63 @@ +/****************************************************************************** + * Copyright (C) 2016 Kitsune Ral + * + * This library 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 2.1 of the License, or (at your option) any later version. + * + * This library 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 library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file logging_util.h - a collection of utilities to facilitate debug logging. + */ + +#pragma once + +#include + +namespace QMatrixClient { + +// QDebug manipulators + +using QDebugManip = QDebug (*)(QDebug); + +/** + * @brief QDebug manipulator to setup the stream for JSON output. + * + * Originally made to encapsulate the change in QDebug behavior in Qt 5.4 + * and the respective addition of QDebug::noquote(). + * Together with the operator<<() helper, the proposed usage is + * (similar to std:: I/O manipulators): + * + * @example qDebug() << formatJson << json_object; // (QJsonObject, or QJsonValue, etc.) + */ +static QDebugManip formatJson = [](QDebug debug_object) { +#if QT_VERSION < QT_VERSION_CHECK(5, 4, 0) + return debug_object; +#else + return debug_object.noquote(); +#endif + }; + +/** + * @brief A helper operator to facilitate using formatJson (and possibly other manipulators) + * + * @param debug_object to output the json to + * @param qdm a QDebug manipulator + * @return a copy of debug_object that has its mode altered by qdm + */ +inline QDebug operator<< (QDebug debug_object, QDebugManip qdm) { + return qdm(debug_object); +} + +} + diff --git a/user.cpp b/user.cpp index 5018fe83..6bef0de1 100644 --- a/user.cpp +++ b/user.cpp @@ -87,7 +87,7 @@ QPixmap User::avatar(int width, int height) { if( !d->avatarOngoingRequest && d->avatarUrl.isValid() ) { - qDebug() << "Get avatar..."; + qDebug() << "Getting avatar for" << id(); d->requestedWidth = width; d->requestedHeight = height; d->avatarOngoingRequest = true; -- cgit v1.2.3