tips
[Top] [All Lists]

REALbasic Tip: Using logs to debug your code

To: REALbasic-tips at lists dot realsoftware dot com
Subject: REALbasic Tip: Using logs to debug your code
From: Geoff Perlman <geoff at realsoftware dot com>
Date: Tue, 28 Sep 2004 14:49:35 -0500
Cc:
Delivered-to: REALbasic-tips at lists dot realsoftware dot com
The debugger really helps when your code is misbehaving. However, there are times when you need more than the snapshot of the current values of properties and variables that the debugger provides. This is where logging values can be handy. Rather than putting in a breakpoint and displaying the debugger, you can put the values you need to see in a log.

A log is simply a listing of values. It can be helpful because you can see values change over time. It's also useful because it can interfere less with your application. For example, if you're trying to debug code that runs in the Paint event of a canvas control, the debugger or a message box can cause the canvas to redraw itself which is not what you want when that's the very code you're trying to debug.

You can write this log to disk or simply display the results in a window. You can create your own log window easily. Just add a window and fill it with a scrollable editfield. Next add a method to the window that will append to the editfield any text you pass in. If you only want to see these values when you are running from REALbasic, consider adding a global method (via a module) that only calls your log window if you are running from REALbasic:

Sub Debug(message)
   #if DebugBuild then
     LogWindow.Append message
   #endif
End Sub

Your append method for the log window would look like this:

Sub Append(message as string)
   EditField1.SelStart = len(EditField1.text)
   EditField1.SelText = message + EndOfLine
End Sub

Note: EndOfLine was added in 5.2 to put the appropriate line ending based on the OS. If you are using REALbasic 4.5 or earlier, you can create your own EndOfLine function that returns a carriage return on the Mac and a carriage return and line feed on Windows.

If you are using REALbasic 5.5, you don't need to create a log window at all. To see your data, simply pass it to the DebugLog method of the System class. This will display your data in the Console on Mac OS X and in the Terminal on Linux. On Windows, there's a free utility that will display debug messages. It's called DebugView and it's available here:
<http://www.sysinternals.com/ntw2k/freeware/debugview.shtml>

Remember that calling System.DebugLog won't launch the app that will show the log values so make sure you launch it before running your app. To get this to work from Linux, you will have to launch the remote debugger from the Terminal. For those of you new to Linux, after launching the Terminal, use the "cd" command to switch to the directory the Remote Debugger Stub is stored in and then use "./RemoteDebuggerStub" to launch it.

If you are using REALbasic 4.5 or earlier, you could create your own Debug Message Viewer application that once a second reads a debug log file from a location your specify. Your log function could then simply write to that file.

The advantage of using the DebugLog method (or writing your own if you're using REALbasic 4.5 or earlier) is that it has no impact on your app since the messages appear in a complete separate application whose windows can stay in the background.
--
Geoff Perlman
President and CEO
REAL Software, Inc.

REAL World 2005 - The REALbasic User Conference
March 23rd - 25th, 2005, Austin, Texas
<http://www.realsoftware.com/realworld>

<Prev in Thread] Current Thread [Next in Thread>
  • REALbasic Tip: Using logs to debug your code, Geoff Perlman <=