Skip to content

Commit

Permalink
Replace local 8-bit decoding with UTF-8 decoding
Browse files Browse the repository at this point in the history
Handles incomplete byte sequences using `QTextDecoder`

Signed-off-by: Mitchell Skaggs <skaggsm333@gmail.com>
  • Loading branch information
magneticflux- committed Aug 9, 2022
1 parent 0e473f4 commit a14476c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
28 changes: 7 additions & 21 deletions launcher/LoggedProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
*/

#include "LoggedProcess.h"
#include "MessageLevel.h"
#include <QDebug>
#include <QTextDecoder>
#include "MessageLevel.h"

LoggedProcess::LoggedProcess(QObject *parent) : QProcess(parent)
{
Expand All @@ -59,25 +60,22 @@ LoggedProcess::~LoggedProcess()
}
}

QStringList reprocess(const QByteArray & data, QString & leftover)
QStringList reprocess(const QByteArray& data, QTextDecoder& decoder)
{
QString str = leftover + QString::fromLocal8Bit(data);

str.remove('\r');
QStringList lines = str.split("\n");
leftover = lines.takeLast();
auto str = decoder.toUnicode(data);
auto lines = str.remove(QChar::CarriageReturn).split(QChar::LineFeed, Qt::SkipEmptyParts);
return lines;
}

void LoggedProcess::on_stdErr()
{
auto lines = reprocess(readAllStandardError(), m_err_leftover);
auto lines = reprocess(readAllStandardError(), m_err_decoder);
emit log(lines, MessageLevel::StdErr);
}

void LoggedProcess::on_stdOut()
{
auto lines = reprocess(readAllStandardOutput(), m_out_leftover);
auto lines = reprocess(readAllStandardOutput(), m_out_decoder);
emit log(lines, MessageLevel::StdOut);
}

Expand All @@ -86,18 +84,6 @@ void LoggedProcess::on_exit(int exit_code, QProcess::ExitStatus status)
// save the exit code
m_exit_code = exit_code;

// Flush console window
if (!m_err_leftover.isEmpty())
{
emit log({m_err_leftover}, MessageLevel::StdErr);
m_err_leftover.clear();
}
if (!m_out_leftover.isEmpty())
{
emit log({m_err_leftover}, MessageLevel::StdOut);
m_out_leftover.clear();
}

// based on state, send signals
if (!m_is_aborting)
{
Expand Down
5 changes: 3 additions & 2 deletions launcher/LoggedProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#pragma once

#include <QProcess>
#include <QTextDecoder>
#include "MessageLevel.h"

/*
Expand Down Expand Up @@ -88,8 +89,8 @@ private slots:
void changeState(LoggedProcess::State state);

private:
QString m_err_leftover;
QString m_out_leftover;
QTextDecoder m_err_decoder = QTextDecoder(QTextCodec::codecForName("UTF-8"));
QTextDecoder m_out_decoder = QTextDecoder(QTextCodec::codecForName("UTF-8"));
bool m_killed = false;
State m_state = NotRunning;
int m_exit_code = 0;
Expand Down

0 comments on commit a14476c

Please sign in to comment.