Can I get info about SUB inside SUB?
#11
Sorry for the wait, needed lunch.

Hey this works just fine!
Code: (Select All)
f = f1
f = f2
GoTo endF
endF:
End

Function f1
    Print "F1"
    GoTo endF
    endF:
End Function
Function f2
    Print "F2"
    GoTo endF
    endF:
End Function
b = b + ...
Reply
#12
So,

Your template might look like this for any function start:
Code: (Select All)
Function f1
    If debug Then Print "Entering f1"
    GoTo endF
    endF:
    If debug Then Print "Exiting f1"
End Function

plug in with copy/paste and modify names, for example:
Code: (Select All)
Dim Shared As Long debug
Print "Hello from main code section."
debug = 0 ' test with 0 and 1 or -1
f = f1
f = f2
GoTo endF
endF:
Print "The end"
End

Function f1
    If debug Then Print "Entering f1"
    GoTo endF
    endF:
    If debug Then Print "Exiting f1"
End Function
Function f2
    If debug Then Print "Entering f2"
    GoTo endF
    endF:
    If debug Then Print "Exiting f2"
End Function

or use one of the other DIYD suggestions.
b = b + ...
Reply
#13
(05-08-2023, 04:37 PM)thesolarcode Wrote: Hi,

if code is running inside a SUB, can I somehow get the SUB name?
This would be great to have for debugging or logging.

Here's link to a discussion on this same subject with a wild solution/hack by RhoSigma:

is there a way to get the name of a function to call from a variable or data? (1/26/2022)
Reply
#14
Can't be done because:

Code: (Select All)
Sub MySub(V)
  Print "This sub is"; NameOfSub$
End Sub
Reply
#15
Like I mentioned before, the simple way I'd do this is just like so:

Code: (Select All)
$Let DEBUG = TRUE

a = 1
foo a, b, c
Print a, b, c




Sub foo (x, y, z)
    $If DEBUG Then
        Print "========================"
        Print "Init Sub Foo..."
        Print "Init value x = "; x
        Print "Init value y = "; y
        Print "Init Value z = "; z
        Print "========================"
    $End If
    x = x + 1
    y = x * 2
    z = y - x
    $If DEBUG Then
        Print "========================"
        Print "Exit Sub Foo..."
        Print "Exit value x = "; x
        Print "Exit value y = "; y
        Print "Exit Value z = "; z
        Print "========================"
    $End If
End Sub


Now you can use the precompiler to set if you want to see debug/trace info or not, and you can specify exactly what pieces of information is relevant for you to track and report on with your program as it's running.  If you want to get fancy, add some COLOR statements to the $IF block so the debug info stands out a little more relevant for you, if you want.  

If you're wanting custom debugging or logging, then it's generally best to just write the simple debug and log code yourself.  Otherwise, exactly what the heck do you expect the program to automatically log for you?  Passed variables?  Internal variables?  Variable values at multiple positions inside the routine?  How about array?  Do you just need a single value or should it update the whole array when logging?  Or mem blocks?  image handles?   Font handles?  How the heck do you expect the program to log *EVERYTHING* inside a complex sub/function?   Isn't it just much better to write the code yourself for the specific pieces of information you need, rather than expecting everything to automatically be there at your fingertips to spew out every possible value just when you type "Debug = True" somewhere?
Reply
#16
Hi
1. I remember that someone uses to print on the console to have debug output on another window aside that of the running program.
I sometimes used this way.
2. in More cases I used to print before and after the block of code that doesn't work as aspected, and this way fixes the 80-90% of bugs.
After print  the variables values, the program stops its flow for a SLEEP or a _DELAY.
3. I rarely print variables variations into a file for analizing it  in a second time.

and if I think that there is a logic bug, I allow the flow of code to drive me to the bug.

PS:
Option _Explicit & Option _ExplicitArray  save so much time! I often mistype the name of variables! I more less declare a wrong data type among MAIN, SUB & FUNCTION parameters, in the SUB & FUNCTION with SHARED. (i.e.  integer vs single vs long)

My two cents

PPS:
Knowing in what SUB/FUNCTION we are give us poor informations...I suggest you to follow the flow on the paper or using the Debug F7/F8 to execute step by step the code.
Reply




Users browsing this thread: 1 Guest(s)