Product Site

15.4.3.3. Exploiting TraceObject's setMakeString Method

This example extends the previous one and demonstrates how to take advantage of the capabilities the TraceObject class offers, in this case its class method setMakeString:

  • The program defines a method named "myMakeString" before any class directive which makes it a floating method. ooRexx will process all directives in the setup phase and if floating methods are encountered ooRexx will create a StringTable to store them under their names which can be accessed using the environment symbol .methods.
  • The floating method "myMakeString" uses some of the entries of the TraceObject instance supplied as its sole argument to create a trace string.
  • In the main program (prolog) the TraceObject class method setMakeString is used to assign the "myMakeString" method retrieved from the floating .methods StringTable.
Example 15.4. Exploiting TraceObject's setMakeString Method for Customizing the Trace Format
-- fetch & assign floating method
.TraceObject~setMakeString(.methods~myMakeString)
do 3
  .test~new~show  -- create instance & send it the "show" message
end

   -- method defined before class, hence a floating method put into
   -- this program's (package) .methods StringTable by ooRexx
::method myMakeString   -- using entries NR, THREAD, TIMESTAMP, TRACELINE
  use arg traceObj      -- fetch traceObject argument to format
  return "  |" right(traceObj~nr,3) -
         "-" traceObj~timeStamp~timeOfDay~makeString~left(12)"|" -
         traceObj~traceLine~insert(traceObj~thread,9)

::class test

::method init     class -- class constructor
  expose counter        -- access class attribute "counter"
  counter=0             -- initialize to 0
::method nextNr   class -- class method
  expose counter        -- access class attribute "counter"
  counter+=1            -- increase counter
  return counter        -- return new value

::method init           -- instance constructor
  expose nr             -- access instance attribute "nr"
  nr=self~class~nextNr  -- get & assign next serial number
::method show           -- instance method
  expose nr             -- access instance attribute "nr"
  trace results         -- trace all statements
  reply                 -- return & execute remainder on new thread
  sleepTime=random(1,750)/1000   -- get random sleep time
  trace off             -- turn off tracing
  call sysSleep sleepTime  -- sleep
  say self": I got created as:" pp("#" nr)   -- show infos for this instance

::routine pp      -- "pretty print": enquotes argument in square brackets
  trace results   -- trace statement and its result
  return "[" || arg(1) || "]"

Running the above program may yield an output like:
  |   1 - 16:43:12.378|     31 *-1* reply                 -- return & execute remainder on new thread
  |   2 - 16:43:12.378|     31 *-1* reply                 -- return & execute remainder on new thread
  |   3 - 16:43:12.378|     31 *-1* reply                 -- return & execute remainder on new thread
  |   4 - 16:43:12.378|        >I2> Method "SHOW" with scope "TEST" in package "G:\test_myMakeString.rex".
  |   5 - 16:43:12.378|     32 *-2* sleepTime=random(1,750)/1000   -- get random sleep time
  |   6 - 16:43:12.378|        >>2>   "0.457"
  |   7 - 16:43:12.378|        >I3> Method "SHOW" with scope "TEST" in package "G:\test_myMakeString.rex".
  |   8 - 16:43:12.378|        >I4> Method "SHOW" with scope "TEST" in package "G:\test_myMakeString.rex".
  |   9 - 16:43:12.378|     33 *-2* trace off             -- turn off tracing
  |  10 - 16:43:12.378|     32 *-3* sleepTime=random(1,750)/1000   -- get random sleep time
  |  11 - 16:43:12.378|     32 *-4* sleepTime=random(1,750)/1000   -- get random sleep time
  |  12 - 16:43:12.378|        >>3>   "0.103"
  |  13 - 16:43:12.378|        >>4>   "0.109"
  |  14 - 16:43:12.378|     33 *-3* trace off             -- turn off tracing
  |  15 - 16:43:12.378|     33 *-4* trace off             -- turn off tracing
  |  16 - 16:43:12.494|     39 *-3* return "[" || arg(1) || "]"
  |  17 - 16:43:12.494|        >>3>   "[# 2]"
a TEST: I got created as: [# 2]
  |  18 - 16:43:12.494|     39 *-4* return "[" || arg(1) || "]"
  |  19 - 16:43:12.494|        >>4>   "[# 3]"
a TEST: I got created as: [# 3]
  |  20 - 16:43:12.842|     39 *-2* return "[" || arg(1) || "]"
  |  21 - 16:43:12.842|        >>2>   "[# 1]"
a TEST: I got created as: [# 1]