HOWTO: Use Remote Logging To Monitor Your App On The Simulator Or Device

Using the following code you can send logging information “prints” to small task that can then echo them to the screen or to a file or whatever you want.

Start by adding adding a new logging function in your app like so:

do  
 local socket = require "socket"  
 local udp = socket.udp()  
  
 function log(text)  
 udp:sendto(text .. "\n", "127.0.0.1", 9999)  
 end  
end  

You can add it to your main.lua and call it from anywhere in your app like so:

log("some text I want to log")  

you could also map all your existing calls to print to this log function by adding

print = log  

Now, you need something to listen for your log messages. You could write it in any language you want.

My example uses Lua:

local socket = require("socket")  
  
local host = "127.0.0.1"  
local port = 9999  
  
print("Binding to host '" ..host.. "' and port " ..port.. "...")  
  
local udp = assert(socket.udp())  
assert(udp:setsockname(host, port))  
assert(udp:settimeout(60))  
  
while true do  
 dgram, ip, port = udp:receivefrom()  
 if dgram then  
 print(tostring(ip) .. ":" .. dgram)  
 else  
 print(ip)  
 end  
end  

EDIT: I liked chinway’s idea of using netcat, so instead of the above little program you can instead use the command:

nc -ul -p 9999 [import]uid: 846 topic_id: 11057 reply_id: 311057[/import]

I implemented something similar using netcat…

what are advantages of UDP over TCP in this case?

http://developer.anscamobile.com/code/print-function-device-small-dirty-hack [import]uid: 48521 topic_id: 11057 reply_id: 40227[/import]

UDP is just less complicated to implement, and for this purpose (debugging) it makes using it on a real device less complex too since you dont have to manage reconnects as the network goes up and down.

Also, you can multicast or broadcast for easier setup of your remote log client.

Oh yes, and UDP sends are non-blocking, so they are fast. [import]uid: 846 topic_id: 11057 reply_id: 40228[/import]