]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/battery-util.c
NEWS: finalize for v256~rc3
[thirdparty/systemd.git] / src / shared / battery-util.c
index 948a5e3b9d6a4216f8a63ed1c0647a6e8b97c82b..37b3f6a6ea35767fa92c6920ddca4d593ba6b928 100644 (file)
@@ -12,7 +12,7 @@
 static int device_is_power_sink(sd_device *device) {
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
         bool found_source = false, found_sink = false;
-        sd_device *parent, *d;
+        sd_device *parent;
         int r;
 
         assert(device);
@@ -109,7 +109,6 @@ static bool battery_is_discharging(sd_device *d) {
 int on_ac_power(void) {
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
         bool found_ac_online = false, found_discharging_battery = false;
-        sd_device *d;
         int r;
 
         r = sd_device_enumerator_new(&e);
@@ -225,7 +224,7 @@ int battery_read_capacity_percentage(sd_device *dev) {
                 return log_device_debug_errno(dev, r, "Failed to read/parse POWER_SUPPLY_CAPACITY: %m");
 
         if (battery_capacity < 0 || battery_capacity > 100)
-                return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ERANGE), "Invalid battery capacity");
+                return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ERANGE), "Invalid battery capacity: %d", battery_capacity);
 
         return battery_capacity;
 }
@@ -234,7 +233,6 @@ int battery_read_capacity_percentage(sd_device *dev) {
 int battery_is_discharging_and_low(void) {
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
         bool unsure = false, found_low = false;
-        sd_device *dev;
         int r;
 
          /* We have not used battery capacity_level since value is set to full
@@ -243,13 +241,13 @@ int battery_is_discharging_and_low(void) {
 
         r = on_ac_power();
         if (r < 0)
-                log_debug_errno(r, "Failed to check if the system is running on AC, assuming it is not: %m");
+                log_warning_errno(r, "Failed to check if the system is running on AC, assuming it is not: %m");
         if (r > 0)
                 return false;
 
         r = battery_enumerator_new(&e);
         if (r < 0)
-                return log_debug_errno(r, "Failed to initialize battery enumerator: %m");
+                return log_error_errno(r, "Failed to initialize battery enumerator: %m");
 
         FOREACH_DEVICE(e, dev) {
                 int level;
@@ -260,20 +258,26 @@ int battery_is_discharging_and_low(void) {
                         continue;
                 }
 
-                if (level > BATTERY_LOW_CAPACITY_LEVEL) /* Found a charged battery */
+                if (level > BATTERY_LOW_CAPACITY_LEVEL) { /* Found a charged battery */
+                        log_device_full(dev,
+                                        found_low ? LOG_INFO : LOG_DEBUG,
+                                        "Found battery with capacity above threshold (%d%% > %d%%).",
+                                        level, BATTERY_LOW_CAPACITY_LEVEL);
                         return false;
+                }
 
+                log_device_info(dev,
+                                "Found battery with capacity below threshold (%d%% <= %d%%).",
+                                level, BATTERY_LOW_CAPACITY_LEVEL);
                 found_low = true;
         }
 
         /* If we found a battery whose state we couldn't read, don't assume we are in low battery state */
-        if (unsure)
+        if (unsure) {
+                log_notice("Found battery with unreadable state, assuming not in low battery state.");
                 return false;
+        }
 
-        /* Found no charged battery, but did find low batteries */
-        if (found_low)
-                return true;
-
-        /* Found neither charged nor low batteries? let's return that we aren't on low battery state */
-        return false;
+        /* If found neither charged nor low batteries, assume that we aren't in low battery state */
+        return found_low;
 }