Script Debugging and Logging

KosmicTask Help

Script Debugging and Logging

Logging and Debugging

Scripts are generally debugged by the use of logging statements that validate the state of important variables at key points within the script. Most of the supported scripting languages support debugging in this way and the Write to log template details how to log a message in a particular language.

All output sent to a task's standard error stream is displayed in the task pane.

Simple debugging can be performed by returning log messages as all or part of a task's actual result. This may be acceptable in simple cases but less so when a complex result object is returned. Use of the standard error stream enables the task result and the logging output to be kept separate.

Most scripting languages can write directly to the standard error stream (commonly identified as stderr) and many include error functions or classes that output to stderr. Some languages, such as AppleScript, cannot write directly to a standard stream but in most cases a native logging command exists whose output can be intercepted by the task runner and directed to stderr. If a native logging command does not exist, as in JavaScript, then the task runner defines one.

The following examples illustrate how to write to the log:

try
    
    -- compute task result
    set myResult to "Hello, kosmos!"
    
    -- send value to log
    log "Goodbye, kosmos!"
    
    -- return result record
    return {kosmicData:myResult}
    
on error errorMessage number errorNumber
    
    return {kosmicError:errorMessage}
    
end try
# send result to stdout
echo "Hello, kosmos!"

# send log value to stderr
echo "Goodbye, kosmos!" >/dev/stderr
#include 

int main() 
{
    // send result to stdout
    printf("Hello, kosmos!");
    
    // send log value to stderr
    fprintf(stderr, "Goodbye, kosmos!");
    
    return 0;
}
class kosmicTask
{ 
    public static void main(String args[])
    {
        // send result to stdout
        System.out.println("Hello, kosmos!");
		
        // send log value to steer
        System.err.println("Goodbye, kosmos!");
    }
}
-- send result to stdout
print("Hello, kosmos!")

-- send log value to stderr
io.stderr:write("Goodbye, kosmos!")
import AppKit

def kosmictask():

    try:
    
        # send log value to stderr
        AppKit.NSLog("Goodbye, kosmos!")
    
        # return result
        return "Hello, kosmos!"
    except:
        return {'KosmicError':"Unexpected error"}