2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/frontend/dbg.lua

55 lines
1.1 KiB
Lua
Raw Normal View History

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 = {
2014-03-13 13:52:43 +00:00
is_on = false,
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
android.LOGI("#"..line)
else
print("#"..line)
io.stdout:flush()
end
2013-10-18 20:38:07 +00:00
end
function Dbg_mt.__call(dbg, ...)
2014-03-13 13:52:43 +00:00
if dbg.is_on then LvDEBUG(math.huge, ...) end
2013-10-18 20:38:07 +00:00
end
2013-03-12 17:18:53 +00:00
function Dbg:turnOn()
2014-03-13 13:52:43 +00:00
self.is_on = true
2013-03-12 17:18:53 +00:00
2014-03-13 13:52:43 +00:00
-- create or clear ev log file
self.ev_log = io.open("ev.log", "w")
2013-03-12 17:18:53 +00:00
end
function Dbg:logEv(ev)
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)
2013-10-18 20:38:07 +00:00
return Dbg