mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Android: write appLog to file
The trick is to pick a path that is accessible from other applications. In theory QStandardPaths::GenericDataLocation should provide that. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
e8b46039f6
commit
c59a74029b
3 changed files with 47 additions and 2 deletions
|
@ -90,6 +90,18 @@ QMLManager::QMLManager() : m_locationServiceEnabled(false),
|
||||||
m_instance = this;
|
m_instance = this;
|
||||||
m_lastDevicePixelRatio = qApp->devicePixelRatio();
|
m_lastDevicePixelRatio = qApp->devicePixelRatio();
|
||||||
connect(qobject_cast<QApplication *>(QApplication::instance()), &QApplication::applicationStateChanged, this, &QMLManager::applicationStateChanged);
|
connect(qobject_cast<QApplication *>(QApplication::instance()), &QApplication::applicationStateChanged, this, &QMLManager::applicationStateChanged);
|
||||||
|
|
||||||
|
#if defined(Q_OS_ANDROID)
|
||||||
|
appLogFileName = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation).first() + "/subsurface.log";
|
||||||
|
appLogFile.setFileName(appLogFileName);
|
||||||
|
if (!appLogFile.open(QIODevice::ReadWrite)) {
|
||||||
|
appLogFileOpen = false;
|
||||||
|
appendTextToLog("Failed to open logfile" + appLogFileName);
|
||||||
|
} else {
|
||||||
|
appLogFileOpen = true;
|
||||||
|
appendTextToLog("Successfully opened logfile" + appLogFileName);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
appendTextToLog("Starting " + getUserAgent());
|
appendTextToLog("Starting " + getUserAgent());
|
||||||
appendTextToLog(QStringLiteral("build with Qt Version %1, runtime from Qt Version %2").arg(QT_VERSION_STR).arg(qVersion()));
|
appendTextToLog(QStringLiteral("build with Qt Version %1, runtime from Qt Version %2").arg(QT_VERSION_STR).arg(qVersion()));
|
||||||
setStartPageText(tr("Starting..."));
|
setStartPageText(tr("Starting..."));
|
||||||
|
@ -234,6 +246,10 @@ void QMLManager::finishSetup()
|
||||||
|
|
||||||
QMLManager::~QMLManager()
|
QMLManager::~QMLManager()
|
||||||
{
|
{
|
||||||
|
#if defined(Q_OS_ANDROID)
|
||||||
|
if (appLogFileOpen)
|
||||||
|
appLogFile.close();
|
||||||
|
#endif
|
||||||
m_instance = NULL;
|
m_instance = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1530,6 +1546,23 @@ void QMLManager::setProgressMessage(QString text)
|
||||||
|
|
||||||
#if defined (Q_OS_ANDROID)
|
#if defined (Q_OS_ANDROID)
|
||||||
|
|
||||||
|
void writeToAppLogFile(QString logText)
|
||||||
|
{
|
||||||
|
// write to storage and flush so that the data doesn't get lost
|
||||||
|
logText.append("\n");
|
||||||
|
QMLManager *self = QMLManager::instance();
|
||||||
|
if (self) {
|
||||||
|
self->writeToAppLogFile(logText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void QMLManager::writeToAppLogFile(QString logText)
|
||||||
|
{
|
||||||
|
if (appLogFileOpen) {
|
||||||
|
appLogFile.write(logText.toUtf8().data());
|
||||||
|
appLogFile.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//HACK to color the system bar on Android, use qtandroidextras and call the appropriate Java methods
|
//HACK to color the system bar on Android, use qtandroidextras and call the appropriate Java methods
|
||||||
//this code is based on code in the Kirigami example app for Android (under LGPL-2) Copyright 2017 Marco Martin
|
//this code is based on code in the Kirigami example app for Android (under LGPL-2) Copyright 2017 Marco Martin
|
||||||
|
|
|
@ -125,6 +125,9 @@ public:
|
||||||
bool showPin() const;
|
bool showPin() const;
|
||||||
void setShowPin(bool enable);
|
void setShowPin(bool enable);
|
||||||
Q_INVOKABLE void setStatusbarColor(QColor color);
|
Q_INVOKABLE void setStatusbarColor(QColor color);
|
||||||
|
#if defined(Q_OS_ANDROID)
|
||||||
|
void writeToAppLogFile(QString logText);
|
||||||
|
#endif
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void applicationStateChanged(Qt::ApplicationState state);
|
void applicationStateChanged(Qt::ApplicationState state);
|
||||||
|
@ -208,6 +211,11 @@ private:
|
||||||
bool m_showPin;
|
bool m_showPin;
|
||||||
DCDeviceData *m_device_data;
|
DCDeviceData *m_device_data;
|
||||||
QString m_progressMessage;
|
QString m_progressMessage;
|
||||||
|
#if defined(Q_OS_ANDROID)
|
||||||
|
QString appLogFileName;
|
||||||
|
QFile appLogFile;
|
||||||
|
bool appLogFileOpen;
|
||||||
|
#endif
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void cloudUserNameChanged();
|
void cloudUserNameChanged();
|
||||||
|
|
|
@ -2,11 +2,12 @@
|
||||||
#include "messagehandlermodel.h"
|
#include "messagehandlermodel.h"
|
||||||
|
|
||||||
/* based on logging bits from libdivecomputer */
|
/* based on logging bits from libdivecomputer */
|
||||||
#ifndef __ANDROID__
|
#if !defined(Q_OS_ANDROID)
|
||||||
#define INFO(fmt, ...) fprintf(stderr, "INFO: " fmt "\n", ##__VA_ARGS__)
|
#define INFO(fmt, ...) fprintf(stderr, "INFO: " fmt "\n", ##__VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
|
extern void writeToAppLogFile(QString logText);
|
||||||
#include <android/log.h>
|
#include <android/log.h>
|
||||||
#define INFO(fmt, ...) __android_log_print(ANDROID_LOG_DEBUG, __FILE__, "INFO: " fmt "\n", ##__VA_ARGS__)
|
#define INFO(fmt, ...) __android_log_print(ANDROID_LOG_DEBUG, __FILE__, "INFO: " fmt "\n", ##__VA_ARGS__);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||||
|
@ -42,6 +43,9 @@ void MessageHandlerModel::addLog(QtMsgType type, const QString& message)
|
||||||
m_data.append({message, type});
|
m_data.append({message, type});
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
INFO("%s", message.toUtf8().constData());
|
INFO("%s", message.toUtf8().constData());
|
||||||
|
#if defined (Q_OS_ANDROID)
|
||||||
|
writeToAppLogFile(message);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant MessageHandlerModel::data(const QModelIndex& idx, int role) const
|
QVariant MessageHandlerModel::data(const QModelIndex& idx, int role) const
|
||||||
|
|
Loading…
Add table
Reference in a new issue