summaryrefslogtreecommitdiff
path: root/lib/supple/track.lua
blob: b22156fbaf9118a45483ed1fa1bf0f049dd52b6d (plain)
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
-- lib/supple/track.lua
--
-- Sandbox (for) Untrusted Procedure Partitioning (in) Lua Engine
--
-- Tracking and logging for debug porpoises.
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
--
-- For licence terms, see COPYING
--

local depth
local track

local function record(...)
   if depth then
      track[#track+1] = { depth, ... }
   end
end

local function enter(...)
   if depth then
      record(">>>", ...)
      depth = depth + 1
   end
end

local function leave(...)
   if depth then
      if depth > 0 then
	 depth = depth - 1
      end
      record("<<<", ...)
   end
end

local function start_tracking()
   depth, track = 0, {}
end

local function stop_tracking()
   if track == nil then
      return "***NO TRACKING DATA***"
   end
   local ret = {}
   for i = 1, #track do
      local ent = track[i]
      local pfx = ("  "):rep(ent[1])
      table.remove(ent, 1)
      ret[#ret+1] = pfx .. table.concat(ent, " ")
   end
   depth, track = nil, {}
   return table.concat(ret, "\n")
end

return {
   start = start_tracking,
   stop = stop_tracking,
   enter = enter,
   leave = leave,
   record = record,
}