Recent Changes - Search:

add Cookbook page

hide

Cookbook


hide

Topics


show

Changed

Visitor's book Site map pmwiki-2.3.33

Debug Messages

To do
  • change skiwi
  • remove this comment
Summary: A function dmsg() for use in debugging PmWiki recipes
Version: 2024-01-14
Prerequisites: PmWiki 2.3.32; PHP 8
Status: In use
Maintainer: skiwi
Licence: GPL3#
Users: (view / edit)
Discussion: DebugMessages-Talk

Questions answered by this recipe

When I am writing a PmWiki recipe how can I quickly and easily display user friendly debug information from the recipe?

Description

This recipe provides a simple function dmsg() that goes to some effort to dump variables passed to it to the PmWiki built in $MessagesFmt array. These can then be displayed using the built in (:messages:) page directive, or better still with the drop in Cookbook:Messages Replacement recipe.

The recipe provides a function function dmsg (string $smsgprefix, $smsgdata, bool $smshtml = false, string $dmsgId = '') {} whose parameters are

$smsgprefix: the prefix to the debug output line or block
$smsgdata: the data, of any type, to be dumped in human friendly format
$smshtml: optional boolean value for when $smsgdata contains preformatted HTML to be passed through
$dmsgId: optional string value

This recipe is inspired by the code snippet in Cookbook:Debugging For Cookbook Authors, and provides an enhanced, tried and tested, reusable version of this.

Installation

  • download debugmessages-2024-01-14.php Δ
  • copy to the /cookbook/ directory as debugmessages.php
  • add to config.php before any recipe that uses it:
    include_once($FarmD/cookbook/debugmessages.php);

Usage

The recipe provides a page variable {$dmsgVersion} which contains the current recipe version, and page variable {$DebugMessages} for a (:if enabled DebugMessages:) recipe installation check.

Configuration

In the recipe that is using dmsg() include the code

if (!function_exists('dmsg')) {
    function dmsg (string $smsgprefix, $smsgdata) { # local instance
        global $MessagesFmt;
        if (!isset ($MessagesFmt['functionName'])) $MessagesFmt['functionName'] = [];
        $MessagesFmt['functionName'] [] = $smsgprefix . ': ' . \PHSC (strval($smsgdata));    
    }
} 
replacing "functionName" with a unique identifier of your choosing.

This will ensure that whether or not the dmsg() function is available globally the recipe will work and be able to call dmsg().

The recipe will use the parameter $dmsgId as the unique identifier if it is supplied. If $dmsgId is not supplied the recipe will general an identifier based on the calling function, or if the calling funtion is include_once() it will use the basename of the recipe's filename.

dmsg() returns the value of the string added to $MessagesFmt.

Change log / Release notes

2024-04-10 Update for PHP 8 warnings 2024-01-14 Initial version

See also

On PmWiki

From the PmWiki developer category:

Contributors

Special thanks to Petko and the PmWiki documenters.

Talkback

See discussion at DebugMessages-Talk

tahi Page last modified on 2024 Apr 11 22:30

Edit - History - Recent Changes - WikiHelp - Search - email page as link -> mailto:?Subject="KiwiWiki: Debug Messages"&Body=