I created a Trace Handler that looks in a data area for a YES/NO flag. I actually have two handlers one for our VLF app and one for our mobile web app.
Here is the VLFTraceHandler. Its not VLF specific, its just how I keep the two application's trace files separate.
Code: Select all
* =================================================================================================================
*
* VLF SERVER SIDE TRACE HANDLER
*
* Enables trace output on server side objects.
* Tracing is toggled via the LANSACFG/TRACE data area
* Trace files are written to *TEMP_DIR, typically
* IBM i - /lansa_dcxpgmlib/x_lansa/tmp
* Windows - C:\Windows\Temp
* Trace files are named VLF-Trace.YYYYMMDD.text
* -----------------------------------------------------------------------------------------------------------------
* TO USE:
*
* Define this component in your object with application scope
* define_com class(#VLFServerTraceHandler) scope(*APPLICATION)
*
* Enable tracing by setting the LANSACFG/TRACE data area to 'YES'
function options(*DIRECT)
begin_com role(*EXTENDS #PRIM_OBJT *implements #PRIM_APP.iTraceHandler)
define field(#FileHandle) type(*dec) length(3) decimals(0)
define_com class(#PRIM_ALPH) name(#Tab)
evtroutine handling(#COM_OWNER.CreateInstance)
#Tab := (09).asChar
#COM_OWNER.InstallTracing
endroutine
mthroutine name(InstallTracing) help('Plug in the trace handler to the Application') access(*private)
if (*TRACE = YES)
#SYS_APPLN.TraceHandler <= #COM_OWNER
endif
endroutine
mthroutine name(Initialize) options(*redefine) access(*private)
#COM_OWNER.OpenTraceFile
endroutine
mthroutine name(Terminate) options(*redefine) access(*private)
#COM_OWNER.CloseTraceFile
endroutine
mthroutine name(TraceMessage) help('Executed whenever #SYS_APPLN.TraceMessageData or #SYS_APPLN.TraceMessageText is used') options(*redefine) access(*private)
#COM_OWNER.WriteToFile( #ComponentName #Description #LineNumber #MessageText )
endroutine
mthroutine name(OpenTraceFile) help('Create a new trace outputfile') access(*private)
use builtin(STM_FILE_OPEN) with_args(#COM_OWNER.GetNextFile Append N Y) to_get(#FileHandle #IO$STS)
endroutine
mthroutine name(WriteToFile) help('Write an entry in the trace output file') access(*private)
define_map for(*Input) class(#PRIM_ALPH) name(#ComponentName)
define_map for(*Input) class(#PRIM_ALPH) name(#Description)
define_map for(*Input) class(#PRIM_NMBR) name(#LineNumber)
define_map for(*Input) class(#PRIM_ALPH) name(#MessageText)
define_com class(#PRIM_DAT) name(#Now)
#MessageText := #Now.now.AsLocalizedDateTime.AsString + #Tab + #ComponentName + #Tab + #LineNumber.asstring + #Tab + #MessageText
use builtin(STM_FILE_WRITE) with_args(#FileHandle #MessageText) to_get(#IO$STS)
endroutine
mthroutine name(CloseTraceFile) access(*private)
use builtin(STM_FILE_CLOSE) with_args(#FileHandle) to_get(#IO$STS)
endroutine
mthroutine name(TracingState) options(*redefine)
#MessageTracingActive := True
endroutine
mthroutine name(GetNextFile) access(*Private)
define_map for(*Result) class(#PRIM_ALPH) name(#Result)
#Result := *TEMP_DIR + "VLF-Trace" + "." + *YYYYMMDD.AsString + ".txt"
endroutine
end_com
It's similar to the trace handler here
http://docs.lansa.com/14/en/lansa012/in ... 6_0025.htm.
I've modified how the trace files are written and in "InstallTracing" I use a custom System Variable "*TRACE".
The *TRACE just does a lookup to a data area LANSACFG/TRACE to determine if tracing should be enabled or not.
I find this easier to turn on and off on the IBM i than changing the file name as done in the example.
Once you have the handler defined, you drop it into your Server Module or other component
Code: Select all
define_com class(#VLFServerTraceHandler) scope(*APPLICATION)
Then all the SYS_APPLN_Trace*** messages get routed to your handler.
Its not by any means perfect and multiple requests to it will lock the file. I would love to find a way to write the trace file according to the user who is triggering it (i.e. the logged on user) but have not found a way to do such right now. That way I can have "Johns Trace File" or "Marys Trace File" etc.