|
| 1 | +// system headers |
| 2 | +#include <tuple> |
| 3 | +#include <thread> |
| 4 | + |
| 5 | +// local headers |
| 6 | +#include <linuxdeploy/plugin/plugin_process_handler.h> |
| 7 | +#include <linuxdeploy/subprocess/process.h> |
| 8 | +#include <linuxdeploy/util/util.h> |
| 9 | +#include <linuxdeploy/core/log.h> |
| 10 | +#include <linuxdeploy/subprocess/pipe_reader.h> |
| 11 | + |
| 12 | +namespace bf = boost::filesystem; |
| 13 | + |
| 14 | +namespace linuxdeploy { |
| 15 | + namespace plugin { |
| 16 | + using namespace core::log; |
| 17 | + |
| 18 | + plugin_process_handler::plugin_process_handler(std::string name, bf::path path) : name_(std::move(name)), path_(std::move(path)) {} |
| 19 | + |
| 20 | + int plugin_process_handler::run(const bf::path& appDir) const { |
| 21 | + // prepare arguments and environment variables |
| 22 | + const std::initializer_list<std::string> args = {path_.string(), "--appdir", appDir.string()}; |
| 23 | + |
| 24 | + subprocess::subprocess_env_map_t environmentVariables{}; |
| 25 | + |
| 26 | + // add $LINUXDEPLOY, which points to the current binary |
| 27 | + // we do not need to pass $APPIMAGE or alike, since while linuxdeploy is running, the path in the |
| 28 | + // temporary mountpoint of its AppImage will be valid anyway |
| 29 | + environmentVariables["LINUXDEPLOY"] = linuxdeploy::util::getOwnExecutablePath(); |
| 30 | + |
| 31 | + linuxdeploy::subprocess::process proc{args, environmentVariables}; |
| 32 | + |
| 33 | + // we want to insert a custom log prefix whenever a CR or LF is written into either buffer |
| 34 | + // like in subprocess's check_output, we use pipe readers to read from the subprocess's stdout/stderr pipes |
| 35 | + // however, we just dump everything we receive directly in the log, using our ĺogging framework |
| 36 | + // we store an ldLog instance per stream so we can just send all data into those, which allows us to get away |
| 37 | + // 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), |
| 42 | + }; |
| 43 | + |
| 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); |
| 51 | + |
| 52 | + const auto log_prefix = "[" + name_ + "/" + stream_name + "] "; |
| 53 | + |
| 54 | + // since we have our own ldLog instance for every pipe, we can get away with this rather small read buffer |
| 55 | + subprocess::subprocess_result_buffer_t intermediate_buffer(4096); |
| 56 | + |
| 57 | + // (try to) read from pipe |
| 58 | + const auto bytes_read = reader.read(intermediate_buffer); |
| 59 | + |
| 60 | + // 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; |
| 66 | + } |
| 67 | + |
| 68 | + // we just trim the buffer to the bytes we read (makes the code below easier) |
| 69 | + intermediate_buffer.resize(bytes_read); |
| 70 | + |
| 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'); |
| 76 | + |
| 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}); |
| 80 | + |
| 81 | + if (next_control_char == intermediate_buffer.end()) { |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 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 | + ); |
| 90 | + |
| 91 | + log << log_prefix; |
| 92 | + |
| 93 | + it = next_control_char; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // do-while might be a little more elegant, but we can save this one unnecessary sleep, so... |
| 99 | + if (proc.is_running()) { |
| 100 | + // reduce load on CPU |
| 101 | + std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 102 | + } else { |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return proc.close(); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | +} |
0 commit comments