Optional output for debugging
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !
7 posts
• Page 1 of 1
Optional output for debugging
To ensure operations are executing as expected, I place several ⎕←'Value x is',x through a program. I would like to leave all of them in place to assist others in examining the operation of the code if they wish. So I would like to set up a variable 'debug' which is set to 1 to produce debug output and set to 0 to run without all the print statements.
I am sure others have implemented such conditional debug statements. How might have this been done?
- Code: Select all
debug←1 ⍝ (or 0) to turn it ON or OFF
<conditional on debug> ⎕←'Value x is',x
I am sure others have implemented such conditional debug statements. How might have this been done?
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
-
jmosk - Posts: 69
- Joined: Thu Jul 18, 2013 5:15 am
Re: Optional output for debugging
In the last project I started, I wrote a function Log which you can see below. It allows me to decide which type of log messages I actually want to see, depending on whether I am debugging or running the application:
Now, you can write:
And when your application becomes something serious, you can start writing the messages to a proper logging mechanism.
- Code: Select all
mode Log msg
⍝ Log modes: (set LOGMODES to select messages to output)
⍝ D: Debug
⍝ E: Error
⍝ W: Warning
⍝ T: Transmit on WebSocket
⍝ R: Receive on WebSocket
⍝ C: Connect or Disconnect
⍝ U: Unsupported feature
→(mode∊LOGMODES)↓0
⎕←((⊃'hh:mm:ss.ffff'(1200⌶)1 ⎕DT'J'),' ',mode,':') msg
Now, you can write:
- Code: Select all
LOGMODES←'D'
'D' Log 'Value x is',x
07:06:53.214 D: Value x is 42
And when your application becomes something serious, you can start writing the messages to a proper logging mechanism.
-
Morten|Dyalog - Posts: 460
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Optional output for debugging
Thank you, Morten. I couldn't figure out how to eliminate the output if I was not in debug mode and never thought to abort the log function early via the →0 to avoid the output statement completely. And not realizing that →⍬ is a NOP. So many APL tricks, so little time to learn them all.
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
-
jmosk - Posts: 69
- Joined: Thu Jul 18, 2013 5:15 am
Re: Optional output for debugging
The branch to zero is an old habit, an :If statement would be nicer...
-
Morten|Dyalog - Posts: 460
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Optional output for debugging
Yes, but you already taught me you can't do →0 in a dfns.
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
-
jmosk - Posts: 69
- Joined: Thu Jul 18, 2013 5:15 am
Re: Optional output for debugging
A fun way to do it in a dfn...
- Code: Select all
Note←{
⍝ ¨bool Note text¨
⍝ bool defaults to 1
⍝ - Use in guard of dfn (to left of colon)
⍺←1 ⋄ ⍺:0⊣⎕←⍵ ⋄ 0
}
MyFn←{
ok←⍵≡⍥,1
⎕←'Started MyFn'
ok Note'⍵ is 1': ⍝ Note displays if ok is 1
(~ok)Note'⍵ ERROR: ⍵ should be 1': ⍝ Note displays if ok is 0
⎕←'Ending MyFn'
}
MyFn 1
Started MyFn
⍵ is 1
Ending MyFn
MyFn 0
Started MyFn
⍵ ERROR: ⍵ should be 1
Ending MyFn
- petermsiegel
- Posts: 159
- Joined: Thu Nov 11, 2010 11:04 pm
Re: Optional output for debugging
If you use ⍞← instead of ⎕ then you can simply compress the output away:
If you do this often, then consider defining a helper function:
Note how I leave out ⍞← from Val so it can be used identically in dfns and tradfns. If you want to include it, then you have to use a technique from Roger Hui's classic "assert" function (and use a trailing ":" in a dfn as per petermsiegel):
x←42
debug←0
⍞←debug/'Value x is',x,⎕UCS 10
debug←1
⍞←debug/'Value x is',x,⎕UCS 10
If you do this often, then consider defining a helper function:
Val←{debug/'Value ',⍵,', is',(⍎⍵),⎕UCS 10}
debug←0
⍞←Val'x'
debug←1
⍞←Val'x'
Value x, is 42
Note how I leave out ⍞← from Val so it can be used identically in dfns and tradfns. If you want to include it, then you have to use a technique from Roger Hui's classic "assert" function (and use a trailing ":" in a dfn as per petermsiegel):
Val←{⍞←debug/'Value ',⍵,', is',(⍎⍵),⎕UCS 10 ⋄ _←0}
∇ foo x
[1] Val'x'
[2] ∇
debug←0
foo 42
debug←1
foo 42
Value x, is 42
goo←{
Val'x':
'done'
}
debug←0
foo 42
debug←1
foo 42
Value x, is 42
-
Adam|Dyalog - Posts: 143
- Joined: Thu Jun 25, 2015 1:13 pm
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: Google [Bot] and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group