Fork me on GitHub
2.15.20
FGx
A cross-platform launcher for FlightGear
xprocess.cpp
Go to the documentation of this file.
1 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
2 // FGx FlightGear Launcher // xprocess.cpp
3 // -=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-
4 // (c) 2010-2012
5 // Yves Sablonier, Pete Morgan
6 // Geoff McLane
7 // GNU GPLv2, see main.cpp and shipped licence.txt for further information
8 
9 #include "xprocess.h"
10 #ifdef Q_OS_WIN
11 #include <process.h>
12 #endif
13 #include "utilities/utilities.h"
14 
15 XProcess::XProcess(MainObject *mob, QString logger_name, QObject *parent) :
16  QObject(parent)
17 {
18 
19  mainObject = mob;
20  log_name = logger_name;
21 
22  process = new QProcess(this);
23  process->setProcessChannelMode(QProcess::MergedChannels);
24  process->setReadChannel(QProcess::StandardOutput);
25 
26  connect(process, SIGNAL(stateChanged(QProcess::ProcessState)),
27  this, SLOT(on_process_state_change(QProcess::ProcessState)));
28 
29  connect(process, SIGNAL(readyRead()),
30  this, SLOT(on_process_output()));
31 
32  connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
33  this, SLOT(on_process_finished(int, QProcess::ExitStatus)));
34 
35  connect(process, SIGNAL(error(QProcess::ProcessError)),
36  this, SLOT(on_process_error(QProcess::ProcessError)));
37 }
38 
39 //===============================================================================
40 //== Start
41 void XProcess::start(QString command_line, QStringList extra_env)
42 {
44 
45  if (extra_env.size()) {
46  //= append new env vars
47  QStringList env = QProcess::systemEnvironment();
48  env << extra_env;
49  process->setEnvironment(env);
50  outLog("ENV:"+log_name+": ["+extra_env.join(" ")+"]");
51  // just to see ALL - outLog("NEW:"+log_name+": ["+env.join(" ")+"]");
52  }
53  outLog("CMD:"+log_name+": ["+command_line+"]");
54  process->start(command_line);
55  process->closeWriteChannel();
56 }
57 
58 //===============================================================================
59 //== Stop
61 {
62  process->kill();
63  process->waitForFinished();
64 }
65 
66 
67 
68 void XProcess::on_process_state_change(QProcess::ProcessState state)
69 {
70  switch (state) {
71  case QProcess::Starting:
72  emit running(true);
73  break;
74  case QProcess::Running:
75  emit running(true);
76  break;
77  case QProcess::NotRunning:
78  emit running(false);
79  break;
80  }
81 }
82 
84 {
85  QString line = process->readLine();
86  while (!line.isEmpty()) {
87  mainObject->add_log(log_name, line);
88  line = process->readLine();
89  }
90 }
91 
92 void XProcess::on_process_finished(int code, QProcess::ExitStatus status)
93 {
94  QString message = QString("process exited with code: %1, status: %2\n")
95  .arg(code)
96  .arg(status == QProcess::NormalExit ? "QProcess::NormalExit" : "QProcess::CrashExit");
97  mainObject->add_log(log_name, message);
98 }
99 
100 void XProcess::on_process_error(QProcess::ProcessError error)
101 {
102  QString message("$ error: ");
103 
104  switch (error) {
105  case QProcess::FailedToStart:
106  message += "failed to start; (incorrect path?)\n";
107  break;
108  case QProcess::Crashed:
109  message += "process crashed!\n";
110  break;
111  case QProcess::Timedout:
112  message += "process timedout!\n";
113  break;
114  case QProcess::WriteError:
115  message += "error writing to process\n";
116  break;
117  case QProcess::ReadError:
118  message += "error reading from process\n";
119  break;
120  case QProcess::UnknownError:
121  message += "unknown error\n";
122  break;
123  default:
124  message += "Uncased error!\n";
125  break;
126  }
127  mainObject->add_log(log_name, message);
128 }
129 
130 
132 #ifdef Q_OS_WIN
133  return _getpid();
134 #else
135  return process->pid();
136 #endif
137 }
void on_process_state_change(QProcess::ProcessState state)
Definition: xprocess.cpp:68
void on_process_finished(int code, QProcess::ExitStatus status)
Definition: xprocess.cpp:92
void clear_log(QString log_name)
Definition: mainobject.cpp:272
QProcess * process
Definition: xprocess.h:27
void running(bool)
The MainObject is an extended QObject, and the main "controller" for FGx.
Definition: mainobject.h:66
MainObject * mainObject
Definition: xprocess.h:26
void on_process_output()
Definition: xprocess.cpp:83
void start(QString command_line, QStringList user_env)
Definition: xprocess.cpp:41
int get_pid()
Definition: xprocess.cpp:131
XProcess(MainObject *mainObject, QString log_type_ki, QObject *parent=0)
Definition: xprocess.cpp:15
void on_process_error(QProcess::ProcessError error)
Definition: xprocess.cpp:100
void stop()
Definition: xprocess.cpp:60
QString log_name
Definition: xprocess.h:28
void add_log(QString log_name, QString data)
Definition: mainobject.cpp:276