adrian

Module
Tracy Debugger

878 posts in this topic

Introducing a new module which brings the Tracy Debugger (https://tracy.nette.org/) to ProcessWire.
 
A big thanks to @tpr for introducing me to Tracy and for the idea for this module and for significant feedback, testing, and feature suggestions.

The most comprehensive set of instructions and examples is now available on the PW blog: https://processwire.com/blog/posts/introducing-tracy-debugger/

Modules Directory: http://modules.processwire.com/modules/tracy-debugger/
Github: https://github.com/adrianbj/TracyDebugger

  • quickly detect and correct errors with an expandable call stack tree
  • log errors (and optionally receive emails when an error occurs in production mode)
  • dump variables
  • measure execution time of scripts/queries
  • see memory consumption between breakpoints

 
Debug Bar
 
I have added a custom ProcessWire panel which shows all the content of the current page object, along with all the info from the admin debug tools mode.
 
It also includes a PW Debug Mode badge to warn you that debug mode is on - great reminder for live sites.
 
A Method Info tab details the dumping/logging calls that you can make from your templates etc.
 
Screen Shot 2016-02-15 at 1.19.29 PM.png
 
 

Error Reporting
 
Expandable call stack of errors and other useful information:
 
joxi_screenshot_1455322556737.png
 
 
Manual Dumping, Logging, and Timing

Content can be dumped to the page via TD::dump() or to the debug bar via TD::barDump(), or logged via TD::log() from PW template files. eg.

 

 

 

TD::barDump($page->body, 'Body Field');
TD::barDump($fields->body, 'Body Settings');

Screen-Shot-2016-02-12-at-4.07.05-PM.png
 

TD::dump($page);

Screen Shot 2016-02-12 at 4.11.34 PM.png
 
 
Logging to Console (FireLog)
 
This works with Chrome and Firefox

TD::fireLog()
fireLog()
fl()

To make this work you need to install these browser extensions:
 
Chrome:
https://github.com/MattSkala/chrome-firelogger
 
Firefox:
http://www.getfirebug.com/
http://firelogger.binaryage.com/
 
Screen Shot 2016-02-15 at 1.21.36 PM.png 
 
Logging to File

TD::log('Log Message');

By default, manually logged content is sent to: /site/assets/logs/tracy/info.log, but you can specify an optional second parameter to one of the following: 'debug', 'info', 'warning', 'error', 'exception', 'critical' files.
eg. which will put the message in the debug.info file. 

 

 

 

TD::log('Log Message', 'debug'); 

 
Available Methods

TD::dump()
TD::barDump()
TD::log()
TD::fireLog()
TD::addBreakpoint()
TD::timer()

 
You can also use these shortcuts/aliases:
 
TD::dump($var)

dump()
d()

 
TD::barDump($var, $title)

barDump()
bd()

 
TD::log($message)

l()

Just in case you are wondering, log() is already defined in PW, which is why it is not included here.
 
 
TD::fireLog($message)

fireLog()
fl()

TD:addBreakpoint($name)

addBreakpoint()
bp()

 
TD:timer($name)

timer()
t()

 
These aliases/shortcuts can be disabled in the module config setting, either altogether, or individually, just in case you have functions with the same names defined them elsewhere on your site and are getting a "previously declared" error.
 
  
 
Config Settings 
 
There are several config settings to control the output of errors, dumps, logging, and emails of errors. If you find any additional Tracy settings that you would also like to have access to, please let me know and I'll be happy to add them.

37 people like this

Share this post


Link to post
Share on other sites

Good stuff!  :rolleyes:

I was hoping to see a more feminine screenshot too :)

2 people like this

Share this post


Link to post
Share on other sites

Thanks to a bug report from horst I just committed a new version that fixes a Windows directory path issue.

He also pointed me to another problem which is turns out means that this module currently only works with the 3.x branch of PW. I will look into supporting 2.x, but for the moment if you want to test, you will need 3.x or you can probably make it work on 2.7 if you uncheck "Show ProcessWire Panel" in the config settings. You'll miss out on that functionality, but hopefully everything else should work.

1 person likes this

Share this post


Link to post
Share on other sites

That is something nearly every developer can make use of. Thanks adrian and tpr!

