Browse code

misc/examples: kemi lua - added debug callback function

- commented - can be enabled to track the execution trace of the lua
script

(cherry picked from commit 491a8327933cb4471260fdd09ff93296a7a0ccfd)

Daniel-Constantin Mierla authored on 14/07/2020 07:21:04
Showing 1 changed files
... ...
@@ -12,6 +12,43 @@
12 12
 --  the execution of the script. Use KSR.x.exit() after it or KSR.x.drop()
13 13
 --
14 14
 
15
+-- debug callback function to print details of execution trace
16
+--[[
17
+local ksr_exec_level=0
18
+
19
+local function ksr_exec_hook(event)
20
+	local s = "";
21
+	local t = debug.getinfo(3)
22
+	s = s .. ksr_exec_level .. ">>> " .. string.rep(" ", ksr_exec_level);
23
+	if t~=nil and t.currentline>=0 then
24
+		s = s .. t.short_src .. ":" .. t.currentline .. " ";
25
+	end
26
+	t=debug.getinfo(2)
27
+	if event=="call" then
28
+		ksr_exec_level = ksr_exec_level + 1;
29
+	else
30
+		ksr_exec_level = ksr_exec_level - 1;
31
+		if ksr_exec_level < 0 then
32
+			ksr_exec_level = 0;
33
+		end
34
+	end
35
+	if t.what=="main" then
36
+		if event=="call" then
37
+			s = s .. "begin " .. t.short_src;
38
+		else
39
+			s = s .. "end " .. t.short_src;
40
+		end
41
+	elseif t.what=="Lua" then
42
+		s = s .. event .. " " .. t.name or "(Lua)" .. " <" .. t.linedefined .. ":" .. t.short_src .. ">";
43
+	else
44
+		s = s .. event .. " " .. t.name or "(C)" .. " [" .. t.what .. "] ";
45
+	end
46
+	KSR.info(s .. "\n");
47
+end
48
+
49
+debug.sethook(ksr_exec_hook, "cr")
50
+ksr_exec_level=0
51
+]]--
15 52
 
16 53
 -- global variables corresponding to defined values (e.g., flags) in kamailio.cfg
17 54
 FLT_ACC=1