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";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).
[self debugWithFormat:@"The value of myString is %@", myString];
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";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:
application().debugString("The value of myString is " + myString);
[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 "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:
+ this.toString());
% defaults write MyApplication WODebuggingEnabled NOWhen 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