]> git.cworth.org Git - apitrace/commitdiff
Abstract retracing into a class of its own.
authorZack Rusin <zack@kde.org>
Wed, 6 Apr 2011 05:11:55 +0000 (01:11 -0400)
committerZack Rusin <zack@kde.org>
Wed, 6 Apr 2011 05:11:55 +0000 (01:11 -0400)
preparing for adding options to benchmark and use double buffering
from the gui.

gui/CMakeLists.txt
gui/mainwindow.cpp
gui/mainwindow.h
gui/retracer.cpp [new file with mode: 0644]
gui/retracer.h [new file with mode: 0644]

index 7917104a58684ad8d6a19eef036e44a745a72244..620f4249359c49c879b2ce6385926507e9fba5cc 100644 (file)
@@ -7,6 +7,7 @@ set(qapitrace_SRCS
    loaderthread.cpp
    mainwindow.cpp
    main.cpp
+   retracer.cpp
    settingsdialog.cpp
  )
 
index 0a3349be4572e855871ac010220277000a462fc4..f221112a16b364f5db64638e4c3f3b82d0975020 100644 (file)
@@ -5,6 +5,7 @@
 #include "apicalldelegate.h"
 #include "apitracemodel.h"
 #include "apitracefilter.h"
+#include "retracer.h"
 #include "settingsdialog.h"
 
 #include <qjson/parser.h>
@@ -15,7 +16,6 @@
 #include <QFileDialog>
 #include <QLineEdit>
 #include <QMessageBox>
-#include <QProcess>
 #include <QProgressBar>
 #include <QToolBar>
 #include <QWebView>
 
 MainWindow::MainWindow()
     : QMainWindow(),
-      m_replayProcess(0),
       m_selectedEvent(0),
       m_stateEvent(0),
-      m_findingState(false),
       m_jsonParser(new QJson::Parser())
 {
     m_ui.setupUi(this);
@@ -38,6 +36,12 @@ MainWindow::MainWindow()
     connect(m_trace, SIGNAL(finishedLoadingTrace()),
             this, SLOT(finishedLoadingTrace()));
 
+    m_retracer = new Retracer(this);
+    connect(m_retracer, SIGNAL(finished(const QByteArray&)),
+            this, SLOT(replayFinished(const QByteArray&)));
+    connect(m_retracer, SIGNAL(error(const QString&)),
+            this, SLOT(replayError(const QString&)));
+
     m_model = new ApiTraceModel();
     m_model->setApiTrace(m_trace);
     m_proxyModel = new ApiTraceFilter();
@@ -142,13 +146,10 @@ void MainWindow::replayStart()
 
 void MainWindow::replayStop()
 {
-    if (m_replayProcess) {
-        m_replayProcess->kill();
-
-        m_ui.actionStop->setEnabled(false);
-        m_ui.actionReplay->setEnabled(true);
-        m_ui.actionLookupState->setEnabled(true);
-    }
+    m_retracer->terminate();
+    m_ui.actionStop->setEnabled(false);
+    m_ui.actionReplay->setEnabled(true);
+    m_ui.actionLookupState->setEnabled(true);
 }
 
 void MainWindow::newTraceFile(const QString &fileName)
@@ -165,21 +166,13 @@ void MainWindow::newTraceFile(const QString &fileName)
     }
 }
 
-void MainWindow::replayFinished()
+void MainWindow::replayFinished(const QByteArray &output)
 {
     m_ui.actionStop->setEnabled(false);
     m_ui.actionReplay->setEnabled(true);
     m_ui.actionLookupState->setEnabled(true);
 
-    QByteArray output = m_replayProcess->readAllStandardOutput();
-
-#if 0
-    qDebug()<<"Process finished = ";
-    qDebug()<<"\terr = "<<m_replayProcess->readAllStandardError();
-    qDebug()<<"\tout = "<<output;
-#endif
-
-    if (m_findingState) {
+    if (m_retracer->captureState()) {
         bool ok = false;
         QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
         parseState(parsedJson[QLatin1String("parameters")].toMap());
@@ -187,23 +180,17 @@ void MainWindow::replayFinished()
         statusBar()->showMessage(output);
     }
     m_stateEvent = 0;
-    m_findingState = false;
 }
 
-void MainWindow::replayError(QProcess::ProcessError err)
+void MainWindow::replayError(const QString &message)
 {
     m_ui.actionStop->setEnabled(false);
     m_ui.actionReplay->setEnabled(true);
     m_ui.actionLookupState->setEnabled(true);
-    m_findingState = false;
     m_stateEvent = 0;
 
-    qDebug()<<"Process error = "<<err;
-    qDebug()<<"\terr = "<<m_replayProcess->readAllStandardError();
-    qDebug()<<"\tout = "<<m_replayProcess->readAllStandardOutput();
     QMessageBox::warning(
-        this, tr("Replay Failed"),
-        tr("Couldn't execute the replay file '%1'").arg(m_traceFileName));
+        this, tr("Replay Failed"), message);
 }
 
 void MainWindow::startedLoadingTrace()
