PeopleCode | Using GetFile to create a custom log file
A. The Goldilocks Point
Although PeopleCode comes delivered with a number of options to perform a trace, such as ‘SetTracePC’ and ‘debug’ mode in App Designer, sometimes the results you get back from the trace are a case of ‘too much information’ or ‘too little information’. As developers, it’s often hard to find the elusive ‘Goldilocks’ point, where the trace file returns just the right amount of detail.
B. GetFile and WriteLine
One way to retain a better sense of control over trace information is to create your own temporary trace file by using the ‘GetFile’ and ‘WriteLine’ commands. Here is a simple example:
/* Write trace details to new file */ &traceFile = GetFile("trace_file.txt", "w", "a", %FilePath_Relative); &traceFile.WriteLine("Application Number: " | &applicationNumber); &traceFile.WriteLine("Student Number: " | &emplid); &traceFile.Close();
The ‘GetFile’ function accepts four parameters:
- File Name (with full directory path if specifying ‘%FilePath_Absolute’)
- File Mode (such as ‘w’ for write, ‘a’ for append and ‘r’ for read)
- Character Set (such as ‘a’ for ANSI or ‘u’ for UNICODE)
- Path Type: %FilePath_Relative or %FilePath_Absolute (refer to Tip 011 for more details)
‘WriteLine’ is self-explanatory, outputting a line of text and then ending with a carriage return. (If you prefer not to include a carriage return, use the ‘WriteString’ function instead.)
Returning to the example above, this will create a new file with a name of ‘trace_file.txt’. If a file of this name already exists, the current file will be overwritten with the new file. If you prefer to retain the contents of the existing file, then change the second parameter of ‘GetFile’ to ‘a’ for append or ‘u’ for update (append will add new content at the end of the file; update will add new content at the start):
/* Append to existing file */ &file = GetFile("trace_file.txt", "a", "a", %FilePath_Relative);
Once you’re done with the troubleshooting effort, remember to comment out the trace code. Other developers will appreciate the head start you’ve given them when it comes to tracing future issues.
See Also:
Tip 008: Create Simple CSV Export
Tip 011: Determine Current Working Directory
Tip 045: Introduction to Tracing
Tip 046: File Manipulation in PeopleCode