18 #include <QCoreApplication>
26 QgsEventTracing::EventType type;
35 static bool sIsTracing =
false;
44 bool QgsEventTracing::startTracing()
50 sTraceEventsMutex()->lock();
51 sTracingTimer()->start();
52 sTraceEvents()->clear();
53 sTraceEvents()->reserve( 1000 );
54 sTraceEventsMutex()->unlock();
58 bool QgsEventTracing::stopTracing()
64 sTracingTimer()->invalidate();
68 bool QgsEventTracing::isTracingEnabled()
73 static char _eventTypeToChar( QgsEventTracing::EventType type )
77 case QgsEventTracing::Begin:
return 'B';
78 case QgsEventTracing::End:
return 'E';
79 case QgsEventTracing::Instant:
return 'i';
80 case QgsEventTracing::AsyncBegin:
return 'b';
81 case QgsEventTracing::AsyncEnd:
return 'e';
86 bool QgsEventTracing::writeTrace(
const QString &fileName )
92 if ( !f.open( QIODevice::WriteOnly ) )
95 f.write(
"{\n\"traceEvents\": [\n" );
98 for (
const auto &item : *sTraceEvents() )
104 char t = _eventTypeToChar( item.type );
105 QString msg = QStringLiteral(
" {\"cat\": \"%1\", \"pid\": 1, \"tid\": %2, \"ts\": %3, \"ph\": \"%4\", \"name\": \"%5\"" )
106 .arg( item.category ).arg( item.threadId ).arg( item.timestamp ).arg( t ).arg( item.name );
109 if ( item.type == Instant )
110 msg += QLatin1String(
", \"s\": \"g\"" );
113 if ( item.type == AsyncBegin || item.type == AsyncEnd )
114 msg += QStringLiteral(
", \"id\": \"%1\"" ).arg( item.id );
118 f.write( msg.toUtf8() );
121 f.write(
"\n]\n}\n" );
126 void QgsEventTracing::addEvent( QgsEventTracing::EventType type,
const QString &category,
const QString &name,
const QString &
id )
131 sTraceEventsMutex()->lock();
134 item.timestamp = sTracingTimer()->nsecsElapsed() / 1000;
135 if ( QThread::currentThread() == QCoreApplication::instance()->thread() )
138 item.threadId =
static_cast<uint
>(
reinterpret_cast<quint64
>( QThread::currentThreadId() ) );
139 item.category = category;
142 sTraceEvents()->append( item );
143 sTraceEventsMutex()->unlock();