@@ -228,33 +215,12 @@ void MainWindow::finishedLoadingTrace()
 
 void MainWindow::replayTrace(bool dumpState)
 {
-    if (!m_replayProcess) {
-#ifdef Q_OS_WIN
-        QString format = QLatin1String("%1;");
-#else
-        QString format = QLatin1String("%1:");
-#endif
-        QString buildPath = format.arg(BUILD_DIR);
-        QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
-        env.insert("PATH", buildPath + env.value("PATH"));
-
-        qputenv("PATH", env.value("PATH").toLatin1());
-
-        m_replayProcess = new QProcess(this);
-        m_replayProcess->setProcessEnvironment(env);
-
-        connect(m_replayProcess, SIGNAL(finished(int, QProcess::ExitStatus)),
-                this, SLOT(replayFinished()));
-        connect(m_replayProcess, SIGNAL(error(QProcess::ProcessError)),
-                this, SLOT(replayError(QProcess::ProcessError)));
-    }
-
     if (m_traceFileName.isEmpty())
         return;
 
-    QStringList arguments;
-    if (dumpState &&
-        m_selectedEvent) {
+    m_retracer->setFileName(m_traceFileName);
+    m_retracer->setCaptureState(dumpState);
+    if (m_retracer->captureState() && m_selectedEvent) {
         int index = 0;
         if (m_selectedEvent->type() == ApiTraceEvent::Call) {
             index = static_cast<ApiTraceCall*>(m_selectedEvent)->index;
@@ -270,13 +236,9 @@ void MainWindow::replayTrace(bool dumpState)
             qDebug()<<"Unknown event type";
             return;
         }
-        arguments << QLatin1String("-D");
-        arguments << QString::number(index);
+        m_retracer->setCaptureAtCallNumber(index);
     }
-    arguments << m_traceFileName;
-
-    m_replayProcess->start(QLatin1String("glretrace"),
-                           arguments);
+    m_retracer->start();
 
     m_ui.actionStop->setEnabled(true);
 }
@@ -290,7 +252,6 @@ void MainWindow::lookupState()
         return;
     }
     m_stateEvent = m_selectedEvent;
-    m_findingState = true;
     replayTrace(true);
 }
 
index e66bd937ffea9dd52f7e041e1d159c80265eb962..9d0c15e49b851d4f1306cd13fd8953f806a644e4 100644 (file)
@@ -13,8 +13,8 @@ class ApiTraceFrame;
 class ApiTraceModel;
 class QLineEdit;
 class QModelIndex;
-class QProcess;
 class QProgressBar;
+class Retracer;
 
 namespace QJson {
     class Parser;
@@ -36,8 +36,8 @@ private slots:
     void filterTrace();
     void replayStart();
     void replayStop();
-    void replayFinished();
-    void replayError(QProcess::ProcessError err);
+    void replayFinished(const QByteArray &output);
+    void replayError(const QString &msg);
     void startedLoadingTrace();
     void finishedLoadingTrace();
     void lookupState();
@@ -56,8 +56,6 @@ private:
     ApiTraceFilter *m_proxyModel;
     QLineEdit *m_filterEdit;
 
-    QProcess *m_replayProcess;
-
     QProgressBar *m_progressBar;
 
     QString m_traceFileName;
@@ -65,9 +63,10 @@ private:
     ApiTraceEvent *m_selectedEvent;
 
     ApiTraceEvent *m_stateEvent;
-    bool m_findingState;
 
     QJson::Parser *m_jsonParser;
+
+    Retracer *m_retracer;
 };
 
 
