Question

I am writing a program to interact with a linux machine through the serial port, and I am using pexpect.spawn as my main communication channel as follows:

proc = pexpect.spawn("cu dir -l /dev/ttyUSB0 -s 115200", logfile = *someFile*)

and I am sending commands to the machine with the sendline("cmd") method, and at the end of each session I parse the log file to see how the commands behaved.

I would like to be able to distinguish between lines that were printed to stdout and stderr from my log file, but currently I have no way of doing that. Is that a way to globally prepend each line printed to stderr with a given string?

Was it helpful?

Solution

You don't mention how you capture stdout and stderr, but one simple way distinguish the stdout and stderr is to simply place stdout and stderr in different files. For example:

./command.py >stdout-log 2>stderr-log

OTHER TIPS

I think this is a limitation of pexpect. You're basically dealing with a black box command prompt, so pexpect has no knowledge about whether a string returned to the console (effectively) is stdout or stderr, just that something came back. Can you safely assume a limited set of message and error formats in your system so that you could write some regex-based post-processor?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top