--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
title = "Debug Function Call Trace"

require("defines");

	
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	print(os.date("setup() %d %b %Y   %X"));

	MyTIUNo = TIU;
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)

	print("loop()");
	MyTIUNo = TIU;
	swapem(5);
	return Stop("Script requested termination");		-- end the script
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
	print(os.date("cleanup()  %d %b %Y   %X"));
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function CreateStackTraceHook()
  local prefix = "-->"
  return function(event, curline)
      local info = debug.getinfo(2, "S")
      if (info.what == "C") then return end				-- do not log C functions
        local info2 = debug.getinfo(2, "n")
        local src = info.short_src
        local name = info2.name or ""
        local line = info.linedefined
      if (event == "call") then
        print(prefix .. "call " .. src .. "(" .. line .. "): " .. name)
        prefix = prefix .. "-->"
      elseif (event == "return" or event == "tail return") then
	    print(prefix .. "return");
        prefix = string.sub(prefix, 4)
	  elseif (event == "line") then
        print(prefix .. "line " .. src .. "(" .. curline .. ")")	    
      end
  end
end
--[[---------------------------------------------------------------------------------------]]
function swapem(NewTIU)
	MyTIUNo = NewTIU;
	return
end
	
--[[---------------------------------------------------------------------------------------]]
debug.sethook(CreateStackTraceHook(), "crl")	
--[[---------------------------------------------------------------------------------------]]
