Skip to content

Commit

Permalink
centralize logging
Browse files Browse the repository at this point in the history
  • Loading branch information
freemountain authored and freemountain committed Nov 28, 2016
1 parent 715368e commit ab4c756
Show file tree
Hide file tree
Showing 21 changed files with 477 additions and 107 deletions.
1 change: 1 addition & 0 deletions example/default/index.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ApplicationWindow {
text: "Run"

onAction: {
store.log({ h: "jjj", b: 90, v: false}, 5, Date);
store.trigger("startProcess", url);
}
}
Expand Down
11 changes: 6 additions & 5 deletions qml-player.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ SOURCES += src/cpp/main.cpp \
src/cpp/rootstore.cpp \
src/cpp/environment.cpp \
src/cpp/quarkprocess.cpp \
src/cpp/debugger.cpp
src/cpp/quarkdebugger.cpp

HEADERS += \
src/cpp/rootstore.h \
src/cpp/environment.h \
src/cpp/quarkprocess.h \
src/cpp/either.h \
src/cpp/logger.h \
src/cpp/debugger.h
src/cpp/logsource.h \
src/cpp/quarkdebugger.h

RESOURCES += qml.qrc

Expand All @@ -29,9 +30,9 @@ OUT_PWD_WIN ~= s,/,\\,g
!win32: NODE_CMD = $$PWD/tmp/bin-$$system(bash tools/uname.sh -o)-$$system(bash tools/uname.sh -a)/node

macx {
copy_node.commands = $(COPY_DIR) $$NODE_CMD $$OUT_PWD/$$TARGET".app"/Contents/MacOS/
copy_node_path.commands = $(COPY_DIR) $$PWD/tmp/node_path $$OUT_PWD/$$TARGET".app"/Contents/Resources/
copy_app.commands = $(COPY_DIR) $$PWD/example/default $$OUT_PWD/$$TARGET".app"/Contents/Resources/
copy_node.commands = rsync -a $$NODE_CMD $$OUT_PWD/$$TARGET".app"/Contents/MacOS/
copy_node_path.commands = rsync -a $$PWD/tmp/node_path $$OUT_PWD/$$TARGET".app"/Contents/Resources/
copy_app.commands = rsync -a $$PWD/example/default $$OUT_PWD/$$TARGET".app"/Contents/Resources/
}

