Fine-tune logging, fix a few messages, fix indentation

This commit is contained in:
Matti Viljanen 2021-04-26 17:34:44 +03:00
parent c6e8145afc
commit 1a0b730a1c
No known key found for this signature in database
GPG key ID: CF32A1495158F888
3 changed files with 79 additions and 98 deletions

View file

@ -24,19 +24,19 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
// Number: charge percentage, e.g. 42
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
logV("Capacity file: " + chargeFile->fileName());
logE("Capacity file: " + chargeFile->fileName());
// Number: battery/charging current, e.g. -1450000 (-145mA)
currentFile = new QFile("/sys/class/power_supply/battery/current_now", this);
logV("Charge state file: " + currentFile->fileName());
logE("Charge state file: " + currentFile->fileName());
// String: charging, discharging, full, empty, unknown (others?)
stateFile = new QFile("/sys/class/power_supply/battery/status", this);
logV("Charger status file: " + stateFile->fileName());
logE("Charger status file: " + stateFile->fileName());
// Number: 0 or 1
chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this);
logV("Reading charger status from" + chargerConnectedFile->fileName());
logE("Reading charger status from" + chargerConnectedFile->fileName());
QString filename;
@ -69,25 +69,6 @@ Battery::Battery(Settings* newSettings, Logger* newLogger, QObject* parent) : QO
logE("Please contact the developer with your device model!");
}
// If we found a usable file, check that it is writable
if(chargingEnabledFile) {
if(chargingEnabledFile->open(QIODevice::WriteOnly)) {
logV("Controlling charging via" + chargingEnabledFile->fileName());
chargingEnabledFile->close();
}
else {
logE("Charger control file" + chargingEnabledFile->fileName() + "is not writable");
logE("Charger control feature disabled");
delete chargingEnabledFile;
chargingEnabledFile = Q_NULLPTR;
}
}
// TODO
// Use DBus instead of polling the files - where is the documentation?
// QFileWatcher doesn't work, because e.g. the charge file
// isn't a real file, and it doesn't "change" when the value changes.
updateData();
}
@ -100,25 +81,16 @@ void Battery::updateData()
if(nextCharge != charge) {
charge = nextCharge;
emit chargeChanged(charge);
logV(QString("Battery: %1").arg(charge));
logV(QString("Battery: %1%").arg(charge));
}
chargeFile->close();
}
if(currentFile && currentFile->open(QIODevice::ReadOnly)) {
nextCurrent = currentFile->readLine().trimmed().toInt();
if(nextCurrent != current) {
current = nextCurrent;
emit currentChanged(current);
logV(QString("Current: %1").arg(current));
}
currentFile->close();
}
if(chargerConnectedFile && chargerConnectedFile->open(QIODevice::ReadOnly)) {
nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt();
if(nextChargerConnected != chargerConnected) {
chargerConnected = nextChargerConnected;
emit chargerConnectedChanged(chargerConnected);
logV(QString("Charger is connected: %1").arg(chargerConnected));
logV(QString("Charger: %1").arg(chargerConnected ? "connected" : "disconnected"));
}
chargerConnectedFile->close();
}
@ -127,10 +99,19 @@ void Battery::updateData()
if(nextState != state) {
state = nextState;
emit stateChanged(state);
logV(QString("Charging status: %1").arg(state));
logV("State: " + state);
}
stateFile->close();
}
if(currentFile && currentFile->open(QIODevice::ReadOnly)) {
nextCurrent = currentFile->readLine().trimmed().toInt();
if(nextCurrent != current) {
current = nextCurrent;
emit currentChanged(current);
logD(QString("Current: %1mA").arg(current / 1000));
}
currentFile->close();
}
}
int Battery::getCharge(){ return charge; }
@ -141,6 +122,4 @@ QString Battery::getState() { return state; }
bool Battery::getChargingEnabled() { return chargingEnabled; }
bool Battery::getChargerConnected() {
return chargerConnected;
}
bool Battery::getChargerConnected() { return chargerConnected; }

View file

@ -74,11 +74,6 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject
disableChargingValue = 1;
}
if(!chargingEnabledFile && QHostInfo::localHostName().contains("SailfishEmul")) {
logE("Charger control file not found!");
logE("Please contact the developer with your device model!");
}
// If we found a usable file, check that it is writable
if(chargingEnabledFile) {
logE("Charger control file: " + chargingEnabledFile->fileName());
@ -86,9 +81,17 @@ Battery::Battery(Logger* newLogger, bool loglevelSet, QObject *parent) : QObject
chargingEnabledFile->close();
}
else {
logE("Charger control file is not writable!");
logE("Charger control file" + chargingEnabledFile->fileName() + "is not writable");
logE("Charger control feature disabled");
delete chargingEnabledFile;
chargingEnabledFile = Q_NULLPTR;
}
}
else if(!QHostInfo::localHostName().contains("SailfishEmul")) {
logE("Charger control file not found!");
logE("Please contact the developer with your device model!");
}
connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateData()));
connect(settings, SIGNAL(resetTimers()), this, SLOT(resetTimers()));
@ -125,7 +128,7 @@ void Battery::updateData()
if(nextChargerConnected != chargerConnected) {
chargerConnected = nextChargerConnected;
emit chargerConnectedChanged(chargerConnected);
logV(QString("Charger was %1").arg(chargerConnected ? "connected" : "disconnected"));
logV(QString("Charger: %1").arg(chargerConnected ? "connected" : "disconnected"));
}
chargerConnectedFile->close();
}
@ -134,7 +137,7 @@ void Battery::updateData()
if(nextState != state) {
state = nextState;
emit stateChanged(state);
logV("Charging state: " + state);
logV("State: " + state);
// Hide/show notification right away
resetTimers();
@ -196,7 +199,6 @@ void Battery::showHighNotification() {
}
void Battery::showLowNotification() {
logD(QString("Low notification if %1% < %2%)").arg(charge).arg(settings->getLowAlert()));
if(settings->getLowNotificationsInterval() < 610 && charge <= settings->getLowAlert() && state != "charging") {
logV(QString("Notification: %1").arg(settings->getNotificationTitle().arg(charge)));
notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile());
@ -266,7 +268,7 @@ void Battery::shutdown() {
logD("Low battery notification stopped");
}
// ENABLE/DISABLE CHARGING
if(!QHostInfo::localHostName().contains("SailfishEmul") && !setChargingEnabled(true)) {
if(!setChargingEnabled(true) && !QHostInfo::localHostName().contains("SailfishEmul")) {
logE("ERROR! Could not restore charger status! Your device "
"may not charge until reboot! If that doesn't help, "
"uninstall Battery Buddy and reboot your device.");

View file

@ -158,7 +158,7 @@ void Settings::updateConfig(const QString path) {
}
else {
watcher->addPath(path);
logD("Config file added to watchlist.");
logD("Config file added to watchlist");
}
if(restartTimers) {