From 3c05b5816aa0fb978686b0ed16a02336e0981a0e Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 7 Jul 2016 23:40:52 +0200 Subject: sinksh trace command including autocompletion The sinksh command allows to easily turn tracing for certain areas on or off. --- common/log.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 10 deletions(-) (limited to 'common/log.cpp') diff --git a/common/log.cpp b/common/log.cpp index 821df06..a3df04c 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -6,8 +6,11 @@ #include #include #include +#include +#include #include #include +#include using namespace Sink::Log; @@ -218,9 +221,63 @@ static QByteArray getProgramName() } } +static QSharedPointer debugAreasConfig() +{ + return QSharedPointer::create(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/sink/debugAreas.ini", QSettings::IniFormat); +} + +class DebugAreaCollector { +public: + DebugAreaCollector() + { + QMutexLocker locker(&mutex); + mDebugAreas = debugAreasConfig()->value("areas").value().split(';').toSet(); + } + + ~DebugAreaCollector() + { + QMutexLocker locker(&mutex); + mDebugAreas += debugAreasConfig()->value("areas").value().split(';').toSet(); + debugAreasConfig()->setValue("areas", QVariant::fromValue(mDebugAreas.toList().join(';'))); + } + + void add(const QString &area) + { + QMutexLocker locker(&mutex); + mDebugAreas << area; + } + + QSet debugAreas() + { + QMutexLocker locker(&mutex); + return mDebugAreas; + } + + QMutex mutex; + QSet mDebugAreas; +}; + +static auto sDebugAreaCollector = std::unique_ptr(new DebugAreaCollector); + +QSet Sink::Log::debugAreas() +{ + return sDebugAreaCollector->debugAreas(); +} + +static void collectDebugArea(const QString &debugArea) +{ + sDebugAreaCollector->add(debugArea); +} + static bool containsItemStartingWith(const QByteArray &pattern, const QByteArrayList &list) { for (const auto &item : list) { + if (item.startsWith('*')) { + auto stripped = item.mid(1); + if (pattern.contains(stripped)) { + return true; + } + } if (pattern.startsWith(item)) { return true; } @@ -248,9 +305,9 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file, if (sPrimaryComponent.isEmpty()) { sPrimaryComponent = getProgramName(); } - QString fullDebugArea = sPrimaryComponent + "."+ QString::fromLatin1(debugComponent) + "." + QString::fromLatin1(debugArea); + QString fullDebugArea = sPrimaryComponent + "." + (debugComponent ? (QString::fromLatin1(debugComponent) + ".") : "") + (debugArea ? QString::fromLatin1(debugArea) : ""); - //TODO add to autocompletion + collectDebugArea(fullDebugArea); auto areas = debugOutputFilter(Sink::Log::Area); if (!areas.isEmpty()) { @@ -258,14 +315,6 @@ QDebug Sink::Log::debugStream(DebugLevel debugLevel, int line, const char *file, return QDebug(&nullstream); } } - // static QByteArray programName = getProgramName(); - // - // auto filter = debugOutputFilter(Sink::Log::ApplicationName); - // if (!filter.isEmpty() && !filter.contains(programName)) { - // if (!containsItemStartingWith(programName, filter)) { - // return QDebug(&nullstream); - // } - // } QString prefix; int prefixColorCode = ANSI_Colors::DoNothing; -- cgit v1.2.3