Jump to content

Tracy Debugger


adrian

Recommended Posts

However, it does open the compiled file in cache instead of the original ones.

Do you have any ideas why?

Hey guys,

I am not sure what is going on. To make this work I have made a small tweak to the Tracy core (hopefully only temporary as I have asked them about a way to publicly modify the filepath) - you can see it here: https://github.com/adrianbj/TracyDebugger/blob/master/tracy/src/Tracy/Dumper.php#L111

Would you mind taking a look at that and debugging why it might not be working - just find out what $file contains before and after that str_replace

I can imagine on Windows I probably need to replace all the "/" with DIRECTORY_SEPARATOR but it sounds like @szabesz is on a Mac so what I have should work?

Thanks to both of you for helping out with this.

  • Like 1
Link to comment
Share on other sites

$file contains an empty string, but even before this line:

list($file, $line, $code) = $loc ? self::findLocation() : NULL;

I cannot seem to find any occurrence of $file before this line, so I suppose that is why it is empty in the first place.

  • Like 1
Link to comment
Share on other sites

list() populates the $file variable from the result of findLocation(), or NULL if $loc returns false. 

I am not sure what's going on, but if your edit links are to the compiled file, then it would suggest that $file is being populated. Could you maybe check the value of $loc and self::findLocation() before that list() call please?

  • Like 1
Link to comment
Share on other sites

$loc is 0

self::findLocation() :

Array
(
 [0] => .../site/assets/cache/FileCompiler/site/modules/TracyDebugger/ProcesswireInfoPanel.inc
 [1] => 162
 [2] => Dumper::toHtml(\ProcessWire\wire('page'), array(Dumper::COLLAPSE => false))
)

EDIT: and similar values, since toHtml() gets called a lot of times...

Edited by szabesz
  • Like 1
Link to comment
Share on other sites

Is $loc always 0 ? If so, then that's the problem.

I get it 0 sometimes - when the first result of findLocation() is a module file. But when it's a template file, $loc returns 7

At least from this:

[0] => .../site/assets/cache/FileCompiler/site/modules/TracyDebugger/ProcesswireInfoPanel.inc

I can see that the path in $file is matching something that the next line can remove the "/site/assets/cache/FileCompiler" component to make it the path to the original file.

Can you please clarify that $loc is always 0? What if you force it to 7 on line 100?

Thanks again for your help with this!

  • Like 1
Link to comment
Share on other sites

@matjazp: I tested with php 5.6, 5.5 and 5.4 and did not make a difference.

$loc is always 0. I put $loc = 7; on line 100 and I still get the compiled file. But this time:
Before the Tracy hack str_replace $file is:
.../site/assets/cache/FileCompiler/site/modules/TracyDebugger/ProcesswireInfoPanel.inc

after modifying the string we get:
.../site/modules/TracyDebugger/ProcesswireInfoPanel.inc

The above is 7 times, and we have "VariablesPanel.inc" 2 times at the end.

So "/site/assets/cache/FileCompiler" is removed as expected, but how is this path related to the my template file? Do not bother explaining it though, I was just wondering a bit :)

  • Like 1
Link to comment
Share on other sites

Thanks!

I am wondering what links are actually showing the compiled file? Can you show a screenshot of the link you are trying to click on?

Here are some examples showing it working as expected:

Link from a dump()

post-985-0-86435700-1457470273_thumb.png

Link from a barDump()

post-985-0-63403500-1457470274_thumb.png

I am starting to think you are talking about other links, but I am not sure what!

  • Like 1
Link to comment
Share on other sites

Oh, I see - I have been running in StrictMode, so haven't been noticing that - there is another location that I need to add that hack to change the path. I'll get that fix committed shortly.

@tpr - is this the case for you also? Do the links from dump() and barDump() calls get converted properly?

  • Like 1
Link to comment
Share on other sites

I have just tested and dump() and barDump() calls are ok. However, I have another question: Tracy\Debugger::dump(); and the others used to work but now I get:  Class 'ProcessWire\Tracy\Debugger' not found and Class 'ProcessWire\TD' not found   so only the shortcut methods work (ProcessWire 3.0.10 and the template file is in namespace ProcessWire; ).

Link to comment
Share on other sites

I have just tested and dump() and barDump() calls are ok. However, I have another question: Tracy\Debugger::dump(); and the others used to work but now I get:  Class 'ProcessWire\Tracy\Debugger' not found and Class 'ProcessWire\TD' not found   so only the shortcut methods work (ProcessWire 3.0.10 and the template file is in namespace ProcessWire; ).

