Fix console log window error

This commit is contained in:
Krad
2021-12-07 14:10:43 +08:00
parent 3db49c8cfb
commit fb20bac489
2 changed files with 16 additions and 9 deletions

View File

@@ -22,40 +22,45 @@ StdOutRedirector::StdOutRedirector()
close(1); close(1);
// Duplicate write end of original // Duplicate write end of original
dup2(fdguistd[1], 1); dup2(fdguistd[1], 1);
buffer = new char[bufferSize];
} }
StdOutRedirector::~StdOutRedirector() StdOutRedirector::~StdOutRedirector()
{ {
delete[] buffer;
} }
void StdOutRedirector::readOutsToTF() void StdOutRedirector::readOutsToTF()
{ {
size_t len;
char* copy;
int n_out; int n_out;
char* buffer = new char[4096];
QString str; QString str;
//char buffer[512]; //char buffer[512];
//qDebug() << "begin read...";
//qDebug() << "from qdebug..."; //qDebug() << "from qdebug...";
//printf("from printf...\n"); //printf("from printf...");
//std::cout << "from std::cout..." << std::endl; //std::cout << "from std::cout..." << std::endl;
fflush(stdout); fflush(stdout);
//Perhaps there is a non-blocking version of _read() that you can call ? //Perhaps there is a non-blocking version of _read() that you can call ?
n_out = read(fdguistd[0], buffer, 4096); memset(buffer, 0, bufferSize);
n_out = read(fdguistd[0], buffer, bufferSize);
if (n_out <= 0) if (n_out <= 0) {
return; return;
}
if (n_out >= 1) { if (n_out >= 1) {
str.append(QString(buffer)); str.append(QString(buffer));
int con = str.lastIndexOf('\n'); int con = str.lastIndexOf('\n');
int remv = str.at(con - 1) == '\n' ? 1 : 0; int remv = str.at(con - 1) == '\n' ? 1 : 0;
if (con) { if (con > 0) {
str = str.remove(con - remv, str.length()); str = str.remove(con - remv, str.length());
output->append(str); output->append(str);
} }
} }
return;
} }

View File

@@ -42,4 +42,6 @@ private:
QTextEdit* output; QTextEdit* output;
int fdStdOut; int fdStdOut;
int fdguistd[2]; int fdguistd[2];
char* buffer = nullptr;
const int bufferSize = 4096;
}; };