Skip to content

Commit 8ee0e43

Browse files
committed
Switch to new subprocess lib in type 0 plugins, mk. 3
This commit fixes the remaining buffer issues by introducing a little more complexity. It shall be refactored in the future to decrease complexity again, but for now, it works well and function is more important than form at this point. CC #143
1 parent e9bbbc8 commit 8ee0e43

1 file changed

Lines changed: 59 additions & 39 deletions

File tree

‎src/plugin/plugin_process_handler.cpp‎

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// system headers
22
#include <tuple>
33
#include <thread>
4+
#include <utility>
45

56
// local headers
67
#include <linuxdeploy/plugin/plugin_process_handler.h>
@@ -15,7 +16,8 @@ namespace linuxdeploy {
1516
namespace plugin {
1617
using namespace core::log;
1718

18-
plugin_process_handler::plugin_process_handler(std::string name, bf::path path) : name_(std::move(name)), path_(std::move(path)) {}
19+
plugin_process_handler::plugin_process_handler(std::string name, bf::path path) : name_(std::move(name)),
20+
path_(std::move(path)) {}
1921

2022
int plugin_process_handler::run(const bf::path& appDir) const {
2123
// prepare arguments and environment variables
@@ -35,62 +37,80 @@ namespace linuxdeploy {
3537
// however, we just dump everything we receive directly in the log, using our ĺogging framework
3638
// we store an ldLog instance per stream so we can just send all data into those, which allows us to get away
3739
// with relatively small buffers (we don't have to cache complete lines or alike)
38-
// parameter order: pipe reader, log type (used in prefix), ldLog instance, first message
39-
std::array<std::tuple<pipe_reader, std::string, ldLog, bool>, 2> pipes_to_be_logged{
40-
std::make_tuple(pipe_reader(proc.stdout_fd()), "stdout", ldLog{}, true),
41-
std::make_tuple(pipe_reader(proc.stderr_fd()), "stderr", ldLog{}, true),
40+
class pipe_to_be_logged {
41+
public:
42+
pipe_reader reader_;
43+
std::string stream_name_;
44+
ldLog log_;
45+
bool print_prefix_in_next_iteration_;
46+
47+
pipe_to_be_logged(int pipe_fd, std::string stream_name) : reader_(pipe_fd),
48+
stream_name_(std::move(stream_name)),
49+
log_(),
50+
print_prefix_in_next_iteration_(true) {}
4251
};
4352

44-
for (;;) {
45-
for (auto& tuple : pipes_to_be_logged) {
46-
// make code in this loop more readable
47-
auto& reader = std::get<0>(tuple);
48-
const auto& stream_name = std::get<1>(tuple);
49-
auto& log = std::get<2>(tuple);
50-
auto& is_first_message = std::get<3>(tuple);
53+
std::array<pipe_to_be_logged, 2> pipes_to_be_logged{
54+
pipe_to_be_logged(proc.stdout_fd(), "stdout"),
55+
pipe_to_be_logged(proc.stderr_fd(), "stderr"),
56+
};
5157

52-
const auto log_prefix = "[" + name_ + "/" + stream_name + "] ";
58+
for (;;) {
59+
for (auto& pipe_to_be_logged : pipes_to_be_logged) {
60+
const auto log_prefix = "[" + name_ + "/" + pipe_to_be_logged.stream_name_ + "] ";
5361

5462
// since we have our own ldLog instance for every pipe, we can get away with this rather small read buffer
5563
subprocess::subprocess_result_buffer_t intermediate_buffer(4096);
5664

5765
// (try to) read from pipe
58-
const auto bytes_read = reader.read(intermediate_buffer);
66+
const auto bytes_read = pipe_to_be_logged.reader_.read(intermediate_buffer);
5967

6068
// no action required in case we have not read anything from the pipe
61-
if (bytes_read > 0) {
62-
// special handling for the first message
63-
if (is_first_message) {
64-
log << log_prefix;
65-
is_first_message = false;
69+
if (bytes_read <= 0) {
70+
continue;
71+
}
72+
73+
// we just trim the buffer to the bytes we read (makes the code below easier)
74+
intermediate_buffer.resize(bytes_read);
75+
76+
// all we have to do now is to look for CR or LF, send everything up to that location into the ldLog instance,
77+
// write our prefix and then repeat
78+
for (auto it = intermediate_buffer.begin(); it != intermediate_buffer.end(); ++it) {
79+
if (pipe_to_be_logged.print_prefix_in_next_iteration_) {
80+
pipe_to_be_logged.log_ << log_prefix;
6681
}
6782

68-
// we just trim the buffer to the bytes we read (makes the code below easier)
69-
intermediate_buffer.resize(bytes_read);
83+
const auto next_lf = std::find(it, intermediate_buffer.end(), '\n');
84+
const auto next_cr = std::find(it, intermediate_buffer.end(), '\r');
7085

71-
// all we have to do now is to look for CR or LF, send everything up to that location into the ldLog instance,
72-
// write our prefix and then repeat
73-
for (auto it = intermediate_buffer.begin(); it != intermediate_buffer.end(); ++it) {
74-
const auto next_lf = std::find(it, intermediate_buffer.end(), '\n');
75-
const auto next_cr = std::find(it, intermediate_buffer.end(), '\r');
86+
// we don't care which one goes first -- we pick the closest one, write everything up to it into our ldLog,
87+
// then print our prefix and repeat that until there's nothing left in our buffer
88+
auto next_control_char = std::min({next_lf, next_cr});
7689

77-
// we don't care which one goes first -- we pick the closest one, write everything up to it into our ldLog,
78-
// then print our prefix and repeat that until there's nothing left in our buffer
79-
auto next_control_char = std::min({next_lf, next_cr});
90+
// if there is a control char, we remember this for the next iteration, where we print our
91+
// log prefix
92+
// in any case, we can write the remaining buffer contents into the ldLog object
93+
pipe_to_be_logged.print_prefix_in_next_iteration_ = (next_control_char !=
94+
intermediate_buffer.end());
8095

81-
if (next_control_char == intermediate_buffer.end()) {
82-
break;
83-
}
96+
const auto distance_from_begin_to_it = std::distance(intermediate_buffer.begin(), it);
97+
auto distance_from_it_to_next_cc = std::distance(it, next_control_char);
98+
99+
if (pipe_to_be_logged.print_prefix_in_next_iteration_) {
100+
distance_from_it_to_next_cc++;
101+
}
84102

85-
// need to make sure we include the control char in the write
86-
log.write(
87-
intermediate_buffer.data() + std::distance(intermediate_buffer.begin(), it),
88-
std::distance(it, next_control_char) + 1
89-
);
103+
// need to make sure we include the control char in the write
104+
pipe_to_be_logged.log_.write(
105+
intermediate_buffer.data() + distance_from_begin_to_it,
106+
distance_from_it_to_next_cc
107+
);
90108

91-
log << log_prefix;
109+
it = next_control_char;
92110

93-
it = next_control_char;
111+
// TODO: should not be necessary, should be fixed in for loop
112+
if (!pipe_to_be_logged.print_prefix_in_next_iteration_) {
113+
break;
94114
}
95115
}
96116
}

0 commit comments

Comments
 (0)