Tips for debugging core code
#1
An integrated development environment (IDE) is a necessity for many programming efforts, but not simple to set up and manage. With the changes in PHP8 that generate warnings from older forum code, one solution is to simply ignore the warnings because the forum still works. This method does not improve the code. For a large project and a casual administrator, another solution is to identify the warnings, report them in Bugs and Issues, and hope it gets picked up and corrected by a MyBB team member. Code base gets incrementally improved. This is more acceptable.

Some problems are easily identifiable from the warnings. Others, like this one, are more complex and need some advanced techniques to understand the program logic. What started as a quirky fix needed more scrutiny over the last 3 days before being satisfied that the problem was solved. Without an IDE, I was ready to just give up. But that's not really my nature, so with guidance from Laird and lots of online searching, I found some tips that helped me trace the logic.

In the past I have used strategically placed code that shows values of variables in the browser window.
echo var_dump($fid);
or the better structured
echo "<pre>"; echo var_dump($fid); echo "</pre>";
will display the value of $fid if it has been set. If not, you could see a warning in your error.log.
Do not var_dump($mybb), it is very large!  Cool  Angel

You should already have your errors logged but not displayed or emailed. You should also consider restricting access to your log files by using your htaccess file. This code denies access to all except my local IP address (not mine, but made up as example). Allow first, then deny.
<FilesMatch "\.(log)$">
Order allow,deny
allow from 69.148.221.47
</FilesMatch>

Sometimes it is helpful to log items in a separate file for review later. And when I need to match an item to an event, a timestamp helps to locate it in the log.

In the case of troubleshooting the forum permissions problem (because I refused to give up!), I identified places in the code that I wanted to see if it was executed, and what certain values held at that time.

$dat=date('Y-m-d H:i:s T', TIME_NOW - 18000); file_put_contents(MYBB_ROOT.'admin/debug.log', var_export($dat, true)." line 497 start permissions  \n", FILE_APPEND); 

$dat is a variable to hold a formatted string as timestamp. Choose a variable name not used anywhere else. Use this crossreference (only shows current version).

date('Y-m-d H:i:s T', TIME_NOW - 18000) results in a timestamp like 2024-02-08 12:34:56 UTC -0500, and TIME_NOW - 18000 is 5 hours west of UTC which corresponds to my server time and which corresponds to my local time.

file_put_contents() writes a file named debug.log, and since I was working in admin, that's where I placed it, right next to admin/error.log, both readily accessible.

var_export($dat, true)." line 497 start permissions \n" places a timestamp and comment in the file. The period signifies to concatenate the 2 elements. The \n is the line break. Since code may execute or not, depending on conditions, it helps to identify the line number and what is taking place at that particular point.

FILE_APPEND adds to the existing file, or if it doesn't exist, creates the file for writing.

Sometimes, like when exporting an array that I know will be generated, I don't worry about the timestamp if it occurs after a series of conditions. But if I'm not sure what the conditions are that will generate the array, I use a combination of timestamped line numbers followed by the var_export.

file_put_contents(MYBB_ROOT.'admin/debug.log', "line 537 update_array = ".var_export($update_array, true)."  \n", FILE_APPEND); 	

In this case I included the line number of the code and the exported variable name concatenated to the actual export concatenated to the line break. I found that $ in the comment part sometimes confused the PHP compiler/interpreter and the string would not appear in the file.

I learned as a kid, "Ya gotta start somewhere." Sometimes, it's at the beginning. Big Grin
Maybe next time I will finally get an IDE in place.

What tips do you use for troubleshooting?
Reply
#2
The simplest way I use: echo '<!-- ', var_dump($whatiwant), ' -->';
And I have a look in the source of the page (ctrl+U)
Tchat en français
Do not ask me help through PM or Discord

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)