From 942949226a7b1c7c0363c1277ddd075043544c69 Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Sat, 24 Apr 2021 18:14:26 +0300 Subject: [PATCH] Add --logfile option to daemon --- application/service/harbour-batterybuddy.service | 2 +- service/src/battery.cpp | 13 +++++++++++-- service/src/battery.h | 2 +- service/src/harbour-batterybuddy-daemon.cpp | 12 ++++++------ 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/application/service/harbour-batterybuddy.service b/application/service/harbour-batterybuddy.service index 322a566..ffe0eaa 100644 --- a/application/service/harbour-batterybuddy.service +++ b/application/service/harbour-batterybuddy.service @@ -19,7 +19,7 @@ Description=Battery Buddy background daemon After=pre-user-session.target [Service] -ExecStart=/usr/bin/harbour-batterybuddy-daemon --verbose --logfile +ExecStart=/usr/bin/harbour-batterybuddy-daemon --logfile [Install] WantedBy=user-session.target diff --git a/service/src/battery.cpp b/service/src/battery.cpp index 645e460..fcf3727 100644 --- a/service/src/battery.cpp +++ b/service/src/battery.cpp @@ -17,11 +17,19 @@ */ #include "battery.h" -Battery::Battery(Logger* newLogger, QObject *parent) : QObject(parent) +Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject(parent) { logger = newLogger; - QString filename; settings = new Settings(logger, this); + + // Read log level from config - if not already set + if(!loglevelSet) { + int logLevel = settings->getLogLevel(); + logger->debug = (logLevel == 2); + logger->verbose = (logLevel > 1); + logE(QString("Log level set to %1").arg(logLevel)); + } + updateTimer = new QTimer(this); highNotifyTimer = new QTimer(this); lowNotifyTimer = new QTimer(this); @@ -40,6 +48,7 @@ Battery::Battery(Logger* newLogger, QObject *parent) : QObject(parent) logV("Charger status file: " + chargerConnectedFile->fileName()); // ENABLE/DISABLE CHARGING + QString filename; if(QHostInfo::localHostName().contains("SailfishEmul")) { logV("Sailfish SDK detected, not using charger control file"); } diff --git a/service/src/battery.h b/service/src/battery.h index bd1debb..33533c7 100644 --- a/service/src/battery.h +++ b/service/src/battery.h @@ -33,7 +33,7 @@ class Battery : public QObject Q_OBJECT public: - Battery(Logger* newLogger, QObject *parent = nullptr); + Battery(Logger* newLogger, bool loglevelSet, QObject *parent = nullptr); ~Battery(); int getCharge(); diff --git a/service/src/harbour-batterybuddy-daemon.cpp b/service/src/harbour-batterybuddy-daemon.cpp index 14b3fcf..d83e582 100644 --- a/service/src/harbour-batterybuddy-daemon.cpp +++ b/service/src/harbour-batterybuddy-daemon.cpp @@ -46,13 +46,11 @@ int main(int argc, char** argv) debug = true; logLevelSet = true; } - else if(!logLevelSet && QHostInfo::localHostName().contains("SailfishEmul")) { - verbose = true; - debug = true; - logLevelSet = true; - } else if(!strcmp(argv[i],"--logfile")) { logfile = true; + verbose = true; + debug = false; + logLevelSet = true; } else if(!strcmp(argv[i],"--help")) { printf("%s %s\n", APP_NAME, APP_VERSION); @@ -60,6 +58,8 @@ int main(int argc, char** argv) printf(" --verbose Enable informational messages\n"); printf(" --debug Enable informational and debugging messages\n"); printf(" --help Print version string and exit\n"); + printf(" --logfile Write log to a file. Implies --verbose\n\n"); + printf("Log file: ~/.cache/harbour-batterybuddy-daemon/harbour-batterybuddy-daemon.log\n"); return 0; } } @@ -71,7 +71,7 @@ int main(int argc, char** argv) Logger* logger = new Logger(verbose, debug, logfile); logE(QString("%1 %2").arg(APP_NAME, APP_VERSION)); - Battery* battery = new Battery(logger); + Battery* battery = new Battery(logger, logLevelSet); // Exit gracefully on Ctrl-C and service stop QObject::connect(&app, SIGNAL(aboutToQuit()), battery, SLOT(shutdown()));