This commit is contained in:
2019-07-08 12:18:29 +02:00
parent 4b10991429
commit 74bb7a3f1b
3 changed files with 64 additions and 29 deletions

View File

@ -127,9 +127,9 @@
</layout> </layout>
</widget> </widget>
<tabstops> <tabstops>
<tabstop>btnSave</tabstop>
<tabstop>btnClear</tabstop>
<tabstop>dispCommand</tabstop> <tabstop>dispCommand</tabstop>
<tabstop>btnClear</tabstop>
<tabstop>btnSave</tabstop>
</tabstops> </tabstops>
<resources> <resources>
<include location="../include/icons.qrc"/> <include location="../include/icons.qrc"/>

View File

@ -3,6 +3,8 @@
#include "ui_form_tab_messages.h" #include "ui_form_tab_messages.h"
#include "qDebugStream.h" #include "qDebugStream.h"
class QProcess;
class qTabMessages:public QWidget, private Ui::TabMessagesObject { class qTabMessages:public QWidget, private Ui::TabMessagesObject {
Q_OBJECT Q_OBJECT
@ -21,5 +23,9 @@ private:
void SetupWidgetWindow(); void SetupWidgetWindow();
void Initialization(); void Initialization();
void PrintNextLine(); void PrintNextLine();
void AppendOutput();
void AppendError();
QProcess* process;
}; };

View File

@ -17,14 +17,17 @@ qTabMessages::qTabMessages(QWidget *parent) : QWidget(parent) {
FILE_LOG(logDEBUG) << "Messages ready"; FILE_LOG(logDEBUG) << "Messages ready";
} }
qTabMessages::~qTabMessages() {} qTabMessages::~qTabMessages() {
process->close();
void qTabMessages::Refresh() { if (process)
dispCommand->clear(); delete process;
} }
void qTabMessages::SetupWidgetWindow() { void qTabMessages::SetupWidgetWindow() {
process = new QProcess;
process->setWorkingDirectory(QDir::cleanPath(QDir::currentPath()));
PrintNextLine(); PrintNextLine();
qDebugStream(std::cout, this); qDebugStream(std::cout, this);
qDebugStream(std::cerr, this); qDebugStream(std::cerr, this);
@ -34,31 +37,51 @@ void qTabMessages::SetupWidgetWindow() {
void qTabMessages::Initialization() { void qTabMessages::Initialization() {
connect(btnSave, SIGNAL(clicked()), this, SLOT(SaveLog())); connect(btnSave, SIGNAL(clicked()), this, SLOT(SaveLog()));
connect(btnClear, SIGNAL(clicked()), this, SLOT(ClearLog())); connect(btnClear, SIGNAL(clicked()), this, SLOT(ClearLog()));
connect(dispCommand, SIGNAL(editingFinished()), this, SLOT(ExecuteCommand())); connect(dispCommand, SIGNAL(returnPressed()), this, SLOT(ExecuteCommand()));
//connect(process, SIGNAL(readAllStandardError()), this, SLOT(AppendError()));
//connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(AppendOutput()));
} }
void qTabMessages::ExecuteCommand() { void qTabMessages::ExecuteCommand() {
QString command = dispCommand->text(); QStringList param = dispCommand->text().split(" ");
dispLog->append(command);
dispCommand->clear(); dispCommand->clear();
// appending command to log without newline
dispLog->moveCursor (QTextCursor::End);
dispLog->insertHtml(QString("<font color = \"DarkBlue\">") + param.join(" ") + QString("</font>"));
QString command = param.at(0);
param.removeFirst();
FILE_LOG(logINFO) << "Executing Command:[" << command.toAscii().constData() << "] with Arguments:[" << param.join(" ").toAscii().constData() << "]";
// take 1st string as program process->setProcessChannelMode(QProcess::MergedChannels);
QStringList arguments; process->start(command, param);
if(!process->waitForFinished()) {
QProcess *myProcess = new QProcess(this); AppendError();
myProcess->start(command, arguments); } else {
AppendOutput();
// print readall }
QByteArray result = myProcess.readAll();
PrintNextLine();
} }
void qTabMessages::PrintNextLine() { void qTabMessages::PrintNextLine() {
QString path = QDir::cleanPath(QDir::currentPath()); dispLog->append(QString("<font color = \"DarkGrey\">") + QDir::current().dirName() + QString("$ ") + QString("</font>"));
dispLog->append(QString("\n") + path + QString("$"));
} }
void qTabMessages::AppendOutput() {
QByteArray result = process->readAll();
result.replace("\n", "<br>");
dispLog->append(QString("<font color = \"DarkBlue\">") + result + QString("</font>"));
FILE_LOG(logDEBUG) << "Command executed successfully";
PrintNextLine();
}
void qTabMessages::AppendError() {
dispLog->append(QString("<font color = \"Red\">") + process->errorString() + QString("</font>"));
FILE_LOG(logERROR) << "Error executing command";
PrintNextLine();
}
void qTabMessages::customEvent(QEvent *e) { void qTabMessages::customEvent(QEvent *e) {
if (e->type() == (STREAMEVENT)) { if (e->type() == (STREAMEVENT)) {
QString temp = ((qStreamEvent *)e)->getString(); QString temp = ((qStreamEvent *)e)->getString();
@ -67,29 +90,35 @@ void qTabMessages::customEvent(QEvent *e) {
} }
void qTabMessages::SaveLog() { void qTabMessages::SaveLog() {
QString fName = QString(""); //FIXME:current directory? QString fName = QDir::cleanPath(QDir::currentPath()) + "/LogFile.txt";
fName = fName + "/LogFile.txt";
fName = QFileDialog::getSaveFileName(this, tr("Save Snapshot "), fName = QFileDialog::getSaveFileName(this, tr("Save Snapshot "),
fName, tr("Text files (*.txt);;All Files(*)")); fName, tr("Text files (*.txt);;All Files(*)"));
if (!fName.isEmpty()) { if (!fName.isEmpty()) {
QFile outfile; QFile outfile;
outfile.setFileName(fName); outfile.setFileName(fName);
if (outfile.open(QIODevice::WriteOnly | QIODevice::Text)) { //Append if (outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&outfile); QTextStream out(&outfile);
out << dispLog->toPlainText() << endl; out << dispLog->toPlainText() << endl;
qDefs::Message(qDefs::INFORMATION, std::string("The Log has been successfully saved to " std::string mess = std::string("The Log has been successfully saved to ") + fName.toAscii().constData();
"") + qDefs::Message(qDefs::INFORMATION, mess, "TabMessages::SaveLog");
fName.toAscii().constData(), FILE_LOG(logINFO) << mess;
"qTabMessages::SaveLog");
} else { } else {
FILE_LOG(logWARNING) << "Attempt to save log file failed."; FILE_LOG(logWARNING) << "Attempt to save log file failed: " << fName.toAscii().constData();
qDefs::Message(qDefs::WARNING, "Attempt to save log file failed.", "qTabMessages::SaveLog"); qDefs::Message(qDefs::WARNING, "Attempt to save log file failed.", "qTabMessages::SaveLog");
} }
} }
dispCommand->setFocus();
} }
void qTabMessages::ClearLog() { void qTabMessages::ClearLog() {
dispLog->clear(); dispLog->clear();
FILE_LOG(logINFO) << "Log Cleared"; FILE_LOG(logINFO) << "Log Cleared";
PrintNextLine();
dispCommand->setFocus();
}
void qTabMessages::Refresh() {
dispCommand->clear();
dispCommand->setFocus();
} }