]> git.cworth.org Git - apitrace/blob - gui/retracer.cpp
Use double buffer visuals by default.
[apitrace] / gui / retracer.cpp
1 #include "retracer.h"
2
3 #include "apitracecall.h"
4
5 #include <QDebug>
6 #include <QVariant>
7
8 #include <qjson/parser.h>
9
10 Retracer::Retracer(QObject *parent)
11     : QThread(parent),
12       m_benchmarking(false),
13       m_doubleBuffered(true),
14       m_captureState(false),
15       m_captureCall(0)
16 {
17 #ifdef Q_OS_WIN
18     QString format = QLatin1String("%1;");
19 #else
20     QString format = QLatin1String("%1:");
21 #endif
22     QString buildPath = format.arg(BUILD_DIR);
23     m_processEnvironment = QProcessEnvironment::systemEnvironment();
24     m_processEnvironment.insert("PATH", buildPath +
25                                 m_processEnvironment.value("PATH"));
26
27     qputenv("PATH",
28             m_processEnvironment.value("PATH").toLatin1());
29 }
30
31 QString Retracer::fileName() const
32 {
33     return m_fileName;
34 }
35
36 void Retracer::setFileName(const QString &name)
37 {
38     m_fileName = name;
39 }
40
41 bool Retracer::isBenchmarking() const
42 {
43     return m_benchmarking;
44 }
45
46 void Retracer::setBenchmarking(bool bench)
47 {
48     m_benchmarking = bench;
49 }
50
51 bool Retracer::isDoubleBuffered() const
52 {
53     return m_doubleBuffered;
54 }
55
56 void Retracer::setDoubleBuffered(bool db)
57 {
58     m_doubleBuffered = db;
59 }
60
61 void Retracer::setCaptureAtCallNumber(qlonglong num)
62 {
63     m_captureCall = num;
64 }
65
66 qlonglong Retracer::captureAtCallNumber() const
67 {
68     return m_captureCall;
69 }
70
71 bool Retracer::captureState() const
72 {
73     return m_captureState;
74 }
75
76 void Retracer::setCaptureState(bool enable)
77 {
78     m_captureState = enable;
79 }
80
81
82 void Retracer::run()
83 {
84     RetraceProcess *retrace = new RetraceProcess();
85     retrace->process()->setProcessEnvironment(m_processEnvironment);
86
87     retrace->setFileName(m_fileName);
88     retrace->setBenchmarking(m_benchmarking);
89     retrace->setDoubleBuffered(m_doubleBuffered);
90     retrace->setCaptureState(m_captureState);
91     retrace->setCaptureAtCallNumber(m_captureCall);
92
93     connect(retrace, SIGNAL(finished(const QString&)),
94             this, SLOT(cleanup()));
95     connect(retrace, SIGNAL(error(const QString&)),
96             this, SLOT(cleanup()));
97     connect(retrace, SIGNAL(finished(const QString&)),
98             this, SIGNAL(finished(const QString&)));
99     connect(retrace, SIGNAL(error(const QString&)),
100             this, SIGNAL(error(const QString&)));
101     connect(retrace, SIGNAL(foundState(const ApiTraceState&)),
102             this, SIGNAL(foundState(const ApiTraceState&)));
103     connect(retrace, SIGNAL(retraceErrors(const QList<RetraceError>&)),
104             this, SIGNAL(retraceErrors(const QList<RetraceError>&)));
105
106     retrace->start();
107
108     exec();
109
110     /* means we need to kill the process */
111     if (retrace->process()->state() != QProcess::NotRunning) {
112         retrace->terminate();
113     }
114
115     delete retrace;
116 }
117
118
119 void RetraceProcess::start()
120 {
121     QStringList arguments;
122
123     if (m_doubleBuffered) {
124         arguments << QLatin1String("-db");
125     } else {
126         arguments << QLatin1String("-sb");
127     }
128
129     if (m_captureState) {
130         arguments << QLatin1String("-D");
131         arguments << QString::number(m_captureCall);
132     } else {
133         if (m_benchmarking) {
134             arguments << QLatin1String("-b");
135         }
136     }
137
138     arguments << m_fileName;
139
140     m_process->start(QLatin1String("glretrace"), arguments);
141 }
142
143
144 void RetraceProcess::replayFinished()
145 {
146     QByteArray output = m_process->readAllStandardOutput();
147     QString msg;
148     QString errStr = m_process->readAllStandardError();
149
150 #if 0
151     qDebug()<<"Process finished = ";
152     qDebug()<<"\terr = "<<errStr;
153     qDebug()<<"\tout = "<<output;
154 #endif
155     if (m_captureState) {
156         bool ok = false;
157         QVariantMap parsedJson = m_jsonParser->parse(output, &ok).toMap();
158         ApiTraceState state(parsedJson);
159         emit foundState(state);
160         msg = tr("State fetched.");
161     } else {
162         msg = QString::fromUtf8(output);
163     }
164
165     QStringList errorLines = errStr.split('\n');
166     QList<RetraceError> errors;
167     QRegExp regexp("(^\\d+): +(\\b\\w+\\b): (.+$)");
168     foreach(QString line, errorLines) {
169         if (regexp.indexIn(line) != -1) {
170             RetraceError error;
171             error.callIndex = regexp.cap(1).toInt();
172             error.type = regexp.cap(2);
173             error.message = regexp.cap(3);
174             errors.append(error);
175         }
176     }
177     if (!errors.isEmpty()) {
178         emit retraceErrors(errors);
179     }
180     emit finished(msg);
181 }
182
183 void RetraceProcess::replayError(QProcess::ProcessError err)
184 {
185     qDebug()<<"Process error = "<<err;
186     qDebug()<<"\terr = "<<m_process->readAllStandardError();
187     qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
188
189     emit error(
190         tr("Couldn't execute the replay file '%1'").arg(m_fileName));
191 }
192
193 Q_DECLARE_METATYPE(QList<RetraceError>);
194 RetraceProcess::RetraceProcess(QObject *parent)
195     : QObject(parent)
196 {
197     m_process = new QProcess(this);
198     m_jsonParser = new QJson::Parser();
199
200     qRegisterMetaType<QList<RetraceError> >();
201
202     connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
203             this, SLOT(replayFinished()));
204     connect(m_process, SIGNAL(error(QProcess::ProcessError)),
205             this, SLOT(replayError(QProcess::ProcessError)));
206 }
207
208 QProcess * RetraceProcess::process() const
209 {
210     return m_process;
211 }
212
213 QString RetraceProcess::fileName() const
214 {
215     return m_fileName;
216 }
217
218 void RetraceProcess::setFileName(const QString &name)
219 {
220     m_fileName = name;
221 }
222
223 bool RetraceProcess::isBenchmarking() const
224 {
225     return m_benchmarking;
226 }
227
228 void RetraceProcess::setBenchmarking(bool bench)
229 {
230     m_benchmarking = bench;
231 }
232
233 bool RetraceProcess::isDoubleBuffered() const
234 {
235     return m_doubleBuffered;
236 }
237
238 void RetraceProcess::setDoubleBuffered(bool db)
239 {
240     m_doubleBuffered = db;
241 }
242
243 void RetraceProcess::setCaptureAtCallNumber(qlonglong num)
244 {
245     m_captureCall = num;
246 }
247
248 qlonglong RetraceProcess::captureAtCallNumber() const
249 {
250     return m_captureCall;
251 }
252
253 bool RetraceProcess::captureState() const
254 {
255     return m_captureState;
256 }
257
258 void RetraceProcess::setCaptureState(bool enable)
259 {
260     m_captureState = enable;
261 }
262
263 void RetraceProcess::terminate()
264 {
265     if (m_process) {
266         m_process->terminate();
267         emit finished(tr("Process terminated."));
268     }
269 }
270
271 void Retracer::cleanup()
272 {
273     quit();
274 }
275
276 RetraceProcess::~RetraceProcess()
277 {
278     delete m_jsonParser;
279 }
280
281 #include "retracer.moc"