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 PB Code Profiler
sample in the PowerBuilder Code Samples at
https://www.appeon.com/developers/library/code-samples-for-pb. 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 2 3 |
TimerKind itk_kind string is_title = 'Trace Options ' string is_starttext |
The open event for the window sets some defaults:
1 2 3 4 5 |
application lapp_current lapp_current = getapplication() itk_kind = Clock! is_starttext = cb_startstop.text 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
// instance variables: // errorreturn le_errorreturn integer li_key // Check that the button label is Start Trace // and a trace file name has been entered if this.text = is_starttext then if len(trim(sle_filename.text)) = 0 then messagebox(parent.title, & 'Trace file name is required',information!) sle_filename.setfocus() return end if // If Prompt for overwrite is checked and the // file exists, pop up a response window if cbx_prompt.checked and & fileexists(sle_filename.text) then li_key = messagebox(parent.title, & 'OK to overwrite '+sle_filename.text, & question!,okcancel!,1) if li_key = 2 then return end if // Open the trace file TraceOpen( sle_filename.text, itk_kind ) // Enable tracing for checked activities // For each activity, check for errors and close // the trace file if an error occurs if cbx_embeddedSQL.checked then le_errorreturn = TraceEnableActivity( ActESQL! ) if le_errorreturn <> Success! then of_errmsg(le_errorreturn, & 'TraceEnableActivity( ActESQL! )') le_errorreturn = Traceclose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if return end if end if if cbx_routineentry.checked then le_errorreturn =TraceEnableActivity(ActRoutine!) if le_errorreturn <> Success! then of_errmsg(le_errorreturn, & 'TraceEnableActivity( ActRoutine! )') Traceclose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if return end if end if if cbx_userdefined.checked then le_errorreturn = TraceEnableActivity( ActUser! ) if le_errorreturn <> Success! then of_errmsg(le_errorreturn, & 'TraceEnableActivity( ActUser! )') Traceclose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if return end if end if if cbx_systemerrors.checked then le_errorreturn = TraceEnableActivity(ActError! ) if le_errorreturn <> Success! then of_errmsg(le_errorreturn, & 'TraceEnableActivity( ActError! )') Traceclose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if return end if end if if cbx_routineline.checked then le_errorreturn = TraceEnableActivity( ActLine! ) if le_errorreturn <> Success! then of_errmsg(le_errorreturn, & ' TraceEnableActivity( ActLine! )') Traceclose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if return end if end if if cbx_objectcreate.checked then le_errorreturn = & TraceEnableActivity( ActObjectCreate! ) if le_errorreturn <> Success! then of_errmsg(le_errorreturn, & 'TraceEnableActivity( ActObject! )') Traceclose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if return end if le_errorreturn = & TraceEnableActivity( ActObjectDestroy! ) if le_errorreturn <> Success! then of_errmsg(le_errorreturn, & 'TraceEnableActivity(ActObjectDestroy!)') Traceclose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if return end if end if if cbx_garbagecoll.checked then le_errorreturn = & TraceEnableActivity( ActGarbageCollect! ) if le_errorreturn <> Success! then of_errmsg(le_errorreturn, & 'TraceEnableActivity(ActGarbageCollect! )') Traceclose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if return end if end if // Start tracing le_errorreturn =TraceBegin( sle_tracelabel.text ) if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceBegin') return end if // Change the title of the window and the // text of the Start Trace button parent.title = is_title + '(Tracing)' this.text = 'Stop &Tracing' // If the button label is Stop Trace, stop tracing // and close the trace file else le_errorreturn =TraceEnd() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceEnd') return end if le_errorreturn =TraceClose() if le_errorreturn <> Success! then of_errmsg(le_errorreturn,'TraceClose') end if this.text = is_starttext parent.title = is_title end if |
of_errmsg function
The window uses two functions to handle error messages. The
of_errmsg function displays a message box:
1 2 3 |
// of_errmsg Messagebox( this.title,'Error executing '+ as_msg + & '. Error code : '+ of_converterror(ae_error) ) |
of_converterror function
The of_converterror function converts the ErrorReturn parameter
to a string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// of_converterror: convert enumerated type // ErrorReturn parameter to text. String ls_result choose case a_error Case Success! ls_result = "Success!" Case FileCloseError! ls_result = "FileCloseError!" Case FileOpenError! ls_result = "FileOpenError!" Case FileReadError! ls_result = "FileReadError!" Case FileWriteError! ls_result = "FileWriteError!" Case FileNotOpenError! ls_result = "FileNotOpenError!" Case FileAlreadyOpenError! ls_result = "FileAlreadyOpenError!" Case FileInvalidFormatError! ls_result = "FileInvalidFormatError!" Case FileNotSetError! ls_result = "FileNotSetError!" Case EventNotExistError! ls_result = "EventNotExistError!" Case EventWrongPrototypeError! ls_result = "EventWrongPrototypeError!" Case ModelNotExistsError! ls_result = "ModelNotExistsError!" Case ModelExistsError! ls_result = "ModelExistsError!" Case TraceStartedError! ls_result = "TraceStartedError!" Case TraceNotStartedError! ls_result = "TraceNotStartedError!" Case TraceNoMoreNodes! ls_result = "TraceNoMoreNodes!" Case TraceGeneralError! ls_result = "TraceGeneralError!" Case FeatureNotSupportedError! ls_result = "FeatureNotSupportedError!" Case else ls_result = "Unknown Error Code" end choose return ls_result |