This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from lua-stdlib/lua-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_ext.lua
More file actions
84 lines (77 loc) · 2.2 KB
/
debug_ext.lua
File metadata and controls
84 lines (77 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--- Additions to the debug module
module ("debug", package.seeall)
require "debug_init"
require "io_ext"
require "string_ext"
--- To activate debugging set _DEBUG either to any true value
-- (equivalent to {level = 1}), or as documented below.
-- @class table
-- @name _DEBUG
-- @field level debugging level
-- @field call do call trace debugging
-- @field std do standard library debugging (run examples & test code)
--- Print a debugging message
-- @param n debugging level, defaults to 1
-- @param ... objects to print (as for print)
function say (n, ...)
local level = 1
local arg = {n, ...}
if type (arg[1]) == "number" then
level = arg[1]
table.remove (arg, 1)
end
if _DEBUG and
((type (_DEBUG) == "table" and type (_DEBUG.level) == "number" and
_DEBUG.level >= level)
or level <= 1) then
io.writelines (io.stderr, table.concat (list.map (tostring, arg), "\t"))
end
end
---
-- The global function <code>debug</code> is an abbreviation for
-- <code>debug.say (1, ...)</code>
-- @class function
-- @name debug
-- @see say
getmetatable (_M).__call =
function (self, ...)
say (1, ...)
end
--- Trace function calls
-- Use as debug.sethook (trace, "cr"), which is done automatically
-- when _DEBUG.call is set.
-- Based on test/trace-calls.lua from the Lua distribution.
-- @class function
-- @name trace
-- @param event event causing the call
local level = 0
function trace (event)
local t = getinfo (3)
local s = " >>> " .. string.rep (" ", level)
if t ~= nil and t.currentline >= 0 then
s = s .. t.short_src .. ":" .. t.currentline .. " "
end
t = getinfo (2)
if event == "call" then
level = level + 1
else
level = math.max (level - 1, 0)
end
if t.what == "main" then
if event == "call" then
s = s .. "begin " .. t.short_src
else
s = s .. "end " .. t.short_src
end
elseif t.what == "Lua" then
s = s .. event .. " " .. (t.name or "(Lua)") .. " <" ..
t.linedefined .. ":" .. t.short_src .. ">"
else
s = s .. event .. " " .. (t.name or "(C)") .. " [" .. t.what .. "]"
end
io.writelines (io.stderr, s)
end
-- Set hooks according to _DEBUG
if type (_DEBUG) == "table" and _DEBUG.call then
sethook (trace, "cr")
end