mirror of https://github.com/koreader/koreader
feat: add logger module & rewrite kobo suspend script in lua
parent
5bda44473d
commit
f95ad00b9e
@ -0,0 +1,88 @@
|
||||
--[[--
|
||||
Logger module.
|
||||
See @{Logger.levels} for list of supported levels.
|
||||
|
||||
Example:
|
||||
|
||||
local logger = require("logger")
|
||||
logger.info("Something happened.")
|
||||
logger.err("House is on fire!")
|
||||
]]
|
||||
|
||||
local dump = require("dump")
|
||||
local isAndroid, android = pcall(require, "android")
|
||||
|
||||
local DEFAULT_DUMP_LVL = 10
|
||||
|
||||
--- Supported logging levels
|
||||
-- @table Logger.levels
|
||||
-- @field dbg debug
|
||||
-- @field info informational (default level)
|
||||
-- @field warn warning
|
||||
-- @field err error
|
||||
local LOG_LVL = {
|
||||
dbg = 1,
|
||||
info = 2,
|
||||
warn = 3,
|
||||
err = 4,
|
||||
}
|
||||
|
||||
local LOG_PREFIX = {
|
||||
dbg = 'DEBUG',
|
||||
info = 'INFO ',
|
||||
warn = 'WARN ',
|
||||
err = 'ERROR',
|
||||
}
|
||||
|
||||
local noop = function() end
|
||||
|
||||
local Logger = {
|
||||
levels = LOG_LVL,
|
||||
}
|
||||
|
||||
local function log(log_lvl, dump_lvl, ...)
|
||||
local line = ""
|
||||
for i,v in ipairs({...}) do
|
||||
if type(v) == "table" then
|
||||
line = line .. " " .. dump(v, dump_lvl)
|
||||
else
|
||||
line = line .. " " .. tostring(v)
|
||||
end
|
||||
end
|
||||
if isAndroid then
|
||||
android.LOGI(LOG_PREFIX[log_lvl]..line)
|
||||
else
|
||||
io.stdout:write(os.date("%x-%X"), " ", LOG_PREFIX[log_lvl], line, "\n")
|
||||
io.stdout:flush()
|
||||
end
|
||||
end
|
||||
|
||||
local LVL_FUNCTIONS = {
|
||||
dbg = function(...) log('dbg', DEFAULT_DUMP_LVL, ...) end,
|
||||
info = function(...) log('info', DEFAULT_DUMP_LVL, ...) end,
|
||||
warn = function(...) log('warn', DEFAULT_DUMP_LVL, ...) end,
|
||||
err = function(...) log('err', DEFAULT_DUMP_LVL, ...) end,
|
||||
}
|
||||
|
||||
|
||||
--[[--
|
||||
Set logging level. By default, level is set to info.
|
||||
|
||||
@int new_lvl new logging level, must be one of the levels from @{Logger.levels}
|
||||
|
||||
@usage
|
||||
Logger:setLevel(Logger.levels.warn)
|
||||
]]
|
||||
function Logger:setLevel(new_lvl)
|
||||
for lvl_name, lvl_value in pairs(LOG_LVL) do
|
||||
if new_lvl <= lvl_value then
|
||||
self[lvl_name] = LVL_FUNCTIONS[lvl_name]
|
||||
else
|
||||
self[lvl_name] = noop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Logger:setLevel(LOG_LVL.info)
|
||||
|
||||
return Logger
|
@ -1,59 +0,0 @@
|
||||
#!/bin/sh
|
||||
export PATH="${PATH}:/sbin:/usr/sbin"
|
||||
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Going to sleep . . ."
|
||||
# NOTE: Sleep as little as possible here, sleeping has a tendency to make everything mysteriously hang...
|
||||
|
||||
# Depending on device/FW version, some kernels do not support wakeup_count, account for that
|
||||
if [ -e "/sys/power/wakeup_count" ] ; then
|
||||
#HAS_WAKEUP_COUNT="true"
|
||||
# NOTE: ... and of course, it appears to be broken, which probably explains why nickel doesn't use this facility...
|
||||
# (By broken, I mean that the system wakes up right away).
|
||||
# So, unless that changes, unconditionally disable it.
|
||||
HAS_WAKEUP_COUNT="false"
|
||||
fi
|
||||
|
||||
# Clear the kernel ring buffer... (we're missing a proper -C flag...)
|
||||
#dmesg -c >/dev/null
|
||||
|
||||
# Go to sleep
|
||||
if [ "${HAS_WAKEUP_COUNT}" == "true" ] ; then
|
||||
current_wakeup_count="$(cat /sys/power/wakeup_count)"
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Current WakeUp count: ${current_wakeup_count}"
|
||||
fi
|
||||
echo 1 > /sys/power/state-extended
|
||||
# NOTE: Sets gSleep_Mode_Suspend to 1. Used as a flag throughout the kernel to suspend/resume various subsystems
|
||||
# cf. kernel/power/main.c @ L#207
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Asked the kernel to put subsystems to sleep"
|
||||
sleep 2
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Waited for 2s because of reasons..."
|
||||
sync
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Synced FS"
|
||||
if [ "${HAS_WAKEUP_COUNT}" == "true" ] ; then
|
||||
echo ${current_wakeup_count} > /sys/power/wakeup_count
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Wrote WakeUp count: ${current_wakeup_count} ($?)"
|
||||
fi
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Asking for a suspend to RAM . . ."
|
||||
echo mem > /sys/power/state
|
||||
# NOTE: At this point, we *should* be in suspend to RAM, as such, execution should only resume on wakeup...
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: ZzZ ZzZ ZzZ? ($?)"
|
||||
## NOTE: Ideally, we'd need a way to warn the user that suspending gloriously failed at this point...
|
||||
## We can safely assume that just from a non-zero return code, without looking at the detailed stderr message
|
||||
## (most of the failures we'll see are -EBUSY anyway)
|
||||
## For reference, when that happens to nickel, it appears to keep retrying to wakeup & sleep ad nauseam,
|
||||
## which is where the non-sensical 1 -> mem -> 0 loop idea comes from...
|
||||
## cf. nickel_suspend_strace.txt for more details.
|
||||
|
||||
if [ "${HAS_WAKEUP_COUNT}" == "true" ] ; then
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Woke up! (WakeUp count: $(cat /sys/power/wakeup_count))"
|
||||
else
|
||||
echo "[$(date +'%x @ %X')] Kobo Suspend: Woke up!"
|
||||
fi
|
||||
|
||||
# Print tke kernel log since our attempt to sleep...
|
||||
#dmesg -c
|
||||
|
||||
# Now that we're up, unflag subsystems for suspend...
|
||||
# NOTE: We do that in Kobo:resume() to keep things tidy and easier to follow
|
||||
#echo 0 > /sys/power/state-extended
|
||||
#echo "[$(date +'%x @ %X')] Kobo Suspend: Unflagged kernel subsystems for suspend"
|
Loading…
Reference in New Issue