c++ - How to catch the last line error message by boost::python -
any way catch last line error message python(using msvc2008)?
try { boost::python::exec_file( "somescript.py", main_namespace, local_namespace ); return true; }catch(const boost::python::error_already_set &){ using namespace boost::python; pyobject *exc,*val,*tb; pyerr_fetch(&exc,&val,&tb); boost::python::handle<> hexc(exc), hval(boost::python::allow_null(val)), htb(boost::python::allow_null(tb)); boost::python::object traceback(boost::python::import("traceback")); boost::python::object format_exception(traceback.attr("format_exception")); boost::python::object formatted_list = format_exception(hexc, hval, htb); boost::python::object formatted = boost::python::str("\n").join(formatted_list); std::string const result = extract<std::string>(formatted); std::cout<<result<<std::endl; //pop out lot of error messages }
when output result, there contains many messages, of messages meaningless end users.by use boost::split catch last line of error messages.
//i miss auto typedef boost::container::vector<std::string>::iterator iter; std::string const result = extract<std::string>(formatted); boost::container::vector<std::string> splitted; boost::split(splitted, result, boost::is_any_of("\n"), boost::token_compress_on); iter = std::remove(splitted.begin(), splitted.end(), ""); splitted.erase(it, splitted.end()); return splitted.back();
Comments
Post a Comment