Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > WebObjects Developer's Guide


Table of Contents Previous Section

Writing Debug Messages

The method debugWithFormat: (in Java, debugString) writes a formatted string to standard error (stderr).

In WebScript and Objective-C, debugWithFormat: 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 debugWithFormat:@"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 debugWithFormat: is debugString. You can send it to WOApplication, WODirectAction, and WOComponent objects. Instead of using printf-style specifications, debugWithFormat: uses concatenation to construct the string. For example, here's how you'd write the same lines of code in Java:

myString = "Elvis";
application().debugString("The value of myString is " + myString);
Perhaps the most effective debugging technique is to use debugWithFormat: 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 debugWithFormat:@"The contents of self in sayHello are %@", 
self];
produces output that resembles the following:

The contents of self in sayHello are <<Main: 0x8cb08 name=Main 
subcomponents=0x0> visitorName=frank>
Here's how you'd write the same line of code in Java:

application().debugString("The contents of this in sayHello are "
+ this.toString());
All objects that respond to the debugWithFormat: message also respond to the logWithFormat: (or logString) message. It's recommended to use debugWithFormat: for debugging messages because you can turn off the output of these messages with the WODebuggingEnabled user default. By default, WODebuggingEnabled is set to YES so all debugWithFormat: messages print to stderr while you are in development mode. When you are ready to deploy the application, you can disable the debugging messages with this command:

% defaults write MyApplication WODebuggingEnabled NO
When WODebuggingEnabled is NO, debugging statements are not printed. Thus, you should use logWithFormat: messages for information that you want to print to stderr whether or not you are in development mode.

Note: Although setting WODebuggingEnabled to NO prevents debugging statements from being printed, debugWithFormat: imposes a small performance penalty each time it is encountered. Once a component has stabilized, you'll want to strip out debugging statements.

Table of Contents Next Section