Merge pull request #1502 from dibdot/adblock
[project/luci.git] / modules / luci-base / luasrc / debug.lua
1 local debug = require "debug"
2 local io = require "io"
3 local collectgarbage, floor = collectgarbage, math.floor
4
5 module "luci.debug"
6 __file__ = debug.getinfo(1, 'S').source:sub(2)
7
8 -- Enables the memory tracer with given flags and returns a function to disable the tracer again
9 function trap_memtrace(flags, dest)
10         flags = flags or "clr"
11         local tracefile = io.open(dest or "/tmp/memtrace", "w")
12         local peak = 0
13
14         local function trap(what, line)
15                 local info = debug.getinfo(2, "Sn")
16                 local size = floor(collectgarbage("count"))
17                 if size > peak then
18                         peak = size
19                 end
20                 if tracefile then
21                         tracefile:write(
22                                 "[", what, "] ", info.source, ":", (line or "?"), "\t",
23                                 (info.namewhat or ""), "\t",
24                                 (info.name or ""), "\t",
25                                 size, " (", peak, ")\n"
26                         )
27                 end
28         end
29
30         debug.sethook(trap, flags)
31
32         return function()
33                 debug.sethook()
34                 tracefile:close()
35         end
36 end
37