child_reader Class Reference

a class to read stdout / stderr from a child process. More...

#include <child_reader.h>

List of all members.

Public Member Functions

 child_reader (std::string const &cmd, std::vector< std::string > const &args)
 fork a process.
 ~child_reader ()
 wait for the termination of the child process if this have not already occur.
bool getline (std::string &result)
 fill result from on line of stdout of the child process.
bool get_data (std::ostream &out, std::ostream &err)
 fill out / err with the stdout / stderr of the child process.
int terminate_process ()
 rather to rely on dtor to wait for the termination of the child you can use terminate_process() to get the return code of the child process
int error () const
 return the status of the first error encoutered != 0 : something feel wrong, use error_str() to get an error message
std::string error_str () const
 return an error message if appropriate, if the process has been successfully exec'ed and is not terminate the error message is always empty.

Detailed Description

a class to read stdout / stderr from a child process.

two interfaces are provided. read line by line: getline() or read all data in one : get_data(). In all case get_data() must be called once to flush the stderr child output


Constructor & Destructor Documentation

child_reader::child_reader ( std::string const &  cmd,
std::vector< std::string > const &  args 
)

fork a process.

use error() to get error code. Do not try to use other public member interface if error() return non-zero

child_reader::~child_reader (  ) 

wait for the termination of the child process if this have not already occur.

In this case return code of the child process is not available.

References terminate_process().


Member Function Documentation

int child_reader::error (  )  const [inline]

return the status of the first error encoutered != 0 : something feel wrong, use error_str() to get an error message

Referenced by _get_vmlinux_address_info(), and do_exec_command().

string child_reader::error_str (  )  const

return an error message if appropriate, if the process has been successfully exec'ed and is not terminate the error message is always empty.

Error message is also empty if the child process terminate successfully. Else three type of error message exist:

  • "unable to fork" followed by sterror(errno)
  • "process_name return xxx" xxx is return code
  • "process_name terminated by signal xxx" xxx is signal number

Referenced by _get_vmlinux_address_info(), and do_exec_command().

bool child_reader::get_data ( std::ostream &  out,
std::ostream &  err 
)

fill out / err with the stdout / stderr of the child process.

You can call this after calling one or more time getline(...). This call is blocking until the child die and so on all subsequent call will fail

Referenced by _get_vmlinux_address_info(), and do_exec_command().

bool child_reader::getline ( std::string &  result  ) 

fill result from on line of stdout of the child process.

must be used as: child_reader reader(...); while (reader.getline(line)) ....

Referenced by _get_vmlinux_address_info().

int child_reader::terminate_process (  ) 

rather to rely on dtor to wait for the termination of the child you can use terminate_process() to get the return code of the child process

Referenced by _get_vmlinux_address_info(), do_exec_command(), and ~child_reader().


The documentation for this class was generated from the following files:

Generated on 29 Jul 2013 for oprofile by  doxygen 1.6.1