Recent Changes - Search:

add Extensions page

hide Extensions

Extensions


hide Topics

Topics

show Changed

Changed

Visitor's book Site map pmwiki-2.3.37

Diagnostic Messages

(redirected from Cookbook.DiagnosticMessages)

To do
  • change skiwi
  • remove this comment

Summary: A function dmsg() for use in diagnosing PmWiki entensions and recipes
Version: 2024-08-18
Prerequisites: PmWiki 2.3.36; PHP 8.3; ExtensionHub
Status: In use
Maintainer: skiwi
Licence: GPL3#
Users: (view / edit)

Questions answered by this extension

When I am writing a PmWiki extension or recipe how can I quickly and easily log diagnostic information from the extension?

Description

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

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

$smsgprefix: the prefix to the diagnostic output line or block
$smsgdata: the data, of any type, to be dumped in human friendly format
$smshtml: an optional boolean value for when $smsgdata contains preformatted HTML to be passed through
$dmsgId: an optional string value used as message identifier
$dmsgLog: optional, if true additionally writes the message to the PHP error log

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

Installation

Usage

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

You can then use Messages Replacement Redirects to Extensions.MessagesReplacement to display the messages.

Configuration

In the extension or recipe that is using dmsg() include the code:

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

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

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

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

Change log / Release notes

2024-08-18 convert to an extension 2024-04-30 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 DiagnosticMessages-Talk

tahi Page last modified on 2024 Sept 14 17:17

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