In model tracing

In this article, we’ll show some model code that explicitly writes to a trace. This trace is a:

  1. File in the log folder, when the application is started via AIMMS Developer.

  2. Part of the session log, when the application is published on AIMMS PRO on premise

  3. File copied to AIMMS PRO storage, when the application is published on AIMMS Cloud.

Running example

The running example is based on a transportation problem: Pellets of bottled water are to be shipped from bottling locations to distribution centers. This example is extended with some logging and can be downloaded here

Calling the tracing procedure

The tracing procedure pr_logMsg just has one scalar input argument: sp_msg. As this is a scalar input argument, you can enter any string valued expression.

pr_logMsg( sp_fullMsg );
! This procedure adds the line sp_fullMsg to the output stream.

Implementation of the tracing procedure

 1Procedure pr_logMsg {
 2    Arguments: (sp_msg);
 3    Body: {
 4        if pro::GetPROEndPoint() then
 5            pro::management::LocalLogInfo( sp_msg );
 6        else
 7            if not p_noLogLinesWritten then
 8                if fileExists( sp_traceFilename ) then
 9                    FileDelete( sp_traceFilename );
10                endif ;
11            endif ;
12            put f_traceFile ;
13            put sp_msg, / ;
14            putclose ;
15            p_noLogLinesWritten += 1 ;
16        endif ;
17    }
18    StringParameter sp_msg {
19        Property: Input;
20    }
21}

Remarks:

  1. Line 4,5: Use the AIMMS PRO session log, if the application is published on AIMMS PRO.

  2. When the application is running via AIMMS Developer, a trace file is used. The trace file is declared as follows. The merge mode indicates that this file is appended to when it already exists.

    1File f_traceFile {
    2    Name: sp_traceFilename;
    3    Device: Disk;
    4    Mode: merge;
    5}
    
  3. Line 7-11: The trace file is deleted first, when it is about to be written to for the first time.

  4. Line 13: Actually adding content to the trace file.

  5. Line 14: Closing the trace file after every line written; in case of a crash, the trace file still contains the latest contents.

How to integrate

The code explained and demoed above is contained in a separate section named: Logging of download offered above. See Export code to another project for copying the code to your project.