Skip to content

Commit

Permalink
rewrite log output
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmnk Freemountainer authored and Dmnk Freemountainer committed Nov 24, 2016
1 parent 10385e8 commit 2e4168a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 34 deletions.
3 changes: 2 additions & 1 deletion qml-player.pro
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ HEADERS += \
src/cpp/rootstore.h \
src/cpp/environment.h \
src/cpp/quarkprocess.h \
src/cpp/either.h
src/cpp/either.h \
src/cpp/logger.h

RESOURCES += qml.qrc

Expand Down
31 changes: 17 additions & 14 deletions src/cpp/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#include <QStringList>
#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>
#include <QStandardPaths>
#include <QFileInfo>

Environment::Environment(QStringList args, QObject *parent) : QObject(parent)
{
this->out = new QTextStream(stdout);
this->parser = new QCommandLineParser();
this->env = QProcessEnvironment::systemEnvironment();

Expand Down Expand Up @@ -42,17 +42,16 @@ QString Environment::getBundledCommand(QString name) {
QString Environment::getSystemCommand(QString name) {
#if defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
QString files[] = {"/bin/", "/usr/bin/", "/usr/local/bin/"};
qDebug() << "files" << sizeof(files) << files;
this->printLine("files" + sizeof(files) + files);
for( unsigned int i = 0; i < 3; i = i + 1 )
{
qDebug() << "get " << name ;
this->printLine("get " + name);
QString current = files[i] + name;
QFileInfo info = QFileInfo(current);
bool isFile = info.exists() && info.isFile();
if(isFile) return current;
}
#endif

return NULL;
}

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

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

if (!proc.waitForStarted()) {
qDebug() << "not started";
this->printLine("not started");
return nullptr;
}

if (!proc.waitForFinished()) {
qDebug() << "not finished";
this->printLine("not finished);
return nullptr;
}
QString result = proc.readAll(); // contains \n
int n = result.size() - 1;
out.flush();
return result.left(n);
#else
return NULL;
Expand Down Expand Up @@ -157,7 +157,7 @@ QProcessEnvironment Environment::getProcEnv() {
if(nodePath != NULL)
procEnv.insert("NODE_PATH", QDir::toNativeSeparators(nodePath));
else
qDebug() << "could not set NODE_PATH";
this->printLine("could not set NODE_PATH\n");
return procEnv;
}
Expand All @@ -170,9 +170,8 @@ QuarkProcess* Environment::startProcess(QString path) {
Either<QMap<QString, QString>, QJsonParseError> mayJson = this->loadJson(path);
if(mayJson.is2nd()) {
qDebug() << "Could not parse: " << path <<
"error: " << mayJson.as2nd().errorString();

this->printLine("Could not parse: " + path +
"error: " + mayJson.as2nd().errorString());
return nullptr;
}
Expand All @@ -186,7 +185,7 @@ QuarkProcess* Environment::startProcess(QString path) {
<< "--configPath" << this->getConfigPath()
<< "--shellPath" << QCoreApplication::applicationFilePath();
QuarkProcess* proc = new QuarkProcess(this->getProcEnv(), this);
QuarkProcess* proc = new QuarkProcess(this->getProcEnv(), this, this);
if(json.contains("initialQml")) proc->handleLoadQml(json.value("initialQml"));
Expand All @@ -213,8 +212,6 @@ Either<QMap<QString, QString>, QJsonParseError> Environment::loadJson(QString pa
json = QJsonDocument::fromJson(data.toUtf8(), &err).object();
if(err.error != QJsonParseError::NoError) {
qDebug() << "Could not parse: " << path <<
"error: " << err.errorString();
return some(err);
}
Expand Down Expand Up @@ -248,3 +245,9 @@ QString Environment::hashPath(QString path) {
return QString::number(hash);
}
void Environment::printLine(QString msg) {
this->out->operator <<(msg);
this->out->operator <<("\n");
this->out->flush();
}
7 changes: 6 additions & 1 deletion src/cpp/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
#include <QMap>
#include <QDir>
#include <QJsonParseError>
#include <QTextStream>

#include "quarkprocess.h"
#include "either.h"
#include "logger.h"

Q_DECLARE_METATYPE(QJsonParseError)


class Environment : public QObject
class Environment : public QObject, Logger
{
Q_OBJECT
public:
Expand All @@ -37,8 +39,11 @@ class Environment : public QObject
QuarkProcess* startProcess(QString path);
QuarkProcess* startProcess();

void printLine(QString msg);


private:
QTextStream* out;
QCommandLineParser* parser;
QProcessEnvironment env;
static QString hashPath(QString path);
Expand Down
14 changes: 14 additions & 0 deletions src/cpp/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef LOGGER_H
#define LOGGER_H

#include <QObject>
#include <QString>

class Logger
{
public:
virtual ~Logger() {}
virtual void printLine(QString msg) =0;
};

#endif // LOGGER_H
13 changes: 5 additions & 8 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
#include <QtGui/QGuiApplication>
#include <QObject>
#include <QtDebug>
#include <QTextStream>

#include "quarkprocess.h"
#include "environment.h"

int main(int argc, char *argv[])
{
QTextStream out(stderr);
QGuiApplication app(argc, argv);

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

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

out.flush();
if(env->getScriptPath() == "") {
proc = env->startProcess(env->getBundledAppPath());
} else {
Expand Down
15 changes: 8 additions & 7 deletions src/cpp/quarkprocess.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "quarkprocess.h"

#include <QDebug>
#include <QCoreApplication>
#include <QFileInfo>
#include <QStandardPaths>
Expand All @@ -12,8 +11,9 @@

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

QuarkProcess::QuarkProcess(QProcessEnvironment env, QObject *parent) : QObject(parent)
QuarkProcess::QuarkProcess(QProcessEnvironment env, Logger *log, QObject* parent) : QObject(parent)
{
this->log = log;
this->qmlEngine = new QQmlApplicationEngine(this);
this->rootStore = new RootStore(this);

Expand All @@ -30,9 +30,10 @@ QuarkProcess::QuarkProcess(QProcessEnvironment env, QObject *parent) : QObject(p
connect(&this->proc, &QProcess::readyReadStandardOutput, this, &QuarkProcess::onData);
connect(this->rootStore, &RootStore::action, this, &QuarkProcess::onAction);
connect(this, &QuarkProcess::loadQml, this, &QuarkProcess::handleLoadQml);
connect(&this->proc, &QProcess::errorOccurred, [=](const QProcess::ProcessError &error) {
qDebug() << "error: " << error;
});
/*connect(&this->proc, &QProcess::errorOccurred, [ out ](const QProcess::ProcessError &error) {
//out << QString("process error \n");
out.flush();
});*/
connect(this->rootStore, &RootStore::data, [this](const QString &line) {
this->proc.write(line.toUtf8());
});
Expand All @@ -55,7 +56,7 @@ void QuarkProcess::start(QString cmd, QStringList arguments) {
if(info.isFile())
this->proc.start(cmd, arguments);
else
qDebug() << "could not find cmd: " << QDir::fromNativeSeparators(cmd);
this->log->printLine("could not find cmd: " + QDir::fromNativeSeparators(cmd));
}

void QuarkProcess::onData() {
Expand All @@ -70,7 +71,7 @@ void QuarkProcess::terminate() {
}

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

4 changes: 3 additions & 1 deletion src/cpp/quarkprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@

#include <QJsonValue>

#include "logger.h"
#include "rootstore.h"

class QuarkProcess : public QObject
{
Q_OBJECT
public:
explicit QuarkProcess(QProcessEnvironment, QObject *parent = 0);
explicit QuarkProcess(QProcessEnvironment, Logger *, QObject*);

public slots:
void start(QString path, QStringList arguments);
void terminate();
void handleLoadQml(QString path);

private:
Logger* log;
QProcess proc;
QQmlApplicationEngine* qmlEngine;
RootStore* rootStore;
Expand Down
6 changes: 4 additions & 2 deletions src/cpp/rootstore.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "rootstore.h"

#include <QDebug>
#include <QTextStream>
#include <QJsonDocument>
#include <QJsonObject>

Expand All @@ -14,6 +14,7 @@ QJsonValue RootStore::value() {
}

void RootStore::writeData(QString data) {
QTextStream out(stderr);
QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());
QJsonObject msg = doc.object();
QString type = msg.value("type").toString();
Expand All @@ -31,7 +32,8 @@ void RootStore::writeData(QString data) {
return;
}

qDebug() << "root store invalid line: " << data;
out << "root store invalid line: " << data;
out.flush();
}

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

0 comments on commit 2e4168a

Please sign in to comment.