Optional output for debugging

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
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 !

Optional output for debugging

Postby jmosk on Sun Mar 03, 2024 3:19 am

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.

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
+
User avatar
jmosk
 
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Re: Optional output for debugging

Postby Morten|Dyalog on Sun Mar 03, 2024 6:12 am

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:

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.
User avatar
Morten|Dyalog
 
Posts: 460
Joined: Tue Sep 09, 2008 3:52 pm

Re: Optional output for debugging

Postby jmosk on Sun Mar 03, 2024 3:27 pm

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
+
User avatar
jmosk
 
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Re: Optional output for debugging

Postby Morten|Dyalog on Sun Mar 03, 2024 3:33 pm

The branch to zero is an old habit, an :If statement would be nicer...
User avatar
Morten|Dyalog
 
Posts: 460
Joined: Tue Sep 09, 2008 3:52 pm

Re: Optional output for debugging

Postby jmosk on Sun Mar 03, 2024 3:37 pm

Yes, but you already taught me you can't do →0 in a dfns.
+←--------------------------------------------------------------→
+ Jay Moskowitz
+←--------------------------------------------------------------→
+ http://www.linkedin.com/in/jay-moskowitz-5745b83
+
User avatar
jmosk
 
Posts: 69
Joined: Thu Jul 18, 2013 5:15 am

Re: Optional output for debugging

Postby petermsiegel on Sun Mar 03, 2024 4:22 pm

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
Exercise left to reader.
petermsiegel
 
Posts: 159
Joined: Thu Nov 11, 2010 11:04 pm

Re: Optional output for debugging

Postby Adam|Dyalog on Mon Mar 11, 2024 1:06 pm

If you use ⍞← instead of ⎕ then you can simply compress the output away:
      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
User avatar
Adam|Dyalog
 
Posts: 143
Joined: Thu Jun 25, 2015 1:13 pm


Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest