2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/dbg.lua

109 lines
2.7 KiB
Lua
Raw Normal View History

local logger = require("logger")
local dump = require("dump")
local isAndroid, android = pcall(require, "android")
2013-03-12 17:18:53 +00:00
2013-10-18 20:38:07 +00:00
local Dbg = {
-- set to nil so first debug:turnOff call won't be skipped
is_on = nil,
is_verbose = nil,
2014-03-13 13:52:43 +00:00
ev_log = nil,
2013-03-12 17:18:53 +00:00
}
2013-10-18 20:38:07 +00:00
local Dbg_mt = {}
local function LvDEBUG(lv, ...)
2014-03-13 13:52:43 +00:00
local line = ""
for i,v in ipairs({...}) do
if type(v) == "table" then
line = line .. " " .. dump(v, lv)
2014-03-13 13:52:43 +00:00
else
line = line .. " " .. tostring(v)
end
end
if isAndroid then
2018-12-29 19:47:45 +00:00
android.LOGV(line)
else
io.stdout:write(string.format("# %s %s\n", os.date("%x-%X"), line))
io.stdout:flush()
end
2013-10-18 20:38:07 +00:00
end
2013-03-12 17:18:53 +00:00
function Dbg:turnOn()
if self.is_on == true then return end
2014-03-13 13:52:43 +00:00
self.is_on = true
logger:setLevel(logger.levels.dbg)
2013-03-12 17:18:53 +00:00
Dbg_mt.__call = function(dbg, ...) LvDEBUG(math.huge, ...) end
2016-06-05 02:51:47 +00:00
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 values = {old_method(...)}
if post_guard then
post_guard(...)
end
return unpack(values)
end
end
Dbg.dassert = function(check, msg)
assert(check, msg)
return check
end
2014-03-13 13:52:43 +00:00
-- create or clear ev log file
--- @note: On Linux, use CLOEXEC to avoid polluting the fd table of our child processes.
--- Otherwise, it can be problematic w/ wpa_supplicant & USBMS...
--- Note that this is entirely undocumented, but at least LuaJIT passes the mode as-is to fopen, so, we're good.
if jit.os == "Linux" then
self.ev_log = io.open("ev.log", "we")
else
self.ev_log = io.open("ev.log", "w")
end
2013-03-12 17:18:53 +00:00
end
function Dbg:turnOff()
if self.is_on == false then return end
self.is_on = false
logger:setLevel(logger.levels.info)
function Dbg_mt.__call() end
function Dbg.guard() end
Dbg.dassert = function(check)
return check
end
if self.ev_log then
io.close(self.ev_log)
self.ev_log = nil
end
end
function Dbg:setVerbose(verbose)
self.is_verbose = verbose
end
function Dbg:v(...)
if self.is_verbose then
LvDEBUG(math.huge, ...)
end
end
2013-03-12 17:18:53 +00:00
function Dbg:logEv(ev)
local ev_value = tostring(ev.value)
2014-03-13 13:52:43 +00:00
local log = ev.type.."|"..ev.code.."|"
..ev_value.."|"..ev.time.sec.."|"..ev.time.usec.."\n"
if self.ev_log then
self.ev_log:write(log)
self.ev_log:flush()
end
2013-03-12 17:18:53 +00:00
end
2013-10-25 12:07:58 +00:00
function Dbg:traceback()
2014-03-13 13:52:43 +00:00
LvDEBUG(math.huge, debug.traceback())
2013-10-25 12:07:58 +00:00
end
2013-10-18 20:38:07 +00:00
setmetatable(Dbg, Dbg_mt)
Dbg:turnOff()
2013-10-18 20:38:07 +00:00
return Dbg