Table of Contents Previous Section

Debugging WebScript

WebScript provides methods that are useful for debugging: logWithFormat: and several trace methods. Using these methods in conjunction with launching your application from a command shell provides you with a fairly complete picture of your running application.

Launching From the Command Line

To debug your application, you should launch it from the command line so that you have better control over the executable and so that you'll be able to see messages written to standard output or standard error.

To start a WebObjects application from the command line:

  1. Locate the application executable.

    If you don't have compiled code and haven't built a custom executable, use the WODefaultApp executable located in NeXT_Root/NextLibrary/Executables.

  2. Change directories to the directory in which the application executable is located.

  3. Start the application by invoking the executable as follows:
    	ApplicationExecutable -d DocumentRoot RelativeApplicationDirectory
    

    You must provide a minimum of two arguments to the executable: the HTTP server's document root and the application directory relative to <DocumentRoot>/WebObjects. For example, the resources for HelloWorld are located in <DocumentRoot>/WebObjects/Examples/HelloWorld.woa, so HelloWorld's relative application directory is Examples/HelloWorld. (You must leave off the .woa extension.) You'd use the following command to start HelloWorld:

    	WODefaultApp.exe -d c:/netscape/ns-home/docs Examples/HelloWorld
    

    To start a compiled application, you'd use the command:

    	AppName.exe -d DocumentRoot RelativeApplicationDirectory
    

    For example, if you wanted to run an application named Registration.woa, you would change directories to the Registration.woa directory and then type:

    	Registration.exe -d c:/netscape/ns-home/docs 
    		MyApplications/Registration
    

    assuming you've placed Registration in a directory called MyApplications.

    Note: If you're using Windows NT, be sure to use forward slashes in the arguments to the application executable, even if you're running the application from the DOS Command Prompt.

  4. In your browser, open the URL you'd normally use to launch your application:
    	http://localhost/cgi-bin/WebObjects/MyApplications/Registration
    
As your application runs, the output from logWithFormat: and other information about your application is displayed in the command shell window.

logWithFormat:

The WebScript method logWithFormat: writes a formatted string to stderr. Like the printf() function in C, this method takes a format string and optionally, 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 WebScript only supports the data type id, 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.

Perhaps the most effective debugging technique you can use in WebScript is to use logWithFormat: to print the contents of self. This causes WebScript to output the values of all of your variables. For example, putting the statement:

[self logWithFormat:@"The contents of self in register are %@", self];
at the end of the register method in the Registration application's Main.wos script produces output that resembles the following:

The contents of self in register are <WOComponent 0xafe04 
	 message = You have been successfully registered. 
	 newPerson = {
   address = "Graceland\015\nNashville, TN"; 
   email = "elvis@graceland.com"; 
   name = Elvis; 
}>
Note: If you're writing Java code, the logWithFormat: method is named logString and you can send it only to WebApplication objects. Instead of using printf() conversion specifications, it uses concatenation. Here's how you'd write the same line of code in Java:

this.application().logString("The contents of this in register are " 
		+ this.toString());

Trace Methods

WOApplication provides trace methods that log different kinds of information about your running application. These methods are useful if you want to see the call stack. The trace methods are described in the following table:

Method Description
- trace

Enables all tracing.

- traceAssignments

Logs information about all assignment statements.

- traceStatements

Logs information about all statements.

- traceScriptedMessages

Logs information when an application enters and exits a scripted method.

- traceObjectiveCMessages

Logs information about all Objective-C method invocations.

To use any of the trace methods, you must run your application from a command shell.

You use the trace methods wherever you want to turn on tracing. Usually, this is in the init method of a component or the application:

- init {
	[super init];
	[self.application traceAssignments:YES];
	[self.application traceScriptedMessages:YES];
	return self;
}
Note: The trace methods are not available in the Java interface.

Table of Contents Next Section