Debugging Timers across Modules

BACKGROUND:

The code in my game file has exceed 10,000 lines and 200 functions so I’ve decided its time to break it into modules :confused:

PROBLEM:

It is extremely difficult to debug timers across modules.  I’ve created an example that purposely causes an error (see below).  In order for me to properly debug this error I need to know where the timer was created (which file, and which line).  The corona debugger doesn’t provide that information.

OLD WORK AROUND:

What I use to do was just run a search on the function being called that caused the error until I found the timer that was calling it.  However, now that I’ve broke up the code into many different files that process is very difficult.  I must now open each file individual and run a search.

Please let me know any ideas to help with debugging timers across modules.  Thanks.

main.lua

[lua]

local heroFunctions = require “heroFunctions”

local gameFunctions = require “gameFunctions”

startGame()

[/lua]

gameFunctions.lua

[lua]

local M = {}

function startGame()

local tempHero

print(“Start Game”)

tempHero = createHero()

print(tempHero)

heroTimer = timer.performWithDelay(1000,moveHero,1)

heroTimer.params = {}

heroTimer.params.object = tempHero

display.remove(tempHero)

tempHero = nil

end

M.startGame = startGame

return M

[/lua]

heroFunctions.lua

[lua]

local M = {}

function createHero()

local hero

print(“Hero Was Created”)

hero = display.newCircle(50,50,50)

return hero

end

M.createHero = createHero

function moveHero( event )

print(“Moved Hero”)

local tempParams

local tempHero

tempParams = event.source.params

tempHero = tempParams.object

tempHero.x = tempHero.x + 50

end

M.moveHero = moveHero

return M

[/lua]

Hi.

It’s quick and dirty, but maybe something like this (defined early on in your program) would help:

if true then -- Make this "timer debugging" option local old\_tpf = timer.performWithDelay function timer.performWithDelay (n, func, ...) local info = debug.getinfo(2) return old\_tpf(n, function(e) local ok, err = pcall(func, e) if not ok then local message = ("Error with timer created in %q at line: %i\n\n"):format(info.short\_src, info.currentline) error(message .. tostring(err)) -- rethrow the error end end, ...) end end

Test:

timer.performWithDelay(1000, function() print("Good?") B[2] = 3 print("Nope :(") end)

EDIT : Doesn’t look like the debug functions are in the Corona docs, but they’re in the manual.

Note: Using the debug.* library is very helpful and informative, but be careful that you don’t retain debug.* calls in your final releases as the debug.* library is removed when building release builds for iOS.  

Also, and I’m not sure this is a correct recollection, but I think it is removed for all device builds on Android.

Awesome thank you both very much!

Star Crunch, I’m not following what the variable ‘e’ does and where it’s coming from in the line

return old_tpf(n, function(e)

Thanks

Ah, sorry, in the timer docs it would be " event" and it just gets passed along to the listener.

Actually, looking the docs over just now, " e" would be better served by " " (alternatively, just have two parameters), to account for table listeners as well.

Hi.

It’s quick and dirty, but maybe something like this (defined early on in your program) would help:

if true then -- Make this "timer debugging" option local old\_tpf = timer.performWithDelay function timer.performWithDelay (n, func, ...) local info = debug.getinfo(2) return old\_tpf(n, function(e) local ok, err = pcall(func, e) if not ok then local message = ("Error with timer created in %q at line: %i\n\n"):format(info.short\_src, info.currentline) error(message .. tostring(err)) -- rethrow the error end end, ...) end end

Test:

timer.performWithDelay(1000, function() print("Good?") B[2] = 3 print("Nope :(") end)

EDIT : Doesn’t look like the debug functions are in the Corona docs, but they’re in the manual.

Note: Using the debug.* library is very helpful and informative, but be careful that you don’t retain debug.* calls in your final releases as the debug.* library is removed when building release builds for iOS.  

Also, and I’m not sure this is a correct recollection, but I think it is removed for all device builds on Android.

Awesome thank you both very much!

Star Crunch, I’m not following what the variable ‘e’ does and where it’s coming from in the line

return old_tpf(n, function(e)

Thanks

Ah, sorry, in the timer docs it would be " event" and it just gets passed along to the listener.

Actually, looking the docs over just now, " e" would be better served by " " (alternatively, just have two parameters), to account for table listeners as well.