Could you please suggest a proper way to debug process modules stuff with it? On the tracy page I have read about outputting stuff to fiebug...

 
 
 

Share this post


Link to post
Share on other sites

That is something nearly every developer can make use of. Thanks adrian and tpr!

Could you please suggest a proper way to debug process modules stuff with it? On the tracy page I have read about outputting stuff to fiebug...

 

Thanks Ivan, there are several options - Firebug is actually something I haven't implement yet in this module. It requires an additional package which I haven't included. I figure that most of us are on Chrome, so didn't bother with it yet. But if there is interest, I will definitely take a look.

I should have pointed to the best of the Tracy docs: https://doc.nette.org/en/2.3/debugging - for some reason the ones on the main page don't include the barDump() option yet.

You can use: TD::dump(), TD::barDump(), TD::log()

I also implemented some aliases - d(), bd(), l() as very short options for lazy typists, but not sure about keeping these due to possible name conflicts.

Anyway, let me know if that links helps and if you have any specific questions.

Edited by adrian
Corrected info about the Firebug option.

Share this post


Link to post
Share on other sites

Thanks everyone for the great interest in this module!

I have just added 2 new sections to the PW panel that shows details of all fields on the current page.

I would love to hear from you if you have any ideas for other sections/info that you would find useful to have in the PW panel.

List View

Shows basic details with the name linked to the edit page for that field:

post-985-0-77931100-1455395881_thumb.png

Object View

Shows the fields object - for details of each field, expand the "data" section:

post-985-0-51904500-1455395880_thumb.png

4 people like this

Share this post


Link to post
Share on other sites

Sorry, this is just too much fun :)

Just added a Current Page Info section, with links to edit the page (from the name), edit the template, and open the page tree with this page expanded to show the children (from the number of children).

post-985-0-19806300-1455399024_thumb.png

3 people like this

Share this post


Link to post
Share on other sites

Looks like you're really in love with Tracy :)

Actually I am, and have been for 18 years now, but that is a very different kind of love ;)

4 people like this

Share this post


Link to post
Share on other sites

That was high time then to make a module from it :)

Page Info: it's useful, but it's a bit inconvenient to show. How about adding it to a separate tab? That way it would only require to hover. 

I don't know how much work is it, just asking.

Share this post


Link to post
Share on other sites

That was high time then to make a module from it :)

Page Info: it's useful, but it's a bit inconvenient to show. How about adding it to a separate tab? That way it would only require to hover. 

I don't know how much work is it, just asking.

New tab/panel isn't hard to do at all, but I wonder whether just having that section always expanded would be ok/better?

There are so many options and ways to present info that I am not totally sure the best option yet. I'd really love some thoughts from others about what they think they would use/access most often. Perhaps I could add several PW tabs/panels with different elements on each. Anyone have any ideas on what else they would like to have accessible and what should be grouped with what?

EDIT: I have just made the Page Info section open by default so a hover over the PW tab is enough to see that info. Still amenable to making separate tabs/panels if that is the consensus though.

1 person likes this

Share this post


Link to post
Share on other sites

Just been playing around with shortcuts to the various dump/log methods.

TD::dump()
TD::barDump()
TD::log()
TD::fireLog()
TD::timer()

 
You can now also use these shortcuts/aliases:
 
TD::dump($var)

dump()
d()

 
TD::barDump($var, $title)

barDump()
bd()

 
TD::log($message)

l()

Just in case you are wondering, log() is already defined in PW, which is why it is not included here.
 

TD::fireLog($message)

fireLog()
fl()

TD:timer($name)

timer()
t()

These aliases/shortcuts can be disabled in the module config setting, either altogether, or individually, just in case you have functions with the same names defined them elsewhere on your site and are getting a "previously declared" error.
 

I'd really appreciate any feedback on these shortcuts/aliases - I think they make dumping/logging substantially quicker, but I am definitely open to suggestion/criticisms to this approach.

Share this post


Link to post
Share on other sites

In followup to that last post, I decided to add an option to load the Tracy library for non-superusers.

