File indexing completed on 2024-12-08 12:44:37

0001 /*
0002     SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "debug.h"
0008 #include "async.h"
0009 
0010 #include <QStringBuilder>
0011 
0012 #ifdef __GNUG__
0013 #include <cxxabi.h>
0014 #include <memory>
0015 #include <cstdlib>
0016 #endif
0017 
0018 namespace KAsync
0019 {
0020 
0021 Q_LOGGING_CATEGORY(Debug, "org.kde.async", QtWarningMsg)
0022 Q_LOGGING_CATEGORY(Trace, "org.kde.async.trace", QtWarningMsg)
0023 
0024 QString demangleName(const char *name)
0025 {
0026     if (!name || !*name) {
0027         return {};
0028     }
0029 #ifdef __GNUG__
0030     int status = 1; // uses -3 to 0 error codes
0031     std::unique_ptr<char, void(*)(void*)> demangled(abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free);
0032     if (status == 0) {
0033         return QString::fromLatin1(demangled.get());
0034     }
0035 #endif
0036     return QString::fromLatin1(name);
0037 }
0038 
0039 }
0040 
0041 using namespace KAsync;
0042 
0043 int Tracer::lastId = 0;
0044 
0045 Tracer::Tracer(Private::Execution *execution)
0046     : mId(lastId++)
0047     , mExecution(execution)
0048 {
0049     msg(KAsync::Tracer::Start);
0050 }
0051 
0052 Tracer::~Tracer()
0053 {
0054     msg(KAsync::Tracer::End);
0055     // FIXME: Does this work on parallel executions?
0056     --lastId;
0057     --mId;
0058 }
0059 
0060 void Tracer::msg(Tracer::MsgType msgType)
0061 {
0062     qCDebug(Trace).nospace() << (QString().fill(QLatin1Char(' '), mId * 2) % 
0063                                  (msgType == KAsync::Tracer::Start ? QStringLiteral(" START ") : QStringLiteral(" END   ")) %
0064                                  QString::number(mId) % QStringLiteral(" ") %
0065                                  mExecution->executor->mExecutorName);
0066 }