7.2 Example Trace Handler

This is an example of a self-contained Trace Handler object. It checks for the existence of a text file called TracingOn.txt and if found will install itself at run time and a trace file will be created. Any trace statements coded into the application will be output to this trace file.

Copy and paste the source into a new RDMLX reusable part. Save and compile.

Function Options(*DIRECT) 
Begin_Com Role(*EXTENDS #PRIM_OBJT *implements #prim_app.iTraceHandler) 
Define Field(#FilHandle) 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) * This example uses a text file as the trigger to implement user tracing. 
* Alternately you might consider using a registry entry
If (#Com_owner.FileExists( (*Sys_dir  "TracingOn.txt") )) 
* Set this object as the system help handler 
#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(#FilHandle #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(#FilHandle #MessageText) To_Get(#io$sts) 
Endroutine 
Mthroutine Name(CloseTraceFile) Access(*private) 
Use Builtin(Stm_File_Close) With_Args(#FilHandle) 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) 
Define_Com Class(#prim_nmbr) Name(#Extension) 
Begin_Loop Using(#Extension) 
#Result := *Sys_dir  "UserTrace"  "."  #Extension.asstring.rightAdjust( 3 "0" ) 
Leave If(*Not #Com_owner.FileExists( #Result )) 
End_Loop 
Endroutine 
Mthroutine Name(FileExists) Access(*private) 
Define_Map For(*input) Class(#prim_alph) Name(#Path) 
Define_Map For(*result) Class(#prim_boln) Name(#Result) 
Use Builtin(OV_FILE_SERVICE) With_Args(Check_File #Path) To_Get(#io$sts) 
#Result := (#io$sts = OK) 
Endroutine
End_Com