-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWrapper.cpp
167 lines (126 loc) · 3.38 KB
/
Wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <QCoreApplication>
#include <windows.h>
#include "Wrapper.h"
#include "print.h"
#include "log.h"
Wrapper::Wrapper() : pump(proc)
{
connect(&proc, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(handle_error()));
connect(&proc, SIGNAL(readyReadStandardError()),
this, SLOT(handle_stderr()));
connect(&proc, SIGNAL(readyReadStandardOutput()),
this, SLOT(handle_stdout()));
connect(&proc, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(handle_finished(int, QProcess::ExitStatus)));
connect(&proc, SIGNAL(started()),
this, SLOT(handle_started()));
connect(&timer, SIGNAL(timeout()),
this, SLOT(handle_timeout()));
connect(&pump, SIGNAL(error(QString)),
this, SLOT(handle_pump_error(QString)));
}
void
Wrapper::start()
{
status = STARTING;
our_log( ( sprint "INFO: Starting %s[%d]\n" % name, pid ) );
proc.start(command);
timer.start(30*1000);
}
void
Wrapper::log(const char* msg)
{
log_message(name, msg);
eprint "%s: %s" % name, msg;
}
void
Wrapper::handle_finished(int exit_code, QProcess::ExitStatus status)
{
// proc.setReadChannel(QProcess::StandardError);
// proc.waitForReadyRead();
// proc.readLine(buf, sizeof buf);
// log(buf);
// printf("<<<%s>>>\n", buf);
handle_stdout();
if (status == QProcess::CrashExit)
our_log( (sprint "CRITICAL: %s[%d] crashed\n" % name, pid) );
else
{
if (exit_code == 0)
our_log( (sprint "INFO: %s[%d] finished with code %d\n" % name, pid, exit_code) );
else
our_log( (sprint "WARN: %s[%d] finished with code %d\n" % name, pid, exit_code) );
}
pump.terminate();
pump.wait(5000);
QCoreApplication::quit();
}
void
Wrapper::handle_stderr()
{
proc.setReadChannel(QProcess::StandardError);
while (proc.canReadLine())
{
proc.readLine(buf, sizeof buf);
log(buf);
}
}
void
Wrapper::handle_stdout()
{
proc.setReadChannel(QProcess::StandardOutput);
int count;
while ( (count = proc.read(buf, sizeof buf)) > 0 )
{
fwrite(buf, 1, count, stdout);
}
fflush(stdout);
}
void
Wrapper::handle_error()
{
if (status == STARTING)
our_log( (sprint "FATAL: Could not start %s[%d]: %s\n" %
name, pid, proc.errorString()) );
else
our_log( (sprint "CRITICAL: %s[%d] encountered an error: %s\n" %
name, pid, proc.errorString()) );
quit();
}
void
Wrapper::handle_started()
{
timer.stop();
status = RUNNING;
pid = proc.pid()->dwProcessId;
pump.start();
if (timeout > 0)
timer.start(timeout);
}
void
Wrapper::handle_timeout()
{
if (status == STARTING)
our_log( ( sprint "FATAL: Could not start %s[%d]: Timed out\n" % name, pid ) );
else if (status == RUNNING)
{
status = KILLING;
our_log( ( sprint "CRITICAL: %s[%d] timed out and will be killed.\n" % name, pid) );
}
quit();
}
void
Wrapper::handle_pump_error(QString const& error)
{
our_log( (sprint "CRITICAL: %s[%d] encountered an error: %s\n" % name, pid, error) );
quit();
}
void
Wrapper::quit()
{
if (proc.state() != QProcess::NotRunning)
proc.kill();
else
QCoreApplication::quit();
}