diff --git a/gui/retracer.cpp b/gui/retracer.cpp
new file mode 100644 (file)
index 0000000..b9d74b6
--- /dev/null
@@ -0,0 +1,136 @@
+#include "retracer.h"
+
+#include <QDebug>
+
+Retracer::Retracer(QObject *parent)
+    : QObject(parent),
+      m_benchmarking(true),
+      m_doubleBuffered(true),
+      m_captureState(false),
+      m_captureCall(0),
+      m_process(0)
+{
+}
+
+QString Retracer::fileName() const
+{
+    return m_fileName;
+}
+
+void Retracer::setFileName(const QString &name)
+{
+    m_fileName = name;
+}
+
+bool Retracer::isBenchmarking() const
+{
+    return m_benchmarking;
+}
+
+void Retracer::setBenchmarking(bool bench)
+{
+    m_benchmarking = bench;
+}
+
+bool Retracer::isDoubleBuffered() const
+{
+    return m_doubleBuffered;
+}
+
+void Retracer::setDoubleBuffered(bool db)
+{
+    m_doubleBuffered = db;
+}
+
+void Retracer::start()
+{
+    if (!m_process) {
+#ifdef Q_OS_WIN
+        QString format = QLatin1String("%1;");
+#else
+        QString format = QLatin1String("%1:");
+#endif
+        QString buildPath = format.arg(BUILD_DIR);
+        QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
+        env.insert("PATH", buildPath + env.value("PATH"));
+
+        qputenv("PATH", env.value("PATH").toLatin1());
+
+        m_process = new QProcess(this);
+        m_process->setProcessEnvironment(env);
+
+        connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
+                this, SLOT(replayFinished()));
+        connect(m_process, SIGNAL(error(QProcess::ProcessError)),
+                this, SLOT(replayError(QProcess::ProcessError)));
+    }
+
+    QStringList arguments;
+
+    if (m_captureState) {
+        arguments << QLatin1String("-D");
+        arguments << QString::number(m_captureCall);
+    } else {
+        if (m_benchmarking) {
+            arguments << QLatin1String("-b");
+        }
+        if (m_doubleBuffered) {
+            arguments << QLatin1String("-db");
+        }
+    }
+
+    arguments << m_fileName;
+
+    m_process->start(QLatin1String("glretrace"), arguments);
+}
+
+void Retracer::terminate()
+{
+    if (m_process) {
+        m_process->terminate();
+    }
+}
+
+void Retracer::setCaptureAtCallNumber(qlonglong num)
+{
+    m_captureCall = num;
+}
+
+qlonglong Retracer::captureAtCallNumber() const
+{
+    return m_captureCall;
+}
+
+bool Retracer::captureState() const
+{
+    return m_captureState;
+}
+
+void Retracer::setCaptureState(bool enable)
+{
+    m_captureState = enable;
+}
+
+void Retracer::replayFinished()
+{
+    QByteArray output = m_process->readAllStandardOutput();
+
+#if 0
+    qDebug()<<"Process finished = ";
+    qDebug()<<"\terr = "<<m_process->readAllStandardError();
+    qDebug()<<"\tout = "<<output;
+#endif
+    emit finished(output);
+}
+
+void Retracer::replayError(QProcess::ProcessError err)
+{
+    qDebug()<<"Process error = "<<err;
+    qDebug()<<"\terr = "<<m_process->readAllStandardError();
+    qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
+
+    emit error(
+        tr("Couldn't execute the replay file '%1'").arg(m_fileName));
+}
+
+#include "retracer.moc"
diff --git a/gui/retracer.h b/gui/retracer.h
new file mode 100644 (file)
index 0000000..37502e4
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef RETRACER_H
+#define RETRACER_H
+
+#include <QObject>
+#include <QProcess>
+
+class Retracer : public QObject
+{
+    Q_OBJECT
+public:
+    Retracer(QObject *parent=0);
+
+    QString fileName() const;
+    void setFileName(const QString &name);
+
+    bool isBenchmarking() const;
+    void setBenchmarking(bool bench);
+
+    bool isDoubleBuffered() const;
+    void setDoubleBuffered(bool db);
+
+    void setCaptureAtCallNumber(qlonglong num);
+    qlonglong captureAtCallNumber() const;
+
+    bool captureState() const;
+    void setCaptureState(bool enable);
+
+public slots:
+    void start();
+    void terminate();
+
+signals:
+    void finished(const QByteArray &output);
+    void error(const QString &msg);
+
+private slots:
+    void replayFinished();
+    void replayError(QProcess::ProcessError err);
+private:
+    QString m_fileName;
+    bool m_benchmarking;
+    bool m_doubleBuffered;
+    bool m_captureState;
+    qlonglong m_captureCall;
+
+    QProcess *m_process;
+};
+
+#endif