Skip to content

Commit 2e4168a

Browse files
Dmnk FreemountainerDmnk Freemountainer
Dmnk Freemountainer
authored and
Dmnk Freemountainer
committed
rewrite log output
1 parent 10385e8 commit 2e4168a

8 files changed

+59
-34
lines changed

qml-player.pro

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ HEADERS += \
1515
src/cpp/rootstore.h \
1616
src/cpp/environment.h \
1717
src/cpp/quarkprocess.h \
18-
src/cpp/either.h
18+
src/cpp/either.h \
19+
src/cpp/logger.h
1920

2021
RESOURCES += qml.qrc
2122

src/cpp/environment.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
#include <QStringList>
99
#include <QJsonObject>
1010
#include <QJsonDocument>
11-
#include <QDebug>
1211
#include <QStandardPaths>
1312
#include <QFileInfo>
1413

1514
Environment::Environment(QStringList args, QObject *parent) : QObject(parent)
1615
{
16+
this->out = new QTextStream(stdout);
1717
this->parser = new QCommandLineParser();
1818
this->env = QProcessEnvironment::systemEnvironment();
1919

@@ -42,17 +42,16 @@ QString Environment::getBundledCommand(QString name) {
4242
QString Environment::getSystemCommand(QString name) {
4343
#if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
4444
QString files[] = {"/bin/", "/usr/bin/", "/usr/local/bin/"};
45-
qDebug() << "files" << sizeof(files) << files;
45+
this->printLine("files" + sizeof(files) + files);
4646
for( unsigned int i = 0; i < 3; i = i + 1 )
4747
{
48-
qDebug() << "get " << name ;
48+
this->printLine("get " + name);
4949
QString current = files[i] + name;
5050
QFileInfo info = QFileInfo(current);
5151
bool isFile = info.exists() && info.isFile();
5252
if(isFile) return current;
5353
}
5454
#endif
55-
5655
return NULL;
5756
}
5857

@@ -77,25 +76,26 @@ QString Environment::getCommand(QString name) {
7776
return info.isFile() && info.isExecutable() ? cmd : NULL;
7877
}
7978

80-
QString Environment::getShellCommand(QString name) {
79+
QString Environment::getShellCommand(QString name) {
8180
#if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
8281
QProcess proc;
8382
QString cmd = "which " + name;
8483
QString shell = env.value("SHELL", "/bin/bash");
8584
proc.start(shell, QStringList() << "-c" << cmd);
8685

8786
if (!proc.waitForStarted()) {
88-
qDebug() << "not started";
87+
this->printLine("not started");
8988
return nullptr;
9089
}
9190

9291
if (!proc.waitForFinished()) {
93-
qDebug() << "not finished";
92+
this->printLine("not finished);
9493
return nullptr;
9594
}
9695
9796
QString result = proc.readAll(); // contains \n
9897
int n = result.size() - 1;
98+
out.flush();
9999
return result.left(n);
100100
#else
101101
return NULL;
@@ -157,7 +157,7 @@ QProcessEnvironment Environment::getProcEnv() {
157157
if(nodePath != NULL)
158158
procEnv.insert("NODE_PATH", QDir::toNativeSeparators(nodePath));
159159
else
160-
qDebug() << "could not set NODE_PATH";
160+
this->printLine("could not set NODE_PATH\n");
161161
162162
return procEnv;
163163
}
@@ -170,9 +170,8 @@ QuarkProcess* Environment::startProcess(QString path) {
170170
Either<QMap<QString, QString>, QJsonParseError> mayJson = this->loadJson(path);
171171
172172
if(mayJson.is2nd()) {
173-
qDebug() << "Could not parse: " << path <<
174-
"error: " << mayJson.as2nd().errorString();
175-
173+
this->printLine("Could not parse: " + path +
174+
"error: " + mayJson.as2nd().errorString());
176175
return nullptr;
177176
}
178177
@@ -186,7 +185,7 @@ QuarkProcess* Environment::startProcess(QString path) {
186185
<< "--configPath" << this->getConfigPath()
187186
<< "--shellPath" << QCoreApplication::applicationFilePath();
188187
189-
QuarkProcess* proc = new QuarkProcess(this->getProcEnv(), this);
188+
QuarkProcess* proc = new QuarkProcess(this->getProcEnv(), this, this);
190189
191190
if(json.contains("initialQml")) proc->handleLoadQml(json.value("initialQml"));
192191
@@ -213,8 +212,6 @@ Either<QMap<QString, QString>, QJsonParseError> Environment::loadJson(QString pa
213212
json = QJsonDocument::fromJson(data.toUtf8(), &err).object();
214213
215214
if(err.error != QJsonParseError::NoError) {
216-
qDebug() << "Could not parse: " << path <<
217-
"error: " << err.errorString();
218215
return some(err);
219216
}
220217
@@ -248,3 +245,9 @@ QString Environment::hashPath(QString path) {
248245
249246
return QString::number(hash);
250247
}
248+
249+
void Environment::printLine(QString msg) {
250+
this->out->operator <<(msg);
251+
this->out->operator <<("\n");
252+
this->out->flush();
253+
}

src/cpp/environment.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
#include <QMap>
1111
#include <QDir>
1212
#include <QJsonParseError>
13+
#include <QTextStream>
1314

1415
#include "quarkprocess.h"
1516
#include "either.h"
17+
#include "logger.h"
1618

1719
Q_DECLARE_METATYPE(QJsonParseError)
1820

1921

20-
class Environment : public QObject
22+
class Environment : public QObject, Logger
2123
{
2224
Q_OBJECT
2325
public:
@@ -37,8 +39,11 @@ class Environment : public QObject
3739
QuarkProcess* startProcess(QString path);
3840
QuarkProcess* startProcess();
3941

42+
void printLine(QString msg);
43+
4044

4145
private:
46+
QTextStream* out;
4247
QCommandLineParser* parser;
4348
QProcessEnvironment env;
4449
static QString hashPath(QString path);

src/cpp/logger.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef LOGGER_H
2+
#define LOGGER_H
3+
4+
#include <QObject>
5+
#include <QString>
6+
7+
class Logger
8+
{
9+
public:
10+
virtual ~Logger() {}
11+
virtual void printLine(QString msg) =0;
12+
};
13+
14+
#endif // LOGGER_H

src/cpp/main.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,23 @@
33
#include <QtGui/QGuiApplication>
44
#include <QObject>
55
#include <QtDebug>
6-
#include <QTextStream>
76

87
#include "quarkprocess.h"
98
#include "environment.h"
109

1110
int main(int argc, char *argv[])
1211
{
13-
QTextStream out(stderr);
1412
QGuiApplication app(argc, argv);
1513

1614
Environment* env = new Environment(app.arguments());
1715
QuarkProcess* proc;
1816

19-
out << "\nnode:" << env->getCommand("node");
20-
out << "\nNODE_PATH" << env->getProcEnv().value("NODE_PATH");
21-
out << "\nscript:" << env->getScriptPath();
22-
out << "\ndata:" << env->getDataPath().path();
23-
out << "\nbundled app:" << env->getBundledAppPath();
17+
env->printLine("node:" + env->getCommand("node"));
18+
env->printLine("NODE_PATH" + env->getProcEnv().value("NODE_PATH"));
19+
env->printLine("script:" + env->getScriptPath());
20+
env->printLine("data:" + env->getDataPath().path());
21+
env->printLine("bundled app:" + env->getBundledAppPath());
2422

25-
out.flush();
2623
if(env->getScriptPath() == "") {
2724
proc = env->startProcess(env->getBundledAppPath());
2825
} else {

src/cpp/quarkprocess.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "quarkprocess.h"
22

3-
#include <QDebug>
43
#include <QCoreApplication>
54
#include <QFileInfo>
65
#include <QStandardPaths>
@@ -12,8 +11,9 @@
1211

1312
#include "com/cutehacks/gel/gel.h"
1413

15-
QuarkProcess::QuarkProcess(QProcessEnvironment env, QObject *parent) : QObject(parent)
14+
QuarkProcess::QuarkProcess(QProcessEnvironment env, Logger *log, QObject* parent) : QObject(parent)
1615
{
16+
this->log = log;
1717
this->qmlEngine = new QQmlApplicationEngine(this);
1818
this->rootStore = new RootStore(this);
1919

@@ -30,9 +30,10 @@ QuarkProcess::QuarkProcess(QProcessEnvironment env, QObject *parent) : QObject(p
3030
connect(&this->proc, &QProcess::readyReadStandardOutput, this, &QuarkProcess::onData);
3131
connect(this->rootStore, &RootStore::action, this, &QuarkProcess::onAction);
3232
connect(this, &QuarkProcess::loadQml, this, &QuarkProcess::handleLoadQml);
33-
connect(&this->proc, &QProcess::errorOccurred, [=](const QProcess::ProcessError &error) {
34-
qDebug() << "error: " << error;
35-
});
33+
/*connect(&this->proc, &QProcess::errorOccurred, [ out ](const QProcess::ProcessError &error) {
34+
//out << QString("process error \n");
35+
out.flush();
36+
});*/
3637
connect(this->rootStore, &RootStore::data, [this](const QString &line) {
3738
this->proc.write(line.toUtf8());
3839
});
@@ -55,7 +56,7 @@ void QuarkProcess::start(QString cmd, QStringList arguments) {
5556
if(info.isFile())
5657
this->proc.start(cmd, arguments);
5758
else
58-
qDebug() << "could not find cmd: " << QDir::fromNativeSeparators(cmd);
59+
this->log->printLine("could not find cmd: " + QDir::fromNativeSeparators(cmd));
5960
}
6061

6162
void QuarkProcess::onData() {
@@ -70,7 +71,7 @@ void QuarkProcess::terminate() {
7071
}
7172

7273
void QuarkProcess::handleLoadQml(QString path) {
73-
qDebug() << "loadQml: " <<path;
74+
this->log->printLine("loadQml: " + path);
7475
this->qmlEngine->load(QDir::toNativeSeparators(path));
7576
}
7677

src/cpp/quarkprocess.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@
99

1010
#include <QJsonValue>
1111

12+
#include "logger.h"
1213
#include "rootstore.h"
1314

1415
class QuarkProcess : public QObject
1516
{
1617
Q_OBJECT
1718
public:
18-
explicit QuarkProcess(QProcessEnvironment, QObject *parent = 0);
19+
explicit QuarkProcess(QProcessEnvironment, Logger *, QObject*);
1920

2021
public slots:
2122
void start(QString path, QStringList arguments);
2223
void terminate();
2324
void handleLoadQml(QString path);
2425

2526
private:
27+
Logger* log;
2628
QProcess proc;
2729
QQmlApplicationEngine* qmlEngine;
2830
RootStore* rootStore;

src/cpp/rootstore.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "rootstore.h"
22

3-
#include <QDebug>
3+
#include <QTextStream>
44
#include <QJsonDocument>
55
#include <QJsonObject>
66

@@ -14,6 +14,7 @@ QJsonValue RootStore::value() {
1414
}
1515

1616
void RootStore::writeData(QString data) {
17+
QTextStream out(stderr);
1718
QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());
1819
QJsonObject msg = doc.object();
1920
QString type = msg.value("type").toString();
@@ -31,7 +32,8 @@ void RootStore::writeData(QString data) {
3132
return;
3233
}
3334

34-
qDebug() << "root store invalid line: " << data;
35+
out << "root store invalid line: " << data;
36+
out.flush();
3537
}
3638

3739
void RootStore::trigger(QString type, QJsonValue payload) {

0 commit comments

Comments
 (0)