While a visual debugger would give everyone thrills, Corona SDK doesn’t have that (yet). But we do have the handy-dandy print() statement so we can print out debug strings and figure out what’s wrong with our code.
Unfortunately, print() doesn’t work on the device, and so if you run into a problem that doesn’t show up in the simulator, you’re reduced to guessing, poking, and prodding to figure out what’s wrong.
Until today. (Cue the “ta-da!” music.)
Combining a small chunk of code with a free web site called Postbin gives you the ability to print messages to the terminal if you’re using the Corona Simulator, or “print” them to a web site if you’re on a device.
Watch this short video showing the process in action:
Here’s the code shown in the video (modified to include the missing line):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
--============================================================= -- use print() if we're in the simulator, otherwise -- post to a URL local isSimulator = "simulator" == system.getInfo("environment") function prnt(msg) -- do return end -- uncomment this for production build if isSimulator then print(msg) else local postURL = "http://www.postbin.org/foobar" local function networkListener( event ) -- nada because if not in sim won't ever see it end local params = {} params.body = msg network.request( postURL, "POST", networkListener, params) end end |
The site you need to get a URL from is http://postbin.org and the service is free.
Now when you want to print debug messages, use prnt(“your message”) and your string (or variables, etc.,) will show up in the Terminal when you’re using the Simulator, or on your postbin.org web page if you’re running your app on a device.
UPDATE: The service at postbin.org has been superceded by http://requestb.in/ — but the process shown in the video works the same, just points to a different URL.