From 81e06f3a53e75bc4aef4888ad006124202fa4567 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sun, 24 Sep 2017 19:23:07 +0200 Subject: [PATCH] `debug::Log` can now be used instead of qDebug() Usage: ``` QString string("world"); debug::Log("hello: {}", string); ``` --- chatterino.pro | 8 ++++++- src/debug/log.hpp | 37 +++++++++++++++++++++++++++++ src/twitch/twitchmessagebuilder.cpp | 13 +++++----- 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 src/debug/log.hpp diff --git a/chatterino.pro b/chatterino.pro index 5afd6277c..328a10b1c 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -164,7 +164,8 @@ HEADERS += \ src/widgets/qualitypopup.hpp \ src/widgets/emotepopup.hpp \ src/messages/messagecolor.hpp \ - src/util/nativeeventhelper.hpp + src/util/nativeeventhelper.hpp \ + src/debug/log.hpp PRECOMPILED_HEADER = @@ -178,6 +179,11 @@ win32 { INCLUDEPATH += C:\local\boost\ } +win32 { + LIBS += -luser32 + LIBS += -lgdi32 +} + # Optional dependency on windows sdk 7.1 win32:exists(C:\Program Files\Microsoft SDKs\Windows\v7.1\Include\Windows.h) { LIBS += -L"C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib" \ diff --git a/src/debug/log.hpp b/src/debug/log.hpp new file mode 100644 index 000000000..83a069bbf --- /dev/null +++ b/src/debug/log.hpp @@ -0,0 +1,37 @@ +#pragma once + +#include "fmt/format.h" + +#include +#include + +namespace chatterino { +namespace debug { + +namespace detail { + +static void _log(const std::string &message) +{ + qDebug().noquote() << QTime::currentTime().toString("hh:mm:ss.zzz") << message.c_str(); +} + +} // namespace detail + +template +inline void Log(const std::string &formatString, Args &&... args) +{ + detail::_log(fmt::format(formatString, std::forward(args)...)); +} + +} // namespace debug +} // namespace chatterino + +namespace fmt { + +// format_arg for QString +inline void format_arg(BasicFormatter &f, const char *&, const QString &v) +{ + f.writer().write("\"{}\"", v.toStdString()); +} + +} // namespace fmt diff --git a/src/twitch/twitchmessagebuilder.cpp b/src/twitch/twitchmessagebuilder.cpp index f03c7fbae..3b0b77ffb 100644 --- a/src/twitch/twitchmessagebuilder.cpp +++ b/src/twitch/twitchmessagebuilder.cpp @@ -1,5 +1,6 @@ #include "twitch/twitchmessagebuilder.hpp" #include "colorscheme.hpp" +#include "debug/log.hpp" #include "emotemanager.hpp" #include "ircmanager.hpp" #include "resources.hpp" @@ -414,8 +415,8 @@ void TwitchMessageBuilder::parseHighlights() if (!blackList.contains(this->ircMessage->nick(), Qt::CaseInsensitive)) { for (const Highlight &highlight : activeHighlights) { if (this->originalMessage.contains(highlight.target, Qt::CaseInsensitive)) { - qDebug() << "Highlight because " << this->originalMessage << " contains " - << highlight.target; + debug::Log("Highlight because {} contains {}", this->originalMessage, + highlight.target); doHighlight = true; if (highlight.sound) { @@ -569,13 +570,11 @@ void TwitchMessageBuilder::parseTwitchBadges() Word(badgeVersion.badgeImage1x, Word::BadgeVanity, QString(), QString("Twitch " + QString::fromStdString(badgeVersion.title)))); } catch (const std::exception &e) { - qDebug() << "Exception caught:" << e.what() - << "when trying to fetch badge version " << versionKey.c_str(); + debug::Log("Exception caught: {} when trying to fetch badge version {}", + e.what(), versionKey); } } catch (const std::exception &e) { - qDebug() << "No badge set with key" - << "bits" - << ". Exception: " << e.what(); + debug::Log("No badge set with key bits. Exception: {}", e.what()); } } else if (badge == "staff/1") { appendWord(Word(this->resources.badgeStaff, Word::BadgeGlobalAuthority, QString(),