Great - thanks for confirming that the paths are correct for dump() and barDump()

The class not found error will be because of the template being the ProcessWire namespace. If you are doing that, then you'll need prepend a "\" to your calls, eg:

\Tracy\Debugger::dump('Test \Tracy\Debugger::dump');
\TD::dump('Test \TD::dump');

and then they'll work fine.

  • Like 1
Link to comment
Share on other sites

Ok, the latest version now correctly modifies the editor path to the file in the errors panel, as well as dump() and barDump().

Thanks again @szabesz for helping so much to figure this out - sorry we got so sidetracked along the way :)

  • Like 3
Link to comment
Share on other sites

@matjazp: I tested with php 5.6, 5.5 and 5.4 and did not make a difference.

In PHP 5, list() assigns the values starting with the right-most parameter. In PHP 7, list() starts with the left-most parameter. But I see that adrian fixed it...

  • Like 1
Link to comment
Share on other sites

I'm playing with Editor Protocol Handler set to "editor://open/?file=%file&line=%line" using UltraEdit. The problem I have is that editor won't open correct file if %line is empty string. So 

editor://open/?file=myfile.txt&line=1

is ok, while 

editor://open/?file=myfile.txt&line=

is not ok. This is bug in UltraEdit. Can you help by providing line number 1, if there is no line number?

  • Like 1
Link to comment
Share on other sites

In PHP 5, list() assigns the values starting with the right-most parameter. In PHP 7, list() starts with the left-most parameter. But I see that adrian fixed it...

@matjazp - thanks for the info on the changes to list() in PHP 7 - I hadn't seen that yet, although from reading about it, I don't think it actually affects that code. 

This is bug in UltraEdit. Can you help by providing line number 1, if there is no line number?

No problem at all - I assume the only places where this is affecting you are the links to the template file from the PW Info panel (both in the template info section and the icon at the bottom)? Are there any other locations with links without line numbers that I haven't thought of?

Finally got the protocol handler thing to work on Win(8), thanks!

Awesome - care to share any tips for setting it up? Did you work with this file: https://github.com/adrianbj/TracyDebugger/blob/master/tracy/tools/open-in-editor/open-editor.js

Thanks!

Link to comment
Share on other sites

Here's how I set up the editor protocol handler on Windows 8.

I started out using this but needed to modify a few things.

Note that I'm using a portable version of PhpStorm (using my own yaP launcher), so installed versions may need some modifications.

Module settings, "Editor protocol handler" field:

pstorm://open/?url=file://%file&line=%line

There's "editor.reg" file with these contents:

REGEDIT4

[HKEY_CLASSES_ROOT\pstorm]
@="URL:pstorm Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\pstorm\shell\open\command]
@="wscript \"D:\\portables\\PhpStorm\\run-editor.js\" \"%1\""

and also its counter-part, which removes registry key, "editor-unreg.reg":

REGEDIT4

[-HKEY_CLASSES_ROOT\pstorm]

The "editor.reg" needs to be merged to the registry - double click on it, or using regedit.exe and "Import" from the menu.

Note that you need to do this in admin mode, otherwise registry settings won't be added.

"editor-unreg" is only needed to clean the registry, possibly you won't ever need this.

Then you'll need a file named "run-editor.js" in the directory you've set in "editor.reg"

There's some hardcoded paths, project name and window title in the code below - these still need revisit.

"run-editor.js" file contents:

var settings = {
	// Set to 'true' (without quotes) if run on Windows 64bit. Set to 'false' (without quotes) otherwise.
	x64: true,

	// Set to folder name, where PhpStorm was installed to (e.g. 'PhpStorm')
	folder_name: 'PhpStorm 9.0',

	// Set to window title (only text after dash sign), that you see, when switching to running PhpStorm instance
	window_title: 'PhpStorm PS-145.61.14',

	// In case your file is mapped via a network share and paths do not match.
	// eg. /var/www will can replaced with Y:/
	projects_basepath: '/YOUR_PATH_HERE',
	projects_path_alias: 'D:/Projects/YOUR_PROJECT_NAME'
};

// don't change anything below this line, unless you know what you're doing
var	url = WScript.Arguments(0),
	match = /^pstorm:\/\/open\/\?url=file:\/\/(.+)&line=(\d+)$/.exec(url),
	project = '',
	editor = "D:\\portables\\PhpStorm\\PhpStormPortable.exe";