This will NOT show errors or the debug bar for non-superusers. It just loads the Tracy library so that you can call Tracy methods directly, eg. Tracy\Debugger::dump() without non-superusers getting a "Fatal error: Class \'Tracy\Debugger\' not found" fatal error.
 
The downside to this setting is that the library is loaded for everyone - while it may not be a significant increase in resources, it is still something, so in general I recommend leaving this unchecked.

Share this post


Link to post
Share on other sites

How about making a dummy Tracy class with barDump/dump/log methods to load for non-superusers? (as a workaround)

This way less resources would be needed.

1 person likes this

Share this post


Link to post
Share on other sites

How about making a dummy Tracy class with barDump/dump/log methods to load for non-superusers? (as a workaround)

This way less resources would be needed.

I think that sounds like a decent approach, so I just implemented it.

Now there is no need to worry if for some reason you want to make direct calls via (Tracy\Debugger::method()) on a live site as non-superusers will just get the dummy methods which return nothing.

I also just implemented the TD::timer() method, along with timer() and t() shortcut aliases.

With timer() you can do:

t();
// insert resource intensive code here
bd(t());

to dump the results of the time to the debug bar.

1 person likes this

Share this post


Link to post
Share on other sites

t() returns number like 3.0994415283203e-6 Could that be "better" formated?

Sure, it is now rounded to 4 decimal places. This matches the precision of PW's built in Debug::timer() which of course you could also use and dump or bardump to Tracy.

Is that what you were getting at?

I have also added the name of the timer to the output - if you specify it like:

t('first');
sleep(1);
bd(t('first'));

t('second');
sleep(2);
bd(t('second'));

This way you can have multiple timers and output them at once, and know which value responds to which timer.

Please let me know if that is better for you, or if you have any more specific suggestions.

Share this post


Link to post
Share on other sites

There's a typo in the name of "ShortcutMethods": the first "S" is lowercase in the filename which causes an error (perhaps not on every server).

Share this post


Link to post
Share on other sites

There's a typo in the name of "ShortcutMethods": the first "S" is lowercase in the filename which causes an error (perhaps not on every server).

That's weird - it was actually a git issue - I had already changed the filename locally to match the require statement, but git didn't update it when syncing. Looks to be fixed now.

1 person likes this

Share this post


Link to post
Share on other sites

4 decimal places is ok (from my point of view), but now it show 0.0 instead of 0.0000

Weird - I am not seeing that at all - could you please post exactly how you are calling this? Also, how many times have you reloaded the page. As you can image if the result was 3.100000000 it would round to be 3.1, rather than 3.1000, although it seems unlikely that the result would only have 2 significant figures.

I am wondering what might be different on your setup. I don't see why PHP version would matter and can't think of anything else.

