Non-bundle build logging to file support; Custom log path with STATUS_LOG_PATH env var.

Signed-off-by: Max Risuhin <risuhin.max@gmail.com>
This commit is contained in:
Max Risuhin 2018-11-01 10:24:31 +02:00
parent b0b2e5013b
commit 711f454c76
No known key found for this signature in database
GPG Key ID: BF733F5ACA0B4448
1 changed files with 53 additions and 24 deletions

View File

@ -15,6 +15,7 @@
#include <QFile> #include <QFile>
#include <QFontDatabase> #include <QFontDatabase>
#include <QGuiApplication> #include <QGuiApplication>
#include <QMutexLocker>
#include <QProcess> #include <QProcess>
#include <QQuickView> #include <QQuickView>
#include <QStandardPaths> #include <QStandardPaths>
@ -28,12 +29,11 @@
#include "exceptionglobalhandler.h" #include "exceptionglobalhandler.h"
#ifdef BUILD_FOR_BUNDLE static QStringList consoleOutputStrings;
#include <QMutexLocker> static QMutex consoleOutputMutex;
QStringList consoleOutputStrings; #ifdef BUILD_FOR_BUNDLE
bool ubuntuServerStarted = false; bool ubuntuServerStarted = false;
QMutex consoleOutputMutex;
QProcess *g_ubuntuServerProcess = nullptr; QProcess *g_ubuntuServerProcess = nullptr;
#endif #endif
@ -43,6 +43,9 @@ const QString CRASH_REPORT_EXECUTABLE = QStringLiteral("reportApp");
const QString CRASH_REPORT_EXECUTABLE_RELATIVE_PATH = const QString CRASH_REPORT_EXECUTABLE_RELATIVE_PATH =
QStringLiteral("/../reportApp"); QStringLiteral("/../reportApp");
const char *ENABLE_LOG_FILE_ENV_VAR_NAME = "STATUS_LOG_FILE_ENABLED";
const char *LOG_FILE_PATH_ENV_VAR_NAME = "STATUS_LOG_PATH";
// TODO: some way to change while running // TODO: some way to change while running
class ReactNativeProperties : public QObject { class ReactNativeProperties : public QObject {
Q_OBJECT Q_OBJECT
@ -55,7 +58,7 @@ class ReactNativeProperties : public QObject {
Q_PROPERTY( Q_PROPERTY(
QString executor READ executor WRITE setExecutor NOTIFY executorChanged) QString executor READ executor WRITE setExecutor NOTIFY executorChanged)
public: public:
ReactNativeProperties(QObject *parent = 0) : QObject(parent) { ReactNativeProperties(QObject *parent = nullptr) : QObject(parent) {
m_codeLocation = m_packagerTemplate.arg(m_packagerHost).arg(m_packagerPort); m_codeLocation = m_packagerTemplate.arg(m_packagerHost).arg(m_packagerPort);
} }
bool liveReload() const { return m_liveReload; } bool liveReload() const { return m_liveReload; }
@ -139,12 +142,14 @@ private:
#endif #endif
}; };
#ifdef BUILD_FOR_BUNDLE
void runUbuntuServer();
void saveMessage(QtMsgType type, const QMessageLogContext &context, void saveMessage(QtMsgType type, const QMessageLogContext &context,
const QString &msg); const QString &msg);
void writeLogsToFile(); void writeLogsToFile();
#ifdef BUILD_FOR_BUNDLE
void runUbuntuServer();
#endif #endif
void loadFontsFromResources() { void loadFontsFromResources() {
@ -167,16 +172,22 @@ void exceptionPostHandledCallback() {
#endif #endif
} }
QString getDataStoragePath() { bool redirectLogIntoFile() {
QString dataStoragePath;
#ifdef BUILD_FOR_BUNDLE #ifdef BUILD_FOR_BUNDLE
dataStoragePath = return true;
#else
return qEnvironmentVariable(ENABLE_LOG_FILE_ENV_VAR_NAME, "") ==
QStringLiteral("1");
#endif
}
QString getDataStoragePath() {
QString dataStoragePath =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(dataStoragePath); QDir dir(dataStoragePath);
if (!dir.exists()) { if (!dir.exists()) {
dir.mkpath("."); dir.mkpath(".");
} }
#endif
return dataStoragePath; return dataStoragePath;
} }
@ -185,23 +196,28 @@ int main(int argc, char **argv) {
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
app.setApplicationName("Status"); QCoreApplication::setApplicationName("Status");
QString appPath = QCoreApplication::applicationDirPath(); QString appPath = QCoreApplication::applicationDirPath();
QString dataStoragePath = getDataStoragePath();
#ifndef BUILD_FOR_BUNDLE #ifndef BUILD_FOR_BUNDLE
appPath.append(CRASH_REPORT_EXECUTABLE_RELATIVE_PATH); appPath.append(CRASH_REPORT_EXECUTABLE_RELATIVE_PATH);
dataStoragePath = "";
#endif #endif
ExceptionGlobalHandler exceptionHandler( ExceptionGlobalHandler exceptionHandler(
appPath + QDir::separator() + CRASH_REPORT_EXECUTABLE, appPath + QDir::separator() + CRASH_REPORT_EXECUTABLE,
exceptionPostHandledCallback, getDataStoragePath()); exceptionPostHandledCallback, dataStoragePath);
Q_INIT_RESOURCE(react_resources); Q_INIT_RESOURCE(react_resources);
loadFontsFromResources(); loadFontsFromResources();
if (redirectLogIntoFile()) {
qInstallMessageHandler(saveMessage);
}
#ifdef BUILD_FOR_BUNDLE #ifdef BUILD_FOR_BUNDLE
qInstallMessageHandler(saveMessage);
runUbuntuServer(); runUbuntuServer();
#endif #endif
@ -244,21 +260,33 @@ int main(int argc, char **argv) {
view.resize(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT); view.resize(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
view.show(); view.show();
#ifdef BUILD_FOR_BUNDLE QTimer flushLogsToFileTimer;
QTimer t; if (redirectLogIntoFile()) {
t.setInterval(500); flushLogsToFileTimer.setInterval(500);
QObject::connect(&t, &QTimer::timeout, [=]() { writeLogsToFile(); }); QObject::connect(&flushLogsToFileTimer, &QTimer::timeout,
t.start(); [=]() { writeLogsToFile(); });
#endif flushLogsToFileTimer.start();
}
return app.exec(); return app.exec();
} }
QString getLogFilePath() {
QString logFilePath;
#ifdef BUILD_FOR_BUNDLE #ifdef BUILD_FOR_BUNDLE
logFilePath = getDataStoragePath() + "/Status.log";
#else
logFilePath = qEnvironmentVariable(LOG_FILE_PATH_ENV_VAR_NAME, "");
if (logFilePath.isEmpty()) {
logFilePath = getDataStoragePath() + "/StatusDev.log";
}
#endif
return logFilePath;
}
void writeLogsToFile() { void writeLogsToFile() {
QMutexLocker locker(&consoleOutputMutex); QMutexLocker locker(&consoleOutputMutex);
QFile logFile(getDataStoragePath() + "/Status.log"); QFile logFile(getLogFilePath());
if (logFile.open(QIODevice::WriteOnly | QIODevice::Append)) { if (logFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
for (QString message : consoleOutputStrings) { for (QString message : consoleOutputStrings) {
logFile.write(message.toStdString().c_str()); logFile.write(message.toStdString().c_str());
@ -270,6 +298,7 @@ void writeLogsToFile() {
} }
} }
#ifdef BUILD_FOR_BUNDLE
void runUbuntuServer() { void runUbuntuServer() {
g_ubuntuServerProcess = new QProcess(); g_ubuntuServerProcess = new QProcess();
g_ubuntuServerProcess->setWorkingDirectory(getDataStoragePath()); g_ubuntuServerProcess->setWorkingDirectory(getDataStoragePath());
@ -314,6 +343,7 @@ void runUbuntuServer() {
qDebug() << "waiting finished"; qDebug() << "waiting finished";
} }
#endif
void appendConsoleString(const QString &msg) { void appendConsoleString(const QString &msg) {
QMutexLocker locker(&consoleOutputMutex); QMutexLocker locker(&consoleOutputMutex);
@ -322,7 +352,7 @@ void appendConsoleString(const QString &msg) {
void saveMessage(QtMsgType type, const QMessageLogContext &context, void saveMessage(QtMsgType type, const QMessageLogContext &context,
const QString &msg) { const QString &msg) {
Q_UNUSED(context);
QByteArray localMsg = msg.toLocal8Bit(); QByteArray localMsg = msg.toLocal8Bit();
QString message = localMsg + "\n"; QString message = localMsg + "\n";
QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss.zzz"); QString timestamp = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
@ -350,6 +380,5 @@ void saveMessage(QtMsgType type, const QMessageLogContext &context,
abort(); abort();
} }
} }
#endif
#include "main.moc" #include "main.moc"