Debugging in Mac OS X with REALbasic by [an error occurred while processing this directive]
This week we'll take a look at a quick and easy hack to help you debug Mac OS X projects that you create with REALbasic. Not a lot of glitz this time, but it is an essential step in your quest for REALbasic "Excellence". (Good tie-in, eh?) Merlin lovers will have to wait another week for the finished project. (I'm still working on it).
Build the Interface
Interface? We don't need no stinkin' interface! Ok, maybe we need a small one. Launch REALbasic, open Window1 and add a PushButton and an EditField control to the window. You're done!
Add the Code
Choose File-New Module. A new module appears in your Project window. A module is global in nature, which is just a fancy way to say that all parts of your REALbasic project can use the calls within it. Once we finish this module, you can drag it from your project and use it in other projects too.
Open the new module and add a method to it (Edit-New Method). Name the Method something you'll remember. This debugging method is going to send data to the Console application, so name it SendToConsole and add msg as string in the parameters field of the Method definition. Press OK.
To this new method add the following code:
dim i as integerThis code gives you access to the Mac API call: "DebugStr". Any text that you give to DebugStr will show up in the Console application. What? You haven't played with the Console yet? Now's the time! It's located here: /Applications/Utilities/Console.app. The Console application is just a GUI front end for the "old-timey" console, where Unix geeks and programmers like to send text for viewing. For RB programmers, it's like adding your own window and editfield without doing a thing. As you test your program, you can follow along with its progression, by littering your code with statements like:
// place this in the PushButton's Action event from earlierAs you program executes, these various text messages will appear in the Console application. It's low-tech, but it gives you another way to debug your applications. If you want to be really slick and do it like "the pros" do it, add a new Boolean constant to the module by selecting Edit-New Constant.

Then, change the code from earlier to read like this: dim i as integer
if debugging then
#if TargetCarbon then
Declare Sub DebugStr Lib "CarbonLib" (msg as Pstring) as Integer
i = DebugStr(msg)
#endif
end if
This way you can sprinkle debugging code all over the place and turn it off by redefining the constant as FALSE. This is fun in OS X, but what about Classic? The DebugStr call works with the Classic Mac OS too, but instead of sending your text to the console (which doesn't exist in OS 9), the string gets sent to the debugger (i.e. Macsbug). Since Macsbug is another topic altogether we'll forego the discussion this time. If I hear of enough interest, I'll gladly talk about it in a future tutorial. It's not terribly scary to use, but it is quite a bit different than the console.
Conclusion
No glitz this week, but you will have lots of glory if it helps you track down a nasty bug. You can download this week's project and example application, in case you don't want to create the project by hand. Have fun and see you next week when we'll hopefully wrap up our Merlin project.