diff options
author | Ulf Hermann <[email protected]> | 2018-05-03 09:50:11 +0200 |
---|---|---|
committer | Ulf Hermann <[email protected]> | 2018-05-04 14:08:47 +0000 |
commit | 734611131dc20283b118353130bdd5e16ce0aeaf (patch) | |
tree | 91adf7b62f98765a46bc5c544a1808cde1b1bf82 /src/libs/tracing/tracestashfile.h | |
parent | b9b2f2c1875030b8259d22cba69161304ada7011 (diff) |
Move Timeline and FlameGraph into a common "Tracing" library
This allows us to share code between the two, in particular the QML code
for the Details window, and the theme code. This way we can potentially
deduplicate some code.
Change-Id: I3a0d26b18488bd2a46b5b077b5b5d79ac2dfc5ce
Reviewed-by: Christian Kandeler <[email protected]>
Reviewed-by: Eike Ziller <[email protected]>
Diffstat (limited to 'src/libs/tracing/tracestashfile.h')
-rw-r--r-- | src/libs/tracing/tracestashfile.h | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/libs/tracing/tracestashfile.h b/src/libs/tracing/tracestashfile.h new file mode 100644 index 00000000000..d860ece9221 --- /dev/null +++ b/src/libs/tracing/tracestashfile.h @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt Creator. +** +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +****************************************************************************/ + +#pragma once + +#include <utils/temporaryfile.h> + +#include <QFile> +#include <QDataStream> + +namespace Timeline { + +template<typename Event> +class TraceStashFile +{ +public: + TraceStashFile(const QString &pattern) : file(pattern) {} + + bool open() + { + if (!file.open()) + return false; + + stream.setDevice(&file); + return true; + } + + void append(const Event &event) + { + stream << event; + } + + enum ReplayResult { + ReplaySuccess, + ReplayOpenFailed, + ReplayLoadFailed, + ReplayReadPastEnd + }; + + template<typename Loader> + ReplayResult replay(const Loader &loader) const + { + QFile readFile(file.fileName()); + if (!readFile.open(QIODevice::ReadOnly)) + return ReplayOpenFailed; + + QDataStream readStream(&readFile); + Event event; + while (!readStream.atEnd()) { + readStream >> event; + if (readStream.status() == QDataStream::ReadPastEnd) + return ReplayReadPastEnd; + if (!loader(event)) + return ReplayLoadFailed; + } + + return ReplaySuccess; + } + + void clear() + { + file.remove(); + stream.unsetDevice(); + } + + bool flush() + { + return file.flush(); + } + +private: + Utils::TemporaryFile file; + QDataStream stream; +}; + +} |