if (match) {

	var	shell = new ActiveXObject('WScript.Shell'),
		file_system = new ActiveXObject('Scripting.FileSystemObject'),
		file = decodeURIComponent(match[1]).replace(/\+/g, ' '),
		search_path = file.replace(/\//g, '\\');

	if (settings.projects_basepath != '' && settings.projects_path_alias != '') {
		file = file.replace(new RegExp('^' + settings.projects_basepath), settings.projects_path_alias);
	}

	// note: uncomment to see the popup message (for testing)
	//shell.Popup(file, 0, "My Title", 64);

	while (search_path.lastIndexOf('\\') != -1) {
		search_path = search_path.substring(0, search_path.lastIndexOf('\\'));

		if(file_system.FileExists(search_path+'\\.idea\\.name')) {
			project = search_path;
			break;
		}
	}

	if (project != '') {
		editor += ' "%project%"';
	}

	editor += ' --line %line% "%file%"';

	var command = editor.replace(/%line%/g, match[2])
						.replace(/%file%/g, file)
						.replace(/%project%/g, project)
						.replace(/\//g, '\\');

	shell.Exec(command);
	shell.AppActivate(settings.window_title);
}

That's all. Clicking on a Tracy error line load PhpStorm at the file/line specified, and also brings the application to the front (activates it).

If you encounter errors, try to "debug" using popup messages (see "shell.Popup(..." line in the js file).

  • Like 4
Link to comment
Share on other sites

On 3/9/2016 at 3:47 PM, adrian said:

No problem at all - I assume the only places where this is affecting you are the links to the template file from the PW Info panel (both in the template info section and the icon at the bottom)? Are there any other locations with links without line numbers that I haven't thought of?

Yes, I was talking about template files.

On 3/9/2016 at 3:47 PM, adrian said:

Awesome - care to share any tips for setting it up? Did you work with this file: https://github.com/adrianbj/TracyDebugger/blob/master/tracy/tools/open-in-editor/open-editor.js

Huh, didn't know the file was there :-), I was using https://pla.nette.org/en/how-open-files-in-ide-from-debugger (link provided by @tpr) and modified just a little. My .js file is very simple, @tpr went few steps forward, here are the files:

editor.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\editor]
@="URL:editor Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\editor\shell\open\command]
@="wscript //E:jscript \"D:\\editor.js\" \"%1\""

and editor.js:

// full path to the editor, you have to use double backslash
var editor = '"C:\\Program Files\\IDM Computer Solutions\\UltraEdit\\uedit64.exe" "%file%" -l%line%';

var url = WScript.Arguments(0);
var match = /^editor:\/\/open\/\?file=(.+)&line=(\d*)$/.exec(url);

if (match) {
    var file = decodeURIComponent(match[1]).replace(/\+/g, ' ').replace(/\//g, "\\");
    var command = editor.replace(/%line%/g, match[2]).replace(/%file%/g, file);
    var shell = new ActiveXObject("WScript.Shell");
    shell.Exec(command.replace(/\\/g, '\\\\'));
}

 

  • Like 1
Link to comment
Share on other sites

Thanks for your writeup also @matjazp - great to have this stuff documented!

I have committed a fix for the blank line number issue you were having. Please let me know if it works now.

New Feature

I have just added a new feature - it's a bit experimental, and I am not sure just how useful it is so would appreciate feedback.

It provides the ability to insert debugging statements (actually any code you want) at designtaed lines in the compiled template at runtime. I see this as useful if you want to quickly debug something on a live site without having to edit the files on the server.

In this example I am inserting two barDump (bd) statements into the home.php template file at lines 61 and 63 and noting which one is before the variable is iterated and which one is after. 

post-985-0-00563400-1457543595_thumb.png

The code for the home.php file looks like this:

post-985-0-29333000-1457543639_thumb.png

And this is the result in the dumps panel:

post-985-0-60835300-1457543721_thumb.png

Obviously the scenario and the inserted debug statements could be quite different, but hopefully you get the idea.

Note: Any lines of code inserted like this are only executed for superusers, so you could even use this as a way to insert lines of code that affect the output on the page without it affecting other users and guests.

Also, be sure to remove any statements when you are done debugging, because the template file is re-compiled every time the page is loaded when this field is populated.

  • Like 1
Link to comment
Share on other sites

  • adrian pinned and locked this topic
  • adrian unpinned and pinned this topic
Guest
This topic is now closed to further replies.
×
×
  • Create New...