Anyone else out there seeing only one decimal place?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VirtuallyCreative
      Hello PW Community,
      I'm trying to modify a small module I found to pull in Gravatars but I'm having a bit of an issue and was curious if someone has already done a Gravatar implementation, or could look over the module code? It installed no problem, but when I try to use it, nothing works.
      Here is the full module:
      <?php /** * Module for generating Gravatar URLs for ProcessWire users. * * Use it like this: * * Get URL only with all defaults: * $user->gravatar() * * Get image tag with different size: * $user->gravatar(array('img' => true, 's' => 200)); * */ class Gravatar extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Gravatar', 'version' => 1, 'summary' => 'Gravatar hook for users', 'singular' => true, 'autoload' => true, 'icon' => 'smile-o', ); } public function init() { $this->addHook('User::gravatar', $this, 'methodGravatar'); } public function methodGravatar($event) { $u = $event->object; if ( ! $u instanceof User) $event->return = false; return; if ( ! $u->email) $event->return = false; return; $params = ($event->arguments(0) ? $event->arguments(0) : array()); // Default options $defaults = array( 's' => 100, 'd' => 'retro', 'r' => 'g', 'img' => false, 'attrs' => array(), ); $opts = array_merge($defaults, $params); extract($opts); $url = '//www.gravatar.com/avatar/'; $url .= md5(strtolower(trim($u->email))); $url .= "?s=$s&d=$d&r=$r"; if ($img) { $url = '<img src="' . $url . '"'; foreach ($attrs as $key => $val ) { $url .= ' ' . $key . '="' . $val . '"'; } $url .= ' />'; } $event->return = $url; } } The issue is when I use $user->gravatar() I'm getting back "unknown". I know that the email being used by the current logged-in user (me) does have a Gravatar, but I'm unable to get the URL so I can place it into my template markup.
      Any insight or direction would be appreciated. I did a search for Gravatar modules / forum topics but it mostly appears to be linked to the Blog profile that uses Gravatar, not a stand-alone module / implementation.
      Regards,
      VirtuallyCreative
    • By cosmicsafari
      Hi All,
      Fairly new to ProcessWire so apologies if this is a daft question, but I am having a go creating my first module. I have set it up and enabled it as per the docs.
      However I have created another class within the same module directory, which was going to be used in the main module file but I can't get it to work for the life of me and I believe its due to me not fully understanding the namespace side of things.
      Example:
      MyModule.module.php
      <?php namespace ProcessWire; class MyModule extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Test Module', 'version' => 1, 'summary' => 'Test module', 'href' => '', 'singular' => true, 'autoload' => true, 'icon' => 'exchange', ); } public function newMethod( $testMessage ) { $foo = new Foo($testMessage); $foo->getFoo(); } } Foo.php
      <?php namespace ProcessWire; class Foo { protected $foo; public function __construct($foo) { $this->foo = $foo; } public function getFoo() { return $this->foo; } } Now as I understand it both these files should exist under the ProcessWire namespace, so in theory I should be able to use Foo within MyModule without any use statements as they both exist at the same level within the ProcessWire namespace?
      However when I try something like so:
      $myModule = $modules->getModule('MyModule'); $myModule->myMethod('Foo'); I would have thought this should return 'Foo', however I keep running into errors like:
      Any help would be appreciated.
    • By Max Allan Niklasson
      Hi, I think this is my first topic, even though I've been working with PW a few years. But now, I'm trying to restructure my way of coding, cause many templates (like activities for birding societies) are re appearing in multiple different websites. Since this, I've been working on redo this into a module, but as far as I have read I cannot store the templates (related to specific module) in the module's folder. It has to be in the templates folder?
       
    • By kongondo
      Menu Builder
       
      Modules Directory
      Project Page
      Read Me (How to install, use, etc..)

      If you want a navigation that mirrors your ProcessWire page tree, the system allows you to easily create recursive menus using either vanilla PHP or Soma's great MarkupSimpleNavigation. In some cases, however, you may wish to create menus that:
      1. Do not mirror you site's page tree (hirarchies and ancestry); and
      2. You can add custom links (external to your site) to.

      That is primarily where Menu Builder comes in. It is also helpful if you:
      3. Prefer creating menus via drag and drop
      4. Have a need for menus (or other listings) that will be changing regularly or that you want to allow your admin users to edit.

      The issue of custom menus is not new here in the forums. The difference is that this module allows you to easily create such menus via drag and drop in the Admin. Actually, you can even use it to just create some list if you wanted to. In the backend, the module uses the jQueryUI plugin nestedSortable by Manuele J Sarfatti for the drag and drop and is inspired in part by the WP Custom Menu feature.
       
      Please read the Read Me completely before using this module. 
      For Complex or highly-customised menus, it is recommended to use the getMenuItems() method as detailed in this post.
       
      Features
      Ability to create menus that do not mirror your ProcessWire Page Tree hierarchy/structure Menus can contain both ProcessWire pages and custom links Create menu hierarchies and nesting via drag and drop Easily add CSS IDs and Classes to each menu item on creating the menu items (both custom and from ProcessWire pages) or post creation. Optionally set custom links to open in a new tab Change menu item titles built from ProcessWire pages (without affecting the original page). E.g. if you have a page titled 'About Us' but you want the menu item title to be 'About' Readily view the structure and settings for each menu item Menus stored as pages (note: just the menu, not the items!) Menu items stored as JSON in a field in the menu pages (empty values not stored) Add menu items from ProcessWire pages using page fields (option to choose between PageAutocomplete and AsmSelect [default]) or a Selector (e.g. template=basic-page, limit=20, sort=title). For page fields, you can specify a selector to return only those specified pages for selection in the page field (i.e. asm and autocomplete) For superusers, optionally allow markup in your menu titles, e.g. <span>About</span> Menu settings for nestedSortable - e.g. maxLevels (limit nesting levels) Advanced features (e.g. add pages via selector, menu settings) currently permissible to superadmins only (may change to be permission-based) Delete single or all menu items without deleting the menu itself Lock down menus for editing Highly configurable MarkupMenuBuilder - e.g. can pass menu id, title, name or array to render(); Passing an array means you can conditionally manipulate it before rendering, e.g. make certain menu branches visible only to certain users [the code is up to you!]  Optionally grab menu items only (as a Menu object WireArray or a normal array) and use your own code to create custom highly complex menus to meet any need. More... In the backend, ProcessMenuBuilder does the menu creation. For the frontend, menus are displayed using MarkupMenuBuilder.

      Credits

      In this module's infancy (way back!), I wanted to know more about ProcessWire modules as well as improve my PHP skills. As they say, what better way to learn than to actually create something? So, I developed this module (instead of writing PW tutorials as promised, tsk, tsk, naughty, naughty!) in my own summer of code . Props to Wanze, Soma, Pete, Antti and Ryan whose modules I studied (read copied ) to help in my module development and to Teppo for his wonderful write-up on the "Anatomy of fields in ProcessWire" that vastly improved my knowledge and understanding of how PW works. Diogo and marcus for idea about using pages (rather than a custom db table), onjegolders for his helpful UI comments, Martijn Geerts, OrganizedFellow, dazzyweb and Mike Anthony for 'pushing me' to complete this module and netcarver for help with the code.
       
      Screens

    • By kongondo
      Introducing Variations, an Input- and Fieldtype for product variations and their attributes.
      Product variations is a topic that has been coming up now and then, especially in the recent past.  This module seeks to fill this gap. Though it's in its early stages of development, it is already functional and can be used as is. I decided to make an early announcement (modules development forum) in order to get early feedback from potential users. 
      The module is an alternative take on how variations can be built for a product. Imagine the ubiquitous T-Shirt. The product could vary by ...Size, Colour, Material, etc...These variations could in turn have internal variations, i.e. attributes....so, Size [Small, Large], Colour [Red, Blue]...etc; you get the drift.
      The usual approaches to building variations have been either to use Multiple pages, Repeaters, Page Table, Table or Matrix (limited to 1x1 variations). In this module, we do it a bit differently.
      First, the variations occur not at the Field level (meaning all product pages would have the same variations and attributes for each template) but at the page level. Secondly, there are no multiple pages for each variation and/or attributes; a product is a single page. The variations and their attributes are defined by site editors at the page level. Once a variations configuration has been defined, it is applied to the page and all possible combinations are generated (i.e. the Red,Small,Cotton; Red,Large,Cotton, etc) in the Inputfield. There is no limit to the number of variations and attributes that can be defined, although you will be amazed at how quickly the combinations grow! Prices are entered for each combination when editing the page. Combinations without prices are not saved to the database. Please note that prices cannot themselves vary at the page-level. Meaning, you cannot have ONE variation configuration that has different price inputs per product in the same FieldtypeVariations field.
      The module ships with an API for outputting variation combinations in the frontend. Search, database and in-memory work as normal in the frontend. In the backend, DataTables provides a nice paginated, filterable table. Prices can be entered on any pages (of the table) without loss of data (meaning you can enter prices on page 1, scroll to page 10, enter more prices, filter or search the products table, enter more prices and finally save; no data will be lost).
      I still have a couple of ideas and plans pending but would love to hear from you, thanks.
      Below is a short video demo of the module in its current state. Things may/will change, both UI and features. Btw, the Fieldtype, although primarily targeted at commerce applications, is by no means limited to this. Other uses requiring combinations of whatever number of variables are very much within the remit of the module.
      Planned/Hoped for features
      Import/Export product variations and attributes (Excel, etc) Full integration with Padloper (I will have to  discuss with @apeisa) SKU fields for each product variation Add/Remove extra subfields, e.g. multi-currency prices (currently there is only 1 input for each product variations' price) Product variation images? Etc... Please not this module is not related to this other planned module (but it may eventually).
      Sneak Peek Demo