LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Embedded & Single-board computer (http://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/)
-   -   Redirecting console output from a Qt application to a telnet session (http://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/redirecting-console-output-from-a-qt-application-to-a-telnet-session-850434/)

bameije 12-15-2010 12:53 PM

Redirecting console output from a Qt application to a telnet session
 
Hi,
if been struggling with this problem for a while now.

I've got an embedded Linux running on an ARM platform (Freescale iMX257).
The bootloader (RedBoot) starts Linux with the option
Code:

console=ttymxc0
When Linux is started, it automatically starts a dedicated Qt application.

When I'm connected via the UART, my terminal program will let me type commands and get output from my running Qt application.

But when the module is in the field, I want to access it via Telnet, as there won't be a UART connection, only LAN.
I can open a telnet session, which will let me type commands and get output from those commands. But I can't see the output of my Qt application as this is still emitted on the UART.

How can I redirect the output of that application to the telnet session?

I thought this would do:
(via telnet on ttyp0)
Code:

$ cat /dev/console > /dev/ttyp0
Now everything I type through the UART appears on the telnet session. But I can't execute it, nor can I see the output of my Qt application.

I tried to modify the command running my application in the /etc/inittab:
Code:

./myQtApp > /dev/ttyp0
but now the application won't start before I've opened the telnet session. Even then, only 'some' output from my App is passed to the telnet session (output written by "std::cout", output written by Qt's "qDebug()" function is still on the UART...)

I started playing around with /dev/ttymxc0 as well, but without result...

What am I doing wrong here?

Thanks in advance!

cnxsoft 12-16-2010 03:10 AM

Can you redirect the output of Qt in a file (e.g. in the ramdisk or storage), then telnet and read the file ?

start script:
./myQtApp > /tmp/qt_debug.log 2>&1
---
telnet:
tail -f /tmp/qt_debug.log

If you need to limit the size of the log, use logrotate

theNbomr 12-16-2010 10:50 AM

I have solved a similar problem by launching applications in a Gnu screen session that is running in multi-user mode. Then, I can attach and detach from the session at arbitrary times or from various connection methods (serial console, telnet, ssh, etc.).

--- rod.

bameije 12-16-2010 11:32 AM

Thanks for the hint.
Although I'd rather avoid using a file (as there isn't much spare space on the device), you gave me a valuable hint:

I added the " 2>&1" to my start-myQtApp script that's called by inittab:
Code:

./myQtApp > /dev/ttyp0 2>&1
Now it works! So Qt's qDebug() writes to stderr...

So now I can:
-open a telnet session
-vi start-myQtApp and add the redirection
-kill myQtApp
-myQtApp will respawn and be redirected to the telnet session.

It's a start but it includes killing myQtApp, which can cause loss of valuable information about the state the application was in before I killed it.

I also can't add the redirection to start-myQtApp as a default, because then I get the following error when the app starts when the telnet session is not active (=99% of the time):
Code:

can't create /dev/ttyp0: Input/output error
and the app is never started...

My question boils down to this:
Is there a way to redirect the stdout/stderr stream of a *running* application?

I already found some solutions involving the use of external programs
(using gdb, using screen), but again these take a lot of space in my file system.
I like the way they do it with gdb. But can't this be done without that workaround?

bameije 01-21-2011 10:32 AM

Hi all,

I found it.

The solution is to add this to the program code: (c++)

Code:

void redirect( QString device )
{
  freopen( device, "w", stdout );
  freopen( device, "w", stderr );
}

Now, I can start a telnet session, send a command to my application (using an interface that I already had) that requests a redirection. The string argument 'device' is set to the tty of the telnet session.

I haven't found a solution that does this without editing the source code. (except using screen or gdb, which wasn't an option for me)


All times are GMT -5. The time now is 12:43 PM.