任何方法从python中捕获最后一行错误消息(使用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 a lot of error messages
}
当我输出结果时,包含的消息太多,大多数消息对最终用户来说都没有意义。现在我使用boost :: split来捕获错误消息的最后一行。
//I miss auto so much
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 it = std::remove(splitted.begin(), splitted.end(), "");
splitted.erase(it, splitted.end());
return splitted.back();