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

@ -17,14 +17,17 @@ qTabMessages::qTabMessages(QWidget *parent) : QWidget(parent) {
FILE_LOG(logDEBUG) << "Messages ready";
}
qTabMessages::~qTabMessages() {}
void qTabMessages::Refresh() {
dispCommand->clear();
qTabMessages::~qTabMessages() {
process->close();
if (process)
delete process;
}
void qTabMessages::SetupWidgetWindow() {
process = new QProcess;
process->setWorkingDirectory(QDir::cleanPath(QDir::currentPath()));
PrintNextLine();
qDebugStream(std::cout, this);
qDebugStream(std::cerr, this);
@ -34,31 +37,51 @@ void qTabMessages::SetupWidgetWindow() {
void qTabMessages::Initialization() {
connect(btnSave, SIGNAL(clicked()), this, SLOT(SaveLog()));
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() {
QString command = dispCommand->text();
dispLog->append(command);
QStringList param = dispCommand->text().split(" ");
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
QStringList arguments;
QProcess *myProcess = new QProcess(this);
myProcess->start(command, arguments);
// print readall
QByteArray result = myProcess.readAll();
PrintNextLine();
process->setProcessChannelMode(QProcess::MergedChannels);
process->start(command, param);
if(!process->waitForFinished()) {
AppendError();
} else {
AppendOutput();
}
}
void qTabMessages::PrintNextLine() {
QString path = QDir::cleanPath(QDir::currentPath());
dispLog->append(QString("\n") + path + QString("$"));
dispLog->append(QString("<font color = \"DarkGrey\">") + QDir::current().dirName() + QString("$ ") + QString("</font>"));
}
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) {
if (e->type() == (STREAMEVENT)) {
QString temp = ((qStreamEvent *)e)->getString();
@ -67,29 +90,35 @@ void qTabMessages::customEvent(QEvent *e) {
}
void qTabMessages::SaveLog() {
QString fName = QString(""); //FIXME:current directory?
fName = fName + "/LogFile.txt";
QString fName = QDir::cleanPath(QDir::currentPath()) + "/LogFile.txt";
fName = QFileDialog::getSaveFileName(this, tr("Save Snapshot "),
fName, tr("Text files (*.txt);;All Files(*)"));
if (!fName.isEmpty()) {
QFile outfile;
outfile.setFileName(fName);
if (outfile.open(QIODevice::WriteOnly | QIODevice::Text)) { //Append
if (outfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&outfile);
out << dispLog->toPlainText() << endl;
qDefs::Message(qDefs::INFORMATION, std::string("The Log has been successfully saved to "
"") +
fName.toAscii().constData(),
"qTabMessages::SaveLog");
std::string mess = std::string("The Log has been successfully saved to ") + fName.toAscii().constData();
qDefs::Message(qDefs::INFORMATION, mess, "TabMessages::SaveLog");
FILE_LOG(logINFO) << mess;
} 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");
}
}
dispCommand->setFocus();
}
void qTabMessages::ClearLog() {
dispLog->clear();
FILE_LOG(logINFO) << "Log Cleared";
PrintNextLine();
dispCommand->setFocus();
}
void qTabMessages::Refresh() {
dispCommand->clear();
dispCommand->setFocus();
}