Using trace methods – Apple WebObjects 3.5 User Manual

Page 59

Advertising
background image

Debugging Techniques

59

In WebScript and Objective-C,

logWithFormat:

works like the

printf()

function in

C. This method takes a format string and a variable number of additional
arguments. For example, the following code excerpt prints the string “The
value of myString is Elvis”:

myString = @"Elvis";
[self logWithFormat:@"The value of myString is %@", myString];

When this code is parsed, the value of

myString

is substituted for the

conversion specification

%@

. The conversion character @ indicates that the

data type of the variable being substituted is an object (that is, of the

id

data

type).

Because in WebScript all variables are objects, the conversion specification
you use must always be

%@

. Unlike

printf()

, you can’t supply conversion

specifications for primitive C data types such as

%d

,

%s

,

%f

, and so on. (If you

do, you might see the address of the variable rather than its value.)

In Java, the equivalent of

logWithFormat:

is

logString

, and you can send it only to

WebApplication objects. Instead of using

printf

specifications, it uses

concatenation. Here’s how you’d write the same lines of code in Java:

myString = "Elvis";
application().logString("The value of myString is " + myString);

Perhaps the most effective debugging technique is to use

logWithFormat:

to

print the contents of

self

. This prints the values of all of your component

variables. For example, this statement at the end of the

sayHello

method in

HelloWorld’s

Main.wos

:

[self logWithFormat:@"The contents of self in sayHello are %@", self];

produces output that resembles the following:

The contents of self in sayHello are
<<WOScriptedClass(/WebObjects/Examples/WebScript/HelloWorld.woa/Main
.wo/Main): 0x8cb08 name=Main subcomponents=0x0> visitorName=frank>

Here’s how you’d write the same line of code in Java:

application().logString("The contents of this in sayHello are "

+ this.toString());

Using Trace Methods

WOApplication (in Java, WebApplication) provides trace methods that log
different kinds of information about your running application. These

Advertising