Using a window
You can create a window that is similar to the Profiling tab
on the System Options dialog box and add it to any application that
is under development, so that you can start and stop tracing when
testing specific actions.
The w_starttrace window is available
in the PowerBuilder Profiler sample in the Profiler section of the PowerBuilder Samples and Utilities page
. This sample
also shows the code used to create the profiling tools described
in “Analyzing trace information
using profiling tools”.
The w_starttrace window lets
you specify a trace file name, label, and timer kind, as well as
which activities you want to trace:

The following instance variables are defined for the window:
|
1 |
TimerKind itk_kind<br>string is_title = 'Trace Options '<br>string is_starttext |
The open event for the window sets some defaults:
|
1 |
application lapp_current<br>lapp_current = getapplication()<br>itk_kind = Clock!<br>is_starttext = cb_startstop.text<br>sle_filename.text = classname(lapp_current)+'.pbp' |
The following code shows the script for the Clicked event
of the Start Trace button. The text for the button is set to Start
Trace in the painter. When the user clicks Start Trace, the button
label changes to Stop Trace. The Clicked event script checks the
text on the button before either starting or stopping tracing. This
script uses the functions described in “Collecting trace information
using PowerScript functions”:
|
1 |
// instance variables:<br>// errorreturn le_errorreturn<br>integer li_key<br> <br>// Check that the button label is Start Trace<br>// and a trace file name has been entered<br>if this.text = is_starttext then<br> <br>   if len(trim(sle_filename.text)) = 0 then<br>       messagebox(parent.title, &<br>         'Trace file name is required',information!)<br>       sle_filename.setfocus()<br>       return<br>   end if<br> <br>   // If Prompt for overwrite is checked and the<br>   // file exists, pop up a response window<br>   if cbx_prompt.checked and &<br>      fileexists(sle_filename.text) then<br>      li_key = messagebox(parent.title, &<br>         'OK to overwrite '+sle_filename.text, &<br>         question!,okcancel!,1)<br>      if li_key = 2 then return<br>   end if<br>      <br>   // Open the trace file<br>   TraceOpen( sle_filename.text, itk_kind )<br>   <br>   // Enable tracing for checked activities<br>   // For each activity, check for errors and close<br>   // the trace file if an error occurs<br>   if cbx_embeddedsql.checked then <br>      le_errorreturn = TraceEnableActivity( ActESql! )<br>      if le_errorreturn <> Success! then<br>         of_errmsg(le_errorreturn, &<br>            'TraceEnableActivity( ActESql! )')<br>          le_errorreturn = Traceclose()<br>         if le_errorreturn <> Success! then<br>            of_errmsg(le_errorreturn,'TraceClose')<br>         end if<br>         return<br>      end if<br>   end if<br> <br>   if cbx_routineentry.checked then <br>      le_errorreturn =TraceEnableActivity(ActRoutine!)<br>      if le_errorreturn <> Success! then<br>         of_errmsg(le_errorreturn, &<br>            'TraceEnableActivity( ActRoutine! )')<br>         Traceclose()<br>         if le_errorreturn <> Success! then<br>            of_errmsg(le_errorreturn,'TraceClose')<br>         end if<br>         return<br>      end if<br>   end if<br> <br>   if cbx_userdefined.checked then <br>      le_errorreturn = TraceEnableActivity( ActUser! )<br>      if le_errorreturn <> Success! then<br>         of_errmsg(le_errorreturn, &<br>            'TraceEnableActivity( ActUser! )')<br>         Traceclose()<br>         if le_errorreturn <> Success! then<br>            of_errmsg(le_errorreturn,'TraceClose')<br>         end if<br>         return<br>      end if<br>   end if<br> <br>   if cbx_systemerrors.checked then <br>      le_errorreturn = TraceEnableActivity(ActError! )<br>      if le_errorreturn <> Success! then<br>         of_errmsg(le_errorreturn, &<br>            'TraceEnableActivity( ActError! )')<br>         Traceclose()<br>         if le_errorreturn <> Success! then<br>            of_errmsg(le_errorreturn,'TraceClose')<br>         end if<br>         return<br>      end if<br>   end if<br> <br>   if cbx_routineline.checked then <br>      le_errorreturn = TraceEnableActivity( ActLine! )<br>      if le_errorreturn <> Success! then<br>         of_errmsg(le_errorreturn, &<br>            ' TraceEnableActivity( ActLine! )')<br>         Traceclose()<br>         if le_errorreturn <> Success! then<br>            of_errmsg(le_errorreturn,'TraceClose')<br>         end if<br>         return<br>      end if<br>   end if<br>   if cbx_objectcreate.checked then <br>      le_errorreturn = &<br>         TraceEnableActivity( ActObjectCreate! )<br>      if le_errorreturn <> Success! then<br>         of_errmsg(le_errorreturn, &<br>            'TraceEnableActivity( ActObject! )')<br>         Traceclose()<br>         if le_errorreturn <> Success! then<br>            of_errmsg(le_errorreturn,'TraceClose')<br>         end if<br>         return<br>      end if<br>      le_errorreturn = &<br>         TraceEnableActivity( ActObjectDestroy! )<br>      if le_errorreturn <> Success! then<br>         of_errmsg(le_errorreturn, &<br>           'TraceEnableActivity(ActObjectDestroy!)')<br>         Traceclose()<br>         if le_errorreturn <> Success! then<br>            of_errmsg(le_errorreturn,'TraceClose')<br>         end if<br>         return<br>      end if<br>   end if<br> <br>   if cbx_garbagecoll.checked then <br>      le_errorreturn = &<br>         TraceEnableActivity( ActGarbageCollect! )<br>      if le_errorreturn <> Success! then<br>         of_errmsg(le_errorreturn, &<br>          'TraceEnableActivity(ActGarbageCollect! )')<br>         Traceclose()<br>         if le_errorreturn <> Success! then<br>            of_errmsg(le_errorreturn,'TraceClose')<br>         end if<br>         return<br>      end if<br>   end if<br> <br>   // Start tracing<br>   le_errorreturn =TraceBegin( sle_tracelabel.text )<br>   if le_errorreturn <> Success! then<br>      of_errmsg(le_errorreturn,'TraceBegin')<br>      return<br>   end if<br>   <br>   // Change the title of the window and the <br>   // text of the Start Trace button<br>   parent.title = is_title + '(Tracing)'<br>   this.text = 'Stop &Tracing'<br> <br>// If the button label is Stop Trace, stop tracing<br>// and close the trace file<br>else<br>   le_errorreturn =TraceEnd()<br>   if le_errorreturn <> Success! then<br>      of_errmsg(le_errorreturn,'TraceEnd')<br>      return<br>   end if<br> <br>   le_errorreturn =TraceClose()<br>   if le_errorreturn <> Success! then<br>      of_errmsg(le_errorreturn,'TraceClose')<br>   end if<br>   this.text = is_starttext<br>   parent.title = is_title <br>end if |
of_errmsg function
The window uses two functions to handle error messages. The of_errmsg function
displays a message box:
|
1 |
// of_errmsg<br>Messagebox( this.title,'Error executing '+ as_msg + &<br>   '. Error code : '+ of_converterror(ae_error) ) |
of_converterror function
The of_converterror function converts
the ErrorReturn parameter to a string:
|
1 |
// of_converterror: convert enumerated type <br>// ErrorReturn parameter to text.<br>String ls_result<br>choose case a_error<br>   Case Success!<br>      ls_result = "Success!"<br>   Case FileCloseError!<br>      ls_result = "FileCloseError!"<br>   Case FileOpenError!<br>      ls_result = "FileOpenError!"<br>   Case FileReadError!<br>      ls_result = "FileReadError!"<br>   Case FileWriteError!<br>      ls_result = "FileWriteError!"<br>   Case FileNotOpenError!<br>      ls_result = "FileNotOpenError!"<br>   Case FileAlreadyOpenError!<br>      ls_result = "FileAlreadyOpenError!"<br>   Case FileInvalidFormatError!<br>      ls_result = "FileInvalidFormatError!"<br>   Case FileNotSetError!<br>      ls_result = "FileNotSetError!"<br>   Case EventNotExistError!<br>      ls_result = "EventNotExistError!"<br>   Case EventWrongPrototypeError!<br>      ls_result = "EventWrongPrototypeError!"<br>   Case ModelNotExistsError!<br>      ls_result = "ModelNotExistsError!"<br>   Case ModelExistsError!<br>      ls_result = "ModelExistsError!"<br>   Case TraceStartedError!<br>      ls_result = "TraceStartedError!"<br>   Case TraceNotStartedError!<br>      ls_result = "TraceNotStartedError!"<br>   Case TraceNoMoreNodes!<br>      ls_result = "TraceNoMoreNodes!"<br>   Case TraceGeneralError!<br>      ls_result = "TraceGeneralError!"<br>   Case FeatureNotSupportedError!<br>      ls_result = "FeatureNotSupportedError!"<br>   Case else<br>      ls_result = "Unknown Error Code"<br>end choose<br>return ls_result |