Implement log level settings

This commit is contained in:
Matti Viljanen 2021-04-24 17:44:25 +03:00
parent 0d67eb3316
commit f542628e56
No known key found for this signature in database
GPG key ID: CF32A1495158F888
6 changed files with 72 additions and 1 deletions

View file

@ -43,9 +43,52 @@ Page {
width: parent.width - 2*Theme.horizontalPageMargin
spacing: Theme.paddingLarge
ComboBox {
label: qsTr("Log level")
currentIndex: 0
menu: ContextMenu {
MenuItem { text: qsTr("Quiet", "Low log setting") }
MenuItem { text: qsTr("Verbose", "Medium log setting") }
MenuItem { text: qsTr("Debug", "High log setting") }
}
onCurrentIndexChanged: settings.logLevel = currentIndex
Component.onCompleted: currentIndex = settings.logLevel
}
Row {
anchors {
left: parent.left
right: parent.right
}
height: updateButton.height
Column {
width: parent.width / 2
Button {
id: updateButton
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Update")
onClicked: logLabel.updateText()
}
}
Column {
width: parent.width / 2
Button {
id: daemonStopButton
anchors.horizontalCenter: parent.horizontalCenter
text: qsTr("Copy")
onClicked: Clipboard.text = logLabel.text
}
}
}
MyLabel {
id: logLabel
text: logger.readLogfile(settings.logFilename)
font.pixelSize: Theme.fontSizeTiny
function updateText() {
logLabel.text = logger.readLogfile(settings.logFilename)
}
}
}
}

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);
loadInteger(sLogLevel, logLevel, 0, 2);
logFilename = mySettings->value(sLogFilename).toString();
notificationTitle = tr("Battery charge %1%");
@ -73,6 +74,7 @@ QString Settings::getLogFilename() { return logFilename; }
QString Settings::getNotificationTitle() { return notificationTitle; }
QString Settings::getNotificationLowText() { return notificationLowText; }
QString Settings::getNotificationHighText() { return notificationHighText; }
int Settings::getLogLevel() { return logLevel; }
void Settings::setLowAlert(const int newLimit) {
lowAlert = newLimit;
@ -147,6 +149,13 @@ void Settings::setNotificationHighText(const QString newText) {
emit notificationHighTextChanged(notificationHighText);
}
void Settings::setLogLevel(const int newLogLevel) {
logLevel = newLogLevel;
saveInteger(sLogLevel, logLevel);
mySettings->sync();
emit logLevelChanged(logLevel);
}
void Settings::loadInteger(const char *key, int &value, const int min, const int max) {
int newValue = mySettings->value(key, value).toInt();
value = (newValue <= min ? min : (newValue >= max ? max : newValue));

View file

@ -38,6 +38,7 @@ class Settings : public QObject
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)
Q_PROPERTY(int logLevel READ getLogLevel WRITE setLogLevel NOTIFY logLevelChanged)
public:
Settings(Logger* newLogger, QObject* parent = nullptr);
@ -56,6 +57,7 @@ public:
QString getNotificationLowText();
QString getNotificationHighText();
QString getLogFilename();
int getLogLevel();
void setLowAlert(const int newLimit);
void setHighAlert(const int newLimit);
@ -67,6 +69,7 @@ public:
void setNotificationTitle(const QString newText);
void setNotificationLowText(const QString newText);
void setNotificationHighText(const QString newText);
void setLogLevel(const int newLogLevel);
private:
QSettings *mySettings = nullptr;
@ -87,6 +90,7 @@ private:
QString notificationLowText;
QString notificationHighText;
QString logFilename;
int logLevel;
// To avoid repeating the same string over and over and over...
const char* sLowAlert = "lowAlert";
@ -102,6 +106,7 @@ private:
const char* sNotificationLowText = "notificationLowText";
const char* sNotificationHighText = "notificationHighText";
const char* sLogFilename = "logFilename";
const char* sLogLevel = "logLevel";
void loadInteger(const char *key, int &value, const int min, const int max);
void saveInteger(const char *key, const int &value);
@ -120,6 +125,7 @@ signals:
void notificationLowTextChanged(QString);
void notificationHighTextChanged(QString);
void logFilenameChanged(QString);
void logLevelChanged(int);
};
#endif // SETTINGS_H

View file

@ -2,7 +2,6 @@
#define LOGGER_H
#include <QObject>
#include <QString>
#include <QFile>
#include <QDir>
#include <QStandardPaths>

View file

@ -131,6 +131,16 @@ void Settings::updateConfig(const QString path) {
loadInteger(sLowLimit, lowLimit, 5, 99);
loadInteger(sHighLimit, highLimit, 6, 100);
// Update log level
int oldLogLevel = logLevel;
loadInteger(sLogLevel, logLevel, 0, 2);
if(oldLogLevel != logLevel) {
logger->debug = (logLevel == 2);
logger->verbose = (logLevel > 1);
logE(QString("Log level set to %1").arg(logLevel));
}
// These are translated in the GUI application
// and delivered here via the config file
notificationTitle = mySettings->value(sNotificationTitle, "Battery charge %1%").toString();
@ -169,3 +179,4 @@ QString Settings::getHighAlertFile() { return highAlertFile; }
QString Settings::getNotificationTitle() { return notificationTitle; }
QString Settings::getNotificationLowText() { return notificationLowText; }
QString Settings::getNotificationHighText() { return notificationHighText; }
int Settings::getLogLevel() { return logLevel; }

View file

@ -38,6 +38,7 @@ public:
int getLowNotificationsInterval();
int getLowLimit();
int getHighLimit();
int getLogLevel();
bool getLimitEnabled();
bool getHighNotificationsEnabled();
bool getLowNotificationsEnabled();
@ -57,6 +58,7 @@ private:
const char* appName = "harbour-batterybuddy";
int oldValue;
int logLevel = 1;
// Default values
int lowAlert = 25;
@ -90,6 +92,7 @@ private:
const char* sNotificationLowText = "notificationLowText";
const char* sNotificationHighText = "notificationHighText";
const char* sLogFilename = "logFilename";
const char* sLogLevel = "logLevel";
bool loadInteger(const char *key, int &value, const int min, const int max);