]> git.cworth.org Git - apitrace/blob - gui/traceprocess.cpp
6f4d0b9350da147249bbf1e522fd8c51d6de4384
[apitrace] / gui / traceprocess.cpp
1 #include "traceprocess.h"
2
3 #include <QDebug>
4 #include <QDir>
5 #include <QFile>
6 #include <QFileInfo>
7
8 TraceProcess::TraceProcess(QObject *parent)
9     : QObject(parent),
10       m_canTrace(true)
11 {
12     m_process = new QProcess(this);
13
14     connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
15             this, SLOT(traceFinished()));
16     connect(m_process, SIGNAL(error(QProcess::ProcessError)),
17             this, SLOT(traceError(QProcess::ProcessError)));
18
19 #ifdef Q_OS_WIN
20     QString format = QLatin1String("%1;");
21 #else
22     QString format = QLatin1String("%1:");
23 #endif
24     QString buildPath = format.arg(APITRACE_BINARY_DIR);
25     QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
26     env.insert("PATH", buildPath + env.value("PATH"));
27     qputenv("PATH", env.value("PATH").toLatin1());
28 }
29
30 TraceProcess::~TraceProcess()
31 {
32 }
33
34 void TraceProcess::setApi(const QString &str)
35 {
36     m_api = str;
37 }
38
39 void TraceProcess::setExecutablePath(const QString &str)
40 {
41     m_execPath = str;
42
43     QFileInfo fi(m_execPath);
44     QString baseName = fi.baseName();
45
46     QString format = QString::fromLatin1("%1.trace");
47
48     m_tracePath = format
49                   .arg(baseName);
50
51     int i = 1;
52     while (QFile::exists(m_tracePath)) {
53         format = QString::fromLatin1("%1.%2.trace");
54         m_tracePath = format
55                       .arg(baseName)
56                       .arg(i++);
57     }
58 }
59
60 QString TraceProcess::executablePath() const
61 {
62     return m_execPath;
63 }
64
65 void TraceProcess::setArguments(const QStringList &args)
66 {
67     m_args = args;
68 }
69
70 QStringList TraceProcess::arguments() const
71 {
72     return m_args;
73 }
74
75 void TraceProcess::traceFinished()
76 {
77 #if 0
78     qDebug()<<"trace finished on " << m_tracePath;
79     qDebug()<<"\terr = "<<m_process->readAllStandardError();
80     qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
81 #endif
82     emit tracedFile(m_tracePath);
83 }
84
85 void TraceProcess::traceError(QProcess::ProcessError err)
86 {
87 #if 1
88     qDebug()<<"trace error = "<<m_tracePath;
89     qDebug()<<"\terr = "<<m_process->readAllStandardError();
90     qDebug()<<"\tout = "<<m_process->readAllStandardOutput();
91 #endif
92     emit error(m_process->readAllStandardError());
93 }
94
95
96 void TraceProcess::start()
97 {
98     QStringList arguments;
99
100     arguments << QLatin1String("trace");
101     arguments << QLatin1String("--api");
102     arguments << m_api;
103     arguments << QLatin1String("--output");
104     arguments << m_tracePath;
105     arguments << QLatin1String("--");
106     arguments << m_execPath;
107     arguments << m_args;
108
109     m_process->start(QLatin1String("apitrace"), arguments);
110 }
111
112 bool TraceProcess::canTrace() const
113 {
114     return m_canTrace;
115 }
116
117 #include "traceprocess.moc"