Back to Contents


The Application class

Summary:

See also: Classes and Objects


Syntax

The Application class is a built-in class providing an interface to the application internals.

Syntax:

base.Application

Notes:

  1. This class does not have to be instantiated; it provides class methods for the current program.

Methods:

Class Methods
Name Description
getArgumentCount()
  RETURNING INTEGER
Returns the number of arguments passed to the program.
getArgument( position INTEGER )
  RETURNING STRING
Returns the argument passed to the program, according to its position.
getProgramName()
  RETURNING STRING
Returns the name of the program.
getProgramDir()
  RETURNING STRING
Returns the system-dependent path of the directory where the program files are located.
getFglDir()
  RETURNING STRING
Returns the system-dependent path of the installation directory of the runtime system (FGLDIR environment variable).
getResourceEntry( name STRING )
  RETURNING STRING
Returns the value of an FGLPROFILE entry.
getStackTrace()
  RETURNING STRING
Returns the current stack trace of the program flow.

Usage:

The Application class groups a set of utility functions related to the program environment. Command line arguments, execution directory and FGLPROFILE resource entries are some of the elements you can query with this class.

Command line arguments

You can query command line arguments with the getArgumentCount() and getArgument() methods. The getArgumentCount() method returns the total number of arguments passed to the program, while getArgument() returns the argument value for the given position.

The first program argument is identified by the position 1. Argument number zero is the program name. If the argument position is negative or greater than getArgumentCount(), the method returns NULL.

01 MAIN
02    DEFINE i INTEGER
03    FOR i=1 TO base.Application.getArgumentCount()
04       DISPLAY base.Application.getArgument(i)
05    END FOR
06 END MAIN

Program information

Basic program execution information can be queried with the getProgramName() and getProgramDir() methods. The getProgramName() method returns the name of the program. The getProgramDir() method returns the directory path where the 42r program file is located. Note that the directory path is system-dependent.

Runtime information

Product information can be queried with the getFglDir() method. The getFglDir() method returns the installation directory path defined by FGLDIR. Note that the directory path is system-dependent.

FGLPROFILE resource

If needed you can query FGLPROFILE resource entries with the getResourceEntry() method. This method returns the fglprofile value of the entry passed as parameter.

01 MAIN
02   DISPLAY base.Application.getResourceEntry("mycompany.params.logmode")
03 END MAIN

Printing the stack trace

In some situations - typically, to identify problems on a production site - you may want to known what functions have been called when a program raises an error. You can get and print the stack trace in a log file by using the getStackTrace() method. This method returns a string containing a formatted list of the current function stack.

You typically use this function in a WHENEVER ERROR CALL handler, as in the following code example:

01 MAIN
02    WHENEVER ERROR CALL my_handler
03    ...
04 END MAIN
05 ...
06 FUNCTION my_handler()
07    DISPLAY base.Application.getStackTrace()
08 END FUNCTION

Example of stack trace output:

#0 my_handler() at debug.4gl:173
#1 save_customer_data() at customer.4gl:1534
#2 edit_customer() at customer.4gl:542
#3 main at main.4gl:23