23#include <boost/algorithm/string/join.hpp>
24#include <boost/iostreams/copy.hpp>
25#include <boost/process/args.hpp>
26#include <boost/process/child.hpp>
27#include <boost/process/env.hpp>
28#include <boost/process/exe.hpp>
29#include <boost/process/io.hpp>
30#include <boost/process/pipe.hpp>
31#include <boost/process/search_path.hpp>
33using namespace boost::process;
34using namespace boost::iostreams;
52 std::streamsize
write(
char const* str, std::streamsize
size)
54 std::string_view consoleStr(str,
size);
55 size_t lineEnd = consoleStr.find_first_of(
"\r\n");
56 std::streamsize processedCharacters =
size;
57 if (lineEnd != std::string_view::npos)
59 consoleStr = consoleStr.substr(0, lineEnd);
60 processedCharacters = lineEnd + 1;
63 if (!consoleStr.empty())
66 return processedCharacters;
74 return { std::forward<T>(callback) };
79 std::vector<std::string>
const& argsVector,
80 std::string
const& logger, std::string
const& input,
83#if TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT
85#pragma warning(disable:4297)
97#if TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT
103 TC_LOG_TRACE(logger,
"Starting process \"{}\" with arguments: \"{}\".",
104 executable, boost::algorithm::join(argsVector,
" "));
108 std::shared_ptr<FILE> inputFile(!input.empty() ? fopen(input.c_str(),
"rb") :
nullptr, [](FILE* ptr)
121 exe = boost::filesystem::absolute(executable).string(),
123 env = environment(boost::this_process::environment()),
124 std_in = inputFile.get(),
133 exe = boost::filesystem::absolute(executable).string(),
135 env = environment(boost::this_process::environment()),
136 std_in = boost::process::close,
153 copy(outStream, outInfo);
154 copy(errStream, outError);
158 int const result = waiter(c);
162 TC_LOG_TRACE(logger,
">> Process \"{}\" finished with return value {}.",
169int StartProcess(std::string
const& executable, std::vector<std::string>
const& args,
170 std::string
const& logger, std::string input_file,
bool secure)
177 return c.exit_code();
183 }, executable, args, logger, input_file, secure);
190 std::vector<std::string>
const args;
203 std::string logger_, std::string input_file_,
221 my_child = std::reference_wrapper<child>(c);
241 result = std::make_shared<std::future<int>>(std::move(result_));
270std::shared_ptr<AsyncProcessResult>
272 std::string logger, std::string input_file,
bool secure)
274 auto handle = std::make_shared<AsyncProcessResultImplementation>(
275 std::move(executable), std::move(args), std::move(logger), std::move(input_file), secure);
277 handle->SetFuture(std::async(std::launch::async, [handle] {
return handle->StartProcess(); }));
285 return search_path(filename).string();
#define TC_LOG_TRACE(filterType__,...)
#define TC_LOG_ERROR(filterType__,...)
#define TC_LOG_INFO(filterType__,...)
std::optional< T > Optional
Optional helper class to wrap optional values within.
Optional< std::reference_wrapper< child > > my_child
std::future< int > & GetFutureResult() override
AsyncProcessResultImplementation & operator=(AsyncProcessResultImplementation const &)=delete
std::vector< std::string > const args
Optional< std::shared_ptr< std::future< int > > > result
std::string const executable
AsyncProcessResultImplementation(AsyncProcessResultImplementation &&)=delete
AsyncProcessResultImplementation(AsyncProcessResultImplementation const &)=delete
void SetFuture(std::future< int > result_)
void Terminate() override
Tries to terminate the process.
std::atomic< bool > was_terminated
AsyncProcessResultImplementation(std::string executable_, std::vector< std::string > args_, std::string logger_, std::string input_file_, bool secure)
std::string const input_file
std::streamsize write(char const *str, std::streamsize size)
std::shared_ptr< AsyncProcessResult > StartAsyncProcess(std::string executable, std::vector< std::string > args, std::string logger, std::string input_file, bool secure)
std::string SearchExecutableInPath(std::string const &filename)
static int CreateChildProcess(T waiter, std::string const &executable, std::vector< std::string > const &argsVector, std::string const &logger, std::string const &input, bool secure)
int StartProcess(std::string const &executable, std::vector< std::string > const &args, std::string const &logger, std::string input_file, bool secure)
auto MakeTCLogSink(T &&callback) -> TCLogSink< typename std::decay< T >::type >
constexpr std::size_t size()