Managing log files

Definition

When batch procedures or complex tasks are managed, it is important to generate log files that will be displayed after the operation is finished. In Sage X3 version 6, a set of sub-programs exists, named OUVRE_TRACE and ECR_TRACE....

In version 7, a new set of functions are implemented in the supervisor layer. They create and fill the same format of a log file, but they are more appropriate to use when working with classes and methods..

The ALOG Class

To create a log file, you must instantiate an 'ALOG' class. After this instance exists, different methods will allow you to write log files. They are all called by Fmet:

ASETNAME(FILE_NAME)

Defines the log file name. If this method is not used, the name will be "F" followed by an incremental number defined by '[C]NUMIMP' current pointer value.

Use this method after the instantiation or after using the 'ABEGINLOG' method. By default, the name is assigned if a call to the 'AGETNAME' method is performed, or if a flush of the write buffer is triggered. The flush is automatically performed when the write buffer is full, but can also be triggered by the 'AFLUSHLOG' method.

This method returns 0 if successful. If the name was already assigned, the return will be equal to '[V]CST_AERROR'.

AGETNAME()

Returns the file name used for the log files and assigns it by default ID if the assignment was not already completed.

ABEGINLOG(TITLE)

Starts the log creation process and sets up the title of the log file. 'TITLE' is a character string parameter. Returns 0.

APPENDLOG(FILENAME)

Reuses a log file which name is given to append further lines in the log. If the file sent does not exist, this method creates a new log.

APUTLINE(TEXT,STAT)

Creates lines in the log and writes them in the log file based on buffering conditions. The parameters are the following:

AENDLOG()

Ends the log creation process, writes the log file on disk, and closes the file. Returns 0.

AFLUSHLOG()

Flushes the log file by writing the buffered log lines on disk. Make sure important lines are written if for any reason the process stops. It is not recommended to do it too frequently because it affects performance.

This method returns 0. The user parameter 'NBTRABUFF' defines the number of lines that are buffered before a periodic flush is performed.

APUTINSTERRS(CLASSINSTANCE)

'CLASSINSTANCE' is a class instance where errors are found. When errors are found during operations or methods of a class, the class business logic scripts must feed the error class associated with the class instance by using the ASETERROR method).

This method writes in the log file the detailed error messages found in the error class associated with the class instance.

APUTERRS(ERRCLASSINSTANCE)

'ERRCLASSINSTANCE' is an 'AERROR' error class instance. This method will write in the log file the content of the error class instance.

Example

# This program generates sales order# Let's declare an instance pointer of a class called C_SALESORDER# PARAMETERS is supposed to be an array of character strings used in the generation algorithm# LOGFILE_NAME is returnedSubprog SALES_ORDER_GENERATION(PARAMETERS,LOGFILE_NAME)Value Char PARAMETERS()(1..)Variable Char LOGFILE_NAME()Local Integer NBSUCCESS,NBFAILED# Instantiate a class to generate the logLocal Instance MYLOG Using C_ALOGMYLOG=NewInstance C_ALOG AllocGroup Null# Open the log fileOK=fmet MYLOG.ABEGINLOG("Sales order generation")LOGFILE_NAME=fmet MYLOG.AGETNAMEOK=fmet MYLOG.APUTLINE("Parameter list",OK)OK=fmet MYLOG.APUTLINE("--------------",OK)OK=fmet MYLOG.APUTLINE(PARAMETERS,OK)OK=fmet MYLOG.APUTLINE("--------------",OK)For I=1 to NB_OF_ORDERSLocal Instance MYOR Using C_SALESORDERMYOR=NewInstance C_SALESORDER AllocGroup Null# Let's fill the order propertiesMYOR.CUSTOMER=...MYOR.CREATION_DATE=date$MYOR.PAYMENT_CODE=...MYOR.AMOUNT=...# Let's fill the order linesFor J=1 to NBLINESMYORD.LINE(J)=NewInstance C_ORDERLINE AllocGroup MYORD...Next J# Let's create the orderOK=Fmet MYOR.AINSERTIf OK=[V]CST_AOKNBSUCCESS+=1# A log line explaining that the creation was successfulOK=fmet MYLOG.APUTLINE("Sales order generation #"+MYOR.ORDNUM+" succeeded",OK)ElseNBFAILED+=1# Error messageOK=fmet MYLOG.APUTLINE("Sales order generation failed",OK)OK=fmet MYLOG.APUTLINE("Error detail",OK)OK=fmet MYLOG.APUTLINE("------------",OK)# Detail of the errors returned by the AINSERT methodOK=fmet MYLOG.APUTINSTERRS(MYOR)OK=fmet MYLOG.APUTLINE("------------",OK)OK=fmet MYLOG.AFLUSHLOGEndif# Let's free the order instanceFreeGroup MYORNext I# Now the generation is endedOK=fmet MYLOG.APUTLINE(num$(NBSUCCESS)-"order(s) successfully created, "+num$(NBFAILED)-"order creation failed",OK)OK=fmet MYLOG.AENDLOG FreeGroup MYLOGIf func END_CONDITIONS()=[V]CST_ATRUEEndEndif# There is an additional piece of script to execute# Let's finally reuse the same log file# Instantiate again a class to generate the logMYLOG=NewInstance C_ALOG AllocGroup Null# Re-open the previous log file and append lines to itOK=fmet MYLOG.APPENDLOG(LOGFILE_NAME)Gosub CHECK_ORDERS : # This piece of code uses APUTLINE to append lines to the previous log# Finally, close everythingOK=fmet MYLOG.AENDLOG FreeGroup MYLOGEnd