Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Sitemap_Logger
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 report
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
 time
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * A message logger for the Jetpack Sitemap module.
4 *
5 * @package automattic/jetpack
6 * @since 4.8.0
7 */
8
9if ( ! defined( 'ABSPATH' ) ) {
10    exit( 0 );
11}
12
13/**
14 * Handles logging errors and debug messages for sitemap generator.
15 *
16 * A Jetpack_Sitemap_Logger object keeps track of its birth time as well
17 * as a "unique" ID string. Calling the report() method writes a message
18 * to the PHP error log as well as the ID string for easier grepping.
19 *
20 * @since 4.8.0
21 */
22class Jetpack_Sitemap_Logger {
23    /**
24     * A unique-ish string for each logger, enabling us to grep
25     * for the messages written by an individual generation phase.
26     *
27     * @access private
28     * @since 4.8.0
29     * @var string $key The key string.
30     */
31    private $key;
32
33    /**
34     * The birth time of this object in microseconds.
35     *
36     * @access private
37     * @since 4.8.0
38     * @var int $starttime The birth time.
39     */
40    private $starttime;
41
42    /**
43     * Initializes a new logger object.
44     *
45     * @access public
46     * @since 4.8.0
47     *
48     * @param string $message An optional message string to be written to the debug log on initialization.
49     */
50    public function __construct( $message = null ) {
51        $this->key       = wp_generate_password( 5, false );
52        $this->starttime = microtime( true );
53        if ( $message !== null ) {
54            $this->report( $message );
55        }
56    }
57
58    /**
59     * Writes a string to the debug log, including the logger's ID string.
60     *
61     * @access public
62     * @since 4.8.0
63     *
64     * @param string  $message  The string to be written to the log.
65     * @param boolean $is_error If true, $message will be logged even if JETPACK_DEV_DEBUG is not enabled.
66     */
67    public function report( $message, $is_error = false ) {
68        $message = 'jp-sitemap-' . $this->key . ': ' . $message;
69        if ( ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) {
70            return;
71        }
72        if ( ! $is_error && ! ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) ) {
73            return;
74        }
75        // Append memory usage in MB (human readable)
76        $usage    = memory_get_usage( true );
77        $usage_mb = round( $usage / MB_IN_BYTES, 2 );
78        $message .= ' [Memory usage: ' . $usage_mb . ' MB]';
79        error_log( $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
80    }
81
82    /**
83     * Writes the elapsed lifetime of the logger to the debug log, with an optional message.
84     *
85     * @access public
86     * @since 4.8.0
87     *
88     * @param string $message The optional message string. Default is the empty string.
89     */
90    public function time( $message = '' ) {
91        $time = round( microtime( true ) - $this->starttime, 3 );
92        $this->report( $message . ' ' . $time . ' seconds elapsed.' );
93    }
94}