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
PullDownMenu {
MenuItem {
text: qsTr("View log")
onClicked: pageStack.push(Qt.resolvedUrl("LogPage.qml"))
}
MenuItem {
text: qsTr("About", "About this application")
onClicked: pageStack.push(Qt.resolvedUrl("AboutPage.qml"))

View file

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

View file

@ -36,6 +36,7 @@ Settings::Settings(Logger *newLogger, QObject *parent) : QObject(parent)
loadInteger(sLimitEnabled, limitEnabled, 0, 1);
loadInteger(sLowLimit, lowLimit, 5, 99);
loadInteger(sHighLimit, highLimit, 6, 100);
logFilename = mySettings->value(sLogFilename).toString();
notificationTitle = tr("Battery charge %1%");
notificationLowText = tr("Please connect the charger.");
@ -68,6 +69,7 @@ int Settings::getHighLimit() { return highLimit; }
bool Settings::getLimitEnabled() { return limitEnabled == 1; }
QString Settings::getLowAlertFile() { return lowAlertFile; }
QString Settings::getHighAlertFile() { return highAlertFile; }
QString Settings::getLogFilename() { return logFilename; }
QString Settings::getNotificationTitle() { return notificationTitle; }
QString Settings::getNotificationLowText() { return notificationLowText; }
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 notificationLowText READ getNotificationLowText WRITE setNotificationLowText NOTIFY notificationLowTextChanged)
Q_PROPERTY(QString notificationHighText READ getNotificationHighText WRITE setNotificationHighText NOTIFY notificationHighTextChanged)
Q_PROPERTY(QString logFilename READ getLogFilename NOTIFY logFilenameChanged)
public:
Settings(Logger* newLogger, QObject* parent = nullptr);
@ -54,6 +55,7 @@ public:
QString getNotificationTitle();
QString getNotificationLowText();
QString getNotificationHighText();
QString getLogFilename();
void setLowAlert(const int newLimit);
void setHighAlert(const int newLimit);
@ -84,6 +86,7 @@ private:
QString notificationTitle;
QString notificationLowText;
QString notificationHighText;
QString logFilename;
// To avoid repeating the same string over and over and over...
const char* sLowAlert = "lowAlert";
@ -98,6 +101,7 @@ private:
const char* sNotificationTitle = "notificationTitle";
const char* sNotificationLowText = "notificationLowText";
const char* sNotificationHighText = "notificationHighText";
const char* sLogFilename = "logFilename";
void loadInteger(const char *key, int &value, const int min, const int max);
void saveInteger(const char *key, const int &value);
@ -115,6 +119,7 @@ signals:
void notificationTitleChanged(QString);
void notificationLowTextChanged(QString);
void notificationHighTextChanged(QString);
void logFilenameChanged(QString);
};
#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>
</message>
</context>
<context>
<name>LogPage</name>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>MainPage</name>
<message>
@ -328,5 +335,9 @@
<source>Never</source>
<translation>Nie</translation>
</message>
<message>
<source>View log</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>

View file

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

View file

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

View file

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

View file

@ -58,3 +58,17 @@ void Logger::enableLogFile() {
QString Logger::getLogFilename() {
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
{
Q_OBJECT
Q_PROPERTY(bool debug READ getDebug)
Q_PROPERTY(bool verbose READ getVerbose)
public:
Logger(const bool enableVerbose = false,
const bool enableDebug = false,
const bool useLogFile = false);
~Logger();
Q_PROPERTY(bool debug READ getDebug)
Q_PROPERTY(bool verbose READ getVerbose)
Q_INVOKABLE void log(const QString message);
Q_INVOKABLE QString readLogfile(QString logFilename);
bool getDebug();
bool getVerbose();
void enableLogFile();

View file

@ -27,6 +27,11 @@ Settings::Settings(Logger* newLogger, QObject *parent) : QObject(parent)
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 key = "";
@ -113,7 +118,8 @@ void Settings::updateConfig(const QString path) {
mySettings = new QSettings(appName, appName, this);
}
logV("Loading values...");
logD("Updating configuration...");
// Read in the values
bool restartTimers = false;
@ -131,13 +137,6 @@ void Settings::updateConfig(const QString path) {
notificationLowText = mySettings->value(sNotificationLowText, "Please connect 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;
mySettings = nullptr;