2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/battery.lua
Tigran Aivazian f047084320 Cleanups of battery logging
1. No need to pass the header through string.format()
2. Check for G_battery_logging value inside logBatteryLevel() function
to simply the code in the callers thereof.
2012-11-21 14:05:00 +00:00

25 lines
757 B
Lua

-- return the current battery level
function BatteryLevel()
local p = io.popen("gasgauge-info -s 2> /dev/null", "r") -- io.popen() _never_ fails!
local battery = p:read("*a") or "?"
if battery == "" then battery = "?" end
p:close()
return string.gsub(battery, "[\n\r]+", "")
end
-- log battery level in "batlog.txt" file, blevent can be any string
function logBatteryLevel(blevent)
if not G_battery_logging then return end
local file = io.open("batlog.txt", "a+")
if file then
if file:seek("end") == 0 then -- write the header only once
file:write("DATE\t\tTIME\t\tBATTERY\tEVENT\n")
end
file:write(string.format("%s\t%s\t%s\t%s\n",
os.date("%d-%b-%y"), os.date("%T"),
BatteryLevel(), blevent or "RUNNING"))
file:close()
end
end