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
|
|
|
|
android.LOGI("#"..line)
|
|
|
|
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
|
|
|
|
2017-02-20 08:51:54 +00:00
|
|
|
-- TODO: close ev.log fd for children
|
2014-03-13 13:52:43 +00:00
|
|
|
-- create or clear ev log file
|
2014-05-28 04:57:53 +00:00
|
|
|
self.ev_log = io.open("ev.log", "w")
|
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)
|
2014-03-13 13:52:43 +00:00
|
|
|
local log = ev.type.."|"..ev.code.."|"
|
|
|
|
..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
|