Add log viewer page, enable logging by default

This commit is contained in:
Matti Viljanen 2021-04-17 23:38:34 +03:00
parent 51d5c8f29d
commit f9354eacc3
No known key found for this signature in database
GPG key ID: CF32A1495158F888
15 changed files with 167 additions and 11 deletions

View file

@ -0,0 +1,52 @@
/**
* Battery Buddy, a Sailfish application to prolong battery lifetime
*
* Copyright (C) 2019-2020 Matti Viljanen
*
* Battery Buddy is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Battery Buddy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details. You should have received a copy of the GNU
* General Public License along with Battery Buddy. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Matti Viljanen
*/
import QtQuick 2.2
import Sailfish.Silica 1.0
import "../components"
Page {
id: logPage
allowedOrientations: Orientation.Portrait | Orientation.Landscape | Orientation.LandscapeInverted
SilicaFlickable {
id: logFlickable
anchors.fill: parent
contentHeight: header.height + column.height
PageHeader {
id: header
title: qsTr("View log")
}
Column {
id: column
anchors {
top: header.bottom
horizontalCenter: parent.horizontalCenter
}
width: parent.width - 2*Theme.horizontalPageMargin
spacing: Theme.paddingLarge
MyLabel {
text: logger.readLogfile(settings.logFilename)
font.pixelSize: Theme.fontSizeTiny
}
}
}
}

View file

