Re: [C++-sig] How to capture stdout/stderr?
Thanks Bert, that put on hopefully the right track to do the same thing in C++. I can almost do it completely, and I'll detail the code in another email. Thanks, Peter
Here is my, considerably low-tech, code :
At init time,
----------- const std::string CatchOutput = "class StdoutCatcher:\n" "\tdef __init__(self):\n" "\t\tself.data = ''\n" "\tdef write(self, stuff):\n" "\t\tself.data = self.data + stuff\n" "\n" "import sys\n" "TheStdoutCatcher = StdoutCatcher()\n" "sys.stdout = TheStdoutCatcher\n";
Run (CatchOutput); // thin wrapper to run a snippet -----------
and then after some python code has run :
----------- using namespace boost::python; object Catcher (main_namespace ["TheStdoutCatcher"]); object CatcherData (borrowed (PyObject_GetAttrString (Catcher.ptr (), "data")));
const std::string &S = extract<std::string>(CatcherData); Output += S; // some log string Run ("sys.stdout.data=''\n"); -----------
Note I'm pretty new at this, so the only warranty is "the above didn't crash so far" :) Now that I paste it, I'm not sure about that const ref to the result of extract.
hth, bert
_______________________________________________ C++-sig mailing list C++-sig@python.org http://mail.python.org/mailman/listinfo/c++-sig
participants (1)
-
Peter