我编写了一个 Qt 应用程序,它作为守护进程在 Ubuntu 后台运行。这个应用程序在随机时间后崩溃,我想看看它在崩溃时给出了什么错误。
我写这个语法是为了看看我是否可以在文本中得到它的输出:
dimit@dimit-FX620DX:~$ ./Myapp &> DebugInfo.txt
但它不会在文本文件中写入任何内容。我写了这个简单的程序来模拟更大的项目
主窗口.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;
}
这是我运行这段代码时终端的输出:
dimit@dimit-FX620DX:~/QtProjects/Test/Test-build-desktop-Qt_4_8_1__System__Debug$ ./Myapp &> DebugInfo.txt
Aborted (core dumped)
我希望在 DebugInfo.txt 文件中看到“Aborted (core dumped)”,但里面什么也没有写。我该怎么办?
我在 Ubuntu 12.04 下使用 Qt 4.8
最佳答案
“中止(核心转储)”文本不是由您的程序打印的,它是由 shell 在程序终止后打印的。所以,解决方案很简单,在另一个 shell 中运行程序,并重定向它的输出:
sh -c ./Myapp &> DebugInfo.txt
(-c
使 shell 将命令作为命令行执行,这是必需的,因为没有它 sh
将尝试运行 ./Myapp
作为 shell 脚本并失败。)
关于c++ - 如何在 Ubuntu 中从命令行将 Qt 控制台输出存储到文本文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24340440/