Table of Contents Previous Section

Debugging

You use the debugging facilities provided in your development environment to debug your compiled code. However, debugging the scripted portions of your application requires a different approach.

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.

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 <WOWebScriptComponentController 0xafe04 
   message = You have been successfully registered. 
   newPerson = {
   address = "Graceland\015\nNashville, TN"; 
   email = "elvis@graceland.com"; 
   name = Elvis; 
}>

To see the output from logWithFormat: statements, you have to run your application from a command shell, as follows:

  1. Locate the application executable.
  2. If you don't have compiled code and haven't built a custom executable, use the DefaultApp program located in NextLibrary/WebObjects/Executables.

  3. Change directories to the directory in which the application executable is located.
  4. Start the application by invoking the executable as follows:
  5.  
    ApplicationExecutableRelativeApplicationDirectory
    

    You must provide a minimum of one argument to the executable: the application directory relative to <DocumentRoot>/WebObjects. For example, the resources for HelloWorld are located in <DocumentRoot>/WebObjects/Examples/HelloWorld, so HelloWorld's relative application directory is Examples/HelloWorld. You'd use the following command to start HelloWorld:

    DefaultApp Examples/HelloWorld
    

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

    Registration MyApplications/Registration
    

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

  6. In your browser, open the URL you'd normally use to launch your application:
  7. http://myHost/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.

    Trace Methods

    WebScript provides trace methods that log different kinds of information about your running application. 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 in either the awake or the willPrepareForRequest:inContext: method:

    - awake {
      [self traceAssignments:YES];
      [self traceScriptedMessages:YES];
    }
    

    Table of Contents Next Section