Can I get info about SUB inside SUB? - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10) +---- Thread: Can I get info about SUB inside SUB? (/showthread.php?tid=1666) Pages:
1
2
|
Can I get info about SUB inside SUB? - thesolarcode - 05-08-2023 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. RE: Can I get info about SUB inside SUB? - mnrvovrfc - 05-08-2023 Must run your code inside the QB64 IDE, and enable debug mode. ("Debug" menu has "Call Stack" and "Watch List" options.) Press [F7] or [F8] instead of [F5] to start your program inside the IDE. Aside from that, I don't know what to tell you because I have never used the IDE like that before. RE: Can I get info about SUB inside SUB? - bplus - 05-08-2023 If you can't figure out debug DIM Shared Debug as Long DIM Shared SubName$ in all your subs set that name SubName$ = ... in Sub (and maybe set to "main" or "" when exit sub?) have a clause If Debug Then Print "Sub is ";SubName$ When Debug is set to -1 then it prints If turn off, ie Debug = 0 then that clutter is removed. Notice you can also comment out 'If Debug Then Print "Sub is ";SubName$ to pin down just the subs you are interested in. Or Instead of print, try out MessageBox so dont mess up screen. Caveat - this does not work well in a fast loop! RE: Can I get info about SUB inside SUB? - madscijr - 05-08-2023 (05-08-2023, 04:37 PM)thesolarcode Wrote: Hi, What mnrvovrfc said is probably right, but I'll share my caveman method of tracing my code and errors, when such features are not readily available, in case it helps... I'm not saying this is the best method, but it has worked for me in the past. Here is the general pattern which should give you the gist. (You could use a global shared variable like bplus, I just opted to use local variables to preserve the state for nested calls, etc.) I should probably look at the built-in debugging features, it can't hurt. Code: (Select All) ' ///////////////////////////////////////////////////////////////////////////// RE: Can I get info about SUB inside SUB? - SMcNeill - 05-08-2023 Wouldn't the easiest way just be to assign a global variable for debug tracing? DIM SHARED ProgFlow$ SUB Foo ProgFlow$ = "Foo Start" .... sub stuff ProgFlow$ = "Foo Clean Exit" END SUB RE: Can I get info about SUB inside SUB? - mnrvovrfc - 05-08-2023 (05-08-2023, 07:18 PM)SMcNeill Wrote: Wouldn't the easiest way just be to assign a global variable for debug tracing? Go further and write a QB64 program that creates a copy of QB64 source code that does this. It wouldn't be very difficult, easier because QB64PE (yet) doesn't support nested subprogram definitions. Only detect "SUB" or "FUNCTION" header and place a line right after it. Then set a flag indicating the processor is inside a subprogram, so it doesn't get confused with "main module" level code. Then when "END SUB" or "END FUNCTION is encountered put a line before that saying the subprogram is about to exit. Also have to check "EXIT SUB" and "EXIT FUNCTION" for it. With "FUNCTION" get even fancier and even report what type it returns. "Entered function ''Zeroes'' which returns STRING." It takes more effort to use such an utility, that's why some people expected the IDE to be able to do it. RE: Can I get info about SUB inside SUB? - TerryRitchie - 05-08-2023 (05-08-2023, 07:18 PM)SMcNeill Wrote: Wouldn't the easiest way just be to assign a global variable for debug tracing? Pretty much what I do. Here is a snippet of code from a massive library I'm working on: Code: (Select All) '+------------------+ I have two global shared variables, __CURRENT_ROUTINE and __PREVIOUS_ROUTINE. I set __CURRENT_ROUTINE, perform my error checks and report any errors. If no errors exist I then set __PREVIOUS_ROUTINE so I have a trail from one routine to the next. __ERROR is used to report any errors. Code: (Select All) SUB __ERROR (Message AS STRING) ' __ERROR | RE: Can I get info about SUB inside SUB? - bplus - 05-09-2023 Yeah so who needs Debug of IDE? ;-)) DIYD = Do It Yourself Debug Way more fun because you programmed it yourself so you know how it's supposed to work, supposedly ;-)) RE: Can I get info about SUB inside SUB? - thesolarcode - 05-10-2023 Thanks for all the ideas. I wanted to do something like this: SUB ApiOne logging "start" ... logging "end" END SUB But without adding the sub names into all the code. Thinking about some simple preprocessor that could inject the sub names automatically. For example if we have: logging "$SUBNAME$: start" it would be replaced with: logging "ApiOne: start" Soooo, how hard is it to implement a simple preprocessor? RE: Can I get info about SUB inside SUB? - bplus - 05-10-2023 (05-10-2023, 03:16 PM)thesolarcode Wrote: Thanks for all the ideas. How about you make a template to copy/paste into a new sub and after you have a name copy/paste that into the template. You could also write some code to read your bas files and insert stuff right after Sub/Function definition and just before End Sub/Function but then you have to direct all Exit Subs... to the end so maybe a line label in template? Wait would a line label in a sub need a unique name from the entire program of just in the sub? Don't know... sounds like a fun little experiment... stay tuned. |