2
0
mirror of https://github.com/koreader/koreader synced 2024-11-04 12:00:25 +00:00
koreader/frontend/dbg.lua
NiLuJe 9bf19d1bb3
Assorted bag'o tweaks & fixes (#9569)
* UIManager: Support more specialized update modes for corner-cases:
  * A2, which we'll use for the VirtualKeyboards keys (they'd... inadvertently switched to UI with the highlight refactor).
  * NO_MERGE variants of ui & partial (for sunxi). Use `[ui]` in ReaderHighlight's popup, because of a Sage kernel bug that could otherwise make it translucent, sometimes completely so (*sigh*).
* UIManager: Assorted code cleanups & simplifications.
* Logger & dbg: Unify logging style, and code cleanups.
* SDL: Unbreak suspend/resume outside of the emulator (fix #9567).
* NetworkMgr: Cache the network status, and allow it to be queried. (Used by AutoSuspend to avoid repeatedly poking the system when computing the standby schedule delay).
* OneTimeMigration: Don't forget about `NETWORK_PROXY` & `STARDICT_DATA_DIR` when migrating `defaults.persistent.lua` (fix #9573)
* WakeupMgr: Workaround an apparent limitation of the RTC found on i.MX5 Kobo devices, where setting a wakealarm further than UINT16_MAX seconds in the future would apparently overflow and wraparound... (fix #8039, many thanks to @yfede for the extensive deep-dive and for actually accurately pinpointing the issue!).
* Kobo: Handle standby transitions at full CPU clock speeds, in order to limit the latency hit.
* UIManager: Properly quit on reboot & exit. This ensures our exit code is preserved, as we exit on our own terms (instead of being killed by the init system). This is important on platforms where exit codes are semantically meaningful (e.g., Kobo).
* UIManager: Speaking of reboot & exit, make sure the Screensaver shows in all circumstances (e.g., autoshutdown, re: #9542)), and that there aren't any extraneous refreshes triggered. (Additionally, fix a minor regression since #9448 about tracking this very transient state on Kobo & Cervantes).
* Kindle: ID the upcoming Scribe.
* Bump base (https://github.com/koreader/koreader-base/pull/1524)
2022-10-02 03:01:49 +02:00

133 lines
3.5 KiB
Lua

--[[--
This module provides development-only asserts and other debug guards.
Instead of a regular Lua @{assert}(), use @{dbg.dassert}() which can be toggled at runtime.
dbg.dassert(important_variable ~= nil)
For checking whether the input given to a function is sane, you can use @{dbg.guard}().
dbg:guard(NickelConf.frontLightLevel, "set",
function(new_intensity)
assert(type(new_intensity) == "number",
"Wrong brightness value type (expected number)!")
assert(new_intensity >= 0 and new_intensity <= 100,
"Wrong brightness value given!")
end)
These functions don't do anything when debugging is turned off.
--]]--
local logger = require("logger")
local dump = require("dump")
local isAndroid, android = pcall(require, "android")
local Dbg = {
-- set to nil so first debug:turnOff call won't be skipped
is_on = nil,
is_verbose = nil,
}
local Dbg_mt = {}
local LvDEBUG
if isAndroid then
LvDEBUG = function(...)
local line = {}
for _, v in ipairs({...}) do
if type(v) == "table" then
table.insert(line, dump(v, math.huge))
else
table.insert(line, tostring(v))
end
end
return android.LOGV(table.concat(line, " "))
end
else
LvDEBUG = function(...)
local line = {
os.date("%x-%X DEBUG"),
}
for _, v in ipairs({...}) do
if type(v) == "table" then
table.insert(line, dump(v, math.huge))
else
table.insert(line, tostring(v))
end
end
table.insert(line, "\n")
return io.write(table.concat(line, " "))
end
end
--- Helper function to help dealing with nils in Dbg:guard...
local function pack_values(...)
return select("#", ...), {...}
end
--- Turn on debug mode.
-- This should only be used in tests and at the user's request.
function Dbg:turnOn()
if self.is_on == true then return end
self.is_on = true
logger:setLevel(logger.levels.dbg)
Dbg_mt.__call = function(_, ...) return LvDEBUG(...) end
--- Pass a guard function to detect bad input values.
Dbg.guard = function(_, mod, method, pre_guard, post_guard)
local old_method = mod[method]
mod[method] = function(...)
if pre_guard then
pre_guard(...)
end
local n, values = pack_values(old_method(...))
if post_guard then
post_guard(...)
end
return unpack(values, 1, n)
end
end
--- Use this instead of a regular Lua @{assert}().
Dbg.dassert = function(check, msg)
assert(check, msg)
return check
end
end
--- Turn off debug mode.
-- This should only be used in tests and at the user's request.
function Dbg:turnOff()
if self.is_on == false then return end
self.is_on = false
logger:setLevel(logger.levels.info)
Dbg_mt.__call = function() end
-- NOTE: This doesn't actually disengage previously wrapped methods!
Dbg.guard = function() end
Dbg.dassert = function(check)
return check
end
end
--- Turn on verbose mode.
-- This should only be used in tests and at the user's request.
function Dbg:setVerbose(verbose)
self.is_verbose = verbose
end
--- Simple table dump.
function Dbg:v(...)
if self.is_verbose then
return LvDEBUG(...)
end
end
--- Simple traceback.
function Dbg:traceback()
return LvDEBUG(debug.traceback())
end
setmetatable(Dbg, Dbg_mt)
Dbg:turnOff()
return Dbg