hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ProcExec.cpp
Go to the documentation of this file.
1 #include <sstream>
2 #include <iterator>
3 #include <string.h>
4 #include <errno.h>
5 #include <stdexcept>
6 #include <Poco/Process.h>
7 #include <Poco/PipeStream.h>
8 #include <Poco/Exception.h>
9 
10 #include "ProcExec.hpp"
11 
12 namespace HCE
13 {
14 //-----------------------------------------------------------------------------
15 ProcExec::ProcExec(const std::string& command_)
16 : command(""), args(), env(), initialDirectory(""), _IsError(false), exitStatus(0)
17 {
18  std::stringstream istr(command_), ostr;
19  std::istream_iterator<std::string> eos;
20  std::istream_iterator<std::string> iit(istr);
21 
22  ostr << *iit;
23  command = ostr.str();
24 
25  while(++iit!=eos)
26  {
27  args.push_back(*iit);
28  }
29 }
30 //-----------------------------------------------------------------------------
31 ProcExec::ProcExec(const std::string& command_, const Poco::Process::Args& args_)
32 : command(command_), args(args_), env(), initialDirectory(""), _IsError(false), exitStatus(0)
33 {
34 }
35 //-----------------------------------------------------------------------------
36 ProcExec::ProcExec(const std::string& command_, const Poco::Process::Args& args_, const Poco::Process::Env& env_)
37 : command(command_), args(args_), env(env_), initialDirectory(""), _IsError(false), exitStatus(0)
38 {
39 }
40 //-----------------------------------------------------------------------------
41 bool ProcExec::exec(std::ostream& os, std::ostream& es)
42 {
43  _IsError = true;
44 
45  try
46  {
47  Poco::Pipe outPipe, errPipe;
48  Poco::ProcessHandle ph = Poco::Process::launch(command, args, initialDirectory, 0, &outPipe, &errPipe, env);
49 
50  Poco::PipeInputStream ostr(outPipe);
51  Poco::PipeInputStream estr(errPipe);
52 
53  exitStatus = ph.wait();
54  if (exitStatus != 0)
55  throw std::runtime_error(std::string("ProcExec::exec: ")+strerror(errno));
56 
57  os << ostr.rdbuf();
58  es << estr.rdbuf();
59 
60  _IsError = false;
61  }
62  catch(Poco::Exception& e)
63  {
64  es << " " << e.displayText();
65  }
66  catch(std::exception& e)
67  {
68  es << " " << e.what();
69  }
70  return !_IsError;
71 }
72 //-----------------------------------------------------------------------------
73 std::ostream& operator << (std::ostream& os, const ProcExec& procExec)
74 {
75  const_cast<ProcExec&>(procExec).exec(os, os);
76  return os;
77 }
78 //-----------------------------------------------------------------------------
79 std::string& operator << (std::string& str, const ProcExec& procExec)
80 {
81  std::stringstream outMsg;
82  outMsg << procExec;
83  if (!procExec.isError())
84  str = outMsg.str();
85  return str;
86 }
87 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
89 } // end namespace HCE
90 
91 
92