Make print() style logging on device console (xCode - organizer) realtime!

9 replies [Last post]
OderWat
User offline. Last seen 28 weeks 1 day ago. Offline
Joined: 4 Jun 2010

I think I just found the solution for "real-time" logging while the code is running on the device!

It is very simple... and should be documented somewhere!?

Place this at the begin of your main.lua file!

1
io.output():setvbuf('no') -- disable output buffering for console in device!

After this you open the organizer window in xCode!

It will show your print() output in the console window of the device!

I believe this is not widely known nor documented. If it is I apologize for my ignorance!

Don't forget to remove you "print()" style debugging/logging code before you release your app :)

Replies

myfogview
User offline. Last seen 2 weeks 5 days ago. Offline
Joined: 14 Apr 2010

That's a great find. Good job. I tried it and it works well.

The solution is kinda documented in the Corona SDK API PDF on page 33. It describes "file:setvbuf(mode [,size])." In this case "file:" would be "io:" and defaults to the "standard" I/O device -- which turns out to be the Console Output window.

I never would have thought of the Xcode Organizer Console Output as the standard I/O for the iPhone, but it does make sense. In other systems I've worked on the local terminal was used as a debugging device for inputting commands and displaying debug information.

It looks like the default mode is "full" for full buffering. That's why I saw the output some times when my program was running and when it was stopped.

I don't think it's wise to run in this mode all the time because I'm sure it would affect performance. You could turn it on if you need some real-time help and you may be able to use the file:flush() call to force the output.

This is another good tool to keep in your back pocket.

Thanks again.
Tom

MikeHart
User offline. Last seen 3 hours 37 min ago. Offline
Joined: 22 Mar 2010

great hint,

thanks for sharing.

myfogview
User offline. Last seen 2 weeks 5 days ago. Offline
Joined: 14 Apr 2010

Here is my solution to quickly disable "print()" output in your final release code.
The best solution is to remove all print() statements but as long as you are not using them in time critical areas I think this solution should work.

1
2
3
4
5
6
7
8
9
10
local ReleaseBuild = true  -- set to "true" to disable "print()" console output
 
-------------------------------------
-- Conditional "print()"
-- Use "printNow()" to always print
local printNow = print
if ReleaseBuild then
        print = function(val) end
end
-------------------------------------

OderWat
User offline. Last seen 28 weeks 1 day ago. Offline
Joined: 4 Jun 2010

@Fogview

Nice one! :)

You can omit the 'val' though

I have

1
-- print = function () end

In the top and hope I won't forget to comment it in on release :)

myfogview
User offline. Last seen 2 weeks 5 days ago. Offline
Joined: 14 Apr 2010

I guess my code is overkill but I was thinking I could use ReleaseBuild in other places to disable any debug code not related to print().

Tom

OderWat
User offline. Last seen 28 weeks 1 day ago. Offline
Joined: 4 Jun 2010

Yeah.. I am just picking on you ... hehe :)

Well I will have something like this in my app too.. because for playtesting I need quick access for later levels and won't build in back doors for this as you can't trust any beta tester to give something like that away later.

Stuff like that would make me wish that we have real conditional code at compile time. So stuff is not compiled in at all... the build process does know if it is a build for distribution possibly because you use different keyfiles.

OderWat
User offline. Last seen 28 weeks 1 day ago. Offline
Joined: 4 Jun 2010

I found that with iOS 4 and the new xCode I had to try multiple times to get any console output at all. Wonder if that happens for others too!

dnhkng
User offline. Last seen 3 days 12 hours ago. Offline
Joined: 16 Mar 2010

Does this work for the iPad? I'n not seeing anything in the Organizer console window when I use "io.output():setvbuf('no')" with the print command.

-Dave

OderWat
User offline. Last seen 28 weeks 1 day ago. Offline
Joined: 4 Jun 2010

The Organizer Window of xCode is pretty unreliable for some reason. Noticed that since last xCode / SDK Update.

I am using this: http://www.apple.com/support/iphone/enterprise/

Which works like a charm!

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.