diff --git a/src/plugins/tracing/qctflib.cpp b/src/plugins/tracing/qctflib.cpp index 783be079f71..f57f1cf71f2 100644 --- a/src/plugins/tracing/qctflib.cpp +++ b/src/plugins/tracing/qctflib.cpp @@ -116,6 +116,7 @@ QCtfLibImpl::QCtfLibImpl() } m_location = location + QStringLiteral("/ust"); std::filesystem::create_directory(qPrintable(m_location), qPrintable(location)); + clearLocation(); } m_session.all = m_session.tracepoints.contains(QStringLiteral("all")); @@ -136,6 +137,30 @@ QCtfLibImpl::QCtfLibImpl() m_timer.start(); } +void QCtfLibImpl::clearLocation() +{ + const std::filesystem::path location{qUtf16Printable(m_location)}; + for (auto const& dirEntry : std::filesystem::directory_iterator{location}) + { + const auto path = dirEntry.path(); +#if __cplusplus > 201703L + if (dirEntry.is_regular_file() + && path.filename().wstring().starts_with(std::wstring_view(L"channel_")) + && !path.has_extension()) { +#else + const auto strview = std::wstring_view(L"channel_"); + const auto sub = path.filename().wstring().substr(0, strview.length()); + if (dirEntry.is_regular_file() && sub.compare(strview) == 0 + && !path.has_extension()) { +#endif + if (!std::filesystem::remove(path)) { + qCInfo(lcDebugTrace) << "Unable to clear output location."; + break; + } + } + } +} + void QCtfLibImpl::writeMetadata(const QString &metadata, bool overwrite) { FILE *file = nullptr; @@ -276,11 +301,6 @@ void QCtfLibImpl::doTracepoint(const QCtfTracePointEvent &point, const QByteArra if (ch.channelName[0] == 0) { m_threadIndices.insert(thread, m_threadIndices.size()); - sprintf(ch.channelName, "%s/channel_%d", qPrintable(m_location), m_threadIndices[thread]); - FILE *f = nullptr; - f = openFile(ch.channelName, "wb"_L1); - if (f) - fclose(f); ch.minTimestamp = ch.maxTimestamp = timestamp; ch.thread = thread; ch.threadIndex = m_threadIndices[thread]; diff --git a/src/plugins/tracing/qctflib_p.h b/src/plugins/tracing/qctflib_p.h index 98f34699bd0..8d88183b84f 100644 --- a/src/plugins/tracing/qctflib_p.h +++ b/src/plugins/tracing/qctflib_p.h @@ -89,6 +89,7 @@ private: QHash m_eventPrivs; void updateMetadata(const QCtfTracePointEvent &point); void writeMetadata(const QString &metadata, bool overwrite = false); + void clearLocation(); static void writeCtfPacket(Channel &ch); static constexpr QUuid s_TraceUuid = QUuid(0x3e589c95, 0xed11, 0xc159, 0x42, 0x02, 0x6a, 0x9b, 0x02, 0x00, 0x12, 0xac);