atrace: Better than print for debugging

Hi everyone,

I haven’t bought the SDK, but I’m starting to get into Corona. One nagging issue I had to deal with was the lack of good logging info, so I ported over my atrace code from AS3.

Instead of print statements, you can use atrace and it’ll print a message to the console as well as the file, line number, time, and you can specify an optional parameter for the depth of the stack, so you can see where your function is being called from.

require("gr\_util")  
atrace("debug me!")  
-- gr\_util.lua  
-- Don-Duong Quach   
-- some utility functions  
  
function string:split(sSeparator, nMax, bRegexp)  
 assert(sSeparator ~= '')  
 assert(nMax == nil or nMax \>= 1)  
  
 local aRecord = {}  
  
 if self:len() \> 0 then  
 local bPlain = not bRegexp  
 nMax = nMax or -1  
  
 local nField=1 nStart=1  
 local nFirst,nLast = self:find(sSeparator, nStart, bPlain)  
 while nFirst and nMax ~= 0 do  
 aRecord[nField] = self:sub(nStart, nFirst-1)  
 nField = nField+1  
 nStart = nLast+1  
 nFirst,nLast = self:find(sSeparator, nStart, bPlain)  
 nMax = nMax-1  
 end  
 aRecord[nField] = self:sub(nStart)  
 end  
  
 return aRecord  
end  
  
function formatMS(\_ms)  
 if not \_ms then  
 return ""  
 end  
  
 local ms = \_ms % 1000  
 local secs = math.floor(\_ms / 1000)  
 local mins = math.floor(secs / 60)  
 local hours = math.floor(mins / 60)  
 secs = secs % 60  
 mins = mins % 60  
 hours = hours % 24  
 return string.format("%02d:%02d:%02d:%03d", hours, mins, secs, ms)  
end  
  
-- Table functions  
function table.indexOf(\_t, \_item)  
 for i, v in ipairs(\_t) do  
 if \_item == v then  
 return i  
 end  
 end  
 return -1  
end  
  
function table.removeItem(\_t, \_item)  
 local i = table.indexOf(\_t, \_item)  
 if i ~= -1 then  
 table.remove(t, i)  
 end  
end  
-- Some utilities for better debugging, print\_r taken from Corona's CodeExchange  
  
-- atrace: pass a message and how deep you want to print the stack  
function atrace(\_msg, \_depth)   
 if \_msg == nil then  
 \_msg = "nil"  
 else  
 \_msg = tostring(\_msg)  
 end  
  
 \_depth = \_depth or 1  
  
 local sysTime = system and ("(" .. formatMS(system.getTimer()) .. ") ") or "- "  
 print(" = \> atrace " .. sysTime .. \_msg)  
 local res = debug.traceback():split("\n")  
  
 local counter = 1  
 for i, v in ipairs(res) do  
 counter = counter + 1  
 if counter \> 3 and counter \< (3+\_depth+1) then  
 print(tostring(v))  
 end  
  
 end  
 --print(table.concat(res, "\n", 3, \_depth + 2))   
 -- sometimes gets an error that some table values in res aren't strings  
 print()  
end  
-- xinspect: returns t as a nicely formatted string, if t is a table  
function xinspect( t )  
 status, retval = pcall(print\_r, t)  
 return retval  
end  
  
function print\_r ( t )   
 local out = {}  
 local print\_r\_cache={}  
 local function sub\_print\_r(t,indent)  
 if (print\_r\_cache[tostring(t)]) then  
 table.insert(out, indent.."\*"..tostring(t))  
 else  
 print\_r\_cache[tostring(t)]=true  
 if (type(t)=="table") then  
 for pos,val in pairs(t) do  
 if (type(val)=="table") then  
 table.insert(out, indent.."["..pos.."] =\> "..tostring(t).." {")  
 sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8))  
 table.insert(out, indent..string.rep(" ",string.len(pos)+6).."}")  
 elseif (type(val)=="string") then  
 table.insert(out, indent.."["..pos..'] =\> "'..val..'"')  
 else  
 table.insert(out, indent.."["..pos.."] =\> "..tostring(val))  
 end  
 end  
 else  
 table.insert(out, indent..tostring(t))  
 end  
 end  
 end  
  
 if (t["\_\_tostring"]) then  
 table.insert(out, tostring(t))  
 elseif (type(t)=="table") then  
 table.insert(out, tostring(t).." {")  
 sub\_print\_r(t," ")  
 table.insert(out, "}")  
 else  
 sub\_print\_r(t," ")  
 end  
  
 return table.concat(out, "\n")  
end  
  

Let me know if you find this useful! Also I’m also interested in connecting with devs and designers just to talk shop, if you’re interested you can reach met a geekrelief @ gmail.com [import]uid: 27183 topic_id: 7242 reply_id: 307242[/import]

Another good candidate for the Code Exchange. Looks handy. I’ll give it a run. [import]uid: 3953 topic_id: 7242 reply_id: 26080[/import]

Thanks!! [import]uid: 42670 topic_id: 7242 reply_id: 26491[/import]

Nice! Thanks for sharing! [import]uid: 8782 topic_id: 7242 reply_id: 26496[/import]