forked from DonLakeFlyer/MavlinkTagController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitoredProcess.cpp
106 lines (88 loc) · 2.71 KB
/
MonitoredProcess.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
#include "MonitoredProcess.h"
#include "sendStatusText.h"
#include "log.h"
#include <string>
#include <iostream>
#include <thread>
#include <filesystem>
MonitoredProcess::MonitoredProcess(
MavlinkOutgoingMessageQueue& outgoingMessageQueue,
const char* name,
const char* command,
const char* logPath,
IntermediatePipeType intermediatePipeType,
bp::pipe* intermediatePipe)
: _outgoingMessageQueue (outgoingMessageQueue)
, _name (name)
, _command (command)
, _logPath (logPath)
, _intermediatePipeType (intermediatePipeType)
, _intermediatePipe (intermediatePipe)
{
}
void MonitoredProcess::start(void)
{
// FIXME: We leak these thread objects
_thread = new std::thread(&MonitoredProcess::_run, this);
_thread->detach();
}
void MonitoredProcess::stop(void)
{
logDebug() << "MonitoredProcess::stop _childProcess:_childProcess.running"
<< _childProcess
<< (_childProcess ? _childProcess->running() : false);
if (_childProcess) {
_terminated = true;
_childProcess->terminate();
}
}
void MonitoredProcess::_run(void)
{
std::string statusStr("Process start: ");
statusStr.append(_name);
logInfo() << statusStr << "'" << _command.c_str() << "' >" << _logPath.c_str();
sendStatusText(_outgoingMessageQueue, statusStr.c_str());
std::filesystem::remove(_logPath);
try {
switch (_intermediatePipeType ) {
case NoPipe:
_childProcess = new bp::child(_command.c_str(), bp::std_out > _logPath, bp::std_err > _logPath);
break;
case InputPipe:
_childProcess = new bp::child(_command.c_str(), bp::std_in < *_intermediatePipe, bp::std_out > _logPath, bp::std_err > _logPath);
break;
case OutputPipe:
_childProcess = new bp::child(_command.c_str(), bp::std_out > *_intermediatePipe, bp::std_err > _logPath);
break;
}
} catch(bp::process_error& e) {
logError() << "MonitoredProcess::run boost::process:child threw process_error exception -" << e.what();
_terminated = true;
// } catch(...) {
// std::cout << "MonitoredProcess::run boost::process:child threw unknown exception" << std::endl;
// _terminated = true;
}
int result = 255;
if (_childProcess) {
_childProcess->wait();
result = _childProcess->exit_code();
}
if (result == 0) {
statusStr = "Process end: ";
} else if (_terminated) {
statusStr = "Process terminated: ";
} else {
char numStr[21];
statusStr = "Process fail: ";
sprintf(numStr, "%d", result);
statusStr.append(numStr);
statusStr.append(" ");
}
statusStr.append(_name);
delete _childProcess;
_childProcess = NULL;
_terminated = false;
logError() << statusStr;
sendStatusText(_outgoingMessageQueue, statusStr.c_str(), result == 0 ? MAV_SEVERITY_INFO : MAV_SEVERITY_ERROR);
delete this;
}