Jump to content

Print debugging statement: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 281:
{{out}}
Example log:
<pre>[26/08/2020 18:56:51] [Line: 0051] File: ...\Debug\DebugApp.dpr
<pre>
2 + 7 = 9</pre>
[26/08/2020 18:56:51] [Line: 0051] File: ...\Debug\DebugApp.dpr
 
2 + 7 = 9
=={{header|FreeBASIC}}==
</pre>
{{works with|FreeBASIC|0.16.1+}}
Using intrinsic Definitions (macro value) set by the compiler
<syntaxhighlight lang="vb">#if __FB_DEBUG__ <> 0
#print Debug mode
Dim err_command_line As Ubyte
err_command_line = __fb_err__
Select Case err_command_line
Case 0
Print "No Error Checking enabled on the Command Line!"
Case 1
Print "Some Error Checking enabled on the Command Line!"
Case 3
Print "QBasic style Error Checking enabled on the Command Line!"
Case 7
Print "Extreme Error Checking enabled on the Command Line!"
Case Else
Print "Some Unknown Error level has been set!"
End Select
#else
#print Release mode
#endif
 
Sleep</syntaxhighlight>
 
=={{header|Go}}==
Go doesn't have a built-in print debugging statement as such. Nor does it have macros.
Line 353 ⟶ 377:
"q" at line 35 type '*main.point'
value: &main.point{x:2, y:3}
</pre>
 
=={{Header|Insitux}}==
 
Here's one method of debugging programs I like to demonstrate: mocking every built-in operation with a function that executes the operation and afterwards outputs its parameters and result.
 
<syntaxhighlight lang="insitux">
(for s (-> (symbols)
(filter about)
(remove ["print" "mock" "unmocked" "unmock" "do" "reset"]))
(mock s (fn (let result ((unmocked ...) (unmocked s) args))
(print "(" s " " ((unmocked join) " " args) ") => " result)
result)))
 
(function inside-2d? X Y areaX areaY areaW areaH
(and (<= areaX X (+ areaX areaW))
(<= areaY Y (+ areaY areaH))))
 
(inside-2d? 50 50 0 0 100 100)
</syntaxhighlight>
 
{{out}}
 
<pre>
(fast+ 0 100) => 100
(<= 0 50 100) => true
(fast+ 0 100) => 100
(<= 0 50 100) => true
true
</pre>
 
For obtaining line and column number, the special value <code>err-ctx</code> evaluates as its own source position.
 
<syntaxhighlight lang="insitux">
err-ctx
</syntaxhighlight>
 
{{out}}
 
<pre>
{:line 1, :col 1}
</pre>
 
Line 1,107 ⟶ 1,172:
 
What is generally done in practice is to hard-code some location indicator (such as the line number itself) at which a variable's value is being obtained. The Go example code then looks like this when translated to Wren.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
class Point {
Line 1,167 ⟶ 1,232:
"l" at line 34 type 'Num String Num'
value: 1 two 3
</pre>
<br>
{{libheader|Wren-debug}}
We can also rewrite this code using the above module which, whilst still quite basic, provides a more structured approach to debugging than the first version.
<syntaxhighlight lang="wren">import "./fmt" for Fmt
import "./debug" for Debug
 
class Point {
construct new(x, y) {
_x = x
_y = y
}
x { _x }
y { _y }
toString { "(%(_x), %(_y))" }
}
 
var add = Fn.new { |x, y|
var result = x + y
Debug.print("x|y|result|result+1", 16, x, y, result, result + 1)
return result
}
 
add.call(2, 7)
var b = true
var s = "Hello"
var p = Point.new(2, 3)
var l = [1, "two", 3]
Debug.nl
Debug.print("b|s|p|l", 25, b, s, p, l)</syntaxhighlight>
 
{{out}}
<pre>
EXPR on line 16 of type Int : x = 2
EXPR on line 16 of type Int : y = 7
EXPR on line 16 of type Int : result = 9
EXPR on line 16 of type Int : result+1 = 10
NL
EXPR on line 25 of type Bool : b = true
EXPR on line 25 of type String : s = Hello
EXPR on line 25 of type Point : p = (2, 3)
EXPR on line 25 of type List : l = [1, two, 3]
</pre>
 
9,476

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.