Dimitry
Dimitry

Reputation: 81

How to store Qt console output into text file from command line in Ubuntu

I wrote a Qt application which runs in Ubuntu Background as a Daemon. This application crashes after a random time, I want to see what error it gives when it crashesh.

I wrote this syntax to see if I can get its output in a text:

dimit@dimit-FX620DX:~$ ./Myapp &> DebugInfo.txt

but it doesn't write anything in the text file. I wrote this simple program to simulate the bigger project

Mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    std::vector<int> Test;
    Test[-1] = 90;  //in here it gives segmentation fault error and exits the program
}

MainWindow::~MainWindow()
{
    delete ui;
}

here is the output of the terminal when I run this code:

dimit@dimit-FX620DX:~/QtProjects/Test/Test-build-desktop-Qt_4_8_1__System__Debug$ ./Myapp  &> DebugInfo.txt
Aborted (core dumped)

I expected to see "Aborted (core dumped)" in the DebugInfo.txt file but nothing were written in it. What should I do ?

I'm using Qt 4.8 under Ubuntu 12.04

Upvotes: 2

Views: 1651

Answers (1)

hyde
hyde

Reputation: 62898

The "Aborted (core dumped)" text is not printed by your program, it's printed by the shell after program terminates. So, solution is simple, run the program in another shell, and redirect it's output:

sh -c ./Myapp &> DebugInfo.txt

(The -c makes the shell execute the command as a command line, and is needed because without it sh would try to run ./Myapp as a shell script and fail.)

Upvotes: 1

Related Questions