2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2014-11-14 15:33:52 +00:00
|
|
|
local dump = require("dump")
|
2014-05-26 15:38:51 +00:00
|
|
|
local isAndroid, android = pcall(require, "android")
|
2013-03-12 17:18:53 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local Dbg = {
|
2016-10-25 07:08:47 +00:00
|
|
|
-- set to nil so first debug:turnOff call won't be skipped
|
2016-03-29 06:37:15 +00:00
|
|
|
is_on = nil,
|
2016-08-12 06:05:18 +00:00
|
|
|
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
|
2014-11-14 15:33:52 +00:00
|
|
|
line = line .. " " .. dump(v, lv)
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
|
|
|
line = line .. " " .. tostring(v)
|
|
|
|
end
|
|
|
|
end
|
2014-05-26 15:38:51 +00:00
|
|
|
if isAndroid then
|
2018-12-29 19:47:45 +00:00
|
|
|
android.LOGV(line)
|
2014-05-26 15:38:51 +00:00
|
|
|
else
|
2016-07-24 23:47:10 +00:00
|
|
|
io.stdout:write(string.format("# %s %s\n", os.date("%x-%X"), line))
|
2014-10-18 13:31:05 +00:00
|
|
|
io.stdout:flush()
|
2014-05-26 15:38:51 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
2013-03-12 17:18:53 +00:00
|
|
|
function Dbg:turnOn()
|
2016-03-29 06:37:15 +00:00
|
|
|
if self.is_on == true then return end
|
2014-03-13 13:52:43 +00:00
|
|
|
self.is_on = true
|
2016-12-29 08:10:38 +00:00
|
|
|
logger:setLevel(logger.levels.dbg)
|
2013-03-12 17:18:53 +00:00
|
|
|
|
2016-03-29 06:37:15 +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(...)
|
2016-03-29 06:37:15 +00:00
|
|
|
if pre_guard then
|
|
|
|
pre_guard(...)
|
|
|
|
end
|
|
|
|
local values = {old_method(...)}
|
|
|
|
if post_guard then
|
|
|
|
post_guard(...)
|
|
|
|
end
|
|
|
|
return unpack(values)
|
|
|
|
end
|
|
|
|
end
|
2017-06-23 17:04:11 +00:00
|
|
|
Dbg.dassert = function(check, msg)
|
|
|
|
assert(check, msg)
|
|
|
|
return check
|
|
|
|
end
|
2016-03-29 06:37:15 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
-- create or clear ev log file
|
2020-08-25 00:00:59 +00:00
|
|
|
--- @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
|
|
|
|
|
2016-03-29 06:37:15 +00:00
|
|
|
function Dbg:turnOff()
|
|
|
|
if self.is_on == false then return end
|
|
|
|
self.is_on = false
|
2016-12-29 08:10:38 +00:00
|
|
|
logger:setLevel(logger.levels.info)
|
2016-03-29 06:37:15 +00:00
|
|
|
function Dbg_mt.__call() end
|
|
|
|
function Dbg.guard() end
|
2017-06-23 17:04:11 +00:00
|
|
|
Dbg.dassert = function(check)
|
|
|
|
return check
|
|
|
|
end
|
2016-03-29 06:37:15 +00:00
|
|
|
if self.ev_log then
|
|
|
|
io.close(self.ev_log)
|
|
|
|
self.ev_log = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-12 06:05:18 +00:00
|
|
|
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)
|
2018-04-03 12:56:28 +00:00
|
|
|
local ev_value = tostring(ev.value)
|
2014-03-13 13:52:43 +00:00
|
|
|
local log = ev.type.."|"..ev.code.."|"
|
2018-04-03 12:56:28 +00:00
|
|
|
..ev_value.."|"..ev.time.sec.."|"..ev.time.usec.."\n"
|
2015-10-03 06:18:47 +00:00
|
|
|
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-03-15 09:01:34 +00:00
|
|
|
|
2016-03-29 06:37:15 +00:00
|
|
|
Dbg:turnOff()
|
2013-10-18 20:38:07 +00:00
|
|
|
return Dbg
|