|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A message logger for the Jetpack Sitemap module. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Jetpack |
|
6
|
|
|
* @since 4.8.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Handles logging errors and debug messages for sitemap generator. |
|
11
|
|
|
* |
|
12
|
|
|
* A Jetpack_Sitemap_Logger object keeps track of its birth time as well |
|
13
|
|
|
* as a "unique" ID string. Calling the report() method writes a message |
|
14
|
|
|
* to the PHP error log as well as the ID string for easier grepping. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 4.8.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Jetpack_Sitemap_Logger { |
|
19
|
|
|
/** |
|
20
|
|
|
* A unique-ish string for each logger, enabling us to grep |
|
21
|
|
|
* for the messages written by an individual generation phase. |
|
22
|
|
|
* |
|
23
|
|
|
* @access private |
|
24
|
|
|
* @since 4.8.0 |
|
25
|
|
|
* @var string $key The key string. |
|
26
|
|
|
*/ |
|
27
|
|
|
private $key; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The birth time of this object in microseconds. |
|
31
|
|
|
* |
|
32
|
|
|
* @access private |
|
33
|
|
|
* @since 4.8.0 |
|
34
|
|
|
* @var int $starttime The birth time. |
|
35
|
|
|
*/ |
|
36
|
|
|
private $starttime; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Initializes a new logger object. |
|
40
|
|
|
* |
|
41
|
|
|
* @access public |
|
42
|
|
|
* @since 4.8.0 |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $message An optional message string to be written to the debug log on initialization. |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( $message = null ) { |
|
47
|
|
|
$this->key = wp_generate_password( 5, false ); |
|
48
|
|
|
$this->starttime = microtime( true ); |
|
|
|
|
|
|
49
|
|
|
if ( ! is_null( $message ) ) { |
|
50
|
|
|
$this->report( $message ); |
|
51
|
|
|
} |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Writes a string to the debug log, including the logger's ID string. |
|
57
|
|
|
* |
|
58
|
|
|
* @access public |
|
59
|
|
|
* @since 4.8.0 |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $message The string to be written to the log. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function report( $message ) { |
|
64
|
|
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
|
65
|
|
|
error_log( 'jp-sitemap-' . $this->key . ': ' . $message ); |
|
66
|
|
|
} |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Writes the elapsed lifetime of the logger to the debug log, with an optional message. |
|
72
|
|
|
* |
|
73
|
|
|
* @access public |
|
74
|
|
|
* @since 4.8.0 |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $message The optional message string. Default is the empty string. |
|
77
|
|
|
*/ |
|
78
|
|
|
public function time( $message = '' ) { |
|
79
|
|
|
$time = round( microtime( true ) - $this->starttime, 3 ); |
|
80
|
|
|
$this->report( $message . ' ' . $time . ' seconds elapsed.' ); |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.