Getting Started with Tcl in FME: Startup / Shutdown Script

Previous: Scripted Parameter

Startup Tcl Script Example: Log the translation beginning time
# "timeStamp" procedure
# This procedure gets the current system time,
# and returns formatted date time string (time stamp).
proc timeStamp {} {
  clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S"

# Save the translation beginning time stamp.
set begTime [timeStamp]

# Show and log the translation beginning time.
FME_LogMessage fme_inform "Translation started at $begTime."
Tcl Commands > clock

In the Startup Script (and the TclCaller), FME pre-defined procedure FME_LogMessage can be used to show a message on the Log window and log it into the log file.

Shutdown Tcl Script Example: Log the translation beginning / ending time
# Save beginning and ending time stamp,
# using global variable and procedure defined in the Startup Script.
set begEndTimeStamp "Beg: $begTime\nEnd: [timeStamp]"

# Show the time stamp on the Log window.
puts $begEndTimeStamp

# Append the time stamp to the log file.
set file [open $FME_LogFileName a]
puts $file $begEndTimeStamp
close $file
Tcl Commands > puts, open, close

Global variables and procedures defined in the Startup Script can be used in the Shutdown Script. But those cannot be used in the TclCaller. This is a different from Python script.
Be aware that FME_LogMessage is invalid in the Shutdown Script because the log file will be closed before Shutdown process.
In the Shutdown Script, use "puts" command to show a message on the Log window; to append a message to the log file, it's necessary to re-open the log file with appending mode and write the message into it.
FME_LogFileName is a global variable defined by FME, it holds the log file path.

In fact, FME prepares global variables named FME_StartingTimeStamp and FME_EndingTimeStamp to hold time stamps, so the first line of the example can be replaced with this line:
set begEndTimeStamp "Beg: $FME_StartingTimeStamp\nEnd: $FME_EndingTimeStamp"

Here is the information about global variables defined by FME:
> FMEpedia: Startup and Shutdown Python and Tcl Scripts
And more examples:
> FMEpedia: Startup and Shutdown TCL Script Examples

Next: List Attribute Manipulation in TclCaller

No comments:

Post a Comment