Rebol Programming/Debugging techniques
Debugging techniques
[edit | edit source]There are various debugging tools available in Rebol.
Probe
[edit | edit source]Probe is a highly useful tool. What it does, is read the return values of functions and output them on the console. The return value is passed through probe, so you can insert it anywhere in your program.
>> wallet: $25 == $25.00 >> probe wallet $25.00 == $25.00
The value is both printed and returned as a value, because probe lets the return value pass through itself, as opposed to print, which doesn't return a useful value.
A more useful example:
>> add 3 probe add 2 2 4 == 7
NOTE: Be careful with probing large objects as they can take a very long time to print on the console. Large amounts of output can be stopped by pressing Escape.
Trace
[edit | edit source]Trace can help you to see function calls or TCP/IP network statistics on the console.
>> trace/func on
Trace/func returns a large amount of feedback, so it's wise only to activate it around the code you need to examine.
trace/func on ... some problematic code ... trace/func off
>> trace/net on
Protect-system
[edit | edit source]it is easy in Rebol to accidentally redefine a system word, as all words can be redefined. This code will cause strange things to happen a long way away from the original error:
gt: func [a b] [ return: a < b ]
That stray ":" on the return redefines return rather than returning the expected result. Any later code that uses return may misbehave.
An answer may be to protect all existing words from change:
>> protect-system
Adding print statements is a time-honored way of debugging code. Rebol has a neat way of letting you leave the statements in unactivated until needed
The trick is to define another word as a synonym of print, eg:
>> dprint: :print
And use that to add debugging print statements:
>> dprint ["my count is " my-count] >> dprint ["file info:" mold info? my-text-file.txt]
In production code, or when you want to turn the debugging statements off, simply add:
>> dprint: func [data] []
??
[edit | edit source]Rebol provides specific functions to help with debugging. The function ?? prints a variable name followed by its molded value. Unlike the print function, it also returns the value it receives.
>> test: "Hello" == "Hello" >> value: ?? test test: "Hello" == "Hello" >> ?? value value: "Hello" == "Hello"
Once you have finished debugging, you can either remove all the ?? from the source, or just redefine ?? so that it just passes the value as though it were no longer present.
??: func [val] [val]
Adding a debugging option to GUI programs
[edit | edit source]While a VID program is running, you can't use the console to type print or ?? or any other debugging functions. One way round that is to add a console input field to your VID program, and execute whatever is typed into that:
view layout [ button "Add" [field1/text: 1 + to-integer field1/text show field1] label "number" field1: field 100 "1" label "console:" console: field 100 [attempt [do console/text]] ]
Try typing help view into the field labeled console.
Quickly restarting a GUI program
[edit | edit source]You can shave off a tiny bit of debugging time, by adding a simple button to your program that quits the program and starts it again.
unview/all do %<programname>
Stopping and continuing programs
[edit | edit source]Stopping a program to the console can be done with Escape key.
Continuing GUI programs can be done with do-events command.