child_reader Class Reference

#include <child_reader.h>

Public Member Functions

 child_reader (std::string const &cmd, std::vector< std::string > const &args)
 ~child_reader ()
bool getline (std::string &result)
bool get_data (std::ostream &out, std::ostream &err)
int terminate_process ()
int error () const
std::string error_str () const

Private Member Functions

void exec_command (std::string const &cmd, std::vector< std::string > const &args)
bool block_read ()

Private Attributes

int fd1
int fd2
ssize_t pos1
ssize_t end1
ssize_t pos2
ssize_t end2
pid_t pid
int first_error
char * buf2
ssize_t sz_buf2
char * buf1
std::string process_name
bool is_terminated
bool terminate_on_exception
bool forked

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

Definition at line 39 of file child_reader.h.


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.

Definition at line 45 of file child_reader.cpp.

References buf1, buf2, and terminate_process().

Here is the call graph for this function:


Member Function Documentation

bool child_reader::block_read (  )  [private]

Definition at line 122 of file child_reader.cpp.

References buf1, buf2, end1, end2, fd1, fd2, and sz_buf2.

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

Definition at line 71 of file child_reader.h.

References first_error.

Referenced by _get_vmlinux_address_info(), and do_exec_command().

Here is the caller graph for this function:

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

Definition at line 256 of file child_reader.cpp.

References first_error, forked, is_terminated, process_name, and terminate_on_exception.

Referenced by _get_vmlinux_address_info(), and do_exec_command().

Here is the caller graph for this function:

void child_reader::exec_command ( std::string const &  cmd,
std::vector< std::string > const &  args 
) [private]
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().

Here is the caller graph for this function:

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().

Here is the caller graph for this function:

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

Definition at line 220 of file child_reader.cpp.

References fd1, fd2, first_error, is_terminated, pid, and terminate_on_exception.

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

Here is the caller graph for this function:


Field Documentation

char* child_reader::buf1 [private]

Definition at line 104 of file child_reader.h.

Referenced by block_read(), and ~child_reader().

char* child_reader::buf2 [private]

Definition at line 102 of file child_reader.h.

Referenced by block_read(), and ~child_reader().

ssize_t child_reader::end1 [private]

Definition at line 95 of file child_reader.h.

Referenced by block_read().

ssize_t child_reader::end2 [private]

Definition at line 97 of file child_reader.h.

Referenced by block_read().

int child_reader::fd1 [private]

Definition at line 92 of file child_reader.h.

Referenced by block_read(), and terminate_process().

int child_reader::fd2 [private]

Definition at line 93 of file child_reader.h.

Referenced by block_read(), and terminate_process().

Definition at line 99 of file child_reader.h.

Referenced by error(), error_str(), and terminate_process().

bool child_reader::forked [private]

Definition at line 108 of file child_reader.h.

Referenced by error_str().

Definition at line 106 of file child_reader.h.

Referenced by error_str(), and terminate_process().

pid_t child_reader::pid [private]

Definition at line 98 of file child_reader.h.

Referenced by terminate_process().

ssize_t child_reader::pos1 [private]

Definition at line 94 of file child_reader.h.

ssize_t child_reader::pos2 [private]

Definition at line 96 of file child_reader.h.

std::string child_reader::process_name [private]

Definition at line 105 of file child_reader.h.

Referenced by error_str().

ssize_t child_reader::sz_buf2 [private]

Definition at line 103 of file child_reader.h.

Referenced by block_read().

Definition at line 107 of file child_reader.h.

Referenced by error_str(), and terminate_process().


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

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1