From f542628e56ff7f4d9b92075a228041b5051fc00e Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Sat, 24 Apr 2021 17:44:25 +0300 Subject: [PATCH] Implement log level settings --- application/qml/pages/LogPage.qml | 43 +++++++++++++++++++++++++++++++ application/src/settings.cpp | 9 +++++++ application/src/settings.h | 6 +++++ service/src/logger.h | 1 - service/src/settings.cpp | 11 ++++++++ service/src/settings.h | 3 +++ 6 files changed, 72 insertions(+), 1 deletion(-) diff --git a/application/qml/pages/LogPage.qml b/application/qml/pages/LogPage.qml index 461b1b1..eeb9c6b 100644 --- a/application/qml/pages/LogPage.qml +++ b/application/qml/pages/LogPage.qml @@ -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) + } } } } diff --git a/application/src/settings.cpp b/application/src/settings.cpp index 30c4e28..9b5e798 100644 --- a/application/src/settings.cpp +++ b/application/src/settings.cpp @@ -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)); diff --git a/application/src/settings.h b/application/src/settings.h index d7427aa..2535e7c 100644 --- a/application/src/settings.h +++ b/application/src/settings.h @@ -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 diff --git a/service/src/logger.h b/service/src/logger.h index f7d5b0f..c0fbea9 100644 --- a/service/src/logger.h +++ b/service/src/logger.h @@ -2,7 +2,6 @@ #define LOGGER_H #include -#include #include #include #include diff --git a/service/src/settings.cpp b/service/src/settings.cpp index f4f5769..169b406 100644 --- a/service/src/settings.cpp +++ b/service/src/settings.cpp @@ -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; } diff --git a/service/src/settings.h b/service/src/settings.h index d2320dc..8d379ba 100644 --- a/service/src/settings.h +++ b/service/src/settings.h @@ -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);