除錯追蹤器

這個選單讓您可以在程式碼執行的當下檢視變數的狀態。無論是要修好一段不再運作的腳本,或只是在除錯階段,都非常有用。

image.png

若要使用除錯功能,您必須在程式碼中放入一道 debug 指令,語法如下:debug(parameter : text);當程式碼執行到這道 debug 指令時,除錯追蹤器(Debug Tracer)檢視中就會產生一筆項目,以參數的內容為標題,下方則是一份結構,列出當下所有可用的變數及其目前的值。

注意:您可以把任何特別關心的變數串進文字字串中,就能在除錯追蹤器視窗裡立刻看到它的值——但要確認型別是 text!(例如 debug("Debug point 1 : " + Variable1); )

仔細觀察除錯視窗就會發現,每執行一次 debug 指令,就會新增一行。

這一行包含除錯點的名稱,也就是傳給 debug 函式的那段文字,例如:debug("my debug point")。

在行首還會顯示距離上一行除錯訊息所經過的時間。這有助於評估程式碼的效能(見上文)。第一行的值一律是「start」。

這行除錯訊息本身包含若干變數行,數量等於 debug 指令之前腳本中所宣告的變數個數。每一行變數都包含名稱、型別、值,以及宣告該變數處的程式碼片段。

debug.svg

在迴圈中使用

把 debug 放進迴圈裡,就能看到迴圈每一次疊代時變數的狀態,也能看到每一次疊代所花的時間。在下面的例子中,間隔小於或等於 1 毫秒。

let a := select Customer
let d := for i in a do
           debug("for each customer");
           i.'first name' + " " + i.'last name';
         end;
debug("all customers");
d;

Enregistrement de l’écran 2024-12-29 à 12.32.33.gif

但如果加入一道執行成本高昂的指令,例如「select」,就會看到每次疊代之間的時間明顯拉長。

let a := select Customer
let d := for i in a do
           let e := select Invoice where Customer = i;
           debug("for each customer");
           i.'first name' + " " + i.'last name' + " " + sum(e.TOTAL);
         end;
debug("all customers");
d;

image.png

在一段腳本的前後各放一道 debug 指令,就能測量該段腳本的總執行時間:

debug("start of script");
let a := select Customer
let d := for i in a do
           i.'first name' + " " + i.'last name';
         end;
debug("end of script");
d;

image.png