@ -108,6 +108,10 @@ Page {
contentHeight: flow.height + Theme.horizontalPageMargin contentHeight: flow.height + Theme.horizontalPageMargin
PullDownMenu { PullDownMenu {
MenuItem {
text: qsTr("View log")
onClicked: pageStack.push(Qt.resolvedUrl("LogPage.qml"))
}
MenuItem { MenuItem {
text: qsTr("About", "About this application") text: qsTr("About", "About this application")
onClicked: pageStack.push(Qt.resolvedUrl("AboutPage.qml")) onClicked: pageStack.push(Qt.resolvedUrl("AboutPage.qml"))

View file

@ -19,7 +19,7 @@ Description=Battery Buddy background daemon
After=pre-user-session.target After=pre-user-session.target
[Service] [Service]
ExecStart=/usr/bin/harbour-batterybuddy-daemon ExecStart=/usr/bin/harbour-batterybuddy-daemon --verbose --logfile
[Install] [Install]
WantedBy=user-session.target WantedBy=user-session.target

View file

@ -36,6 +36,7 @@ Settings::Settings(Logger *newLogger, QObject *parent) : QObject(parent)
loadInteger(sLimitEnabled, limitEnabled, 0, 1); loadInteger(sLimitEnabled, limitEnabled, 0, 1);
loadInteger(sLowLimit, lowLimit, 5, 99); loadInteger(sLowLimit, lowLimit, 5, 99);
loadInteger(sHighLimit, highLimit, 6, 100); loadInteger(sHighLimit, highLimit, 6, 100);
logFilename = mySettings->value(sLogFilename).toString();
notificationTitle = tr("Battery charge %1%"); notificationTitle = tr("Battery charge %1%");
notificationLowText = tr("Please connect the charger."); notificationLowText = tr("Please connect the charger.");
@ -68,6 +69,7 @@ int Settings::getHighLimit() { return highLimit; }
bool Settings::getLimitEnabled() { return limitEnabled == 1; } bool Settings::getLimitEnabled() { return limitEnabled == 1; }
QString Settings::getLowAlertFile() { return lowAlertFile; } QString Settings::getLowAlertFile() { return lowAlertFile; }
QString Settings::getHighAlertFile() { return highAlertFile; } QString Settings::getHighAlertFile() { return highAlertFile; }
QString Settings::getLogFilename() { return logFilename; }
QString Settings::getNotificationTitle() { return notificationTitle; } QString Settings::getNotificationTitle() { return notificationTitle; }
QString Settings::getNotificationLowText() { return notificationLowText; } QString Settings::getNotificationLowText() { return notificationLowText; }
QString Settings::getNotificationHighText() { return notificationHighText; } QString Settings::getNotificationHighText() { return notificationHighText; }

View file

@ -37,6 +37,7 @@ class Settings : public QObject
Q_PROPERTY(QString notificationTitle READ getNotificationTitle WRITE setNotificationTitle NOTIFY notificationTitleChanged) Q_PROPERTY(QString notificationTitle READ getNotificationTitle WRITE setNotificationTitle NOTIFY notificationTitleChanged)
Q_PROPERTY(QString notificationLowText READ getNotificationLowText WRITE setNotificationLowText NOTIFY notificationLowTextChanged) Q_PROPERTY(QString notificationLowText READ getNotificationLowText WRITE setNotificationLowText NOTIFY notificationLowTextChanged)
Q_PROPERTY(QString notificationHighText READ getNotificationHighText WRITE setNotificationHighText NOTIFY notificationHighTextChanged) Q_PROPERTY(QString notificationHighText READ getNotificationHighText WRITE setNotificationHighText NOTIFY notificationHighTextChanged)
Q_PROPERTY(QString logFilename READ getLogFilename NOTIFY logFilenameChanged)
public: public:
Settings(Logger* newLogger, QObject* parent = nullptr); Settings(Logger* newLogger, QObject* parent = nullptr);
@ -54,6 +55,7 @@ public:
QString getNotificationTitle(); QString getNotificationTitle();
QString getNotificationLowText(); QString getNotificationLowText();
QString getNotificationHighText(); QString getNotificationHighText();
QString getLogFilename();
void setLowAlert(const int newLimit); void setLowAlert(const int newLimit);
void setHighAlert(const int newLimit); void setHighAlert(const int newLimit);
@ -84,6 +86,7 @@ private:
QString notificationTitle; QString notificationTitle;
QString notificationLowText; QString notificationLowText;
QString notificationHighText; QString notificationHighText;
QString logFilename;
// To avoid repeating the same string over and over and over... // To avoid repeating the same string over and over and over...
const char* sLowAlert = "lowAlert"; const char* sLowAlert = "lowAlert";
@ -98,6 +101,7 @@ private:
const char* sNotificationTitle = "notificationTitle"; const char* sNotificationTitle = "notificationTitle";
const char* sNotificationLowText = "notificationLowText"; const char* sNotificationLowText = "notificationLowText";
const char* sNotificationHighText = "notificationHighText"; const char* sNotificationHighText = "notificationHighText";
const char* sLogFilename = "logFilename";
void loadInteger(const char *key, int &value, const int min, const int max); void loadInteger(const char *key, int &value, const int min, const int max);
void saveInteger(const char *key, const int &value); void saveInteger(const char *key, const int &value);
@ -115,6 +119,7 @@ signals:
void notificationTitleChanged(QString); void notificationTitleChanged(QString);
void notificationLowTextChanged(QString); void notificationLowTextChanged(QString);
void notificationHighTextChanged(QString); void notificationHighTextChanged(QString);
void logFilenameChanged(QString);
}; };
#endif // SETTINGS_H #endif // SETTINGS_H

View file

@ -152,6 +152,13 @@
<translation>Großartig! Es gibt eine Menge Artikel und Veröffentlichungen zum Nachlesen. Klicke auf den Link, um loszulegen!</translation> <translation>Großartig! Es gibt eine Menge Artikel und Veröffentlichungen zum Nachlesen. Klicke auf den Link, um loszulegen!</translation>
</message> </message>
</context> </context>
<context>
<name>LogPage</name>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>MainPage</name> <name>MainPage</name>
<message> <message>
@ -328,5 +335,9 @@
<source>Never</source> <source>Never</source>
<translation>Nie</translation> <translation>Nie</translation>
</message> </message>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -150,6 +150,13 @@
<translation>Hienoa! Arikkeleita ja tutkimustietoa on vaikka millä mitalla, joten alkuun pääset helpoimmin klikkaamalla linkistä!</translation> <translation>Hienoa! Arikkeleita ja tutkimustietoa on vaikka millä mitalla, joten alkuun pääset helpoimmin klikkaamalla linkistä!</translation>
</message> </message>
</context> </context>
<context>
<name>LogPage</name>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>MainPage</name> <name>MainPage</name>
<message> <message>
@ -326,5 +333,9 @@
<source>Never</source> <source>Never</source>
<translation>Ei koskaan</translation> <translation>Ei koskaan</translation>
</message> </message>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -150,6 +150,13 @@
<translation>Super! Il y a plein darticles à lire et de rapports à étudier, cliquez sur le lien pour commencer!</translation> <translation>Super! Il y a plein darticles à lire et de rapports à étudier, cliquez sur le lien pour commencer!</translation>
</message> </message>
</context> </context>
<context>
<name>LogPage</name>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>MainPage</name> <name>MainPage</name>
<message> <message>
@ -326,5 +333,9 @@
<source>Never</source> <source>Never</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -150,6 +150,13 @@
<translation>Świetnie! Jest wiele artykułów do przeczytania i artykułów do przestudiowania, więc kliknij link, aby rozpocząć!</translation> <translation>Świetnie! Jest wiele artykułów do przeczytania i artykułów do przestudiowania, więc kliknij link, aby rozpocząć!</translation>
</message> </message>
</context> </context>
<context>
<name>LogPage</name>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>MainPage</name> <name>MainPage</name>
<message> <message>
@ -326,5 +333,9 @@
<source>Never</source> <source>Never</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -150,6 +150,13 @@
<translation>Bra! Det finns många artiklar att läsa och skrifter att studera, klicka länken för att börja!</translation> <translation>Bra! Det finns många artiklar att läsa och skrifter att studera, klicka länken för att börja!</translation>
</message> </message>
</context> </context>
<context>
<name>LogPage</name>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>MainPage</name> <name>MainPage</name>
<message> <message>
@ -326,5 +333,9 @@
<source>Never</source> <source>Never</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -151,6 +151,13 @@
<translation></translation> <translation></translation>
</message> </message>
</context> </context>
<context>
<name>LogPage</name>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>MainPage</name> <name>MainPage</name>
<message> <message>
@ -328,5 +335,9 @@
<source>Never</source> <source>Never</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -150,6 +150,13 @@
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
</context> </context>
<context>
<name>LogPage</name>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
<context> <context>
<name>MainPage</name> <name>MainPage</name>
<message> <message>
@ -326,5 +333,9 @@
<source>Never</source> <source>Never</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
</TS> </TS>

View file

@ -58,3 +58,17 @@ void Logger::enableLogFile() {
QString Logger::getLogFilename() { QString Logger::getLogFilename() {
return filename; return filename;
} }
QString Logger::readLogfile(QString logFilename) {
QFile logFile(logFilename);
QString contents;
if(logFile.open(QIODevice::ReadOnly)) {
contents = QString(logFile.readAll());
logFile.close();
}
else {
contents = "Couldn't open log file:\n" + logFilename;
}
return contents;
}

View file

@ -25,15 +25,18 @@
class Logger : public QObject class Logger : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool debug READ getDebug)
Q_PROPERTY(bool verbose READ getVerbose)
public: public:
Logger(const bool enableVerbose = false, Logger(const bool enableVerbose = false,
const bool enableDebug = false, const bool enableDebug = false,
const bool useLogFile = false); const bool useLogFile = false);
~Logger(); ~Logger();
Q_PROPERTY(bool debug READ getDebug)
Q_PROPERTY(bool verbose READ getVerbose)
Q_INVOKABLE void log(const QString message); Q_INVOKABLE void log(const QString message);
Q_INVOKABLE QString readLogfile(QString logFilename);
bool getDebug(); bool getDebug();
bool getVerbose(); bool getVerbose();
void enableLogFile(); void enableLogFile();

View file

@ -27,6 +27,11 @@ Settings::Settings(Logger* newLogger, QObject *parent) : QObject(parent)
logV("Using " + mySettings->fileName()); logV("Using " + mySettings->fileName());
QString logFilename = logger->getLogFilename();
if(mySettings->value(sLogFilename,QString()).toString() != logFilename) {
mySettings->setValue(sLogFilename, logFilename);
}
QString migrate = "Migrated value %1"; QString migrate = "Migrated value %1";
QString key = ""; QString key = "";
@ -113,7 +118,8 @@ void Settings::updateConfig(const QString path) {
mySettings = new QSettings(appName, appName, this); mySettings = new QSettings(appName, appName, this);
} }
logV("Loading values..."); logD("Updating configuration...");
// Read in the values // Read in the values
bool restartTimers = false; bool restartTimers = false;
@ -131,13 +137,6 @@ void Settings::updateConfig(const QString path) {
notificationLowText = mySettings->value(sNotificationLowText, "Please connect the charger.").toString(); notificationLowText = mySettings->value(sNotificationLowText, "Please connect the charger.").toString();
notificationHighText = mySettings->value(sNotificationHighText, "Please disconnect the charger.").toString(); notificationHighText = mySettings->value(sNotificationHighText, "Please disconnect the charger.").toString();
QString logFilename = logger->getLogFilename();
if(mySettings->value(sLogFilename,QString()).toString() != logFilename) {
mySettings->setValue(sLogFilename, logFilename);
}
logV("Values loaded.");
delete mySettings; delete mySettings;
mySettings = nullptr; mySettings = nullptr;