linux {
Expand Down
1 change: 1 addition & 0 deletions qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<file>src/qml/Quark/jsonpath.js</file>
<file>src/qml/Quark/ListModel.qml</file>
<file>src/qml/Debugger.qml</file>
<file>src/qml/Quark/inspect.js</file>
</qresource>
</RCC>
6 changes: 0 additions & 6 deletions src/cpp/debugger.cpp

This file was deleted.

36 changes: 0 additions & 36 deletions src/cpp/debugger.h

This file was deleted.

13 changes: 6 additions & 7 deletions src/cpp/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ QString Environment::getSystemCommand(QString name) {
QString files[] = {"/bin/", "/usr/bin/", "/usr/local/bin/"};
for( unsigned int i = 0; i < 3; i = i + 1 )
{
this->printLine("get " + name);
QString current = files[i] + name;
QFileInfo info = QFileInfo(current);
bool isFile = info.exists() && info.isFile();
Expand Down Expand Up @@ -83,12 +82,12 @@ QString Environment::getShellCommand(QString name) {
proc.start(shell, QStringList() << "-c" << cmd);

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

if (!proc.waitForFinished()) {
this->printLine("not finished");
emit log("not finished");
return nullptr;
}

Expand Down Expand Up @@ -156,7 +155,7 @@ QProcessEnvironment Environment::getProcEnv() {
if(nodePath != NULL)
procEnv.insert("NODE_PATH", QDir::toNativeSeparators(nodePath));
else
this->printLine("could not set NODE_PATH\n");
emit log("could not set NODE_PATH\n");

return procEnv;
}
Expand All @@ -169,7 +168,7 @@ QuarkProcess* Environment::startProcess(QString path) {
Either<QMap<QString, QString>, QJsonParseError> mayJson = this->loadJson(path);

if(mayJson.is2nd()) {
this->printLine("Could not parse: " + path +
emit log("Could not parse: " + path +
"error: " + mayJson.as2nd().errorString());
return nullptr;
}
Expand All @@ -184,9 +183,9 @@ QuarkProcess* Environment::startProcess(QString path) {
<< "--configPath" << this->getConfigPath()
<< "--shellPath" << QCoreApplication::applicationFilePath();

QuarkProcess* proc = new QuarkProcess(this->getProcEnv(), this, this);
QuarkProcess* proc = new QuarkProcess(this->getProcEnv(), this);

if(json.contains("initialQml")) proc->handleLoadQml(json.value("initialQml"));
if(json.contains("initialQml")) proc->loadQml(json.value("initialQml"));

connect(proc, &QuarkProcess::startProcess, [this](const QString &path) {
this->startProcess(path);
Expand Down
8 changes: 6 additions & 2 deletions src/cpp/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

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

Q_DECLARE_METATYPE(QJsonParseError)


class Environment : public QObject, Logger
class Environment : public QObject, public LogSource
{
Q_OBJECT
//Q_INTERFACES(LogSource)

public:
explicit Environment(QStringList = QStringList(), QObject *parent = 0);

Expand All @@ -41,6 +43,8 @@ class Environment : public QObject, Logger

void printLine(QString msg);

signals:
void log(QString msg, QString topic = "log");

private:
QTextStream* out;
Expand Down
6 changes: 4 additions & 2 deletions src/cpp/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#include <QObject>
#include <QString>

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

public slots:
virtual void log(QString msg) = 0;
};

#endif // LOGGER_H
18 changes: 18 additions & 0 deletions src/cpp/logsource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef LOGSOURCE_H
#define LOGSOURCE_H

#include <QObject>

class LogSource
{
public:
//virtual ~LogSource(){}

signals:
virtual void log(QString msg, QString topic = "log") = 0;
};

//Q_DECLARE_INTERFACE(LogSource, "LogSource")


#endif // LOGSOURCE_H
17 changes: 11 additions & 6 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@

#include "quarkprocess.h"
#include "environment.h"
#include "quarkdebugger.h"
#include "logsource.h"

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

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

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());
debugger->attachLogSource(env);
debugger->printLine("node:" + env->getCommand("node"));
debugger->printLine("NODE_PATH" + env->getProcEnv().value("NODE_PATH"));
debugger->printLine("script:" + env->getScriptPath());
debugger->printLine("data:" + env->getDataPath().path());
debugger->printLine("bundled app:" + env->getBundledAppPath());

if(env->getScriptPath() == "") {
proc = env->startProcess(env->getBundledAppPath());
debugger->attach(proc);
} else {
proc = env->startProcess(env->getScriptPath());
debugger->attach(proc);
}

return app.exec();
Expand Down
40 changes: 40 additions & 0 deletions src/cpp/quarkdebugger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "quarkdebugger.h"

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

QuarkDebugger::QuarkDebugger(QObject *parent) : QObject(parent)
{
this->qmlEngine = new QQmlApplicationEngine(this);
this->qmlEngine->load(QUrl("qrc:/src/qml/Debugger.qml"));

QObject* obj = this->qmlEngine->rootObjects().at(0);
this->window = qobject_cast<QQuickWindow *>(obj);
connect(this, SIGNAL(_logMsg(QVariant, QVariant)), this->window, SLOT(appendLogLine(QVariant, QVariant)));
}

void QuarkDebugger::attachLogSource(LogSource *source, QString topic) {
connect(dynamic_cast<QObject*>(source), SIGNAL(log(QString, QString)), this, SLOT(printLine(QString, QString)));
}

void QuarkDebugger::attach(QuarkProcess *proc) {
connect(proc, &QuarkProcess::value, [this](const QJsonValue &val) {
QJsonDocument doc = QJsonDocument::fromVariant(val.toVariant());
printLine(doc.toJson(), "_debugger value");
});

connect(proc, &QuarkProcess::action, [this](const QString &type, const QJsonValue &payload, const QuarkProcess::Source &source) {
QJsonDocument doc = QJsonDocument::fromVariant(payload.toVariant());
printLine(payload.toString(), "_debugger action: " + type );
});

connect(proc, &QuarkProcess::log, [this](const QString &msg, const QString &topic) {
printLine(msg , topic);
});
}

void QuarkDebugger::printLine(QString msg, QString topic) {
fprintf(stderr, "%s: %s\n", topic.toStdString().c_str(), msg.toStdString().c_str());
emit _logMsg(QVariant(msg), QVariant(topic));
}
31 changes: 31 additions & 0 deletions src/cpp/quarkdebugger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef QUARKDEBUGGER_H
#define QUARKDEBUGGER_H
#include <QObject>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QVariant>

#include "logsource.h"
#include "quarkprocess.h"

class QuarkDebugger : public QObject
{
Q_OBJECT
public:
explicit QuarkDebugger(QObject *parent = 0);
void attachLogSource(LogSource* source, QString topic = "log");
void attach(QuarkProcess* proc);


private:
QQmlApplicationEngine* qmlEngine;
QQuickWindow* window;

public slots:
void printLine(QString msg, QString topic = "log");

signals:
void _logMsg(QVariant msg, QVariant topic);
};

#endif // QUARKDEBUGGER_H
Loading

0 comments on commit ab4c756

Please sign in to comment.